Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Splitting ScriptMsg into various enums... #7006

Merged
merged 1 commit into from Aug 15, 2015
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -5,11 +5,12 @@
//! Tracking of pending loads in a document.
//! https://html.spec.whatwg.org/multipage/#the-end

use script_task::{ScriptMsg, ScriptChan};
use script_task::MainThreadScriptMsg;
use msg::constellation_msg::{PipelineId};
use net_traits::{Metadata, load_whole_resource, ResourceTask, PendingAsyncLoad};
use net_traits::AsyncResponseTarget;
use std::sync::Arc;
use std::sync::mpsc::Sender;
use url::Url;

#[derive(JSTraceable, PartialEq, Clone, Debug, HeapSizeOf)]
@@ -46,7 +47,7 @@ pub struct DocumentLoader {
#[derive(JSTraceable, HeapSizeOf)]
pub struct NotifierData {
#[ignore_heap_size_of = "trait objects are hard"]
pub script_chan: Box<ScriptChan + Send>,
pub script_chan: Sender<MainThreadScriptMsg>,
pub pipeline: PipelineId,
}

@@ -100,7 +101,7 @@ impl DocumentLoader {

if let Some(NotifierData { ref script_chan, pipeline }) = self.notifier_data {
if !self.is_blocked() {
script_chan.send(ScriptMsg::DocumentLoadsComplete(pipeline)).unwrap();
script_chan.send(MainThreadScriptMsg::DocumentLoadsComplete(pipeline)).unwrap();
}
}
}
@@ -15,7 +15,7 @@ use dom::document::DocumentHelpers;
use dom::workerglobalscope::{WorkerGlobalScope, WorkerGlobalScopeHelpers};
use dom::window::{self, WindowHelpers, ScriptHelpers};
use devtools_traits::ScriptToDevtoolsControlMsg;
use script_task::{ScriptChan, ScriptPort, ScriptMsg, ScriptTask};
use script_task::{ScriptChan, ScriptPort, CommonScriptMsg, ScriptTask};

use msg::constellation_msg::{ConstellationChan, PipelineId, WorkerId};
use net_traits::ResourceTask;
@@ -168,7 +168,7 @@ impl<'a> GlobalRef<'a> {

/// Process a single event as if it were the next event in the task queue for
/// this global.
pub fn process_event(&self, msg: ScriptMsg) {
pub fn process_event(&self, msg: CommonScriptMsg) {
match *self {
GlobalRef::Window(_) => ScriptTask::process_event(msg),
GlobalRef::Worker(ref worker) => worker.process_event(msg),
@@ -25,7 +25,7 @@
use dom::bindings::js::Root;
use dom::bindings::utils::{Reflector, Reflectable};
use dom::bindings::trace::trace_reflector;
use script_task::{ScriptMsg, ScriptChan};
use script_task::{ScriptChan, CommonScriptMsg};

use js::jsapi::{JSContext, JSTracer};

@@ -130,7 +130,7 @@ impl<T: Reflectable> Drop for Trusted<T> {
// It's possible this send will fail if the script task
// has already exited. There's not much we can do at this
// point though.
let msg = ScriptMsg::RefcountCleanup(TrustedReference(self.ptr));
let msg = CommonScriptMsg::RefcountCleanup(TrustedReference(self.ptr));
let _ = self.script_chan.send(msg);
}
}
@@ -22,8 +22,7 @@ use dom::messageevent::MessageEvent;
use dom::worker::{TrustedWorkerAddress, WorkerMessageHandler, WorkerEventHandler, WorkerErrorHandler};
use dom::workerglobalscope::{WorkerGlobalScope, WorkerGlobalScopeHelpers};
use dom::workerglobalscope::{WorkerGlobalScopeTypeId, WorkerGlobalScopeInit};
use script_task::{ScriptTask, ScriptChan, ScriptMsg, TimerSource, ScriptPort};
use script_task::StackRootTLS;
use script_task::{ScriptTask, ScriptChan, TimerSource, ScriptPort, StackRootTLS, CommonScriptMsg};

use devtools_traits::DevtoolScriptControlMsg;
use msg::constellation_msg::PipelineId;
@@ -45,17 +44,25 @@ use std::mem::replace;
use std::rc::Rc;
use std::sync::mpsc::{Sender, Receiver, channel, Select, RecvError};

/// Messages used to control the worker event loops
pub enum WorkerScriptMsg {
/// Common variants associated with the script messages
Common(CommonScriptMsg),
/// Message sent through Worker.postMessage
DOMMessage(StructuredCloneData),
}

/// A ScriptChan that can be cloned freely and will silently send a TrustedWorkerAddress with
/// every message. While this SendableWorkerScriptChan is alive, the associated Worker object
/// will remain alive.
/// common event loop messages. While this SendableWorkerScriptChan is alive, the associated
/// Worker object will remain alive.
#[derive(JSTraceable, Clone)]
pub struct SendableWorkerScriptChan {
sender: Sender<(TrustedWorkerAddress, ScriptMsg)>,
sender: Sender<(TrustedWorkerAddress, CommonScriptMsg)>,
worker: TrustedWorkerAddress,
}

impl ScriptChan for SendableWorkerScriptChan {
fn send(&self, msg: ScriptMsg) -> Result<(), ()> {
fn send(&self, msg: CommonScriptMsg) -> Result<(), ()> {
return self.sender.send((self.worker.clone(), msg)).map_err(|_| ());
}

@@ -67,6 +74,39 @@ impl ScriptChan for SendableWorkerScriptChan {
}
}

/// A ScriptChan that can be cloned freely and will silently send a TrustedWorkerAddress with
/// worker event loop messages. While this SendableWorkerScriptChan is alive, the associated
/// Worker object will remain alive.
#[derive(JSTraceable, Clone)]
pub struct WorkerThreadWorkerChan {
sender: Sender<(TrustedWorkerAddress, WorkerScriptMsg)>,
worker: TrustedWorkerAddress,
}

impl ScriptChan for WorkerThreadWorkerChan {
fn send(&self, msg: CommonScriptMsg) -> Result<(), ()> {
return self.sender
.send((self.worker.clone(), WorkerScriptMsg::Common(msg)))
.map_err(|_| ());
}

fn clone(&self) -> Box<ScriptChan + Send> {
box WorkerThreadWorkerChan {
sender: self.sender.clone(),
worker: self.worker.clone(),
}
}
}

impl ScriptPort for Receiver<(TrustedWorkerAddress, WorkerScriptMsg)> {
fn recv(&self) -> CommonScriptMsg {
match self.recv().unwrap().1 {
WorkerScriptMsg::Common(script_msg) => script_msg,
WorkerScriptMsg::DOMMessage(_) => panic!("unexpected worker event message!"),
}
}
}

/// Set the `worker` field of a related DedicatedWorkerGlobalScope object to a particular
/// value for the duration of this object's lifetime. This ensures that the related Worker
/// object only lives as long as necessary (ie. while events are being executed), while
@@ -92,7 +132,7 @@ impl<'a> Drop for AutoWorkerReset<'a> {
}

enum MixedMessage {
FromWorker((TrustedWorkerAddress, ScriptMsg)),
FromWorker((TrustedWorkerAddress, WorkerScriptMsg)),
FromDevtools(DevtoolScriptControlMsg),
}

@@ -103,9 +143,9 @@ pub struct DedicatedWorkerGlobalScope {
workerglobalscope: WorkerGlobalScope,
id: PipelineId,
#[ignore_heap_size_of = "Defined in std"]
receiver: Receiver<(TrustedWorkerAddress, ScriptMsg)>,
receiver: Receiver<(TrustedWorkerAddress, WorkerScriptMsg)>,
#[ignore_heap_size_of = "Defined in std"]
own_sender: Sender<(TrustedWorkerAddress, ScriptMsg)>,
own_sender: Sender<(TrustedWorkerAddress, WorkerScriptMsg)>,
#[ignore_heap_size_of = "Trusted<T> has unclear ownership like JS<T>"]
worker: DOMRefCell<Option<TrustedWorkerAddress>>,
#[ignore_heap_size_of = "Can't measure trait objects"]
@@ -120,8 +160,8 @@ impl DedicatedWorkerGlobalScope {
devtools_port: Receiver<DevtoolScriptControlMsg>,
runtime: Rc<Runtime>,
parent_sender: Box<ScriptChan+Send>,
own_sender: Sender<(TrustedWorkerAddress, ScriptMsg)>,
receiver: Receiver<(TrustedWorkerAddress, ScriptMsg)>)
own_sender: Sender<(TrustedWorkerAddress, WorkerScriptMsg)>,
receiver: Receiver<(TrustedWorkerAddress, WorkerScriptMsg)>)
-> DedicatedWorkerGlobalScope {
DedicatedWorkerGlobalScope {
workerglobalscope: WorkerGlobalScope::new_inherited(
@@ -141,8 +181,8 @@ impl DedicatedWorkerGlobalScope {
devtools_port: Receiver<DevtoolScriptControlMsg>,
runtime: Rc<Runtime>,
parent_sender: Box<ScriptChan+Send>,
own_sender: Sender<(TrustedWorkerAddress, ScriptMsg)>,
receiver: Receiver<(TrustedWorkerAddress, ScriptMsg)>)
own_sender: Sender<(TrustedWorkerAddress, WorkerScriptMsg)>,
receiver: Receiver<(TrustedWorkerAddress, WorkerScriptMsg)>)
-> Root<DedicatedWorkerGlobalScope> {
let scope = box DedicatedWorkerGlobalScope::new_inherited(
init, worker_url, id, devtools_port, runtime.clone(), parent_sender,
@@ -158,8 +198,8 @@ impl DedicatedWorkerGlobalScope {
devtools_ipc_port: IpcReceiver<DevtoolScriptControlMsg>,
worker: TrustedWorkerAddress,
parent_sender: Box<ScriptChan+Send>,
own_sender: Sender<(TrustedWorkerAddress, ScriptMsg)>,
receiver: Receiver<(TrustedWorkerAddress, ScriptMsg)>) {
own_sender: Sender<(TrustedWorkerAddress, WorkerScriptMsg)>,
receiver: Receiver<(TrustedWorkerAddress, WorkerScriptMsg)>) {
let serialized_worker_url = worker_url.serialize();
spawn_named(format!("WebWorker for {}", serialized_worker_url), move || {
task_state::initialize(SCRIPT | IN_WORKER);
@@ -170,7 +210,7 @@ impl DedicatedWorkerGlobalScope {
let (url, source) = match load_whole_resource(&init.resource_task, worker_url) {
Err(_) => {
println!("error loading script {}", serialized_worker_url);
parent_sender.send(ScriptMsg::RunnableMsg(
parent_sender.send(CommonScriptMsg::RunnableMsg(
box WorkerEventHandler::new(worker))).unwrap();
return;
}
@@ -201,7 +241,7 @@ impl DedicatedWorkerGlobalScope {
while let Ok(event) = global.receive_event() {
global.handle_event(event);
}
}, reporter_name, parent_sender, ScriptMsg::CollectReports);
}, reporter_name, parent_sender, CommonScriptMsg::CollectReports);
});
}
}
@@ -210,12 +250,12 @@ pub trait DedicatedWorkerGlobalScopeHelpers {
fn script_chan(self) -> Box<ScriptChan+Send>;
fn pipeline(self) -> PipelineId;
fn new_script_pair(self) -> (Box<ScriptChan+Send>, Box<ScriptPort+Send>);
fn process_event(self, msg: ScriptMsg);
fn process_event(self, msg: CommonScriptMsg);
}

impl<'a> DedicatedWorkerGlobalScopeHelpers for &'a DedicatedWorkerGlobalScope {
fn script_chan(self) -> Box<ScriptChan+Send> {
box SendableWorkerScriptChan {
box WorkerThreadWorkerChan {
sender: self.own_sender.clone(),
worker: self.worker.borrow().as_ref().unwrap().clone(),
}
@@ -234,13 +274,13 @@ impl<'a> DedicatedWorkerGlobalScopeHelpers for &'a DedicatedWorkerGlobalScope {
(chan, box rx)
}

fn process_event(self, msg: ScriptMsg) {
self.handle_script_event(msg);
fn process_event(self, msg: CommonScriptMsg) {
self.handle_script_event(WorkerScriptMsg::Common(msg));
}
}

trait PrivateDedicatedWorkerGlobalScopeHelpers {
fn handle_script_event(self, msg: ScriptMsg);
fn handle_script_event(self, msg: WorkerScriptMsg);
fn dispatch_error_to_worker(self, &ErrorEvent);
fn receive_event(self) -> Result<MixedMessage, RecvError>;
fn handle_event(self, event: MixedMessage);
@@ -272,9 +312,9 @@ impl<'a> PrivateDedicatedWorkerGlobalScopeHelpers for &'a DedicatedWorkerGlobalS
}
}

fn handle_script_event(self, msg: ScriptMsg) {
fn handle_script_event(self, msg: WorkerScriptMsg) {
match msg {
ScriptMsg::DOMMessage(data) => {
WorkerScriptMsg::DOMMessage(data) => {
let scope = WorkerGlobalScopeCast::from_ref(self);
let target = EventTargetCast::from_ref(self);
let _ar = JSAutoRequest::new(scope.get_cx());
@@ -283,24 +323,27 @@ impl<'a> PrivateDedicatedWorkerGlobalScopeHelpers for &'a DedicatedWorkerGlobalS
data.read(GlobalRef::Worker(scope), message.handle_mut());
MessageEvent::dispatch_jsval(target, GlobalRef::Worker(scope), message.handle());
},
ScriptMsg::RunnableMsg(runnable) => {
WorkerScriptMsg::Common(CommonScriptMsg::RunnableMsg(runnable)) => {
runnable.handler()
},
ScriptMsg::RefcountCleanup(addr) => {
WorkerScriptMsg::Common(CommonScriptMsg::RefcountCleanup(addr)) => {
LiveDOMReferences::cleanup(addr);
}
ScriptMsg::FireTimer(TimerSource::FromWorker, timer_id) => {
},
WorkerScriptMsg::Common(
CommonScriptMsg::FireTimer(TimerSource::FromWorker, timer_id)) => {
let scope = WorkerGlobalScopeCast::from_ref(self);
scope.handle_fire_timer(timer_id);
}
ScriptMsg::CollectReports(reports_chan) => {
},
WorkerScriptMsg::Common(CommonScriptMsg::CollectReports(reports_chan)) => {
let scope = WorkerGlobalScopeCast::from_ref(self);
let cx = scope.get_cx();
let path_seg = format!("url({})", scope.get_url());
let reports = ScriptTask::get_reports(cx, path_seg);
reports_chan.send(reports);
}
_ => panic!("Unexpected message"),
},
WorkerScriptMsg::Common(CommonScriptMsg::FireTimer(_, _)) => {
panic!("obtained a fire timeout from window for the worker!")
},
}
}

@@ -331,7 +374,7 @@ impl<'a> PrivateDedicatedWorkerGlobalScopeHelpers for &'a DedicatedWorkerGlobalS
let line_num = errorevent.Lineno();
let col_num = errorevent.Colno();
let worker = self.worker.borrow().as_ref().unwrap().clone();
self.parent_sender.send(ScriptMsg::RunnableMsg(
self.parent_sender.send(CommonScriptMsg::RunnableMsg(
box WorkerErrorHandler::new(worker, msg, file_name, line_num, col_num))).unwrap();
}
}
@@ -341,7 +384,7 @@ impl<'a> DedicatedWorkerGlobalScopeMethods for &'a DedicatedWorkerGlobalScope {
fn PostMessage(self, cx: *mut JSContext, message: HandleValue) -> ErrorResult {
let data = try!(StructuredCloneData::write(cx, message));
let worker = self.worker.borrow().as_ref().unwrap().clone();
self.parent_sender.send(ScriptMsg::RunnableMsg(
self.parent_sender.send(CommonScriptMsg::RunnableMsg(
box WorkerMessageHandler::new(worker, data))).unwrap();
Ok(())
}
@@ -22,7 +22,7 @@ use encoding::types::{EncodingRef, DecoderTrap};
use encoding::label::encoding_from_whatwg_label;
use hyper::mime::{Mime, Attr};
use std::sync::mpsc;
use script_task::{ScriptChan, ScriptMsg, Runnable, ScriptPort};
use script_task::{ScriptChan, Runnable, ScriptPort, CommonScriptMsg};
use std::cell::{Cell, RefCell};
use std::sync::mpsc::Receiver;
use util::str::DOMString;
@@ -408,22 +408,22 @@ fn perform_annotated_read_operation(gen_id: GenerationId, data: ReadMetaData, bl
let chan = &script_chan;
// Step 4
let task = box FileReaderEvent::ProcessRead(filereader.clone(), gen_id);
chan.send(ScriptMsg::RunnableMsg(task)).unwrap();
chan.send(CommonScriptMsg::RunnableMsg(task)).unwrap();

let task = box FileReaderEvent::ProcessReadData(filereader.clone(),
gen_id, DOMString::new());
chan.send(ScriptMsg::RunnableMsg(task)).unwrap();
chan.send(CommonScriptMsg::RunnableMsg(task)).unwrap();

let bytes = match blob_contents.recv() {
Ok(bytes) => bytes,
Err(_) => {
let task = box FileReaderEvent::ProcessReadError(filereader,
gen_id, DOMErrorName::NotFoundError);
chan.send(ScriptMsg::RunnableMsg(task)).unwrap();
chan.send(CommonScriptMsg::RunnableMsg(task)).unwrap();
return;
}
};

let task = box FileReaderEvent::ProcessReadEOF(filereader, gen_id, data, bytes);
chan.send(ScriptMsg::RunnableMsg(task)).unwrap();
chan.send(CommonScriptMsg::RunnableMsg(task)).unwrap();
}
@@ -34,7 +34,7 @@ use hyper::header::ContentType;
use hyper::mime;
use msg::constellation_msg::LoadData;
use util::str::DOMString;
use script_task::{ScriptChan, ScriptMsg};
use script_task::{ScriptChan, MainThreadScriptMsg};
use url::UrlParser;
use url::form_urlencoded::serialize;
use string_cache::Atom;
@@ -232,7 +232,8 @@ impl<'a> HTMLFormElementHelpers for &'a HTMLFormElement {
}

// This is wrong. https://html.spec.whatwg.org/multipage/#planned-navigation
win.r().script_chan().send(ScriptMsg::Navigate(win.r().pipeline(), load_data)).unwrap();
win.r().main_thread_script_chan().send(MainThreadScriptMsg::Navigate(
win.r().pipeline(), load_data)).unwrap();
}

fn get_form_dataset<'b>(self, submitter: Option<FormSubmitter<'b>>) -> Vec<FormDatum> {
@@ -23,7 +23,7 @@ use dom::htmlelement::{HTMLElement, HTMLElementTypeId};
use dom::node::{document_from_node, Node, NodeTypeId, NodeHelpers, NodeDamage, window_from_node};
use dom::virtualmethods::VirtualMethods;
use dom::window::WindowHelpers;
use script_task::{Runnable, ScriptChan, ScriptMsg};
use script_task::{Runnable, ScriptChan, CommonScriptMsg};
use util::str::DOMString;
use string_cache::Atom;

@@ -140,9 +140,9 @@ impl<'a> PrivateHTMLImageElementHelpers for &'a HTMLImageElement {
// Return the image via a message to the script task, which marks the element
// as dirty and triggers a reflow.
let image_response = message.to().unwrap();
script_chan.send(ScriptMsg::RunnableMsg(box ImageResponseHandlerRunnable::new(
trusted_node.clone(),
image_response))).unwrap();
script_chan.send(CommonScriptMsg::RunnableMsg(
box ImageResponseHandlerRunnable::new(
trusted_node.clone(), image_response))).unwrap();
});

image_cache.request_image(img_url,
@@ -345,4 +345,3 @@ impl<'a> VirtualMethods for &'a HTMLImageElement {
}
}
}

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.