add a control chan to workers, use to signal shutdown

This commit is contained in:
Gregory Terzian 2020-05-24 16:36:10 +08:00
parent ed688fe2c1
commit 947fa8bbb7
6 changed files with 165 additions and 31 deletions

View file

@ -116,9 +116,16 @@ impl QueuedTaskConversion for ServiceWorkerScriptMsg {
}
}
/// Messages sent from the owning registration.
pub enum ServiceWorkerControlMsg {
/// Shutdown.
Exit,
}
pub enum MixedMessage {
FromServiceWorker(ServiceWorkerScriptMsg),
FromDevtools(DevtoolScriptControlMsg),
FromControl(ServiceWorkerControlMsg),
}
#[derive(Clone, JSTraceable)]
@ -165,24 +172,34 @@ pub struct ServiceWorkerGlobalScope {
swmanager_sender: IpcSender<ServiceWorkerMsg>,
scope_url: ServoUrl,
/// A receiver of control messages,
/// currently only used to signal shutdown.
#[ignore_malloc_size_of = "Channels are hard"]
control_receiver: Receiver<ServiceWorkerControlMsg>,
}
impl WorkerEventLoopMethods for ServiceWorkerGlobalScope {
type WorkerMsg = ServiceWorkerScriptMsg;
type ControlMsg = ServiceWorkerControlMsg;
type Event = MixedMessage;
fn task_queue(&self) -> &TaskQueue<ServiceWorkerScriptMsg> {
&self.task_queue
}
fn handle_event(&self, event: MixedMessage) {
self.handle_mixed_message(event);
fn handle_event(&self, event: MixedMessage) -> bool {
self.handle_mixed_message(event)
}
fn handle_worker_post_event(&self, _worker: &TrustedWorkerAddress) -> Option<AutoWorkerReset> {
None
}
fn from_control_msg(&self, msg: ServiceWorkerControlMsg) -> MixedMessage {
MixedMessage::FromControl(msg)
}
fn from_worker_msg(&self, msg: ServiceWorkerScriptMsg) -> MixedMessage {
MixedMessage::FromServiceWorker(msg)
}
@ -190,6 +207,10 @@ impl WorkerEventLoopMethods for ServiceWorkerGlobalScope {
fn from_devtools_msg(&self, msg: DevtoolScriptControlMsg) -> MixedMessage {
MixedMessage::FromDevtools(msg)
}
fn control_receiver(&self) -> &Receiver<ServiceWorkerControlMsg> {
&self.control_receiver
}
}
impl ServiceWorkerGlobalScope {
@ -203,6 +224,7 @@ impl ServiceWorkerGlobalScope {
time_out_port: Receiver<Instant>,
swmanager_sender: IpcSender<ServiceWorkerMsg>,
scope_url: ServoUrl,
control_receiver: Receiver<ServiceWorkerControlMsg>,
) -> ServiceWorkerGlobalScope {
ServiceWorkerGlobalScope {
workerglobalscope: WorkerGlobalScope::new_inherited(
@ -220,6 +242,7 @@ impl ServiceWorkerGlobalScope {
time_out_port,
swmanager_sender: swmanager_sender,
scope_url: scope_url,
control_receiver,
}
}
@ -234,6 +257,7 @@ impl ServiceWorkerGlobalScope {
time_out_port: Receiver<Instant>,
swmanager_sender: IpcSender<ServiceWorkerMsg>,
scope_url: ServoUrl,
control_receiver: Receiver<ServiceWorkerControlMsg>,
) -> DomRoot<ServiceWorkerGlobalScope> {
let cx = runtime.cx();
let scope = Box::new(ServiceWorkerGlobalScope::new_inherited(
@ -246,6 +270,7 @@ impl ServiceWorkerGlobalScope {
time_out_port,
swmanager_sender,
scope_url,
control_receiver,
));
unsafe { ServiceWorkerGlobalScopeBinding::Wrap(SafeJSContext::from_ptr(cx), scope) }
}
@ -259,6 +284,7 @@ impl ServiceWorkerGlobalScope {
devtools_receiver: IpcReceiver<DevtoolScriptControlMsg>,
swmanager_sender: IpcSender<ServiceWorkerMsg>,
scope_url: ServoUrl,
control_receiver: Receiver<ServiceWorkerControlMsg>,
) -> JoinHandle<()> {
let ScopeThings {
script_url,
@ -315,6 +341,7 @@ impl ServiceWorkerGlobalScope {
time_out_port,
swmanager_sender,
scope_url,
control_receiver,
);
let (_url, source) =
@ -366,23 +393,23 @@ impl ServiceWorkerGlobalScope {
fn handle_mixed_message(&self, msg: MixedMessage) -> bool {
match msg {
MixedMessage::FromDevtools(msg) => {
match msg {
DevtoolScriptControlMsg::EvaluateJS(_pipe_id, string, sender) => {
devtools::handle_evaluate_js(self.upcast(), string, sender)
},
DevtoolScriptControlMsg::WantsLiveNotifications(_pipe_id, bool_val) => {
devtools::handle_wants_live_notifications(self.upcast(), bool_val)
},
_ => debug!("got an unusable devtools control message inside the worker!"),
}
true
MixedMessage::FromDevtools(msg) => match msg {
DevtoolScriptControlMsg::EvaluateJS(_pipe_id, string, sender) => {
devtools::handle_evaluate_js(self.upcast(), string, sender)
},
DevtoolScriptControlMsg::WantsLiveNotifications(_pipe_id, bool_val) => {
devtools::handle_wants_live_notifications(self.upcast(), bool_val)
},
_ => debug!("got an unusable devtools control message inside the worker!"),
},
MixedMessage::FromServiceWorker(msg) => {
self.handle_script_event(msg);
true
},
MixedMessage::FromControl(ServiceWorkerControlMsg::Exit) => {
return false;
},
}
true
}
fn has_timed_out(&self) -> bool {