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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* 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 https://mozilla.org/MPL/2.0/. */

use std::default::Default;
use std::rc::Rc;

use dom_struct::dom_struct;
use ipc_channel::ipc;
use ipc_channel::router::ROUTER;
use script_traits::{Job, JobError, JobResult, JobResultValue, JobType, ScriptMsg};

use crate::dom::bindings::codegen::Bindings::ServiceWorkerContainerBinding::{
    RegistrationOptions, ServiceWorkerContainerMethods,
};
use crate::dom::bindings::error::Error;
use crate::dom::bindings::refcounted::TrustedPromise;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject};
use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
use crate::dom::bindings::str::USVString;
use crate::dom::client::Client;
use crate::dom::eventtarget::EventTarget;
use crate::dom::globalscope::GlobalScope;
use crate::dom::promise::Promise;
use crate::dom::serviceworker::ServiceWorker;
use crate::dom::serviceworkerregistration::ServiceWorkerRegistration;
use crate::realms::{enter_realm, InRealm};
use crate::script_runtime::CanGc;
use crate::task::TaskCanceller;
use crate::task_source::dom_manipulation::DOMManipulationTaskSource;
use crate::task_source::{TaskSource, TaskSourceName};

#[dom_struct]
pub struct ServiceWorkerContainer {
    eventtarget: EventTarget,
    controller: MutNullableDom<ServiceWorker>,
    client: Dom<Client>,
}

impl ServiceWorkerContainer {
    fn new_inherited(client: &Client) -> ServiceWorkerContainer {
        ServiceWorkerContainer {
            eventtarget: EventTarget::new_inherited(),
            controller: Default::default(),
            client: Dom::from_ref(client),
        }
    }

    #[allow(crown::unrooted_must_root)]
    pub fn new(global: &GlobalScope) -> DomRoot<ServiceWorkerContainer> {
        let client = Client::new(global.as_window());
        let container = ServiceWorkerContainer::new_inherited(&client);
        reflect_dom_object(Box::new(container), global)
    }
}

impl ServiceWorkerContainerMethods for ServiceWorkerContainer {
    // https://w3c.github.io/ServiceWorker/#service-worker-container-controller-attribute
    fn GetController(&self) -> Option<DomRoot<ServiceWorker>> {
        self.client.get_controller()
    }

    /// <https://w3c.github.io/ServiceWorker/#dom-serviceworkercontainer-register> - A
    /// and <https://w3c.github.io/ServiceWorker/#start-register> - B
    fn Register(
        &self,
        script_url: USVString,
        options: &RegistrationOptions,
        comp: InRealm,
        can_gc: CanGc,
    ) -> Rc<Promise> {
        // A: Step 2.
        let global = self.client.global();

        // A: Step 1
        let promise = Promise::new_in_current_realm(comp, can_gc);
        let USVString(ref script_url) = script_url;

        // A: Step 3
        let api_base_url = global.api_base_url();
        let script_url = match api_base_url.join(script_url) {
            Ok(url) => url,
            Err(_) => {
                // B: Step 1
                promise.reject_error(Error::Type("Invalid script URL".to_owned()));
                return promise;
            },
        };

        // A: Step 4-5
        let scope = match options.scope {
            Some(ref scope) => {
                let USVString(inner_scope) = scope;
                match api_base_url.join(inner_scope) {
                    Ok(url) => url,
                    Err(_) => {
                        promise.reject_error(Error::Type("Invalid scope URL".to_owned()));
                        return promise;
                    },
                }
            },
            None => script_url.join("./").unwrap(),
        };

        // A: Step 6 -> invoke B.

        // B: Step 3
        match script_url.scheme() {
            "https" | "http" => {},
            _ => {
                promise.reject_error(Error::Type("Only secure origins are allowed".to_owned()));
                return promise;
            },
        }
        // B: Step 4
        if script_url.path().to_ascii_lowercase().contains("%2f") ||
            script_url.path().to_ascii_lowercase().contains("%5c")
        {
            promise.reject_error(Error::Type(
                "Script URL contains forbidden characters".to_owned(),
            ));
            return promise;
        }

        // B: Step 6
        match scope.scheme() {
            "https" | "http" => {},
            _ => {
                promise.reject_error(Error::Type("Only secure origins are allowed".to_owned()));
                return promise;
            },
        }
        // B: Step 7
        if scope.path().to_ascii_lowercase().contains("%2f") ||
            scope.path().to_ascii_lowercase().contains("%5c")
        {
            promise.reject_error(Error::Type(
                "Scope URL contains forbidden characters".to_owned(),
            ));
            return promise;
        }

        // Setup the callback for reject/resolve of the promise,
        // from steps running "in-parallel" from here in the serviceworker manager.
        let (task_source, task_canceller) = (
            global.dom_manipulation_task_source(),
            global.task_canceller(TaskSourceName::DOMManipulation),
        );

        let mut handler = RegisterJobResultHandler {
            trusted_promise: Some(TrustedPromise::new(promise.clone())),
            task_source,
            task_canceller,
        };

        let (job_result_sender, job_result_receiver) = ipc::channel().expect("ipc channel failure");

        ROUTER.add_typed_route(
            job_result_receiver,
            Box::new(move |message| match message {
                Ok(msg) => handler.handle(msg),
                Err(err) => warn!("Error receiving a JobResult: {:?}", err),
            }),
        );

        let scope_things =
            ServiceWorkerRegistration::create_scope_things(&global, script_url.clone());

        // B: Step 8 - 13
        let job = Job::create_job(
            JobType::Register,
            scope,
            script_url,
            job_result_sender,
            self.client.creation_url(),
            Some(scope_things),
        );

        // B: Step 14: schedule job.
        let _ = global
            .script_to_constellation_chan()
            .send(ScriptMsg::ScheduleJob(job));

        // A: Step 7
        promise
    }
}

/// Callback for resolve/reject job promise for Register.
/// <https://w3c.github.io/ServiceWorker/#register>
struct RegisterJobResultHandler {
    trusted_promise: Option<TrustedPromise>,
    task_source: DOMManipulationTaskSource,
    task_canceller: TaskCanceller,
}

impl RegisterJobResultHandler {
    /// <https://w3c.github.io/ServiceWorker/#reject-job-promise>
    /// <https://w3c.github.io/ServiceWorker/#resolve-job-promise>
    /// Handle a result to either resolve or reject the register job promise.
    pub fn handle(&mut self, result: JobResult) {
        match result {
            JobResult::RejectPromise(error) => {
                let promise = self
                    .trusted_promise
                    .take()
                    .expect("No promise to resolve for SW Register job.");

                // Step 1
                let _ = self.task_source.queue_with_canceller(
                    task!(reject_promise_with_security_error: move || {
                        let promise = promise.root();
                        let _ac = enter_realm(&*promise.global());
                        match error {
                            JobError::TypeError => {
                                promise.reject_error(Error::Type("Failed to register a ServiceWorker".to_string()));
                            },
                            JobError::SecurityError => {
                                promise.reject_error(Error::Security);
                            },
                        }

                    }),
                    &self.task_canceller,
                );

                // TODO: step 2, handle equivalent jobs.
            },
            JobResult::ResolvePromise(job, value) => {
                let promise = self
                    .trusted_promise
                    .take()
                    .expect("No promise to resolve for SW Register job.");

                // Step 1
                let _ = self.task_source.queue_with_canceller(
                    task!(resolve_promise: move || {
                        let promise = promise.root();
                        let global = promise.global();
                        let _ac = enter_realm(&*global);

                        // Step 1.1
                        let JobResultValue::Registration {
                            id,
                            installing_worker,
                            waiting_worker,
                            active_worker,
                        } = value;

                        // Step 1.2 (Job type is "register").
                        let registration = global.get_serviceworker_registration(
                            &job.script_url,
                            &job.scope_url,
                            id,
                            installing_worker,
                            waiting_worker,
                            active_worker,
                        );

                        // Step 1.4
                        promise.resolve_native(&*registration);
                    }),
                    &self.task_canceller,
                );

                // TODO: step 2, handle equivalent jobs.
            },
        }
    }
}