Auto merge of #23309 - CYBAI:update-workers, r=nox

Support WorkerOptions for Worker

I'd like to start working on updating SW related codes and I found it will have some algorithms related to module workers. And I found parts of the spec update is related to [fetch a module worker script graph](https://html.spec.whatwg.org/multipage/#fetch-a-module-worker-script-tree), maybe it's worth being a separate PR?

---
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix part of #23308
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because ___

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/23309)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2019-05-11 03:36:23 -04:00 committed by GitHub
commit 965f57e3f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 57 additions and 22 deletions

View file

@ -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;
@ -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,
@ -225,6 +228,8 @@ impl DedicatedWorkerGlobalScope {
DedicatedWorkerGlobalScope {
workerglobalscope: WorkerGlobalScope::new_inherited(
init,
worker_name,
worker_type,
worker_url,
runtime,
from_devtools_receiver,
@ -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,
@ -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,
@ -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();
@ -338,6 +349,8 @@ impl DedicatedWorkerGlobalScope {
let global = DedicatedWorkerGlobalScope::new(
init,
DOMString::from_string(worker_name),
worker_type,
worker_url,
devtools_mpsc_port,
runtime,

View file

@ -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
@ -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

View file

@ -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};
@ -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,

View file

@ -24,6 +24,6 @@ interface ServiceWorkerContainer : EventTarget {
dictionary RegistrationOptions {
USVString scope;
//WorkerType type = "classic";
WorkerType type = "classic";
ServiceWorkerUpdateViaCache updateViaCache = "imports";
};

View file

@ -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;

View file

@ -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,
@ -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,
@ -118,6 +122,8 @@ impl Worker {
sender,
receiver,
worker_load_origin,
String::from(&*worker_options.name),
worker_options.type_,
closing,
);
@ -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);
}

View file

@ -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};
@ -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"]
@ -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>,
@ -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,

View file

@ -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;
@ -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,
@ -58,6 +60,7 @@ impl Job {
scope_url: ServoUrl,
script_url: ServoUrl,
promise: Rc<Promise>,
worker_type: WorkerType,
client: &Client,
) -> Job {
Job {
@ -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(),
}