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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
/* 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 crate::dom::bindings::codegen::Bindings::ServiceWorkerBinding::ServiceWorkerState;
use crate::dom::bindings::codegen::Bindings::ServiceWorkerRegistrationBinding::{
ServiceWorkerRegistrationMethods, Wrap,
};
use crate::dom::bindings::reflector::reflect_dom_object;
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::USVString;
use crate::dom::eventtarget::EventTarget;
use crate::dom::globalscope::GlobalScope;
use crate::dom::serviceworker::ServiceWorker;
use crate::dom::workerglobalscope::prepare_workerscope_init;
use dom_struct::dom_struct;
use script_traits::{ScopeThings, WorkerScriptLoadOrigin};
use servo_url::ServoUrl;
use std::cell::Cell;
#[dom_struct]
pub struct ServiceWorkerRegistration {
eventtarget: EventTarget,
active: Option<Dom<ServiceWorker>>,
installing: Option<Dom<ServiceWorker>>,
waiting: Option<Dom<ServiceWorker>>,
scope: ServoUrl,
uninstalling: Cell<bool>,
}
impl ServiceWorkerRegistration {
fn new_inherited(active_sw: &ServiceWorker, scope: ServoUrl) -> ServiceWorkerRegistration {
ServiceWorkerRegistration {
eventtarget: EventTarget::new_inherited(),
active: Some(Dom::from_ref(active_sw)),
installing: None,
waiting: None,
scope: scope,
uninstalling: Cell::new(false),
}
}
#[allow(unrooted_must_root)]
pub fn new(
global: &GlobalScope,
script_url: &ServoUrl,
scope: ServoUrl,
) -> DomRoot<ServiceWorkerRegistration> {
let active_worker =
ServiceWorker::install_serviceworker(global, script_url.clone(), scope.clone(), true);
active_worker.set_transition_state(ServiceWorkerState::Installed);
reflect_dom_object(
Box::new(ServiceWorkerRegistration::new_inherited(
&*active_worker,
scope,
)),
global,
Wrap,
)
}
pub fn get_installed(&self) -> &ServiceWorker {
self.active.as_ref().unwrap()
}
pub fn get_uninstalling(&self) -> bool {
self.uninstalling.get()
}
pub fn set_uninstalling(&self, flag: bool) {
self.uninstalling.set(flag)
}
pub fn create_scope_things(global: &GlobalScope, script_url: ServoUrl) -> ScopeThings {
let worker_load_origin = WorkerScriptLoadOrigin {
referrer_url: None,
referrer_policy: None,
pipeline_id: Some(global.pipeline_id()),
};
let worker_id = global.get_next_worker_id();
let devtools_chan = global.devtools_chan().cloned();
let init = prepare_workerscope_init(&global, None);
ScopeThings {
script_url: script_url,
init: init,
worker_load_origin: worker_load_origin,
devtools_chan: devtools_chan,
worker_id: worker_id,
}
}
// https://w3c.github.io/ServiceWorker/#get-newest-worker-algorithm
pub fn get_newest_worker(&self) -> Option<DomRoot<ServiceWorker>> {
if self.installing.as_ref().is_some() {
self.installing.as_ref().map(|sw| DomRoot::from_ref(&**sw))
} else if self.waiting.as_ref().is_some() {
self.waiting.as_ref().map(|sw| DomRoot::from_ref(&**sw))
} else {
self.active.as_ref().map(|sw| DomRoot::from_ref(&**sw))
}
}
}
pub fn longest_prefix_match(stored_scope: &ServoUrl, potential_match: &ServoUrl) -> bool {
if stored_scope.origin() != potential_match.origin() {
return false;
}
let scope_chars = stored_scope.path().chars();
let matching_chars = potential_match.path().chars();
if scope_chars.count() > matching_chars.count() {
return false;
}
stored_scope
.path()
.chars()
.zip(potential_match.path().chars())
.all(|(scope, matched)| scope == matched)
}
impl ServiceWorkerRegistrationMethods for ServiceWorkerRegistration {
// https://w3c.github.io/ServiceWorker/#service-worker-registration-installing-attribute
fn GetInstalling(&self) -> Option<DomRoot<ServiceWorker>> {
self.installing.as_ref().map(|sw| DomRoot::from_ref(&**sw))
}
// https://w3c.github.io/ServiceWorker/#service-worker-registration-active-attribute
fn GetActive(&self) -> Option<DomRoot<ServiceWorker>> {
self.active.as_ref().map(|sw| DomRoot::from_ref(&**sw))
}
// https://w3c.github.io/ServiceWorker/#service-worker-registration-waiting-attribute
fn GetWaiting(&self) -> Option<DomRoot<ServiceWorker>> {
self.waiting.as_ref().map(|sw| DomRoot::from_ref(&**sw))
}
// https://w3c.github.io/ServiceWorker/#service-worker-registration-scope-attribute
fn Scope(&self) -> USVString {
USVString(self.scope.as_str().to_owned())
}
}
|