This replaces the specialized TrustedXHRAddress and TrustedWorkerAddress code that was used for the same purpose. A non-zero refcount pins the given DOM object's reflector and prevents it from being GCed even when there are no other outstanding references visible to SpiderMonkey. This will enable us to implement asynchronous operations that refer to particular DOM objects (such as "queue a task to fire a simple event named load at the iframe element" from the spec) safely and conveniently, and paves the way for things like asynchronous network responses. Some concerns about the resulting size of XHR progress messages have been expressed, but I believe optimizations to reduce that can be implemented in subsequent PRs. r? @Ms2ger - note in particular the changes to the worker lifetime code. I couldn't figure out how to achieve an identical lifetime to the previous addref/release pairing, and I also was having trouble figuring out why the existing setup was safe. The new implementation now holds the main script task Worker object alive via the TrustedWorkerAddress field in the dedicated worker global scope, which is a significant difference. Source-Repo: https://github.com/servo/servo Source-Revision: 2c259f477c41331e66beab8bda865971982a1ff4
123 lines
4.4 KiB
Rust
123 lines
4.4 KiB
Rust
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
use dom::bindings::codegen::Bindings::WorkerBinding;
|
|
use dom::bindings::codegen::Bindings::WorkerBinding::WorkerMethods;
|
|
use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull;
|
|
use dom::bindings::codegen::InheritTypes::EventTargetCast;
|
|
use dom::bindings::error::{Fallible, ErrorResult};
|
|
use dom::bindings::error::Error::{Syntax, DataClone};
|
|
use dom::bindings::global::{GlobalRef, GlobalField};
|
|
use dom::bindings::js::{JSRef, Temporary};
|
|
use dom::bindings::refcounted::Trusted;
|
|
use dom::bindings::trace::JSTraceable;
|
|
use dom::bindings::utils::{Reflectable, reflect_dom_object};
|
|
use dom::dedicatedworkerglobalscope::DedicatedWorkerGlobalScope;
|
|
use dom::eventtarget::{EventTarget, EventTargetHelpers, EventTargetTypeId};
|
|
use dom::messageevent::MessageEvent;
|
|
use script_task::{ScriptChan, ScriptMsg};
|
|
|
|
use servo_util::str::DOMString;
|
|
|
|
use js::glue::JS_STRUCTURED_CLONE_VERSION;
|
|
use js::jsapi::JSContext;
|
|
use js::jsapi::{JS_ReadStructuredClone, JS_WriteStructuredClone, JS_ClearPendingException};
|
|
use js::jsval::{JSVal, UndefinedValue};
|
|
use url::UrlParser;
|
|
|
|
use libc::size_t;
|
|
use std::cell::Cell;
|
|
use std::ptr;
|
|
|
|
pub type TrustedWorkerAddress = Trusted<Worker>;
|
|
|
|
#[dom_struct]
|
|
pub struct Worker {
|
|
eventtarget: EventTarget,
|
|
refcount: Cell<uint>,
|
|
global: GlobalField,
|
|
/// Sender to the Receiver associated with the DedicatedWorkerGlobalScope
|
|
/// this Worker created.
|
|
sender: Sender<(TrustedWorkerAddress, ScriptMsg)>,
|
|
}
|
|
|
|
impl Worker {
|
|
fn new_inherited(global: &GlobalRef, sender: Sender<(TrustedWorkerAddress, ScriptMsg)>) -> Worker {
|
|
Worker {
|
|
eventtarget: EventTarget::new_inherited(EventTargetTypeId::Worker),
|
|
refcount: Cell::new(0),
|
|
global: GlobalField::from_rooted(global),
|
|
sender: sender,
|
|
}
|
|
}
|
|
|
|
pub fn new(global: &GlobalRef, sender: Sender<(TrustedWorkerAddress, ScriptMsg)>) -> Temporary<Worker> {
|
|
reflect_dom_object(box Worker::new_inherited(global, sender),
|
|
*global,
|
|
WorkerBinding::Wrap)
|
|
}
|
|
|
|
// http://www.whatwg.org/html/#dom-worker
|
|
pub fn Constructor(global: &GlobalRef, scriptURL: DOMString) -> Fallible<Temporary<Worker>> {
|
|
// Step 2-4.
|
|
let worker_url = match UrlParser::new().base_url(&global.get_url())
|
|
.parse(scriptURL.as_slice()) {
|
|
Ok(url) => url,
|
|
Err(_) => return Err(Syntax),
|
|
};
|
|
|
|
let resource_task = global.resource_task();
|
|
|
|
let (sender, receiver) = channel();
|
|
let worker = Worker::new(global, sender.clone()).root();
|
|
let worker_ref = Trusted::new(global.get_cx(), *worker, global.script_chan());
|
|
|
|
DedicatedWorkerGlobalScope::run_worker_scope(
|
|
worker_url, worker_ref, resource_task, global.script_chan(),
|
|
sender, receiver);
|
|
|
|
Ok(Temporary::from_rooted(*worker))
|
|
}
|
|
|
|
pub fn handle_message(address: TrustedWorkerAddress,
|
|
data: *mut u64, nbytes: size_t) {
|
|
let worker = address.to_temporary().root();
|
|
|
|
let global = worker.global.root();
|
|
|
|
let mut message = UndefinedValue();
|
|
unsafe {
|
|
assert!(JS_ReadStructuredClone(
|
|
global.root_ref().get_cx(), data as *const u64, nbytes,
|
|
JS_STRUCTURED_CLONE_VERSION, &mut message,
|
|
ptr::null(), ptr::null_mut()) != 0);
|
|
}
|
|
|
|
let target: JSRef<EventTarget> = EventTargetCast::from_ref(*worker);
|
|
MessageEvent::dispatch_jsval(target, global.root_ref(), message);
|
|
}
|
|
}
|
|
|
|
impl<'a> WorkerMethods for JSRef<'a, Worker> {
|
|
fn PostMessage(self, cx: *mut JSContext, message: JSVal) -> ErrorResult {
|
|
let mut data = ptr::null_mut();
|
|
let mut nbytes = 0;
|
|
let result = unsafe {
|
|
JS_WriteStructuredClone(cx, message, &mut data, &mut nbytes,
|
|
ptr::null(), ptr::null_mut())
|
|
};
|
|
if result == 0 {
|
|
unsafe { JS_ClearPendingException(cx); }
|
|
return Err(DataClone);
|
|
}
|
|
|
|
let address = Trusted::new(cx, self, self.global.root().root_ref().script_chan().clone());
|
|
self.sender.send((address, ScriptMsg::DOMMessage(data, nbytes)));
|
|
Ok(())
|
|
}
|
|
|
|
event_handler!(message, GetOnmessage, SetOnmessage)
|
|
}
|
|
|