servo: Merge #18503 - Simplify WorkerGlobalScope::process_event (from servo:globalism); r=emilio
authorAnthony Ramine <n.oxyde@gmail.com>
Thu, 14 Sep 2017 18:17:55 -0500
changeset 665377 28d266b1cc870fc1bf6c91fdbc38ef85e7006d50
parent 665376 849f008bce82bd547dd4e092efcce43c3f89f61c
child 665378 9db826022521f825d9332ba560139836667d8bab
push id80026
push userbmo:ralin@mozilla.com
push dateFri, 15 Sep 2017 10:04:21 +0000
reviewersemilio
milestone57.0a1
servo: Merge #18503 - Simplify WorkerGlobalScope::process_event (from servo:globalism); r=emilio Source-Repo: https://github.com/servo/servo Source-Revision: 42680374245c149b9bccc3ee5ea65f36d58c6d9e
servo/components/script/dom/dedicatedworkerglobalscope.rs
servo/components/script/dom/serviceworkerglobalscope.rs
servo/components/script/dom/workerglobalscope.rs
--- a/servo/components/script/dom/dedicatedworkerglobalscope.rs
+++ b/servo/components/script/dom/dedicatedworkerglobalscope.rs
@@ -24,17 +24,17 @@ use ipc_channel::ipc::{self, IpcReceiver
 use ipc_channel::router::ROUTER;
 use js::jsapi::{HandleValue, JS_SetInterruptCallback};
 use js::jsapi::{JSAutoCompartment, JSContext};
 use js::jsval::UndefinedValue;
 use js::rust::Runtime;
 use msg::constellation_msg::TopLevelBrowsingContextId;
 use net_traits::{IpcSend, load_whole_resource};
 use net_traits::request::{CredentialsMode, Destination, RequestInit, Type as RequestType};
-use script_runtime::{CommonScriptMsg, ScriptChan, ScriptPort, StackRootTLS, get_reports, new_rt_and_cx};
+use script_runtime::{CommonScriptMsg, ScriptChan, ScriptPort, StackRootTLS, new_rt_and_cx};
 use script_runtime::ScriptThreadEventCategory::WorkerEvent;
 use script_traits::{TimerEvent, TimerSource, WorkerGlobalScopeInit, WorkerScriptLoadOrigin};
 use servo_rand::random;
 use servo_url::ServoUrl;
 use std::mem::replace;
 use std::sync::{Arc, Mutex};
 use std::sync::atomic::AtomicBool;
 use std::sync::mpsc::{Receiver, RecvError, Select, Sender, channel};
@@ -264,20 +264,16 @@ impl DedicatedWorkerGlobalScope {
         let (tx, rx) = channel();
         let chan = box SendableWorkerScriptChan {
             sender: tx,
             worker: self.worker.borrow().as_ref().unwrap().clone(),
         };
         (chan, box rx)
     }
 
-    pub fn process_event(&self, msg: CommonScriptMsg) {
-        self.handle_script_event(WorkerScriptMsg::Common(msg));
-    }
-
     #[allow(unsafe_code)]
     fn receive_event(&self) -> Result<MixedMessage, RecvError> {
         let scope = self.upcast::<WorkerGlobalScope>();
         let worker_port = &self.receiver;
         let timer_event_port = &self.timer_event_port;
         let devtools_port = scope.from_devtools_receiver();
 
         let sel = Select::new();
@@ -309,26 +305,19 @@ impl DedicatedWorkerGlobalScope {
                 let scope = self.upcast::<WorkerGlobalScope>();
                 let target = self.upcast();
                 let _ac = JSAutoCompartment::new(scope.get_cx(),
                                                  scope.reflector().get_jsobject().get());
                 rooted!(in(scope.get_cx()) let mut message = UndefinedValue());
                 data.read(scope.upcast(), message.handle_mut());
                 MessageEvent::dispatch_jsval(target, scope.upcast(), message.handle());
             },
-            WorkerScriptMsg::Common(CommonScriptMsg::RunnableMsg(_, runnable)) => {
-                runnable.handler()
+            WorkerScriptMsg::Common(msg) => {
+                self.upcast::<WorkerGlobalScope>().process_event(msg);
             },
-            WorkerScriptMsg::Common(CommonScriptMsg::CollectReports(reports_chan)) => {
-                let scope = self.upcast::<WorkerGlobalScope>();
-                let cx = scope.get_cx();
-                let path_seg = format!("url({})", scope.get_url());
-                let reports = get_reports(cx, path_seg);
-                reports_chan.send(reports);
-            }
         }
     }
 
     fn handle_event(&self, event: MixedMessage) {
         match event {
             MixedMessage::FromDevtools(msg) => {
                 match msg {
                     DevtoolScriptControlMsg::EvaluateJS(_pipe_id, string, sender) =>
--- a/servo/components/script/dom/serviceworkerglobalscope.rs
+++ b/servo/components/script/dom/serviceworkerglobalscope.rs
@@ -20,17 +20,17 @@ use dom::workerglobalscope::WorkerGlobal
 use dom_struct::dom_struct;
 use ipc_channel::ipc::{self, IpcSender, IpcReceiver};
 use ipc_channel::router::ROUTER;
 use js::jsapi::{JS_SetInterruptCallback, JSAutoCompartment, JSContext};
 use js::jsval::UndefinedValue;
 use js::rust::Runtime;
 use net_traits::{load_whole_resource, IpcSend, CustomResponseMediator};
 use net_traits::request::{CredentialsMode, Destination, RequestInit, Type as RequestType};
-use script_runtime::{CommonScriptMsg, StackRootTLS, get_reports, new_rt_and_cx, ScriptChan};
+use script_runtime::{CommonScriptMsg, ScriptChan, StackRootTLS, new_rt_and_cx};
 use script_traits::{TimerEvent, WorkerGlobalScopeInit, ScopeThings, ServiceWorkerMsg, WorkerScriptLoadOrigin};
 use servo_config::prefs::PREFS;
 use servo_rand::random;
 use servo_url::ServoUrl;
 use std::sync::mpsc::{Receiver, RecvError, Select, Sender, channel};
 use std::thread;
 use std::time::Duration;
 use style::thread_state::{self, IN_WORKER, SCRIPT};
@@ -257,25 +257,18 @@ impl ServiceWorkerGlobalScope {
             CommonWorker(WorkerScriptMsg::DOMMessage(data)) => {
                 let scope = self.upcast::<WorkerGlobalScope>();
                 let target = self.upcast();
                 let _ac = JSAutoCompartment::new(scope.get_cx(), scope.reflector().get_jsobject().get());
                 rooted!(in(scope.get_cx()) let mut message = UndefinedValue());
                 data.read(scope.upcast(), message.handle_mut());
                 ExtendableMessageEvent::dispatch_jsval(target, scope.upcast(), message.handle());
             },
-            CommonWorker(WorkerScriptMsg::Common(CommonScriptMsg::RunnableMsg(_, runnable))) => {
-                runnable.handler()
-            },
-            CommonWorker(WorkerScriptMsg::Common(CommonScriptMsg::CollectReports(reports_chan))) => {
-                let scope = self.upcast::<WorkerGlobalScope>();
-                let cx = scope.get_cx();
-                let path_seg = format!("url({})", scope.get_url());
-                let reports = get_reports(cx, path_seg);
-                reports_chan.send(reports);
+            CommonWorker(WorkerScriptMsg::Common(msg)) => {
+                self.upcast::<WorkerGlobalScope>().process_event(msg);
             },
             Response(mediator) => {
                 // TODO XXXcreativcoder This will eventually use a FetchEvent interface to fire event
                 // when we have the Request and Response dom api's implemented
                 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker_1/index.html#fetch-event-section
                 self.upcast::<EventTarget>().fire_event(atom!("fetch"));
                 let _ = mediator.response_chan.send(None);
             }
@@ -308,20 +301,16 @@ impl ServiceWorkerGlobalScope {
             Ok(MixedMessage::FromDevtools(devtools_port.recv()?))
         } else if ret == timer_port_handle.id() {
             Ok(MixedMessage::FromTimeoutThread(timer_event_port.recv()?))
         } else {
             panic!("unexpected select result!")
         }
     }
 
-    pub fn process_event(&self, msg: CommonScriptMsg) {
-        self.handle_script_event(ServiceWorkerScriptMsg::CommonWorker(WorkerScriptMsg::Common(msg)));
-    }
-
     pub fn script_chan(&self) -> Box<ScriptChan + Send> {
         box ServiceWorkerChan {
             sender: self.own_sender.clone()
         }
     }
 
     fn dispatch_activate(&self) {
         let event = ExtendableEvent::new(self, atom!("activate"), false, false);
--- a/servo/components/script/dom/workerglobalscope.rs
+++ b/servo/components/script/dom/workerglobalscope.rs
@@ -27,17 +27,17 @@ use dom_struct::dom_struct;
 use fetch;
 use ipc_channel::ipc::IpcSender;
 use js::jsapi::{HandleValue, JSAutoCompartment, JSContext, JSRuntime};
 use js::jsval::UndefinedValue;
 use js::panic::maybe_resume_unwind;
 use js::rust::Runtime;
 use net_traits::{IpcSend, load_whole_resource};
 use net_traits::request::{CredentialsMode, Destination, RequestInit as NetRequestInit, Type as RequestType};
-use script_runtime::{CommonScriptMsg, ScriptChan, ScriptPort};
+use script_runtime::{CommonScriptMsg, ScriptChan, ScriptPort, get_reports};
 use script_thread::RunnableWrapper;
 use script_traits::{TimerEvent, TimerEventId};
 use script_traits::WorkerGlobalScopeInit;
 use servo_url::{MutableOrigin, ServoUrl};
 use std::default::Default;
 use std::rc::Rc;
 use std::sync::Arc;
 use std::sync::atomic::{AtomicBool, Ordering};
@@ -381,27 +381,29 @@ impl WorkerGlobalScope {
         if let Some(dedicated) = dedicated {
             return dedicated.new_script_pair();
         } else {
             panic!("need to implement a sender for SharedWorker/ServiceWorker")
         }
     }
 
     pub fn process_event(&self, msg: CommonScriptMsg) {
-        let dedicated = self.downcast::<DedicatedWorkerGlobalScope>();
-        let service_worker = self.downcast::<ServiceWorkerGlobalScope>();
-        if let Some(dedicated) = dedicated {
-            return dedicated.process_event(msg);
-        } else if let Some(service_worker) = service_worker {
-            return service_worker.process_event(msg);
-        } else {
-            panic!("need to implement a sender for SharedWorker")
+        match msg {
+            CommonScriptMsg::RunnableMsg(_, runnable) => {
+                runnable.handler()
+            },
+            CommonScriptMsg::CollectReports(reports_chan) => {
+                let cx = self.get_cx();
+                let path_seg = format!("url({})", self.get_url());
+                let reports = get_reports(cx, path_seg);
+                reports_chan.send(reports);
+            },
         }
 
-        //XXXjdm should we do a microtask checkpoint here?
+        // FIXME(jdm): Should we do a microtask checkpoint here?
     }
 
     pub fn handle_fire_timer(&self, timer_id: TimerEventId) {
         self.upcast::<GlobalScope>().fire_timer(timer_id);
     }
 
     pub fn close(&self) {
         if let Some(ref closing) = self.closing {