mirror of
https://github.com/servo/servo.git
synced 2025-08-06 06:00:15 +01:00
Format script component
This commit is contained in:
parent
2ca7a13473
commit
c37a345dc9
357 changed files with 25485 additions and 18076 deletions
|
@ -55,7 +55,9 @@ impl QueuedTaskConversion for ServiceWorkerScriptMsg {
|
|||
_ => return None,
|
||||
};
|
||||
match script_msg {
|
||||
CommonScriptMsg::Task(_category, _boxed, _pipeline_id, task_source) => Some(&task_source),
|
||||
CommonScriptMsg::Task(_category, _boxed, _pipeline_id, task_source) => {
|
||||
Some(&task_source)
|
||||
},
|
||||
_ => return None,
|
||||
}
|
||||
}
|
||||
|
@ -66,8 +68,9 @@ impl QueuedTaskConversion for ServiceWorkerScriptMsg {
|
|||
_ => return None,
|
||||
};
|
||||
let (category, boxed, pipeline_id, task_source) = match script_msg {
|
||||
CommonScriptMsg::Task(category, boxed, pipeline_id, task_source) =>
|
||||
(category, boxed, pipeline_id, task_source),
|
||||
CommonScriptMsg::Task(category, boxed, pipeline_id, task_source) => {
|
||||
(category, boxed, pipeline_id, task_source)
|
||||
},
|
||||
_ => return None,
|
||||
};
|
||||
Some((None, category, boxed, pipeline_id, task_source))
|
||||
|
@ -94,18 +97,20 @@ impl QueuedTaskConversion for ServiceWorkerScriptMsg {
|
|||
pub enum MixedMessage {
|
||||
FromServiceWorker(ServiceWorkerScriptMsg),
|
||||
FromDevtools(DevtoolScriptControlMsg),
|
||||
FromTimeoutThread(())
|
||||
FromTimeoutThread(()),
|
||||
}
|
||||
|
||||
#[derive(Clone, JSTraceable)]
|
||||
pub struct ServiceWorkerChan {
|
||||
pub sender: Sender<ServiceWorkerScriptMsg>
|
||||
pub sender: Sender<ServiceWorkerScriptMsg>,
|
||||
}
|
||||
|
||||
impl ScriptChan for ServiceWorkerChan {
|
||||
fn send(&self, msg: CommonScriptMsg) -> Result<(), ()> {
|
||||
self.sender
|
||||
.send(ServiceWorkerScriptMsg::CommonWorker(WorkerScriptMsg::Common(msg)))
|
||||
.send(ServiceWorkerScriptMsg::CommonWorker(
|
||||
WorkerScriptMsg::Common(msg),
|
||||
))
|
||||
.map_err(|_| ())
|
||||
}
|
||||
|
||||
|
@ -167,44 +172,48 @@ impl WorkerEventLoopMethods for ServiceWorkerGlobalScope {
|
|||
}
|
||||
|
||||
impl ServiceWorkerGlobalScope {
|
||||
fn new_inherited(init: WorkerGlobalScopeInit,
|
||||
worker_url: ServoUrl,
|
||||
from_devtools_receiver: Receiver<DevtoolScriptControlMsg>,
|
||||
runtime: Runtime,
|
||||
own_sender: Sender<ServiceWorkerScriptMsg>,
|
||||
receiver: Receiver<ServiceWorkerScriptMsg>,
|
||||
timer_event_chan: IpcSender<TimerEvent>,
|
||||
timer_event_port: Receiver<()>,
|
||||
swmanager_sender: IpcSender<ServiceWorkerMsg>,
|
||||
scope_url: ServoUrl)
|
||||
-> ServiceWorkerGlobalScope {
|
||||
fn new_inherited(
|
||||
init: WorkerGlobalScopeInit,
|
||||
worker_url: ServoUrl,
|
||||
from_devtools_receiver: Receiver<DevtoolScriptControlMsg>,
|
||||
runtime: Runtime,
|
||||
own_sender: Sender<ServiceWorkerScriptMsg>,
|
||||
receiver: Receiver<ServiceWorkerScriptMsg>,
|
||||
timer_event_chan: IpcSender<TimerEvent>,
|
||||
timer_event_port: Receiver<()>,
|
||||
swmanager_sender: IpcSender<ServiceWorkerMsg>,
|
||||
scope_url: ServoUrl,
|
||||
) -> ServiceWorkerGlobalScope {
|
||||
ServiceWorkerGlobalScope {
|
||||
workerglobalscope: WorkerGlobalScope::new_inherited(init,
|
||||
worker_url,
|
||||
runtime,
|
||||
from_devtools_receiver,
|
||||
timer_event_chan,
|
||||
None),
|
||||
workerglobalscope: WorkerGlobalScope::new_inherited(
|
||||
init,
|
||||
worker_url,
|
||||
runtime,
|
||||
from_devtools_receiver,
|
||||
timer_event_chan,
|
||||
None,
|
||||
),
|
||||
task_queue: TaskQueue::new(receiver, own_sender.clone()),
|
||||
timer_event_port: timer_event_port,
|
||||
own_sender: own_sender,
|
||||
swmanager_sender: swmanager_sender,
|
||||
scope_url: scope_url
|
||||
scope_url: scope_url,
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
pub fn new(init: WorkerGlobalScopeInit,
|
||||
worker_url: ServoUrl,
|
||||
from_devtools_receiver: Receiver<DevtoolScriptControlMsg>,
|
||||
runtime: Runtime,
|
||||
own_sender: Sender<ServiceWorkerScriptMsg>,
|
||||
receiver: Receiver<ServiceWorkerScriptMsg>,
|
||||
timer_event_chan: IpcSender<TimerEvent>,
|
||||
timer_event_port: Receiver<()>,
|
||||
swmanager_sender: IpcSender<ServiceWorkerMsg>,
|
||||
scope_url: ServoUrl)
|
||||
-> DomRoot<ServiceWorkerGlobalScope> {
|
||||
pub fn new(
|
||||
init: WorkerGlobalScopeInit,
|
||||
worker_url: ServoUrl,
|
||||
from_devtools_receiver: Receiver<DevtoolScriptControlMsg>,
|
||||
runtime: Runtime,
|
||||
own_sender: Sender<ServiceWorkerScriptMsg>,
|
||||
receiver: Receiver<ServiceWorkerScriptMsg>,
|
||||
timer_event_chan: IpcSender<TimerEvent>,
|
||||
timer_event_port: Receiver<()>,
|
||||
swmanager_sender: IpcSender<ServiceWorkerMsg>,
|
||||
scope_url: ServoUrl,
|
||||
) -> DomRoot<ServiceWorkerGlobalScope> {
|
||||
let cx = runtime.cx();
|
||||
let scope = Box::new(ServiceWorkerGlobalScope::new_inherited(
|
||||
init,
|
||||
|
@ -216,119 +225,161 @@ impl ServiceWorkerGlobalScope {
|
|||
timer_event_chan,
|
||||
timer_event_port,
|
||||
swmanager_sender,
|
||||
scope_url
|
||||
scope_url,
|
||||
));
|
||||
unsafe {
|
||||
ServiceWorkerGlobalScopeBinding::Wrap(cx, scope)
|
||||
}
|
||||
unsafe { ServiceWorkerGlobalScopeBinding::Wrap(cx, scope) }
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
// https://html.spec.whatwg.org/multipage/#run-a-worker
|
||||
pub fn run_serviceworker_scope(scope_things: ScopeThings,
|
||||
own_sender: Sender<ServiceWorkerScriptMsg>,
|
||||
receiver: Receiver<ServiceWorkerScriptMsg>,
|
||||
devtools_receiver: IpcReceiver<DevtoolScriptControlMsg>,
|
||||
swmanager_sender: IpcSender<ServiceWorkerMsg>,
|
||||
scope_url: ServoUrl) {
|
||||
let ScopeThings { script_url,
|
||||
init,
|
||||
worker_load_origin,
|
||||
.. } = scope_things;
|
||||
pub fn run_serviceworker_scope(
|
||||
scope_things: ScopeThings,
|
||||
own_sender: Sender<ServiceWorkerScriptMsg>,
|
||||
receiver: Receiver<ServiceWorkerScriptMsg>,
|
||||
devtools_receiver: IpcReceiver<DevtoolScriptControlMsg>,
|
||||
swmanager_sender: IpcSender<ServiceWorkerMsg>,
|
||||
scope_url: ServoUrl,
|
||||
) {
|
||||
let ScopeThings {
|
||||
script_url,
|
||||
init,
|
||||
worker_load_origin,
|
||||
..
|
||||
} = scope_things;
|
||||
|
||||
let serialized_worker_url = script_url.to_string();
|
||||
let origin = GlobalScope::current().expect("No current global object").origin().immutable().clone();
|
||||
thread::Builder::new().name(format!("ServiceWorker for {}", serialized_worker_url)).spawn(move || {
|
||||
thread_state::initialize(ThreadState::SCRIPT | ThreadState::IN_WORKER);
|
||||
let roots = RootCollection::new();
|
||||
let _stack_roots = ThreadLocalStackRoots::new(&roots);
|
||||
let origin = GlobalScope::current()
|
||||
.expect("No current global object")
|
||||
.origin()
|
||||
.immutable()
|
||||
.clone();
|
||||
thread::Builder::new()
|
||||
.name(format!("ServiceWorker for {}", serialized_worker_url))
|
||||
.spawn(move || {
|
||||
thread_state::initialize(ThreadState::SCRIPT | ThreadState::IN_WORKER);
|
||||
let roots = RootCollection::new();
|
||||
let _stack_roots = ThreadLocalStackRoots::new(&roots);
|
||||
|
||||
let WorkerScriptLoadOrigin { referrer_url, referrer_policy, pipeline_id } = worker_load_origin;
|
||||
let WorkerScriptLoadOrigin {
|
||||
referrer_url,
|
||||
referrer_policy,
|
||||
pipeline_id,
|
||||
} = worker_load_origin;
|
||||
|
||||
let request = RequestInit {
|
||||
url: script_url.clone(),
|
||||
destination: Destination::ServiceWorker,
|
||||
credentials_mode: CredentialsMode::Include,
|
||||
use_url_credentials: true,
|
||||
pipeline_id: pipeline_id,
|
||||
referrer_url: referrer_url,
|
||||
referrer_policy: referrer_policy,
|
||||
origin,
|
||||
.. RequestInit::default()
|
||||
};
|
||||
let request = RequestInit {
|
||||
url: script_url.clone(),
|
||||
destination: Destination::ServiceWorker,
|
||||
credentials_mode: CredentialsMode::Include,
|
||||
use_url_credentials: true,
|
||||
pipeline_id: pipeline_id,
|
||||
referrer_url: referrer_url,
|
||||
referrer_policy: referrer_policy,
|
||||
origin,
|
||||
..RequestInit::default()
|
||||
};
|
||||
|
||||
let (url, source) = match load_whole_resource(request,
|
||||
&init.resource_threads.sender()) {
|
||||
Err(_) => {
|
||||
println!("error loading script {}", serialized_worker_url);
|
||||
return;
|
||||
let (url, source) =
|
||||
match load_whole_resource(request, &init.resource_threads.sender()) {
|
||||
Err(_) => {
|
||||
println!("error loading script {}", serialized_worker_url);
|
||||
return;
|
||||
},
|
||||
Ok((metadata, bytes)) => {
|
||||
(metadata.final_url, String::from_utf8(bytes).unwrap())
|
||||
},
|
||||
};
|
||||
|
||||
let runtime = unsafe { new_rt_and_cx() };
|
||||
|
||||
let (devtools_mpsc_chan, devtools_mpsc_port) = channel();
|
||||
route_ipc_receiver_to_new_servo_sender(devtools_receiver, devtools_mpsc_chan);
|
||||
// TODO XXXcreativcoder use this timer_ipc_port, when we have a service worker instance here
|
||||
let (timer_ipc_chan, _timer_ipc_port) = ipc::channel().unwrap();
|
||||
let (timer_chan, timer_port) = channel();
|
||||
let global = ServiceWorkerGlobalScope::new(
|
||||
init,
|
||||
url,
|
||||
devtools_mpsc_port,
|
||||
runtime,
|
||||
own_sender,
|
||||
receiver,
|
||||
timer_ipc_chan,
|
||||
timer_port,
|
||||
swmanager_sender,
|
||||
scope_url,
|
||||
);
|
||||
let scope = global.upcast::<WorkerGlobalScope>();
|
||||
|
||||
unsafe {
|
||||
// Handle interrupt requests
|
||||
JS_AddInterruptCallback(scope.get_cx(), Some(interrupt_callback));
|
||||
}
|
||||
Ok((metadata, bytes)) => {
|
||||
(metadata.final_url, String::from_utf8(bytes).unwrap())
|
||||
}
|
||||
};
|
||||
|
||||
let runtime = unsafe { new_rt_and_cx() };
|
||||
scope.execute_script(DOMString::from(source));
|
||||
// Service workers are time limited
|
||||
thread::Builder::new()
|
||||
.name("SWTimeoutThread".to_owned())
|
||||
.spawn(move || {
|
||||
let sw_lifetime_timeout = PREFS
|
||||
.get("dom.serviceworker.timeout_seconds")
|
||||
.as_u64()
|
||||
.unwrap();
|
||||
thread::sleep(Duration::new(sw_lifetime_timeout, 0));
|
||||
let _ = timer_chan.send(());
|
||||
})
|
||||
.expect("Thread spawning failed");
|
||||
|
||||
let (devtools_mpsc_chan, devtools_mpsc_port) = channel();
|
||||
route_ipc_receiver_to_new_servo_sender(devtools_receiver, devtools_mpsc_chan);
|
||||
// TODO XXXcreativcoder use this timer_ipc_port, when we have a service worker instance here
|
||||
let (timer_ipc_chan, _timer_ipc_port) = ipc::channel().unwrap();
|
||||
let (timer_chan, timer_port) = channel();
|
||||
let global = ServiceWorkerGlobalScope::new(
|
||||
init, url, devtools_mpsc_port, runtime,
|
||||
own_sender, receiver,
|
||||
timer_ipc_chan, timer_port, swmanager_sender, scope_url);
|
||||
let scope = global.upcast::<WorkerGlobalScope>();
|
||||
|
||||
unsafe {
|
||||
// Handle interrupt requests
|
||||
JS_AddInterruptCallback(scope.get_cx(), Some(interrupt_callback));
|
||||
}
|
||||
|
||||
scope.execute_script(DOMString::from(source));
|
||||
// Service workers are time limited
|
||||
thread::Builder::new().name("SWTimeoutThread".to_owned()).spawn(move || {
|
||||
let sw_lifetime_timeout = PREFS.get("dom.serviceworker.timeout_seconds").as_u64().unwrap();
|
||||
thread::sleep(Duration::new(sw_lifetime_timeout, 0));
|
||||
let _ = timer_chan.send(());
|
||||
}).expect("Thread spawning failed");
|
||||
|
||||
global.dispatch_activate();
|
||||
let reporter_name = format!("service-worker-reporter-{}", random::<u64>());
|
||||
scope.upcast::<GlobalScope>().mem_profiler_chan().run_with_memory_reporting(|| {
|
||||
// Step 29, Run the responsible event loop specified by inside settings until it is destroyed.
|
||||
// The worker processing model remains on this step until the event loop is destroyed,
|
||||
// which happens after the closing flag is set to true.
|
||||
while !scope.is_closing() {
|
||||
run_worker_event_loop(&*global, None);
|
||||
}
|
||||
}, reporter_name, scope.script_chan(), CommonScriptMsg::CollectReports);
|
||||
}).expect("Thread spawning failed");
|
||||
global.dispatch_activate();
|
||||
let reporter_name = format!("service-worker-reporter-{}", random::<u64>());
|
||||
scope
|
||||
.upcast::<GlobalScope>()
|
||||
.mem_profiler_chan()
|
||||
.run_with_memory_reporting(
|
||||
|| {
|
||||
// Step 29, Run the responsible event loop specified
|
||||
// by inside settings until it is destroyed.
|
||||
// The worker processing model remains on this step
|
||||
// until the event loop is destroyed,
|
||||
// which happens after the closing flag is set to true.
|
||||
while !scope.is_closing() {
|
||||
run_worker_event_loop(&*global, None);
|
||||
}
|
||||
},
|
||||
reporter_name,
|
||||
scope.script_chan(),
|
||||
CommonScriptMsg::CollectReports,
|
||||
);
|
||||
})
|
||||
.expect("Thread spawning failed");
|
||||
}
|
||||
|
||||
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::GetCachedMessages(pipe_id, message_types, sender) =>
|
||||
devtools::handle_get_cached_messages(pipe_id, message_types, sender),
|
||||
DevtoolScriptControlMsg::WantsLiveNotifications(_pipe_id, bool_val) =>
|
||||
devtools::handle_wants_live_notifications(self.upcast(), bool_val),
|
||||
DevtoolScriptControlMsg::EvaluateJS(_pipe_id, string, sender) => {
|
||||
devtools::handle_evaluate_js(self.upcast(), string, sender)
|
||||
},
|
||||
DevtoolScriptControlMsg::GetCachedMessages(pipe_id, message_types, sender) => {
|
||||
devtools::handle_get_cached_messages(pipe_id, message_types, 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::FromServiceWorker(msg) => {
|
||||
self.handle_script_event(msg);
|
||||
true
|
||||
}
|
||||
},
|
||||
MixedMessage::FromTimeoutThread(_) => {
|
||||
let _ = self.swmanager_sender.send(ServiceWorkerMsg::Timeout(self.scope_url.clone()));
|
||||
let _ = self
|
||||
.swmanager_sender
|
||||
.send(ServiceWorkerMsg::Timeout(self.scope_url.clone()));
|
||||
false
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -339,7 +390,8 @@ 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());
|
||||
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());
|
||||
|
@ -360,7 +412,7 @@ impl ServiceWorkerGlobalScope {
|
|||
|
||||
pub fn script_chan(&self) -> Box<ScriptChan + Send> {
|
||||
Box::new(ServiceWorkerChan {
|
||||
sender: self.own_sender.clone()
|
||||
sender: self.own_sender.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -373,9 +425,8 @@ impl ServiceWorkerGlobalScope {
|
|||
|
||||
#[allow(unsafe_code)]
|
||||
unsafe extern "C" fn interrupt_callback(cx: *mut JSContext) -> bool {
|
||||
let worker =
|
||||
DomRoot::downcast::<WorkerGlobalScope>(GlobalScope::from_context(cx))
|
||||
.expect("global is not a worker scope");
|
||||
let worker = DomRoot::downcast::<WorkerGlobalScope>(GlobalScope::from_context(cx))
|
||||
.expect("global is not a worker scope");
|
||||
assert!(worker.is::<ServiceWorkerGlobalScope>());
|
||||
|
||||
// A false response causes the script to terminate
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue