summaryrefslogtreecommitdiffstats
path: root/dom/serviceworkers/ServiceWorkerRegistrationProxy.cpp
blob: 60756b828d79e49db7b156f3575a7a13223b98aa (plain)
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */

#include "ServiceWorkerRegistrationProxy.h"

#include "mozilla/SchedulerGroup.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/ipc/BackgroundParent.h"
#include "ServiceWorkerManager.h"
#include "ServiceWorkerRegistrationParent.h"
#include "ServiceWorkerUnregisterCallback.h"

namespace mozilla::dom {

using mozilla::ipc::AssertIsOnBackgroundThread;

class ServiceWorkerRegistrationProxy::DelayedUpdate final
    : public nsITimerCallback,
      public nsINamed {
  RefPtr<ServiceWorkerRegistrationProxy> mProxy;
  RefPtr<ServiceWorkerRegistrationPromise::Private> mPromise;
  nsCOMPtr<nsITimer> mTimer;
  nsCString mNewestWorkerScriptUrl;

  ~DelayedUpdate() = default;

 public:
  NS_DECL_THREADSAFE_ISUPPORTS
  NS_DECL_NSITIMERCALLBACK
  NS_DECL_NSINAMED

  DelayedUpdate(RefPtr<ServiceWorkerRegistrationProxy>&& aProxy,
                RefPtr<ServiceWorkerRegistrationPromise::Private>&& aPromise,
                nsCString&& aNewestWorkerScriptUrl, uint32_t delay);

  void ChainTo(RefPtr<ServiceWorkerRegistrationPromise::Private> aPromise);

  void Reject();

  void SetNewestWorkerScriptUrl(nsCString&& aNewestWorkerScriptUrl);
};

ServiceWorkerRegistrationProxy::~ServiceWorkerRegistrationProxy() {
  // Any thread
  MOZ_DIAGNOSTIC_ASSERT(!mActor);
  MOZ_DIAGNOSTIC_ASSERT(!mReg);
}

void ServiceWorkerRegistrationProxy::MaybeShutdownOnBGThread() {
  AssertIsOnBackgroundThread();
  if (!mActor) {
    return;
  }
  mActor->MaybeSendDelete();
}

void ServiceWorkerRegistrationProxy::UpdateStateOnBGThread(
    const ServiceWorkerRegistrationDescriptor& aDescriptor) {
  AssertIsOnBackgroundThread();
  if (!mActor) {
    return;
  }
  Unused << mActor->SendUpdateState(aDescriptor.ToIPC());
}

void ServiceWorkerRegistrationProxy::FireUpdateFoundOnBGThread() {
  AssertIsOnBackgroundThread();
  if (!mActor) {
    return;
  }
  Unused << mActor->SendFireUpdateFound();
}

void ServiceWorkerRegistrationProxy::InitOnMainThread() {
  AssertIsOnMainThread();

  auto scopeExit = MakeScopeExit([&] { MaybeShutdownOnMainThread(); });

  RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
  NS_ENSURE_TRUE_VOID(swm);

  RefPtr<ServiceWorkerRegistrationInfo> reg =
      swm->GetRegistration(mDescriptor.PrincipalInfo(), mDescriptor.Scope());
  NS_ENSURE_TRUE_VOID(reg);

  if (reg->Id() != mDescriptor.Id()) {
    // This registration has already been replaced by another one.
    return;
  }

  scopeExit.release();

  mReg = new nsMainThreadPtrHolder<ServiceWorkerRegistrationInfo>(
      "ServiceWorkerRegistrationProxy::mInfo", reg);

  mReg->AddInstance(this, mDescriptor);
}

void ServiceWorkerRegistrationProxy::MaybeShutdownOnMainThread() {
  AssertIsOnMainThread();

  if (mDelayedUpdate) {
    mDelayedUpdate->Reject();
    mDelayedUpdate = nullptr;
  }
  nsCOMPtr<nsIRunnable> r = NewRunnableMethod(
      __func__, this, &ServiceWorkerRegistrationProxy::MaybeShutdownOnBGThread);

  MOZ_ALWAYS_SUCCEEDS(mEventTarget->Dispatch(r.forget(), NS_DISPATCH_NORMAL));
}

void ServiceWorkerRegistrationProxy::StopListeningOnMainThread() {
  AssertIsOnMainThread();

  if (!mReg) {
    return;
  }

  mReg->RemoveInstance(this);
  mReg = nullptr;
}

void ServiceWorkerRegistrationProxy::UpdateState(
    const ServiceWorkerRegistrationDescriptor& aDescriptor) {
  AssertIsOnMainThread();

  if (mDescriptor == aDescriptor) {
    return;
  }
  mDescriptor = aDescriptor;

  nsCOMPtr<nsIRunnable> r =
      NewRunnableMethod<ServiceWorkerRegistrationDescriptor>(
          __func__, this,
          &ServiceWorkerRegistrationProxy::UpdateStateOnBGThread, aDescriptor);

  MOZ_ALWAYS_SUCCEEDS(mEventTarget->Dispatch(r.forget(), NS_DISPATCH_NORMAL));
}

void ServiceWorkerRegistrationProxy::FireUpdateFound() {
  AssertIsOnMainThread();

  nsCOMPtr<nsIRunnable> r = NewRunnableMethod(
      __func__, this,
      &ServiceWorkerRegistrationProxy::FireUpdateFoundOnBGThread);

  MOZ_ALWAYS_SUCCEEDS(mEventTarget->Dispatch(r.forget(), NS_DISPATCH_NORMAL));
}

void ServiceWorkerRegistrationProxy::RegistrationCleared() {
  MaybeShutdownOnMainThread();
}

void ServiceWorkerRegistrationProxy::GetScope(nsAString& aScope) const {
  CopyUTF8toUTF16(mDescriptor.Scope(), aScope);
}

bool ServiceWorkerRegistrationProxy::MatchesDescriptor(
    const ServiceWorkerRegistrationDescriptor& aDescriptor) {
  AssertIsOnMainThread();
  return aDescriptor.Id() == mDescriptor.Id() &&
         aDescriptor.PrincipalInfo() == mDescriptor.PrincipalInfo() &&
         aDescriptor.Scope() == mDescriptor.Scope();
}

ServiceWorkerRegistrationProxy::ServiceWorkerRegistrationProxy(
    const ServiceWorkerRegistrationDescriptor& aDescriptor)
    : mEventTarget(GetCurrentSerialEventTarget()), mDescriptor(aDescriptor) {}

void ServiceWorkerRegistrationProxy::Init(
    ServiceWorkerRegistrationParent* aActor) {
  AssertIsOnBackgroundThread();
  MOZ_DIAGNOSTIC_ASSERT(aActor);
  MOZ_DIAGNOSTIC_ASSERT(!mActor);
  MOZ_DIAGNOSTIC_ASSERT(mEventTarget);

  mActor = aActor;

  // Note, this must be done from a separate Init() method and not in
  // the constructor.  If done from the constructor the runnable can
  // execute, complete, and release its reference before the constructor
  // returns.
  nsCOMPtr<nsIRunnable> r =
      NewRunnableMethod("ServiceWorkerRegistrationProxy::Init", this,
                        &ServiceWorkerRegistrationProxy::InitOnMainThread);
  MOZ_ALWAYS_SUCCEEDS(SchedulerGroup::Dispatch(r.forget()));
}

void ServiceWorkerRegistrationProxy::RevokeActor(
    ServiceWorkerRegistrationParent* aActor) {
  AssertIsOnBackgroundThread();
  MOZ_DIAGNOSTIC_ASSERT(mActor);
  MOZ_DIAGNOSTIC_ASSERT(mActor == aActor);
  mActor = nullptr;

  nsCOMPtr<nsIRunnable> r = NewRunnableMethod(
      __func__, this,
      &ServiceWorkerRegistrationProxy::StopListeningOnMainThread);
  MOZ_ALWAYS_SUCCEEDS(SchedulerGroup::Dispatch(r.forget()));
}

RefPtr<GenericPromise> ServiceWorkerRegistrationProxy::Unregister() {
  AssertIsOnBackgroundThread();

  RefPtr<ServiceWorkerRegistrationProxy> self = this;
  RefPtr<GenericPromise::Private> promise =
      new GenericPromise::Private(__func__);

  nsCOMPtr<nsIRunnable> r =
      NS_NewRunnableFunction(__func__, [self, promise]() mutable {
        nsresult rv = NS_ERROR_DOM_INVALID_STATE_ERR;
        auto scopeExit = MakeScopeExit([&] { promise->Reject(rv, __func__); });

        NS_ENSURE_TRUE_VOID(self->mReg);

        RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
        NS_ENSURE_TRUE_VOID(swm);

        RefPtr<UnregisterCallback> cb = new UnregisterCallback(promise);

        rv = swm->Unregister(self->mReg->Principal(), cb,
                             NS_ConvertUTF8toUTF16(self->mReg->Scope()));
        NS_ENSURE_SUCCESS_VOID(rv);

        scopeExit.release();
      });

  MOZ_ALWAYS_SUCCEEDS(SchedulerGroup::Dispatch(r.forget()));

  return promise;
}

namespace {

class UpdateCallback final : public ServiceWorkerUpdateFinishCallback {
  RefPtr<ServiceWorkerRegistrationPromise::Private> mPromise;

  ~UpdateCallback() = default;

 public:
  explicit UpdateCallback(
      RefPtr<ServiceWorkerRegistrationPromise::Private>&& aPromise)
      : mPromise(std::move(aPromise)) {
    MOZ_DIAGNOSTIC_ASSERT(mPromise);
  }

  void UpdateSucceeded(ServiceWorkerRegistrationInfo* aInfo) override {
    mPromise->Resolve(aInfo->Descriptor(), __func__);
  }

  void UpdateFailed(ErrorResult& aResult) override {
    mPromise->Reject(CopyableErrorResult(aResult), __func__);
  }
};

}  // anonymous namespace

NS_IMPL_ISUPPORTS(ServiceWorkerRegistrationProxy::DelayedUpdate,
                  nsITimerCallback, nsINamed)

ServiceWorkerRegistrationProxy::DelayedUpdate::DelayedUpdate(
    RefPtr<ServiceWorkerRegistrationProxy>&& aProxy,
    RefPtr<ServiceWorkerRegistrationPromise::Private>&& aPromise,
    nsCString&& aNewestWorkerScriptUrl, uint32_t delay)
    : mProxy(std::move(aProxy)),
      mPromise(std::move(aPromise)),
      mNewestWorkerScriptUrl(std::move(aNewestWorkerScriptUrl)) {
  MOZ_DIAGNOSTIC_ASSERT(mProxy);
  MOZ_DIAGNOSTIC_ASSERT(mPromise);
  MOZ_ASSERT(!mNewestWorkerScriptUrl.IsEmpty());
  mProxy->mDelayedUpdate = this;
  Result<nsCOMPtr<nsITimer>, nsresult> result =
      NS_NewTimerWithCallback(this, delay, nsITimer::TYPE_ONE_SHOT);
  mTimer = result.unwrapOr(nullptr);
  MOZ_DIAGNOSTIC_ASSERT(mTimer);
}

void ServiceWorkerRegistrationProxy::DelayedUpdate::ChainTo(
    RefPtr<ServiceWorkerRegistrationPromise::Private> aPromise) {
  AssertIsOnMainThread();
  MOZ_ASSERT(mProxy->mDelayedUpdate == this);
  MOZ_ASSERT(mPromise);

  mPromise->ChainTo(aPromise.forget(), __func__);
}

void ServiceWorkerRegistrationProxy::DelayedUpdate::Reject() {
  MOZ_DIAGNOSTIC_ASSERT(mPromise);
  if (mTimer) {
    mTimer->Cancel();
    mTimer = nullptr;
  }
  mPromise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__);
}

void ServiceWorkerRegistrationProxy::DelayedUpdate::SetNewestWorkerScriptUrl(
    nsCString&& aNewestWorkerScriptUrl) {
  MOZ_ASSERT(NS_IsMainThread());
  mNewestWorkerScriptUrl = std::move(aNewestWorkerScriptUrl);
}

NS_IMETHODIMP
ServiceWorkerRegistrationProxy::DelayedUpdate::Notify(nsITimer* aTimer) {
  // Already shutting down.
  if (mProxy->mDelayedUpdate != this) {
    return NS_OK;
  }

  auto scopeExit = MakeScopeExit(
      [&] { mPromise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__); });

  NS_ENSURE_TRUE(mProxy->mReg, NS_ERROR_FAILURE);

  RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
  NS_ENSURE_TRUE(swm, NS_ERROR_FAILURE);

  RefPtr<UpdateCallback> cb = new UpdateCallback(std::move(mPromise));
  swm->Update(mProxy->mReg->Principal(), mProxy->mReg->Scope(),
              std::move(mNewestWorkerScriptUrl), cb);

  mTimer = nullptr;
  mProxy->mDelayedUpdate = nullptr;

  scopeExit.release();
  return NS_OK;
}

NS_IMETHODIMP
ServiceWorkerRegistrationProxy::DelayedUpdate::GetName(nsACString& aName) {
  aName.AssignLiteral("ServiceWorkerRegistrationProxy::DelayedUpdate");
  return NS_OK;
}

RefPtr<ServiceWorkerRegistrationPromise> ServiceWorkerRegistrationProxy::Update(
    const nsACString& aNewestWorkerScriptUrl) {
  AssertIsOnBackgroundThread();

  RefPtr<ServiceWorkerRegistrationProxy> self = this;
  RefPtr<ServiceWorkerRegistrationPromise::Private> promise =
      new ServiceWorkerRegistrationPromise::Private(__func__);

  nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
      __func__,
      [self, promise,
       newestWorkerScriptUrl = nsCString(aNewestWorkerScriptUrl)]() mutable {
        auto scopeExit = MakeScopeExit(
            [&] { promise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__); });

        // Get the delay value for the update
        NS_ENSURE_TRUE_VOID(self->mReg);
        uint32_t delay = self->mReg->GetUpdateDelay(false);

        // If the delay value does not equal to 0, create a timer and a timer
        // callback to perform the delayed update. Otherwise, update directly.
        if (delay) {
          if (self->mDelayedUpdate) {
            // NOTE: if we `ChainTo(),` there will ultimately be a single
            // update, and this update will resolve all promises that were
            // issued while the update's timer was ticking down.
            self->mDelayedUpdate->ChainTo(std::move(promise));

            // Use the "newest newest worker"'s script URL.
            self->mDelayedUpdate->SetNewestWorkerScriptUrl(
                std::move(newestWorkerScriptUrl));
          } else {
            RefPtr<ServiceWorkerRegistrationProxy::DelayedUpdate> du =
                new ServiceWorkerRegistrationProxy::DelayedUpdate(
                    std::move(self), std::move(promise),
                    std::move(newestWorkerScriptUrl), delay);
          }
        } else {
          RefPtr<ServiceWorkerManager> swm =
              ServiceWorkerManager::GetInstance();
          NS_ENSURE_TRUE_VOID(swm);

          RefPtr<UpdateCallback> cb = new UpdateCallback(std::move(promise));
          swm->Update(self->mReg->Principal(), self->mReg->Scope(),
                      std::move(newestWorkerScriptUrl), cb);
        }
        scopeExit.release();
      });

  MOZ_ALWAYS_SUCCEEDS(SchedulerGroup::Dispatch(r.forget()));

  return promise;
}

RefPtr<GenericPromise>
ServiceWorkerRegistrationProxy::SetNavigationPreloadEnabled(
    const bool& aEnabled) {
  AssertIsOnBackgroundThread();

  RefPtr<ServiceWorkerRegistrationProxy> self = this;
  RefPtr<GenericPromise::Private> promise =
      new GenericPromise::Private(__func__);

  nsCOMPtr<nsIRunnable> r =
      NS_NewRunnableFunction(__func__, [aEnabled, self, promise]() mutable {
        nsresult rv = NS_ERROR_DOM_INVALID_STATE_ERR;
        auto scopeExit = MakeScopeExit([&] { promise->Reject(rv, __func__); });

        NS_ENSURE_TRUE_VOID(self->mReg);
        NS_ENSURE_TRUE_VOID(self->mReg->GetActive());

        auto reg = self->mReg;
        reg->SetNavigationPreloadEnabled(aEnabled);

        RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
        NS_ENSURE_TRUE_VOID(swm);
        swm->StoreRegistration(reg->Principal(), reg);

        scopeExit.release();

        promise->Resolve(true, __func__);
      });

  MOZ_ALWAYS_SUCCEEDS(SchedulerGroup::Dispatch(r.forget()));

  return promise;
}

RefPtr<GenericPromise>
ServiceWorkerRegistrationProxy::SetNavigationPreloadHeader(
    const nsACString& aHeader) {
  AssertIsOnBackgroundThread();

  RefPtr<ServiceWorkerRegistrationProxy> self = this;
  RefPtr<GenericPromise::Private> promise =
      new GenericPromise::Private(__func__);

  nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
      __func__, [aHeader = nsCString(aHeader), self, promise]() mutable {
        nsresult rv = NS_ERROR_DOM_INVALID_STATE_ERR;
        auto scopeExit = MakeScopeExit([&] { promise->Reject(rv, __func__); });

        NS_ENSURE_TRUE_VOID(self->mReg);
        NS_ENSURE_TRUE_VOID(self->mReg->GetActive());

        auto reg = self->mReg;
        reg->SetNavigationPreloadHeader(aHeader);

        RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
        NS_ENSURE_TRUE_VOID(swm);
        swm->StoreRegistration(reg->Principal(), reg);

        scopeExit.release();

        promise->Resolve(true, __func__);
      });

  MOZ_ALWAYS_SUCCEEDS(SchedulerGroup::Dispatch(r.forget()));

  return promise;
}

RefPtr<NavigationPreloadStatePromise>
ServiceWorkerRegistrationProxy::GetNavigationPreloadState() {
  AssertIsOnBackgroundThread();

  RefPtr<ServiceWorkerRegistrationProxy> self = this;
  RefPtr<NavigationPreloadStatePromise::Private> promise =
      new NavigationPreloadStatePromise::Private(__func__);

  nsCOMPtr<nsIRunnable> r =
      NS_NewRunnableFunction(__func__, [self, promise]() mutable {
        nsresult rv = NS_ERROR_DOM_INVALID_STATE_ERR;
        auto scopeExit = MakeScopeExit([&] { promise->Reject(rv, __func__); });

        NS_ENSURE_TRUE_VOID(self->mReg);
        scopeExit.release();

        promise->Resolve(self->mReg->GetNavigationPreloadState(), __func__);
      });

  MOZ_ALWAYS_SUCCEEDS(SchedulerGroup::Dispatch(r.forget()));

  return promise;
}

}  // namespace mozilla::dom