Skip to content
Permalink
Browse files

implement ExtendableEvent as base type for ServiceWorker events

  • Loading branch information...
creativcoder committed Sep 16, 2016
1 parent 6ac9dfd commit 8b10cca91fb8163d648838706525b25935e8374b
@@ -0,0 +1,68 @@
/* 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::EventBinding::EventMethods;
use dom::bindings::codegen::Bindings::ExtendableEventBinding;
use dom::bindings::error::{Error, ErrorResult, Fallible};
use dom::bindings::global::GlobalRef;
use dom::bindings::inheritance::Castable;
use dom::bindings::js::Root;
use dom::bindings::reflector::reflect_dom_object;
use dom::bindings::str::DOMString;
use dom::event::Event;
use js::jsapi::{HandleValue, JSContext};
use string_cache::Atom;

// https://w3c.github.io/ServiceWorker/#extendable-event
#[dom_struct]
pub struct ExtendableEvent {
event: Event,
extensions_allowed: bool
}

impl ExtendableEvent {
pub fn new_inherited() -> ExtendableEvent {
ExtendableEvent {
event: Event::new_inherited(),
extensions_allowed: true
}
}
pub fn new(global: GlobalRef,
type_: Atom,
bubbles: bool,
cancelable: bool)
-> Root<ExtendableEvent> {
let ev = reflect_dom_object(box ExtendableEvent::new_inherited(), global, ExtendableEventBinding::Wrap);
{
let event = ev.upcast::<Event>();
event.init_event(type_, bubbles, cancelable);
}
ev
}

pub fn Constructor(global: GlobalRef,
type_: DOMString,
init: &ExtendableEventBinding::ExtendableEventInit) -> Fallible<Root<ExtendableEvent>> {
Ok(ExtendableEvent::new(global,
Atom::from(type_),
init.parent.bubbles,
init.parent.cancelable))
}

// https://w3c.github.io/ServiceWorker/#wait-until-method
pub fn WaitUntil(&self, _cx: *mut JSContext, val: HandleValue) -> ErrorResult {
// Step 1
if !self.extensions_allowed {
return Err(Error::InvalidState);
}
// Step 2
// TODO add a extended_promises array to enqueue the `val`
Ok(())
}

// https://dom.spec.whatwg.org/#dom-event-istrusted
pub fn IsTrusted(&self) -> bool {
self.event.IsTrusted()
}
}
@@ -0,0 +1,95 @@
/* 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::ExtendableMessageEventBinding;
use dom::bindings::codegen::Bindings::ExtendableMessageEventBinding::ExtendableMessageEventMethods;
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::inheritance::Castable;
use dom::bindings::js::Root;
use dom::bindings::reflector::reflect_dom_object;
use dom::bindings::str::DOMString;
use dom::event::Event;
use dom::eventtarget::EventTarget;
use dom::extendableevent::ExtendableEvent;
use js::jsapi::{HandleValue, Heap, JSContext};
use js::jsval::JSVal;
use std::default::Default;
use string_cache::Atom;

#[dom_struct]
pub struct ExtendableMessageEvent {
event: ExtendableEvent,
data: Heap<JSVal>,
origin: DOMString,
lastEventId: DOMString,
}

impl ExtendableMessageEvent {
pub fn new(global: GlobalRef, type_: Atom,
bubbles: bool, cancelable: bool,
data: HandleValue, origin: DOMString, lastEventId: DOMString)
-> Root<ExtendableMessageEvent> {
let mut ev = box ExtendableMessageEvent {
event: ExtendableEvent::new_inherited(),
data: Heap::default(),
origin: origin,
lastEventId: lastEventId,
};
ev.data.set(data.get());
let ev = reflect_dom_object(ev, global, ExtendableMessageEventBinding::Wrap);
{
let event = ev.upcast::<Event>();
event.init_event(type_, bubbles, cancelable);
}
ev
}

pub fn Constructor(global: GlobalRef,
type_: DOMString,
init: &ExtendableMessageEventBinding::ExtendableMessageEventInit)
-> Fallible<Root<ExtendableMessageEvent>> {
rooted!(in(global.get_cx()) let data = init.data);
let ev = ExtendableMessageEvent::new(global, Atom::from(type_),
init.parent.parent.bubbles,
init.parent.parent.cancelable,
data.handle(),
init.origin.clone().unwrap(),
init.lastEventId.clone().unwrap());
Ok(ev)
}
}

impl ExtendableMessageEvent {
pub fn dispatch_jsval(target: &EventTarget,
scope: GlobalRef,
message: HandleValue) {
let Extendablemessageevent = ExtendableMessageEvent::new(
scope, atom!("message"), false, false, message,
DOMString::new(), DOMString::new());
Extendablemessageevent.upcast::<Event>().fire(target);
}
}

impl ExtendableMessageEventMethods for ExtendableMessageEvent {
// https://w3c.github.io/ServiceWorker/#extendablemessage-event-data-attribute
fn Data(&self, _cx: *mut JSContext) -> JSVal {
self.data.get()
}

// https://w3c.github.io/ServiceWorker/#extendablemessage-event-origin-attribute
fn Origin(&self) -> DOMString {
self.origin.clone()
}

// https://w3c.github.io/ServiceWorker/#extendablemessage-event-lasteventid-attribute
fn LastEventId(&self) -> DOMString {
self.lastEventId.clone()
}

// https://dom.spec.whatwg.org/#dom-event-istrusted
fn IsTrusted(&self) -> bool {
self.event.IsTrusted()
}
}
@@ -267,6 +267,8 @@ pub mod event;
pub mod eventdispatcher;
pub mod eventsource;
pub mod eventtarget;
pub mod extendableevent;
pub mod extendablemessageevent;
pub mod file;
pub mod filelist;
pub mod filereader;
@@ -13,8 +13,10 @@ use dom::bindings::inheritance::Castable;
use dom::bindings::js::{Root, RootCollection};
use dom::bindings::reflector::Reflectable;
use dom::bindings::str::DOMString;
use dom::event::Event;
use dom::eventtarget::EventTarget;
use dom::messageevent::MessageEvent;
use dom::extendableevent::ExtendableEvent;
use dom::extendablemessageevent::ExtendableMessageEvent;
use dom::workerglobalscope::WorkerGlobalScope;
use ipc_channel::ipc::{self, IpcSender, IpcReceiver};
use ipc_channel::router::ROUTER;
@@ -195,7 +197,7 @@ impl ServiceWorkerGlobalScope {
let _ = timer_chan.send(());
});

scope.upcast::<EventTarget>().fire_simple_event("activate");
global.dispatch_activate();
let reporter_name = format!("service-worker-reporter-{}", random::<u64>());
scope.mem_profiler_chan().run_with_memory_reporting(|| {
while let Ok(event) = global.receive_event() {
@@ -243,7 +245,7 @@ impl ServiceWorkerGlobalScope {
let _ac = JSAutoCompartment::new(scope.get_cx(), scope.reflector().get_jsobject().get());
rooted!(in(scope.get_cx()) let mut message = UndefinedValue());
data.read(GlobalRef::Worker(scope), message.handle_mut());
MessageEvent::dispatch_jsval(target, GlobalRef::Worker(scope), message.handle());
ExtendableMessageEvent::dispatch_jsval(target, GlobalRef::Worker(scope), message.handle());
},
CommonWorker(WorkerScriptMsg::Common(CommonScriptMsg::RunnableMsg(_, runnable))) => {
runnable.handler()
@@ -309,6 +311,13 @@ impl ServiceWorkerGlobalScope {
sender: self.own_sender.clone()
}
}

fn dispatch_activate(&self) {
let global = GlobalRef::Worker(self.upcast::<WorkerGlobalScope>());
let event = ExtendableEvent::new(global, atom!("activate"), false, false);
let event = (&*event).upcast::<Event>();
self.upcast::<EventTarget>().dispatch_event(event);
}
}

#[allow(unsafe_code)]
@@ -0,0 +1,17 @@
/* 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/. */

// https://w3c.github.io/ServiceWorker/#extendable-event

[Constructor(DOMString type,
optional ExtendableEventInit eventInitDict),
Exposed=ServiceWorker,
Pref="dom.serviceworker.enabled"]
interface ExtendableEvent : Event {
[Throws] void waitUntil(/*Promise<*/any/*>*/ f);
};

dictionary ExtendableEventInit : EventInit {
// Defined for the forward compatibility across the derived events
};
@@ -0,0 +1,24 @@
/* 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/. */

// https://w3c.github.io/ServiceWorker/#extendablemessage-event-section

[Constructor(DOMString type, optional ExtendableMessageEventInit eventInitDict),
Exposed=ServiceWorker,
Pref="dom.serviceworker.enabled"]
interface ExtendableMessageEvent : ExtendableEvent {
readonly attribute any data;
readonly attribute DOMString origin;
readonly attribute DOMString lastEventId;
// [SameObject] readonly attribute (Client or ServiceWorker /*or MessagePort*/)? source;
// readonly attribute FrozenArray<MessagePort>? ports;
};

dictionary ExtendableMessageEventInit : ExtendableEventInit {
any data;
DOMString origin;
DOMString lastEventId;
// (Client or ServiceWorker /*or MessagePort*/)? source;
// sequence<MessagePort>? ports;
};

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 8b10cca

Please sign in to comment.
You can’t perform that action at this time.