1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 use dom::bindings::codegen::Bindings::ServiceWorkerBinding::ServiceWorkerState;
6 use dom::bindings::codegen::Bindings::ServiceWorkerRegistrationBinding::{ServiceWorkerRegistrationMethods, Wrap};
7 use dom::bindings::reflector::reflect_dom_object;
8 use dom::bindings::root::{Dom, DomRoot};
9 use dom::bindings::str::USVString;
10 use dom::eventtarget::EventTarget;
11 use dom::globalscope::GlobalScope;
12 use dom::serviceworker::ServiceWorker;
13 use dom::workerglobalscope::prepare_workerscope_init;
14 use dom_struct::dom_struct;
15 use script_traits::{WorkerScriptLoadOrigin, ScopeThings};
16 use servo_url::ServoUrl;
17 use std::cell::Cell;
18
19
20 #[dom_struct]
21 pub struct ServiceWorkerRegistration {
22 eventtarget: EventTarget,
23 active: Option<Dom<ServiceWorker>>,
24 installing: Option<Dom<ServiceWorker>>,
25 waiting: Option<Dom<ServiceWorker>>,
26 scope: ServoUrl,
27 uninstalling: Cell<bool>
28 }
29
30 impl ServiceWorkerRegistration {
new_inherited(active_sw: &ServiceWorker, scope: ServoUrl) -> ServiceWorkerRegistration31 fn new_inherited(active_sw: &ServiceWorker, scope: ServoUrl) -> ServiceWorkerRegistration {
32 ServiceWorkerRegistration {
33 eventtarget: EventTarget::new_inherited(),
34 active: Some(Dom::from_ref(active_sw)),
35 installing: None,
36 waiting: None,
37 scope: scope,
38 uninstalling: Cell::new(false)
39 }
40 }
41 #[allow(unrooted_must_root)]
new(global: &GlobalScope, script_url: &ServoUrl, scope: ServoUrl) -> DomRoot<ServiceWorkerRegistration>42 pub fn new(global: &GlobalScope,
43 script_url: &ServoUrl,
44 scope: ServoUrl) -> DomRoot<ServiceWorkerRegistration> {
45 let active_worker = ServiceWorker::install_serviceworker(global, script_url.clone(), scope.clone(), true);
46 active_worker.set_transition_state(ServiceWorkerState::Installed);
47 reflect_dom_object(Box::new(ServiceWorkerRegistration::new_inherited(&*active_worker, scope)), global, Wrap)
48 }
49
get_installed(&self) -> &ServiceWorker50 pub fn get_installed(&self) -> &ServiceWorker {
51 self.active.as_ref().unwrap()
52 }
53
get_uninstalling(&self) -> bool54 pub fn get_uninstalling(&self) -> bool {
55 self.uninstalling.get()
56 }
57
set_uninstalling(&self, flag: bool)58 pub fn set_uninstalling(&self, flag: bool) {
59 self.uninstalling.set(flag)
60 }
61
create_scope_things(global: &GlobalScope, script_url: ServoUrl) -> ScopeThings62 pub fn create_scope_things(global: &GlobalScope, script_url: ServoUrl) -> ScopeThings {
63 let worker_load_origin = WorkerScriptLoadOrigin {
64 referrer_url: None,
65 referrer_policy: None,
66 pipeline_id: Some(global.pipeline_id())
67 };
68
69 let worker_id = global.get_next_worker_id();
70 let devtools_chan = global.devtools_chan().cloned();
71 let init = prepare_workerscope_init(&global, None);
72 ScopeThings {
73 script_url: script_url,
74 init: init,
75 worker_load_origin: worker_load_origin,
76 devtools_chan: devtools_chan,
77 worker_id: worker_id
78 }
79 }
80
81 // https://w3c.github.io/ServiceWorker/#get-newest-worker-algorithm
get_newest_worker(&self) -> Option<DomRoot<ServiceWorker>>82 pub fn get_newest_worker(&self) -> Option<DomRoot<ServiceWorker>> {
83 if self.installing.as_ref().is_some() {
84 self.installing.as_ref().map(|sw| DomRoot::from_ref(&**sw))
85 } else if self.waiting.as_ref().is_some() {
86 self.waiting.as_ref().map(|sw| DomRoot::from_ref(&**sw))
87 } else {
88 self.active.as_ref().map(|sw| DomRoot::from_ref(&**sw))
89 }
90 }
91 }
92
longest_prefix_match(stored_scope: &ServoUrl, potential_match: &ServoUrl) -> bool93 pub fn longest_prefix_match(stored_scope: &ServoUrl, potential_match: &ServoUrl) -> bool {
94 if stored_scope.origin() != potential_match.origin() {
95 return false;
96 }
97 let scope_chars = stored_scope.path().chars();
98 let matching_chars = potential_match.path().chars();
99 if scope_chars.count() > matching_chars.count() {
100 return false;
101 }
102
103 stored_scope.path().chars().zip(potential_match.path().chars()).all(|(scope, matched)| scope == matched)
104 }
105
106 impl ServiceWorkerRegistrationMethods for ServiceWorkerRegistration {
107 // https://w3c.github.io/ServiceWorker/#service-worker-registration-installing-attribute
GetInstalling(&self) -> Option<DomRoot<ServiceWorker>>108 fn GetInstalling(&self) -> Option<DomRoot<ServiceWorker>> {
109 self.installing.as_ref().map(|sw| DomRoot::from_ref(&**sw))
110 }
111
112 // https://w3c.github.io/ServiceWorker/#service-worker-registration-active-attribute
GetActive(&self) -> Option<DomRoot<ServiceWorker>>113 fn GetActive(&self) -> Option<DomRoot<ServiceWorker>> {
114 self.active.as_ref().map(|sw| DomRoot::from_ref(&**sw))
115 }
116
117 // https://w3c.github.io/ServiceWorker/#service-worker-registration-waiting-attribute
GetWaiting(&self) -> Option<DomRoot<ServiceWorker>>118 fn GetWaiting(&self) -> Option<DomRoot<ServiceWorker>> {
119 self.waiting.as_ref().map(|sw| DomRoot::from_ref(&**sw))
120 }
121
122 // https://w3c.github.io/ServiceWorker/#service-worker-registration-scope-attribute
Scope(&self) -> USVString123 fn Scope(&self) -> USVString {
124 USVString(self.scope.as_str().to_owned())
125 }
126 }
127