tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ServiceWorkerContainerProxy.cpp (5158B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "ServiceWorkerContainerProxy.h"
      8 
      9 #include "mozilla/SchedulerGroup.h"
     10 #include "mozilla/ScopeExit.h"
     11 #include "mozilla/dom/ServiceWorkerContainerParent.h"
     12 #include "mozilla/dom/ServiceWorkerManager.h"
     13 #include "mozilla/ipc/BackgroundParent.h"
     14 
     15 namespace mozilla::dom {
     16 
     17 using mozilla::ipc::AssertIsOnBackgroundThread;
     18 
     19 ServiceWorkerContainerProxy::~ServiceWorkerContainerProxy() {
     20  // Any thread
     21  MOZ_DIAGNOSTIC_ASSERT(!mActor);
     22 }
     23 
     24 ServiceWorkerContainerProxy::ServiceWorkerContainerProxy(
     25    ServiceWorkerContainerParent* aActor)
     26    : mActor(aActor) {
     27  AssertIsOnBackgroundThread();
     28  MOZ_DIAGNOSTIC_ASSERT(mActor);
     29 
     30  // The container does not directly listen for updates, so we don't need
     31  // to immediately initialize.  The controllerchange event comes via the
     32  // ClientSource associated with the ServiceWorkerContainer's bound global.
     33 }
     34 
     35 void ServiceWorkerContainerProxy::RevokeActor(
     36    ServiceWorkerContainerParent* aActor) {
     37  AssertIsOnBackgroundThread();
     38  MOZ_DIAGNOSTIC_ASSERT(mActor);
     39  MOZ_DIAGNOSTIC_ASSERT(mActor == aActor);
     40  mActor = nullptr;
     41 }
     42 
     43 RefPtr<ServiceWorkerRegistrationPromise> ServiceWorkerContainerProxy::Register(
     44    const ClientInfo& aClientInfo, const nsACString& aScopeURL,
     45    const WorkerType& aType, const nsACString& aScriptURL,
     46    ServiceWorkerUpdateViaCache aUpdateViaCache) {
     47  AssertIsOnBackgroundThread();
     48 
     49  RefPtr<ServiceWorkerRegistrationPromise::Private> promise =
     50      new ServiceWorkerRegistrationPromise::Private(__func__);
     51 
     52  nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
     53      __func__,
     54      [aClientInfo, aScopeURL = nsCString(aScopeURL), aType,
     55       aScriptURL = nsCString(aScriptURL), aUpdateViaCache, promise]() mutable {
     56        auto scopeExit = MakeScopeExit(
     57            [&] { promise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__); });
     58 
     59        RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
     60        NS_ENSURE_TRUE_VOID(swm);
     61 
     62        swm->Register(aClientInfo, aScopeURL, aType, aScriptURL,
     63                      aUpdateViaCache)
     64            ->ChainTo(promise.forget(), __func__);
     65 
     66        scopeExit.release();
     67      });
     68 
     69  MOZ_ALWAYS_SUCCEEDS(SchedulerGroup::Dispatch(r.forget()));
     70 
     71  return promise;
     72 }
     73 
     74 RefPtr<ServiceWorkerRegistrationPromise>
     75 ServiceWorkerContainerProxy::GetRegistration(const ClientInfo& aClientInfo,
     76                                             const nsACString& aURL) {
     77  AssertIsOnBackgroundThread();
     78 
     79  RefPtr<ServiceWorkerRegistrationPromise::Private> promise =
     80      new ServiceWorkerRegistrationPromise::Private(__func__);
     81 
     82  nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
     83      __func__, [aClientInfo, aURL = nsCString(aURL), promise]() mutable {
     84        auto scopeExit = MakeScopeExit(
     85            [&] { promise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__); });
     86 
     87        RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
     88        NS_ENSURE_TRUE_VOID(swm);
     89 
     90        swm->GetRegistration(aClientInfo, aURL)
     91            ->ChainTo(promise.forget(), __func__);
     92 
     93        scopeExit.release();
     94      });
     95 
     96  MOZ_ALWAYS_SUCCEEDS(SchedulerGroup::Dispatch(r.forget()));
     97 
     98  return promise;
     99 }
    100 
    101 RefPtr<ServiceWorkerRegistrationListPromise>
    102 ServiceWorkerContainerProxy::GetRegistrations(const ClientInfo& aClientInfo) {
    103  AssertIsOnBackgroundThread();
    104 
    105  RefPtr<ServiceWorkerRegistrationListPromise::Private> promise =
    106      new ServiceWorkerRegistrationListPromise::Private(__func__);
    107 
    108  nsCOMPtr<nsIRunnable> r =
    109      NS_NewRunnableFunction(__func__, [aClientInfo, promise]() mutable {
    110        auto scopeExit = MakeScopeExit(
    111            [&] { promise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__); });
    112 
    113        RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
    114        NS_ENSURE_TRUE_VOID(swm);
    115 
    116        swm->GetRegistrations(aClientInfo)->ChainTo(promise.forget(), __func__);
    117 
    118        scopeExit.release();
    119      });
    120 
    121  MOZ_ALWAYS_SUCCEEDS(SchedulerGroup::Dispatch(r.forget()));
    122 
    123  return promise;
    124 }
    125 
    126 RefPtr<ServiceWorkerRegistrationPromise> ServiceWorkerContainerProxy::GetReady(
    127    const ClientInfo& aClientInfo) {
    128  AssertIsOnBackgroundThread();
    129 
    130  RefPtr<ServiceWorkerRegistrationPromise::Private> promise =
    131      new ServiceWorkerRegistrationPromise::Private(__func__);
    132 
    133  nsCOMPtr<nsIRunnable> r =
    134      NS_NewRunnableFunction(__func__, [aClientInfo, promise]() mutable {
    135        auto scopeExit = MakeScopeExit(
    136            [&] { promise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__); });
    137 
    138        RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
    139        NS_ENSURE_TRUE_VOID(swm);
    140 
    141        swm->WhenReady(aClientInfo)->ChainTo(promise.forget(), __func__);
    142 
    143        scopeExit.release();
    144      });
    145 
    146  MOZ_ALWAYS_SUCCEEDS(SchedulerGroup::Dispatch(r.forget()));
    147 
    148  return promise;
    149 }
    150 
    151 }  // namespace mozilla::dom