Integrate service worker manager thread

This commit is contained in:
Rahul Sharma 2016-06-09 18:52:52 +05:30
parent e8fa02a07f
commit 1e6293ea1d
39 changed files with 764 additions and 582 deletions

View file

@ -65,12 +65,11 @@ use js::jsval::UndefinedValue;
use js::rust::Runtime;
use mem::heap_size_of_self_and_children;
use msg::constellation_msg::{FrameType, LoadData, PanicMsg, PipelineId, PipelineNamespace};
use msg::constellation_msg::{ReferrerPolicy, SubpageId, WindowSizeType};
use net_traits::LoadData as NetLoadData;
use msg::constellation_msg::{SubpageId, WindowSizeType, ReferrerPolicy};
use net_traits::bluetooth_thread::BluetoothMethodMsg;
use net_traits::image_cache_thread::{ImageCacheChan, ImageCacheResult, ImageCacheThread};
use net_traits::{AsyncResponseTarget, CoreResourceMsg, LoadConsumer, LoadContext, Metadata, ResourceThreads};
use net_traits::{RequestSource, CustomResponse, CustomResponseSender, IpcSend};
use net_traits::{IpcSend, LoadData as NetLoadData};
use network_listener::NetworkListener;
use parse::ParserRoot;
use parse::html::{ParseContext, parse_html};
@ -216,8 +215,7 @@ enum MixedMessage {
FromScript(MainThreadScriptMsg),
FromDevtools(DevtoolScriptControlMsg),
FromImageCache(ImageCacheResult),
FromScheduler(TimerEvent),
FromNetwork(IpcSender<Option<CustomResponse>>),
FromScheduler(TimerEvent)
}
/// Messages used to control the script event loop
@ -342,12 +340,6 @@ pub struct ScriptThread {
/// events in the event queue.
chan: MainThreadScriptChan,
/// A handle to network event messages
custom_message_chan: IpcSender<CustomResponseSender>,
/// The port which receives a sender from the network
custom_message_port: Receiver<CustomResponseSender>,
dom_manipulation_task_source: DOMManipulationTaskSource,
user_interaction_task_source: UserInteractionTaskSource,
@ -509,10 +501,10 @@ impl ScriptThread {
}
// stores a service worker registration
pub fn set_registration(scope_url: Url, registration:&ServiceWorkerRegistration) {
pub fn set_registration(scope_url: Url, registration:&ServiceWorkerRegistration, pipeline_id: PipelineId) {
SCRIPT_THREAD_ROOT.with(|root| {
let script_thread = unsafe { &*root.get().unwrap() };
script_thread.handle_serviceworker_registration(scope_url, registration);
script_thread.handle_serviceworker_registration(scope_url, registration, pipeline_id);
});
}
@ -562,9 +554,6 @@ impl ScriptThread {
let (ipc_devtools_sender, ipc_devtools_receiver) = ipc::channel().unwrap();
let devtools_port = ROUTER.route_ipc_receiver_to_new_mpsc_receiver(ipc_devtools_receiver);
let (ipc_custom_resp_chan, ipc_custom_resp_port) = ipc::channel().unwrap();
let custom_msg_port = ROUTER.route_ipc_receiver_to_new_mpsc_receiver(ipc_custom_resp_port);
// Ask the router to proxy IPC messages from the image cache thread to us.
let (ipc_image_cache_channel, ipc_image_cache_port) = ipc::channel().unwrap();
let image_cache_port =
@ -590,8 +579,6 @@ impl ScriptThread {
bluetooth_thread: state.bluetooth_thread,
port: port,
custom_message_chan: ipc_custom_resp_chan,
custom_message_port: custom_msg_port,
chan: MainThreadScriptChan(chan.clone()),
dom_manipulation_task_source: DOMManipulationTaskSource(chan.clone()),
@ -653,7 +640,7 @@ impl ScriptThread {
/// Handle incoming control messages.
fn handle_msgs(&self) -> bool {
use self::MixedMessage::{FromConstellation, FromDevtools, FromImageCache};
use self::MixedMessage::{FromScheduler, FromScript, FromNetwork};
use self::MixedMessage::{FromScheduler, FromScript};
// Handle pending resize events.
// Gather them first to avoid a double mut borrow on self.
@ -687,7 +674,6 @@ impl ScriptThread {
let mut timer_event_port = sel.handle(&self.timer_event_port);
let mut devtools_port = sel.handle(&self.devtools_port);
let mut image_cache_port = sel.handle(&self.image_cache_port);
let mut custom_message_port = sel.handle(&self.custom_message_port);
unsafe {
script_port.add();
control_port.add();
@ -696,7 +682,6 @@ impl ScriptThread {
devtools_port.add();
}
image_cache_port.add();
custom_message_port.add();
}
let ret = sel.wait();
if ret == script_port.id() {
@ -709,8 +694,6 @@ impl ScriptThread {
FromDevtools(self.devtools_port.recv().unwrap())
} else if ret == image_cache_port.id() {
FromImageCache(self.image_cache_port.recv().unwrap())
} else if ret == custom_message_port.id() {
FromNetwork(self.custom_message_port.recv().unwrap())
} else {
panic!("unexpected select result")
}
@ -778,10 +761,7 @@ impl ScriptThread {
Err(_) => match self.timer_event_port.try_recv() {
Err(_) => match self.devtools_port.try_recv() {
Err(_) => match self.image_cache_port.try_recv() {
Err(_) => match self.custom_message_port.try_recv() {
Err(_) => break,
Ok(ev) => event = FromNetwork(ev)
},
Err(_) => break,
Ok(ev) => event = FromImageCache(ev),
},
Ok(ev) => event = FromDevtools(ev),
@ -807,7 +787,6 @@ impl ScriptThread {
},
FromConstellation(inner_msg) => self.handle_msg_from_constellation(inner_msg),
FromScript(inner_msg) => self.handle_msg_from_script(inner_msg),
FromNetwork(inner_msg) => self.handle_msg_from_network(inner_msg),
FromScheduler(inner_msg) => self.handle_timer_event(inner_msg),
FromDevtools(inner_msg) => self.handle_msg_from_devtools(inner_msg),
FromImageCache(inner_msg) => self.handle_msg_from_image_cache(inner_msg),
@ -866,8 +845,7 @@ impl ScriptThread {
_ => ScriptThreadEventCategory::ScriptEvent
}
},
MixedMessage::FromScheduler(_) => ScriptThreadEventCategory::TimerEvent,
MixedMessage::FromNetwork(_) => ScriptThreadEventCategory::NetworkEvent
MixedMessage::FromScheduler(_) => ScriptThreadEventCategory::TimerEvent
}
}
@ -1056,12 +1034,6 @@ impl ScriptThread {
msg.responder.unwrap().respond(msg.image_response);
}
fn handle_msg_from_network(&self, msg: IpcSender<Option<CustomResponse>>) {
// We may detect controlling service workers here
// We send None as default
let _ = msg.send(None);
}
fn handle_webdriver_msg(&self, pipeline_id: PipelineId, msg: WebDriverScriptCommand) {
let context = self.root_browsing_context();
match msg {
@ -1457,8 +1429,33 @@ impl ScriptThread {
}
}
fn handle_serviceworker_registration(&self, scope: Url, registration: &ServiceWorkerRegistration) {
self.registration_map.borrow_mut().insert(scope, JS::from_ref(registration));
fn handle_serviceworker_registration(&self,
scope: Url,
registration: &ServiceWorkerRegistration,
pipeline_id: PipelineId) {
{
let ref mut reg_ref = *self.registration_map.borrow_mut();
// according to spec we should replace if an older registration exists for
// same scope otherwise just insert the new one
let _ = reg_ref.remove(&scope);
reg_ref.insert(scope.clone(), JS::from_ref(registration));
}
// send ScopeThings to sw-manager
let ref maybe_registration_ref = *self.registration_map.borrow();
let maybe_registration = match maybe_registration_ref.get(&scope) {
Some(r) => r,
None => return
};
if let Some(context) = self.root_browsing_context().find(pipeline_id) {
let window = context.active_window();
let global_ref = GlobalRef::Window(window.r());
let script_url = maybe_registration.get_installed().get_script_url();
let scope_things = ServiceWorkerRegistration::create_scope_things(global_ref, script_url);
let _ = self.constellation_chan.send(ConstellationMsg::RegisterServiceWorker(scope_things, scope));
} else {
warn!("Registration failed for {}", pipeline_id);
}
}
/// Handles a request for the window title.
@ -1601,7 +1598,6 @@ impl ScriptThread {
HistoryTraversalTaskSource(history_sender.clone()),
self.file_reading_task_source.clone(),
self.image_cache_channel.clone(),
self.custom_message_chan.clone(),
self.image_cache_thread.clone(),
self.resource_threads.clone(),
self.bluetooth_thread.clone(),
@ -2112,8 +2108,7 @@ impl ScriptThread {
pipeline_id: Some(id),
credentials_flag: true,
referrer_policy: load_data.referrer_policy,
referrer_url: load_data.referrer_url,
source: RequestSource::Window(self.custom_message_chan.clone())
referrer_url: load_data.referrer_url
}, LoadConsumer::Listener(response_target), None)).unwrap();
self.incomplete_loads.borrow_mut().push(incomplete);