aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/serviceworker.rs
blob: 2b79be43dc857045865715fac57b2d5a8f27cb1f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* 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::abstractworker::{SimpleWorkerErrorHandler, WorkerScriptMsg};
use dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull;
use dom::bindings::codegen::Bindings::ServiceWorkerBinding::{ServiceWorkerMethods, ServiceWorkerState, Wrap};
use dom::bindings::global::GlobalRef;
use dom::bindings::inheritance::Castable;
use dom::bindings::js::Root;
use dom::bindings::refcounted::Trusted;
use dom::bindings::reflector::reflect_dom_object;
use dom::bindings::str::USVString;
use dom::eventtarget::EventTarget;
use script_thread::Runnable;
use std::cell::Cell;
use std::sync::mpsc::Sender;
use url::Url;

pub type TrustedServiceWorkerAddress = Trusted<ServiceWorker>;

#[dom_struct]
pub struct ServiceWorker {
    eventtarget: EventTarget,
    script_url: DOMRefCell<String>,
    state: Cell<ServiceWorkerState>,
    #[ignore_heap_size_of = "Defined in std"]
    sender: Option<Sender<(TrustedServiceWorkerAddress, WorkerScriptMsg)>>,
    skip_waiting: Cell<bool>
}

impl ServiceWorker {
    fn new_inherited(script_url: &str,
                     skip_waiting: bool) -> ServiceWorker {
        ServiceWorker {
            eventtarget: EventTarget::new_inherited(),
            sender: None,
            script_url: DOMRefCell::new(String::from(script_url)),
            state: Cell::new(ServiceWorkerState::Installing),
            skip_waiting: Cell::new(skip_waiting)
        }
    }

    pub fn new(global: GlobalRef,
                script_url: &str,
                skip_waiting: bool) -> Root<ServiceWorker> {
        reflect_dom_object(box ServiceWorker::new_inherited(script_url, skip_waiting), global, Wrap)
    }

    pub fn dispatch_simple_error(address: TrustedServiceWorkerAddress) {
        let service_worker = address.root();
        service_worker.upcast().fire_simple_event("error");
    }

    pub fn set_transition_state(&self, state: ServiceWorkerState) {
        self.state.set(state);
        self.upcast::<EventTarget>().fire_simple_event("statechange");
    }

    pub fn get_script_url(&self) -> Url {
        Url::parse(&self.script_url.borrow().clone()).unwrap()
    }

    pub fn install_serviceworker(global: GlobalRef,
                                 script_url: Url,
                                 skip_waiting: bool) -> Root<ServiceWorker> {
        ServiceWorker::new(global,
                           script_url.as_str(),
                           skip_waiting)
    }
}

impl ServiceWorkerMethods for ServiceWorker {
    // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#service-worker-state-attribute
    fn State(&self) -> ServiceWorkerState {
        self.state.get()
    }

    // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#service-worker-url-attribute
    fn ScriptURL(&self) -> USVString {
        USVString(self.script_url.borrow().clone())
    }

    // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#service-worker-container-onerror-attribute
    event_handler!(error, GetOnerror, SetOnerror);

    // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#ref-for-service-worker-onstatechange-attribute-1
    event_handler!(statechange, GetOnstatechange, SetOnstatechange);
}

impl Runnable for SimpleWorkerErrorHandler<ServiceWorker> {
    #[allow(unrooted_must_root)]
    fn handler(self: Box<SimpleWorkerErrorHandler<ServiceWorker>>) {
        let this = *self;
        ServiceWorker::dispatch_simple_error(this.addr);
    }
}