Make all task source runnables cancellable

Implement all Runnable methods on CancellableRunnable to redirect to their inner runnable
This commit is contained in:
Connor Brewster 2016-07-11 22:59:53 -06:00
parent afc0ccb48d
commit 5f7324a9a5
14 changed files with 69 additions and 58 deletions

View file

@ -1488,9 +1488,10 @@ impl Document {
update_with_current_time_ms(&self.dom_content_loaded_event_start); update_with_current_time_ms(&self.dom_content_loaded_event_start);
self.window().dom_manipulation_task_source().queue_event(self.upcast(), atom!("DOMContentLoaded"), let window = self.window();
EventBubbles::Bubbles, EventCancelable::NotCancelable); window.dom_manipulation_task_source().queue_event(self.upcast(), atom!("DOMContentLoaded"),
self.window().reflow(ReflowGoal::ForDisplay, EventBubbles::Bubbles, EventCancelable::NotCancelable, window);
window.reflow(ReflowGoal::ForDisplay,
ReflowQueryType::NoQuery, ReflowQueryType::NoQuery,
ReflowReason::DOMContentLoaded); ReflowReason::DOMContentLoaded);

View file

@ -19,7 +19,6 @@ use script_thread::Runnable;
use std::cell::Cell; use std::cell::Cell;
use string_cache::Atom; use string_cache::Atom;
use task_source::TaskSource; use task_source::TaskSource;
use task_source::dom_manipulation::DOMManipulationTask;
#[dom_struct] #[dom_struct]
pub struct HTMLDetailsElement { pub struct HTMLDetailsElement {
@ -72,14 +71,13 @@ impl VirtualMethods for HTMLDetailsElement {
self.toggle_counter.set(counter); self.toggle_counter.set(counter);
let window = window_from_node(self); let window = window_from_node(self);
let window = window.r();
let task_source = window.dom_manipulation_task_source(); let task_source = window.dom_manipulation_task_source();
let details = Trusted::new(self); let details = Trusted::new(self);
let runnable = box DetailsNotificationRunnable { let runnable = DetailsNotificationRunnable {
element: details, element: details,
toggle_number: counter toggle_number: counter
}; };
let _ = task_source.queue(DOMManipulationTask(runnable)); let _ = task_source.queue(runnable, window.r());
} }
} }
} }

View file

@ -53,7 +53,6 @@ use string_cache::Atom;
use style::attr::AttrValue; use style::attr::AttrValue;
use style::str::split_html_space_chars; use style::str::split_html_space_chars;
use task_source::TaskSource; use task_source::TaskSource;
use task_source::dom_manipulation::DOMManipulationTask;
use url::form_urlencoded; use url::form_urlencoded;
#[derive(JSTraceable, PartialEq, Clone, Copy, HeapSizeOf)] #[derive(JSTraceable, PartialEq, Clone, Copy, HeapSizeOf)]
@ -475,7 +474,7 @@ impl HTMLFormElement {
self.generation_id.set(GenerationId(prev_id + 1)); self.generation_id.set(GenerationId(prev_id + 1));
// Step 2 // Step 2
let nav = box PlannedNavigation { let nav = PlannedNavigation {
load_data: load_data, load_data: load_data,
pipeline_id: window.pipeline(), pipeline_id: window.pipeline(),
script_chan: window.main_thread_script_chan().clone(), script_chan: window.main_thread_script_chan().clone(),
@ -484,7 +483,7 @@ impl HTMLFormElement {
}; };
// Step 3 // Step 3
window.dom_manipulation_task_source().queue(DOMManipulationTask(nav)).unwrap(); window.dom_manipulation_task_source().queue(nav, window).unwrap();
} }
/// Interactively validate the constraints of form elements /// Interactively validate the constraints of form elements

View file

@ -32,7 +32,6 @@ use std::sync::Arc;
use string_cache::Atom; use string_cache::Atom;
use style::attr::{AttrValue, LengthOrPercentageOrAuto}; use style::attr::{AttrValue, LengthOrPercentageOrAuto};
use task_source::TaskSource; use task_source::TaskSource;
use task_source::dom_manipulation::DOMManipulationTask;
use url::Url; use url::Url;
#[derive(JSTraceable, HeapSizeOf)] #[derive(JSTraceable, HeapSizeOf)]
@ -180,12 +179,12 @@ impl HTMLImageElement {
} }
} }
let runnable = Box::new(ImgParseErrorRunnable { let runnable = ImgParseErrorRunnable {
img: Trusted::new(self), img: Trusted::new(self),
src: src.into(), src: src.into(),
}); };
let task = window.dom_manipulation_task_source(); let task = window.dom_manipulation_task_source();
let _ = task.queue(DOMManipulationTask(runnable)); let _ = task.queue(runnable, window);
} }
} }
} }

View file

@ -576,7 +576,8 @@ impl HTMLInputElementMethods for HTMLInputElement {
&self.upcast(), &self.upcast(),
atom!("select"), atom!("select"),
EventBubbles::Bubbles, EventBubbles::Bubbles,
EventCancelable::NotCancelable); EventCancelable::NotCancelable,
window.r());
self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage); self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage);
} }
@ -1061,7 +1062,8 @@ impl VirtualMethods for HTMLInputElement {
&self.upcast(), &self.upcast(),
atom!("input"), atom!("input"),
EventBubbles::Bubbles, EventBubbles::Bubbles,
EventCancelable::NotCancelable); EventCancelable::NotCancelable,
window.r());
} }
self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage); self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage);

View file

@ -33,7 +33,6 @@ use std::cell::Cell;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use string_cache::Atom; use string_cache::Atom;
use task_source::TaskSource; use task_source::TaskSource;
use task_source::dom_manipulation::DOMManipulationTask;
use time::{self, Timespec, Duration}; use time::{self, Timespec, Duration};
use url::Url; use url::Url;
@ -242,7 +241,7 @@ impl HTMLMediaElement {
elem: Trusted::new(self), elem: Trusted::new(self),
}; };
let win = window_from_node(self); let win = window_from_node(self);
let _ = win.dom_manipulation_task_source().queue(DOMManipulationTask(box task)); let _ = win.dom_manipulation_task_source().queue(task, win.r());
} }
// https://html.spec.whatwg.org/multipage/#internal-pause-steps step 2.2 // https://html.spec.whatwg.org/multipage/#internal-pause-steps step 2.2
@ -266,13 +265,13 @@ impl HTMLMediaElement {
elem: Trusted::new(self), elem: Trusted::new(self),
}; };
let win = window_from_node(self); let win = window_from_node(self);
let _ = win.dom_manipulation_task_source().queue(DOMManipulationTask(box task)); let _ = win.dom_manipulation_task_source().queue(task, win.r());
} }
fn queue_fire_simple_event(&self, type_: &'static str) { fn queue_fire_simple_event(&self, type_: &'static str) {
let win = window_from_node(self); let win = window_from_node(self);
let task = FireSimpleEventTask::new(self, type_); let task = FireSimpleEventTask::new(self, type_);
let _ = win.dom_manipulation_task_source().queue(DOMManipulationTask(box task)); let _ = win.dom_manipulation_task_source().queue(task, win.r());
} }
fn fire_simple_event(&self, type_: &str) { fn fire_simple_event(&self, type_: &str) {
@ -498,8 +497,8 @@ impl HTMLMediaElement {
} }
fn queue_dedicated_media_source_failure_steps(&self) { fn queue_dedicated_media_source_failure_steps(&self) {
let _ = window_from_node(self).dom_manipulation_task_source().queue( let window = window_from_node(self);
DOMManipulationTask(box DedicatedMediaSourceFailureTask::new(self))); let _ = window.dom_manipulation_task_source().queue(DedicatedMediaSourceFailureTask::new(self), window.r());
} }
// https://html.spec.whatwg.org/multipage/#dedicated-media-source-failure-steps // https://html.spec.whatwg.org/multipage/#dedicated-media-source-failure-steps

View file

@ -460,12 +460,13 @@ impl HTMLScriptElement {
if external { if external {
self.dispatch_load_event(); self.dispatch_load_event();
} else { } else {
window.dom_manipulation_task_source().queue_simple_event(self.upcast(), atom!("load")); window.dom_manipulation_task_source().queue_simple_event(self.upcast(), atom!("load"), window.r());
} }
} }
pub fn queue_error_event(&self) { pub fn queue_error_event(&self) {
window_from_node(self).dom_manipulation_task_source().queue_simple_event(self.upcast(), atom!("error")); let window = window_from_node(self);
window.dom_manipulation_task_source().queue_simple_event(self.upcast(), atom!("error"), window.r());
} }
pub fn dispatch_before_script_execute_event(&self) -> bool { pub fn dispatch_before_script_execute_event(&self) -> bool {

View file

@ -260,7 +260,8 @@ impl HTMLTextAreaElementMethods for HTMLTextAreaElement {
&self.upcast(), &self.upcast(),
atom!("select"), atom!("select"),
EventBubbles::Bubbles, EventBubbles::Bubbles,
EventCancelable::NotCancelable); EventCancelable::NotCancelable,
window.r());
self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage); self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage);
} }
} }
@ -383,7 +384,8 @@ impl VirtualMethods for HTMLTextAreaElement {
&self.upcast(), &self.upcast(),
atom!("input"), atom!("input"),
EventBubbles::Bubbles, EventBubbles::Bubbles,
EventCancelable::NotCancelable); EventCancelable::NotCancelable,
window.r());
} }
self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage); self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage);

View file

@ -19,7 +19,6 @@ use net_traits::IpcSend;
use net_traits::storage_thread::{StorageThreadMsg, StorageType}; use net_traits::storage_thread::{StorageThreadMsg, StorageType};
use script_thread::{Runnable, ScriptThread}; use script_thread::{Runnable, ScriptThread};
use task_source::TaskSource; use task_source::TaskSource;
use task_source::dom_manipulation::DOMManipulationTask;
use url::Url; use url::Url;
#[dom_struct] #[dom_struct]
@ -159,10 +158,10 @@ impl Storage {
new_value: Option<String>) { new_value: Option<String>) {
let global_root = self.global(); let global_root = self.global();
let global_ref = global_root.r(); let global_ref = global_root.r();
let task_source = global_ref.as_window().dom_manipulation_task_source(); let window = global_ref.as_window();
let task_source = window.dom_manipulation_task_source();
let trusted_storage = Trusted::new(self); let trusted_storage = Trusted::new(self);
task_source.queue(DOMManipulationTask( task_source.queue(StorageEventRunnable::new(trusted_storage, key, old_value, new_value), window).unwrap();
box StorageEventRunnable::new(trusted_storage, key, old_value, new_value))).unwrap();
} }
} }

View file

@ -23,18 +23,15 @@ impl<Listener: PreInvoke + Send + 'static> NetworkListener<Listener> {
context: self.context.clone(), context: self.context.clone(),
action: action, action: action,
}; };
if let Some(ref wrapper) = self.wrapper { let result = if let Some(ref wrapper) = self.wrapper {
if let Err(err) = self.script_chan.send( self.script_chan.send(CommonScriptMsg::RunnableMsg(NetworkEvent, wrapper.wrap_runnable(runnable)))
CommonScriptMsg::RunnableMsg(NetworkEvent, wrapper.wrap_runnable(runnable))) {
warn!("failed to deliver network data: {:?}", err);
}
} else { } else {
if let Err(err) = self.script_chan.send( self.script_chan.send(CommonScriptMsg::RunnableMsg(NetworkEvent, box runnable))
CommonScriptMsg::RunnableMsg(NetworkEvent, box runnable)) { };
if let Err(err) = result {
warn!("failed to deliver network data: {:?}", err); warn!("failed to deliver network data: {:?}", err);
} }
} }
}
} }
// helps type inference // helps type inference

View file

@ -189,10 +189,16 @@ pub struct CancellableRunnable<T: Runnable + Send> {
} }
impl<T: Runnable + Send> Runnable for CancellableRunnable<T> { impl<T: Runnable + Send> Runnable for CancellableRunnable<T> {
fn name(&self) -> &'static str { self.inner.name() }
fn is_cancelled(&self) -> bool { fn is_cancelled(&self) -> bool {
self.cancelled.load(Ordering::SeqCst) self.cancelled.load(Ordering::SeqCst)
} }
fn main_thread_handler(self: Box<CancellableRunnable<T>>, script_thread: &ScriptThread) {
self.inner.main_thread_handler(script_thread);
}
fn handler(self: Box<CancellableRunnable<T>>) { fn handler(self: Box<CancellableRunnable<T>>) {
self.inner.handler() self.inner.handler()
} }
@ -1220,8 +1226,8 @@ impl ScriptThread {
doc.mut_loader().inhibit_events(); doc.mut_loader().inhibit_events();
// https://html.spec.whatwg.org/multipage/#the-end step 7 // https://html.spec.whatwg.org/multipage/#the-end step 7
let handler = box DocumentProgressHandler::new(Trusted::new(doc)); let handler = DocumentProgressHandler::new(Trusted::new(doc));
self.dom_manipulation_task_source.queue(DOMManipulationTask(handler)).unwrap(); self.dom_manipulation_task_source.queue(handler, doc.window()).unwrap();
self.constellation_chan.send(ConstellationMsg::LoadComplete(pipeline)).unwrap(); self.constellation_chan.send(ConstellationMsg::LoadComplete(pipeline)).unwrap();
} }

View file

@ -5,6 +5,7 @@
use dom::bindings::refcounted::Trusted; use dom::bindings::refcounted::Trusted;
use dom::event::{EventBubbles, EventCancelable, EventRunnable, SimpleEventRunnable}; use dom::event::{EventBubbles, EventCancelable, EventRunnable, SimpleEventRunnable};
use dom::eventtarget::EventTarget; use dom::eventtarget::EventTarget;
use dom::window::Window;
use script_thread::{MainThreadScriptMsg, Runnable, ScriptThread}; use script_thread::{MainThreadScriptMsg, Runnable, ScriptThread};
use std::result::Result; use std::result::Result;
use std::sync::mpsc::Sender; use std::sync::mpsc::Sender;
@ -14,8 +15,9 @@ use task_source::TaskSource;
#[derive(JSTraceable, Clone)] #[derive(JSTraceable, Clone)]
pub struct DOMManipulationTaskSource(pub Sender<MainThreadScriptMsg>); pub struct DOMManipulationTaskSource(pub Sender<MainThreadScriptMsg>);
impl TaskSource<DOMManipulationTask> for DOMManipulationTaskSource { impl TaskSource for DOMManipulationTaskSource {
fn queue(&self, msg: DOMManipulationTask) -> Result<(), ()> { fn queue<T: Runnable + Send + 'static>(&self, msg: T, window: &Window) -> Result<(), ()> {
let msg = DOMManipulationTask(window.get_runnable_wrapper().wrap_runnable(msg));
self.0.send(MainThreadScriptMsg::DOMManipulation(msg)).map_err(|_| ()) self.0.send(MainThreadScriptMsg::DOMManipulation(msg)).map_err(|_| ())
} }
} }
@ -25,24 +27,25 @@ impl DOMManipulationTaskSource {
target: &EventTarget, target: &EventTarget,
name: Atom, name: Atom,
bubbles: EventBubbles, bubbles: EventBubbles,
cancelable: EventCancelable) { cancelable: EventCancelable,
window: &Window) {
let target = Trusted::new(target); let target = Trusted::new(target);
let runnable = box EventRunnable { let runnable = EventRunnable {
target: target, target: target,
name: name, name: name,
bubbles: bubbles, bubbles: bubbles,
cancelable: cancelable, cancelable: cancelable,
}; };
let _ = self.queue(DOMManipulationTask(runnable)); let _ = self.queue(runnable, window);
} }
pub fn queue_simple_event(&self, target: &EventTarget, name: Atom) { pub fn queue_simple_event(&self, target: &EventTarget, name: Atom, window: &Window) {
let target = Trusted::new(target); let target = Trusted::new(target);
let runnable = box SimpleEventRunnable { let runnable = SimpleEventRunnable {
target: target, target: target,
name: name, name: name,
}; };
let _ = self.queue(DOMManipulationTask(runnable)); let _ = self.queue(runnable, window);
} }
} }

View file

@ -8,8 +8,10 @@ pub mod history_traversal;
pub mod networking; pub mod networking;
pub mod user_interaction; pub mod user_interaction;
use dom::window::Window;
use script_thread::Runnable;
use std::result::Result; use std::result::Result;
pub trait TaskSource<T> { pub trait TaskSource {
fn queue(&self, msg: T) -> Result<(), ()>; fn queue<T: Runnable + Send + 'static>(&self, msg: T, window: &Window) -> Result<(), ()>;
} }

View file

@ -5,6 +5,7 @@
use dom::bindings::refcounted::Trusted; use dom::bindings::refcounted::Trusted;
use dom::event::{EventBubbles, EventCancelable, EventRunnable}; use dom::event::{EventBubbles, EventCancelable, EventRunnable};
use dom::eventtarget::EventTarget; use dom::eventtarget::EventTarget;
use dom::window::Window;
use script_thread::{MainThreadScriptMsg, Runnable, ScriptThread}; use script_thread::{MainThreadScriptMsg, Runnable, ScriptThread};
use std::result::Result; use std::result::Result;
use std::sync::mpsc::Sender; use std::sync::mpsc::Sender;
@ -14,8 +15,9 @@ use task_source::TaskSource;
#[derive(JSTraceable, Clone)] #[derive(JSTraceable, Clone)]
pub struct UserInteractionTaskSource(pub Sender<MainThreadScriptMsg>); pub struct UserInteractionTaskSource(pub Sender<MainThreadScriptMsg>);
impl TaskSource<UserInteractionTask> for UserInteractionTaskSource { impl TaskSource for UserInteractionTaskSource {
fn queue(&self, msg: UserInteractionTask) -> Result<(), ()> { fn queue<T: Runnable + Send + 'static>(&self, msg: T, window: &Window) -> Result<(), ()> {
let msg = UserInteractionTask(window.get_runnable_wrapper().wrap_runnable(msg));
self.0.send(MainThreadScriptMsg::UserInteraction(msg)).map_err(|_| ()) self.0.send(MainThreadScriptMsg::UserInteraction(msg)).map_err(|_| ())
} }
} }
@ -25,15 +27,16 @@ impl UserInteractionTaskSource {
target: &EventTarget, target: &EventTarget,
name: Atom, name: Atom,
bubbles: EventBubbles, bubbles: EventBubbles,
cancelable: EventCancelable) { cancelable: EventCancelable,
window: &Window) {
let target = Trusted::new(target); let target = Trusted::new(target);
let runnable = box EventRunnable { let runnable = EventRunnable {
target: target, target: target,
name: name, name: name,
bubbles: bubbles, bubbles: bubbles,
cancelable: cancelable, cancelable: cancelable,
}; };
let _ = self.queue(UserInteractionTask(runnable)); let _ = self.queue(runnable, window);
} }
} }