Skip to content

Commit

Permalink
Update Worker webidl to support WorkerOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
CYBAI committed May 11, 2019
1 parent 81f750a commit dececad
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 22 deletions.
13 changes: 13 additions & 0 deletions components/script/dom/dedicatedworkerglobalscope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::dom::abstractworkerglobalscope::{SendableWorkerScriptChan, WorkerThre
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::DedicatedWorkerGlobalScopeBinding;
use crate::dom::bindings::codegen::Bindings::DedicatedWorkerGlobalScopeBinding::DedicatedWorkerGlobalScopeMethods;
use crate::dom::bindings::codegen::Bindings::WorkerBinding::WorkerType;
use crate::dom::bindings::error::{ErrorInfo, ErrorResult};
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::DomObject;
Expand Down Expand Up @@ -212,6 +213,8 @@ impl WorkerEventLoopMethods for DedicatedWorkerGlobalScope {
impl DedicatedWorkerGlobalScope {
fn new_inherited(
init: WorkerGlobalScopeInit,
worker_name: DOMString,
worker_type: WorkerType,
worker_url: ServoUrl,
from_devtools_receiver: Receiver<DevtoolScriptControlMsg>,
runtime: Runtime,
Expand All @@ -225,6 +228,8 @@ impl DedicatedWorkerGlobalScope {
DedicatedWorkerGlobalScope {
workerglobalscope: WorkerGlobalScope::new_inherited(
init,
worker_name,
worker_type,
worker_url,
runtime,
from_devtools_receiver,
Expand All @@ -242,6 +247,8 @@ impl DedicatedWorkerGlobalScope {
#[allow(unsafe_code)]
pub fn new(
init: WorkerGlobalScopeInit,
worker_name: DOMString,
worker_type: WorkerType,
worker_url: ServoUrl,
from_devtools_receiver: Receiver<DevtoolScriptControlMsg>,
runtime: Runtime,
Expand All @@ -255,6 +262,8 @@ impl DedicatedWorkerGlobalScope {
let cx = runtime.cx();
let scope = Box::new(DedicatedWorkerGlobalScope::new_inherited(
init,
worker_name,
worker_type,
worker_url,
from_devtools_receiver,
runtime,
Expand All @@ -279,6 +288,8 @@ impl DedicatedWorkerGlobalScope {
own_sender: Sender<DedicatedWorkerScriptMsg>,
receiver: Receiver<DedicatedWorkerScriptMsg>,
worker_load_origin: WorkerScriptLoadOrigin,
worker_name: String,
worker_type: WorkerType,
closing: Arc<AtomicBool>,
) {
let serialized_worker_url = worker_url.to_string();
Expand Down Expand Up @@ -338,6 +349,8 @@ impl DedicatedWorkerGlobalScope {

let global = DedicatedWorkerGlobalScope::new(
init,
DOMString::from_string(worker_name),
worker_type,
worker_url,
devtools_mpsc_port,
runtime,
Expand Down
3 changes: 2 additions & 1 deletion components/script/dom/serviceworkercontainer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl ServiceWorkerContainerMethods for ServiceWorkerContainer {
}

#[allow(unrooted_must_root)] // Job is unrooted
/// https://w3c.github.io/ServiceWorker/#service-worker-container-register-method and - A
/// https://w3c.github.io/ServiceWorker/#navigator-service-worker-register and - A
/// https://w3c.github.io/ServiceWorker/#start-register-algorithm - B
fn Register(&self, script_url: USVString, options: &RegistrationOptions) -> Rc<Promise> {
// A: Step 1
Expand Down Expand Up @@ -127,6 +127,7 @@ impl ServiceWorkerContainerMethods for ServiceWorkerContainer {
scope,
script_url,
promise.clone(),
options.type_,
&*self.client,
);
// Job is unrooted here, do not do anything other than immediately scheduling
Expand Down
3 changes: 3 additions & 0 deletions components/script/dom/serviceworkerglobalscope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::dom::abstractworker::WorkerScriptMsg;
use crate::dom::abstractworkerglobalscope::{run_worker_event_loop, WorkerEventLoopMethods};
use crate::dom::bindings::codegen::Bindings::ServiceWorkerGlobalScopeBinding;
use crate::dom::bindings::codegen::Bindings::ServiceWorkerGlobalScopeBinding::ServiceWorkerGlobalScopeMethods;
use crate::dom::bindings::codegen::Bindings::WorkerBinding::WorkerType;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::DomObject;
use crate::dom::bindings::root::{DomRoot, RootCollection, ThreadLocalStackRoots};
Expand Down Expand Up @@ -203,6 +204,8 @@ impl ServiceWorkerGlobalScope {
ServiceWorkerGlobalScope {
workerglobalscope: WorkerGlobalScope::new_inherited(
init,
DOMString::new(),
WorkerType::Classic, // FIXME(cybai): Should be provided from `Run Service Worker`
worker_url,
runtime,
from_devtools_receiver,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ interface ServiceWorkerContainer : EventTarget {

dictionary RegistrationOptions {
USVString scope;
//WorkerType type = "classic";
WorkerType type = "classic";
ServiceWorkerUpdateViaCache updateViaCache = "imports";
};
18 changes: 14 additions & 4 deletions components/script/dom/webidls/Worker.webidl
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,22 @@ interface AbstractWorker {
};

// https://html.spec.whatwg.org/multipage/#worker
[Constructor(DOMString scriptURL), Exposed=(Window,Worker)]
[Constructor(USVString scriptURL, optional WorkerOptions options), Exposed=(Window,Worker)]
interface Worker : EventTarget {
void terminate();

[Throws]
void postMessage(any message/*, optional sequence<Transferable> transfer*/);
attribute EventHandler onmessage;
[Throws] void postMessage(any message/*, sequence<object> transfer*/);
// void postMessage(any message, optional PostMessageOptions options);
attribute EventHandler onmessage;
attribute EventHandler onmessageerror;
};

dictionary WorkerOptions {
WorkerType type = "classic";
RequestCredentials credentials = "same-origin"; // credentials is only used if type is "module"
DOMString name = "";
};

enum WorkerType { "classic", "module" };

Worker implements AbstractWorker;
15 changes: 12 additions & 3 deletions components/script/dom/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
use crate::dom::abstractworker::SimpleWorkerErrorHandler;
use crate::dom::abstractworker::WorkerScriptMsg;
use crate::dom::bindings::codegen::Bindings::WorkerBinding;
use crate::dom::bindings::codegen::Bindings::WorkerBinding::WorkerMethods;
use crate::dom::bindings::codegen::Bindings::WorkerBinding::{WorkerMethods, WorkerOptions};
use crate::dom::bindings::error::{Error, ErrorResult, Fallible};
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::refcounted::Trusted;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::bindings::str::USVString;
use crate::dom::bindings::structuredclone::StructuredCloneData;
use crate::dom::dedicatedworkerglobalscope::{
DedicatedWorkerGlobalScope, DedicatedWorkerScriptMsg,
Expand Down Expand Up @@ -72,7 +72,11 @@ impl Worker {

// https://html.spec.whatwg.org/multipage/#dom-worker
#[allow(unsafe_code)]
pub fn Constructor(global: &GlobalScope, script_url: DOMString) -> Fallible<DomRoot<Worker>> {
pub fn Constructor(
global: &GlobalScope,
script_url: USVString,
worker_options: &WorkerOptions,
) -> Fallible<DomRoot<Worker>> {
// Step 2-4.
let worker_url = match global.api_base_url().join(&script_url) {
Ok(url) => url,
Expand Down Expand Up @@ -118,6 +122,8 @@ impl Worker {
sender,
receiver,
worker_load_origin,
String::from(&*worker_options.name),
worker_options.type_,
closing,
);

Expand Down Expand Up @@ -184,6 +190,9 @@ impl WorkerMethods for Worker {
// https://html.spec.whatwg.org/multipage/#handler-worker-onmessage
event_handler!(message, GetOnmessage, SetOnmessage);

// https://html.spec.whatwg.org/multipage/#handler-worker-onmessageerror
event_handler!(messageerror, GetOnmessageerror, SetOnmessageerror);

// https://html.spec.whatwg.org/multipage/#handler-workerglobalscope-onerror
event_handler!(error, GetOnerror, SetOnerror);
}
Expand Down
8 changes: 8 additions & 0 deletions components/script/dom/workerglobalscope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::FunctionBinding::Function;
use crate::dom::bindings::codegen::Bindings::RequestBinding::RequestInit;
use crate::dom::bindings::codegen::Bindings::WorkerBinding::WorkerType;
use crate::dom::bindings::codegen::Bindings::WorkerGlobalScopeBinding::WorkerGlobalScopeMethods;
use crate::dom::bindings::codegen::UnionTypes::RequestOrUSVString;
use crate::dom::bindings::error::{report_pending_exception, Error, ErrorResult, Fallible};
Expand Down Expand Up @@ -79,6 +80,9 @@ pub fn prepare_workerscope_init(
pub struct WorkerGlobalScope {
globalscope: GlobalScope,

worker_name: DOMString,
worker_type: WorkerType,

worker_id: WorkerId,
worker_url: DomRefCell<ServoUrl>,
#[ignore_malloc_size_of = "Arc"]
Expand All @@ -105,6 +109,8 @@ pub struct WorkerGlobalScope {
impl WorkerGlobalScope {
pub fn new_inherited(
init: WorkerGlobalScopeInit,
worker_name: DOMString,
worker_type: WorkerType,
worker_url: ServoUrl,
runtime: Runtime,
from_devtools_receiver: Receiver<DevtoolScriptControlMsg>,
Expand All @@ -125,6 +131,8 @@ impl WorkerGlobalScope {
Default::default(),
),
worker_id: init.worker_id,
worker_name,
worker_type,
worker_url: DomRefCell::new(worker_url),
closing,
runtime,
Expand Down
4 changes: 4 additions & 0 deletions components/script/serviceworkerjob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//! by multiple service worker clients in a Vec.

use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::WorkerBinding::WorkerType;
use crate::dom::bindings::error::Error;
use crate::dom::bindings::refcounted::{Trusted, TrustedPromise};
use crate::dom::bindings::reflector::DomObject;
Expand Down Expand Up @@ -45,6 +46,7 @@ pub struct Job {
pub script_url: ServoUrl,
pub promise: Rc<Promise>,
pub equivalent_jobs: Vec<Job>,
pub worker_type: WorkerType,
// client can be a window client, worker client so `Client` will be an enum in future
pub client: Dom<Client>,
pub referrer: ServoUrl,
Expand All @@ -58,6 +60,7 @@ impl Job {
scope_url: ServoUrl,
script_url: ServoUrl,
promise: Rc<Promise>,
worker_type: WorkerType,
client: &Client,
) -> Job {
Job {
Expand All @@ -66,6 +69,7 @@ impl Job {
script_url: script_url,
promise: promise,
equivalent_jobs: vec![],
worker_type,
client: Dom::from_ref(client),
referrer: client.creation_url(),
}
Expand Down
3 changes: 0 additions & 3 deletions tests/wpt/metadata/html/dom/interfaces.https.html.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10511,9 +10511,6 @@
[BroadcastChannel interface: attribute onmessageerror]
expected: FAIL

[Worker interface: attribute onmessageerror]
expected: FAIL

[SharedWorker interface: existence and properties of interface object]
expected: FAIL

Expand Down
3 changes: 0 additions & 3 deletions tests/wpt/metadata/html/dom/interfaces.worker.js.ini
Original file line number Diff line number Diff line change
Expand Up @@ -558,9 +558,6 @@
[WorkerGlobalScope interface: attribute onrejectionhandled]
expected: FAIL

[Worker interface: attribute onmessageerror]
expected: FAIL

[OffscreenCanvasRenderingContext2D interface: operation createImageData(ImageData)]
expected: FAIL

Expand Down

This file was deleted.

0 comments on commit dececad

Please sign in to comment.