Skip to content

Commit

Permalink
Issue #3236 - Implement timers (setTimeout/setInterval) for workers
Browse files Browse the repository at this point in the history
  • Loading branch information
mukilan committed Oct 16, 2014
1 parent fd70b36 commit 0d7e13f
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 185 deletions.
7 changes: 5 additions & 2 deletions components/script/dom/dedicatedworkerglobalscope.rs
Expand Up @@ -15,10 +15,10 @@ use dom::eventtarget::WorkerGlobalScopeTypeId;
use dom::messageevent::MessageEvent;
use dom::worker::{Worker, TrustedWorkerAddress};
use dom::workerglobalscope::DedicatedGlobalScope;
use dom::workerglobalscope::WorkerGlobalScope;
use dom::workerglobalscope::{WorkerGlobalScope, WorkerGlobalScopeHelpers};
use dom::xmlhttprequest::XMLHttpRequest;
use script_task::{ScriptTask, ScriptChan};
use script_task::{ScriptMsg, DOMMessage, XHRProgressMsg, WorkerRelease};
use script_task::{ScriptMsg, FromWorker, DOMMessage, FireTimerMsg, XHRProgressMsg, WorkerRelease};
use script_task::WorkerPostMessage;
use script_task::StackRootTLS;

Expand Down Expand Up @@ -142,6 +142,9 @@ impl DedicatedWorkerGlobalScope {
Ok(WorkerRelease(addr)) => {
Worker::handle_release(addr)
},
Ok(FireTimerMsg(FromWorker, timer_id)) => {
scope.handle_fire_timer(timer_id, js_context.ptr);
}
Ok(_) => fail!("Unexpected message"),
Err(_) => break,
}
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/webidls/WorkerGlobalScope.webidl
Expand Up @@ -22,7 +22,7 @@ partial interface WorkerGlobalScope { // not obsolete
void importScripts(DOMString... urls);
readonly attribute WorkerNavigator navigator;
};
//WorkerGlobalScope implements WindowTimers;
WorkerGlobalScope implements WindowTimers;
WorkerGlobalScope implements WindowBase64;

// Proprietary
Expand Down
173 changes: 20 additions & 153 deletions components/script/dom/window.rs
Expand Up @@ -20,63 +20,30 @@ use dom::performance::Performance;
use dom::screen::Screen;
use layout_interface::{ReflowGoal, ReflowForDisplay};
use page::Page;
use script_task::{ExitWindowMsg, FireTimerMsg, ScriptChan, TriggerLoadMsg, TriggerFragmentMsg};
use script_task::{ExitWindowMsg, ScriptChan, TriggerLoadMsg, TriggerFragmentMsg};
use script_task::FromWindow;
use script_traits::ScriptControlChan;
use timers::{TimerId, TimerManager};

use servo_msg::compositor_msg::ScriptListener;
use servo_msg::constellation_msg::LoadData;
use servo_net::image_cache_task::ImageCacheTask;
use servo_util::str::{DOMString,HTML_SPACE_CHARACTERS};
use servo_util::task::{spawn_named};

use js::jsapi::{JS_CallFunctionValue, JS_EvaluateUCScript};
use js::jsapi::JS_EvaluateUCScript;
use js::jsapi::JSContext;
use js::jsapi::{JS_GC, JS_GetRuntime};
use js::jsval::JSVal;
use js::jsval::{UndefinedValue, NullValue};
use js::jsval::{JSVal, UndefinedValue};
use js::rust::with_compartment;
use url::{Url, UrlParser};

use libc;
use serialize::base64::{FromBase64, ToBase64, STANDARD};
use std::collections::hashmap::HashMap;
use std::cell::{Cell, Ref, RefCell};
use std::cmp;
use std::comm::{channel, Sender};
use std::comm::Select;
use std::cell::{Ref, RefCell};
use std::default::Default;
use std::hash::{Hash, sip};
use std::io::timer::Timer;
use std::ptr;
use std::rc::Rc;
use std::time::duration::Duration;
use time;

#[deriving(PartialEq, Eq)]
#[jstraceable]
pub struct TimerId(i32);

#[jstraceable]
#[privatize]
pub struct TimerHandle {
handle: TimerId,
data: TimerData,
cancel_chan: Option<Sender<()>>,
}

impl Hash for TimerId {
fn hash(&self, state: &mut sip::SipState) {
let TimerId(id) = *self;
id.hash(state);
}
}

impl TimerHandle {
fn cancel(&mut self) {
self.cancel_chan.as_ref().map(|chan| chan.send_opt(()).ok());
}
}

#[jstraceable]
#[must_root]
#[privatize]
Expand All @@ -88,15 +55,14 @@ pub struct Window {
location: MutNullableJS<Location>,
navigator: MutNullableJS<Navigator>,
image_cache_task: ImageCacheTask,
active_timers: RefCell<HashMap<TimerId, TimerHandle>>,
next_timer_handle: Cell<i32>,
compositor: Box<ScriptListener+'static>,
browser_context: RefCell<Option<BrowserContext>>,
page: Rc<Page>,
performance: MutNullableJS<Performance>,
navigation_start: u64,
navigation_start_precise: f64,
screen: MutNullableJS<Screen>,
timers: TimerManager
}

impl Window {
Expand Down Expand Up @@ -142,25 +108,6 @@ impl Window {
}
}

#[unsafe_destructor]
impl Drop for Window {
fn drop(&mut self) {
for (_, timer_handle) in self.active_timers.borrow_mut().iter_mut() {
timer_handle.cancel();
}
}
}

// Holder for the various JS values associated with setTimeout
// (ie. function value to invoke and all arguments to pass
// to the function when calling it)
#[jstraceable]
#[privatize]
pub struct TimerData {
is_interval: bool,
funval: JSVal,
}

// http://www.whatwg.org/html/#atob
pub fn base64_btoa(btoa: DOMString) -> Fallible<DOMString> {
let input = btoa.as_slice();
Expand Down Expand Up @@ -278,21 +225,23 @@ impl<'a> WindowMethods for JSRef<'a, Window> {
}

fn SetTimeout(self, _cx: *mut JSContext, callback: JSVal, timeout: i32) -> i32 {
self.set_timeout_or_interval(callback, timeout, false)
self.timers.set_timeout_or_interval(callback,
timeout,
false, // is_interval
FromWindow(self.page.id.clone()),
self.script_chan.clone())
}

fn ClearTimeout(self, handle: i32) {
let mut timers = self.active_timers.borrow_mut();
let mut timer_handle = timers.pop(&TimerId(handle));
match timer_handle {
Some(ref mut handle) => handle.cancel(),
None => { }
}
timers.remove(&TimerId(handle));
self.timers.clear_timeout_or_interval(handle);
}

fn SetInterval(self, _cx: *mut JSContext, callback: JSVal, timeout: i32) -> i32 {
self.set_timeout_or_interval(callback, timeout, true)
self.timers.set_timeout_or_interval(callback,
timeout,
true, // is_interval
FromWindow(self.page.id.clone()),
self.script_chan.clone())
}

fn ClearInterval(self, handle: i32) {
Expand Down Expand Up @@ -408,9 +357,6 @@ pub trait WindowHelpers {
fn evaluate_js_with_result(self, code: &str) -> JSVal;
}

trait PrivateWindowHelpers {
fn set_timeout_or_interval(self, callback: JSVal, timeout: i32, is_interval: bool) -> i32;
}

impl<'a> WindowHelpers for JSRef<'a, Window> {
fn evaluate_js_with_result(self, code: &str) -> JSVal {
Expand Down Expand Up @@ -471,85 +417,7 @@ impl<'a> WindowHelpers for JSRef<'a, Window> {

fn handle_fire_timer(self, timer_id: TimerId, cx: *mut JSContext) {
let this_value = self.reflector().get_jsobject();

let data = match self.active_timers.borrow().find(&timer_id) {
None => return,
Some(timer_handle) => timer_handle.data,
};

// TODO: Support extra arguments. This requires passing a `*JSVal` array as `argv`.
with_compartment(cx, this_value, || {
let mut rval = NullValue();
unsafe {
JS_CallFunctionValue(cx, this_value, data.funval,
0, ptr::null_mut(), &mut rval);
}
});

if !data.is_interval {
self.active_timers.borrow_mut().remove(&timer_id);
}
}
}

impl<'a> PrivateWindowHelpers for JSRef<'a, Window> {
fn set_timeout_or_interval(self, callback: JSVal, timeout: i32, is_interval: bool) -> i32 {
let timeout = cmp::max(0, timeout) as u64;
let handle = self.next_timer_handle.get();
self.next_timer_handle.set(handle + 1);

// Post a delayed message to the per-window timer task; it will dispatch it
// to the relevant script handler that will deal with it.
let tm = Timer::new().unwrap();
let (cancel_chan, cancel_port) = channel();
let chan = self.script_chan.clone();
let page_id = self.page.id.clone();
let spawn_name = if is_interval {
"Window:SetInterval"
} else {
"Window:SetTimeout"
};
spawn_named(spawn_name, proc() {
let mut tm = tm;
let duration = Duration::milliseconds(timeout as i64);
let timeout_port = if is_interval {
tm.periodic(duration)
} else {
tm.oneshot(duration)
};
let cancel_port = cancel_port;

let select = Select::new();
let mut timeout_handle = select.handle(&timeout_port);
unsafe { timeout_handle.add() };
let mut cancel_handle = select.handle(&cancel_port);
unsafe { cancel_handle.add() };

loop {
let id = select.wait();
if id == timeout_handle.id() {
timeout_port.recv();
let ScriptChan(ref chan) = chan;
chan.send(FireTimerMsg(page_id, TimerId(handle)));
if !is_interval {
break;
}
} else if id == cancel_handle.id() {
break;
}
}
});
let timer_id = TimerId(handle);
let timer = TimerHandle {
handle: timer_id,
cancel_chan: Some(cancel_chan),
data: TimerData {
is_interval: is_interval,
funval: callback,
}
};
self.active_timers.borrow_mut().insert(timer_id, timer);
handle
self.timers.fire_timer(timer_id, this_value, cx);
}
}

Expand All @@ -571,13 +439,12 @@ impl Window {
location: Default::default(),
navigator: Default::default(),
image_cache_task: image_cache_task,
active_timers: RefCell::new(HashMap::new()),
next_timer_handle: Cell::new(0),
browser_context: RefCell::new(None),
performance: Default::default(),
navigation_start: time::get_time().sec as u64,
navigation_start_precise: time::precise_time_s(),
screen: Default::default(),
timers: TimerManager::new()
};

WindowBinding::Wrap(cx, win)
Expand Down
43 changes: 42 additions & 1 deletion components/script/dom/workerglobalscope.rs
Expand Up @@ -12,12 +12,14 @@ use dom::eventtarget::{EventTarget, WorkerGlobalScopeTypeId};
use dom::workerlocation::WorkerLocation;
use dom::workernavigator::WorkerNavigator;
use dom::window::{base64_atob, base64_btoa};
use script_task::ScriptChan;
use script_task::{ScriptChan, FromWorker};
use timers::{TimerId, TimerManager};

use servo_net::resource_task::{ResourceTask, load_whole_resource};
use servo_util::str::DOMString;

use js::jsapi::JSContext;
use js::jsval::JSVal;
use js::rust::Cx;

use std::default::Default;
Expand All @@ -42,6 +44,7 @@ pub struct WorkerGlobalScope {
location: MutNullableJS<WorkerLocation>,
navigator: MutNullableJS<WorkerNavigator>,
console: MutNullableJS<Console>,
timers: TimerManager,
}

impl WorkerGlobalScope {
Expand All @@ -59,6 +62,7 @@ impl WorkerGlobalScope {
location: Default::default(),
navigator: Default::default(),
console: Default::default(),
timers: TimerManager::new()
}
}

Expand Down Expand Up @@ -152,6 +156,43 @@ impl<'a> WorkerGlobalScopeMethods for JSRef<'a, WorkerGlobalScope> {
fn Atob(self, atob: DOMString) -> Fallible<DOMString> {
base64_atob(atob)
}

fn SetTimeout(self, _cx: *mut JSContext, handler: JSVal, timeout: i32) -> i32 {
self.timers.set_timeout_or_interval(handler,
timeout,
false, // is_interval
FromWorker,
self.script_chan.clone())
}

fn ClearTimeout(self, handle: i32) {
self.timers.clear_timeout_or_interval(handle);
}

fn SetInterval(self, _cx: *mut JSContext, handler: JSVal, timeout: i32) -> i32 {
self.timers.set_timeout_or_interval(handler,
timeout,
true, // is_interval
FromWorker,
self.script_chan.clone())
}

fn ClearInterval(self, handle: i32) {
self.ClearTimeout(handle);
}
}

pub trait WorkerGlobalScopeHelpers {
fn handle_fire_timer(self, timer_id: TimerId, cx: *mut JSContext);
}

impl<'a> WorkerGlobalScopeHelpers for JSRef<'a, WorkerGlobalScope> {

fn handle_fire_timer(self, timer_id: TimerId, cx: *mut JSContext) {
let this_value = self.reflector().get_jsobject();
self.timers.fire_timer(timer_id, this_value, cx);
}

}

impl Reflectable for WorkerGlobalScope {
Expand Down
1 change: 1 addition & 0 deletions components/script/lib.rs
Expand Up @@ -217,3 +217,4 @@ pub mod html {
pub mod layout_interface;
pub mod page;
pub mod script_task;
mod timers;

5 comments on commit 0d7e13f

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from jdm
at mukilan@0d7e13f

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging mukilan/servo/worker_timer = 0d7e13f into auto

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mukilan/servo/worker_timer = 0d7e13f merged ok, testing candidate = f94228d

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = f94228d

Please sign in to comment.