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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};

use delegate::delegate;
use wasm_bindgen::{JsCast, JsValue};
use wasm_bindgen_futures::JsFuture;

use crate::error::ServiceWorkerRegistrationError;
use crate::event::{OnControllerChange, OnError, OnMessage, OnStateChange};

pub use web_sys::ServiceWorkerState;

pub struct ServiceWorkerDescriptor<'a, 'b> {
    script_url: &'a str,
    scope: Option<&'b str>,
}

impl<'a, 'b> ServiceWorkerDescriptor<'a, 'b> {
    pub fn new(script_url: &'a str) -> Self {
        ServiceWorkerDescriptor {
            script_url,
            scope: None,
        }
    }

    pub fn scoped(script_url: &'a str, scope: &'b str) -> Self {
        ServiceWorkerDescriptor {
            script_url,
            scope: Some(scope),
        }
    }

    pub fn script_url(&self) -> &str {
        self.script_url
    }

    pub fn set_script_url(&mut self, script_url: &'a str) {
        self.script_url = script_url;
    }

    pub fn scope(&self) -> Option<&str> {
        self.scope
    }

    pub fn set_scope(&mut self, scope: Option<&'b str>) {
        self.scope = scope;
    }
}

pub struct ServiceWorkerContainer {
    inner: web_sys::ServiceWorkerContainer,
}

impl ServiceWorkerContainer {
    pub fn ready(&self) -> ServiceWorkerReady {
        ServiceWorkerReady {
            inner: self.inner.ready().unwrap().into(),
        }
    }

    pub fn controller(&self) -> Option<ServiceWorker> {
        self.inner.controller().map(|s| s.into())
    }

    pub fn register(&self, descriptor: ServiceWorkerDescriptor) -> ServiceWorkerRegister {
        let ServiceWorkerDescriptor { script_url, scope } = descriptor;

        let promise = if let Some(scope) = scope {
            self.inner
                .register_with_options(script_url, web_sys::RegistrationOptions::new().scope(scope))
        } else {
            self.inner.register(script_url)
        };

        ServiceWorkerRegister {
            inner: promise.into(),
        }
    }

    // Note: while get_registration make the scope url argument optional, we don't here. A scope of
    // `None` should be equivalent to `ready`.

    pub fn registration_for(&self, scope: &str) -> ServiceWorkerRegistrationFor {
        ServiceWorkerRegistrationFor {
            inner: self.inner.get_registration_with_document_url(scope).into(),
        }
    }

    // TODO: decide on naming of an equivalent for `get_registrations`

    // TODO: `start_messages` is missing in web_sys.

    pub fn on_controller_change(&self) -> OnControllerChange {
        OnControllerChange::new(self.inner.clone().into())
    }

    pub fn on_message(&self) -> OnMessage {
        OnMessage::new(self.inner.clone().into())
    }

    pub fn on_error(&self) -> OnError {
        OnError::new(self.inner.clone().into())
    }
}

impl From<web_sys::ServiceWorkerContainer> for ServiceWorkerContainer {
    fn from(inner: web_sys::ServiceWorkerContainer) -> Self {
        ServiceWorkerContainer { inner }
    }
}

impl AsRef<web_sys::ServiceWorkerContainer> for ServiceWorkerContainer {
    fn as_ref(&self) -> &web_sys::ServiceWorkerContainer {
        &self.inner
    }
}

pub struct ServiceWorkerReady {
    inner: JsFuture,
}

impl Future for ServiceWorkerReady {
    type Output = ServiceWorkerRegistration;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        unsafe {
            self.map_unchecked_mut(|s| &mut s.inner)
                .poll(cx)
                .map(|result| {
                    let registration: web_sys::ServiceWorkerRegistration =
                        result.unwrap().unchecked_into();

                    registration.into()
                })
        }
    }
}

pub struct ServiceWorkerRegister {
    inner: JsFuture,
}

impl Future for ServiceWorkerRegister {
    type Output = Result<ServiceWorkerRegistration, ServiceWorkerRegistrationError>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        unsafe {
            self.map_unchecked_mut(|s| &mut s.inner)
                .poll(cx)
                .map_ok(|ok| {
                    let registration: web_sys::ServiceWorkerRegistration = ok.unchecked_into();

                    registration.into()
                })
                .map_err(|err| ServiceWorkerRegistrationError::new(err.unchecked_into()))
        }
    }
}

pub struct ServiceWorkerRegistrationFor {
    inner: JsFuture,
}

impl Future for ServiceWorkerRegistrationFor {
    type Output = Option<ServiceWorkerRegistration>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        unsafe {
            self.map_unchecked_mut(|s| &mut s.inner)
                .poll(cx)
                .map(|result| {
                    result.ok().and_then(|ok| {
                        if ok.is_undefined() {
                            None
                        } else {
                            let registration: web_sys::ServiceWorkerRegistration =
                                ok.unchecked_into();

                            Some(registration.into())
                        }
                    })
                })
        }
    }
}

pub struct ServiceWorkerRegistration {
    inner: web_sys::ServiceWorkerRegistration,
}

impl ServiceWorkerRegistration {
    delegate! {
        target self.inner {
            pub fn scope(&self) -> String;
        }
    }

    pub fn installing(&self) -> Option<ServiceWorker> {
        self.inner.installing().map(|s| s.into())
    }

    pub fn waiting(&self) -> Option<ServiceWorker> {
        self.inner.waiting().map(|s| s.into())
    }

    pub fn active(&self) -> Option<ServiceWorker> {
        self.inner.active().map(|s| s.into())
    }

    pub fn update(&self) -> ServiceWorkerRegistrationUpdate {
        ServiceWorkerRegistrationUpdate {
            inner: self.inner.update().unwrap().into(),
        }
    }

    pub fn unregister(&self) -> ServiceWorkerRegistrationUnregister {
        ServiceWorkerRegistrationUnregister {
            inner: self.inner.update().unwrap().into(),
        }
    }

    pub fn on_update_found(&self) -> OnStateChange {
        OnStateChange::new(self.inner.clone().into())
    }
}

impl From<web_sys::ServiceWorkerRegistration> for ServiceWorkerRegistration {
    fn from(inner: web_sys::ServiceWorkerRegistration) -> Self {
        ServiceWorkerRegistration { inner }
    }
}

impl AsRef<web_sys::ServiceWorkerRegistration> for ServiceWorkerRegistration {
    fn as_ref(&self) -> &web_sys::ServiceWorkerRegistration {
        &self.inner
    }
}

pub struct ServiceWorkerRegistrationUpdate {
    inner: JsFuture,
}

impl Future for ServiceWorkerRegistrationUpdate {
    type Output = Result<ServiceWorkerRegistration, ServiceWorkerRegistrationError>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        unsafe {
            self.map_unchecked_mut(|s| &mut s.inner)
                .poll(cx)
                .map_ok(|ok| {
                    let registration: web_sys::ServiceWorkerRegistration = ok.unchecked_into();

                    registration.into()
                })
                .map_err(|err| ServiceWorkerRegistrationError::new(err.unchecked_into()))
        }
    }
}

pub struct ServiceWorkerRegistrationUnregister {
    inner: JsFuture,
}

impl Future for ServiceWorkerRegistrationUnregister {
    type Output = bool;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        unsafe {
            self.map_unchecked_mut(|s| &mut s.inner)
                .poll(cx)
                .map(|result| result.unwrap().as_bool().unwrap())
        }
    }
}

pub struct ServiceWorker {
    inner: web_sys::ServiceWorker,
}

impl ServiceWorker {
    delegate! {
        target self.inner {
            pub fn script_url(&self) -> String;

            pub fn state(&self) -> ServiceWorkerState;
        }
    }

    pub fn post_message(&self, message: &JsValue) {
        // No indication in the spec that this can fail if invoked without transferables
        self.inner.post_message(message).unwrap();
    }

    // TODO: post_message with transferables

    pub fn on_state_change(&self) -> OnStateChange {
        OnStateChange::new(self.inner.clone().into())
    }

    pub fn on_error(&self) -> OnError {
        OnError::new(self.inner.clone().into())
    }
}

impl From<web_sys::ServiceWorker> for ServiceWorker {
    fn from(inner: web_sys::ServiceWorker) -> Self {
        ServiceWorker { inner }
    }
}

impl AsRef<web_sys::ServiceWorker> for ServiceWorker {
    fn as_ref(&self) -> &web_sys::ServiceWorker {
        &self.inner
    }
}