diff --git a/components/script/dom/bindings/global.rs b/components/script/dom/bindings/global.rs index ecca46a5a5e..67ed8766932 100644 --- a/components/script/dom/bindings/global.rs +++ b/components/script/dom/bindings/global.rs @@ -25,7 +25,7 @@ use libc; use profile_traits::time; use script_runtime::{CommonScriptMsg, EnqueuedPromiseCallback, ScriptChan}; use script_runtime::{ScriptPort, maybe_take_panic_result}; -use script_thread::{MainThreadScriptChan, RunnableWrapper, ScriptThread}; +use script_thread::{RunnableWrapper, ScriptThread}; use script_traits::MsDuration; use std::ffi::CString; use std::panic; @@ -67,16 +67,6 @@ impl<'a> GlobalRef<'a> { } } - /// `ScriptChan` used to send messages to the event loop of this global's - /// thread. - pub fn script_chan(&self) -> Box { - match *self { - GlobalRef::Window(ref window) => - MainThreadScriptChan(window.main_thread_script_chan().clone()).clone(), - GlobalRef::Worker(ref worker) => worker.script_chan(), - } - } - /// `ScriptChan` used to send messages to the event loop of this global's /// thread. pub fn networking_task_source(&self) -> Box { diff --git a/components/script/dom/globalscope.rs b/components/script/dom/globalscope.rs index 71405239122..7e5f0407e36 100644 --- a/components/script/dom/globalscope.rs +++ b/components/script/dom/globalscope.rs @@ -23,6 +23,8 @@ use js::jsapi::{HandleValue, JS_GetContext, JS_GetObjectRuntime, JSContext}; use msg::constellation_msg::PipelineId; use net_traits::{CoreResourceThread, ResourceThreads, IpcSend}; use profile_traits::{mem, time}; +use script_runtime::ScriptChan; +use script_thread::MainThreadScriptChan; use script_traits::{ScriptMsg as ConstellationMsg, TimerEventRequest}; use std::cell::Cell; use std::collections::HashMap; @@ -257,6 +259,17 @@ impl GlobalScope { pub fn core_resource_thread(&self) -> CoreResourceThread { self.resource_threads().sender() } + + /// `ScriptChan` to send messages to the event loop of this global scope. + pub fn script_chan(&self) -> Box { + if let Some(window) = self.downcast::() { + return MainThreadScriptChan(window.main_thread_script_chan().clone()).clone(); + } + if let Some(worker) = self.downcast::() { + return worker.script_chan(); + } + unreachable!(); + } } fn timestamp_in_ms(time: Timespec) -> u64 { diff --git a/components/script/dom/websocket.rs b/components/script/dom/websocket.rs index aa2a61b6c38..ac1cf53b2ae 100644 --- a/components/script/dom/websocket.rs +++ b/components/script/dom/websocket.rs @@ -330,8 +330,10 @@ impl WebSocket { address: address, }; - let global = self.global(); - global.r().script_chan().send(CommonScriptMsg::RunnableMsg(WebSocketEvent, task)).unwrap(); + self.global_scope() + .script_chan() + .send(CommonScriptMsg::RunnableMsg(WebSocketEvent, task)) + .unwrap(); } Ok(true) diff --git a/components/script/dom/worker.rs b/components/script/dom/worker.rs index 1ef6242720d..035aa83bdf9 100644 --- a/components/script/dom/worker.rs +++ b/components/script/dom/worker.rs @@ -109,7 +109,7 @@ impl Worker { DedicatedWorkerGlobalScope::run_worker_scope( init, worker_url, devtools_receiver, worker.runtime.clone(), worker_ref, - global.script_chan(), sender, receiver, worker_load_origin, closing); + global_scope.script_chan(), sender, receiver, worker_load_origin, closing); Ok(worker) }