tor-browser

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

RemoteWorkerControllerParent.cpp (5813B)


      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 "RemoteWorkerControllerParent.h"
      8 
      9 #include <utility>
     10 
     11 #include "mozilla/Assertions.h"
     12 #include "mozilla/dom/FetchEventOpParent.h"
     13 #include "mozilla/dom/RemoteWorkerParent.h"
     14 #include "mozilla/dom/ServiceWorkerOpPromise.h"
     15 #include "mozilla/ipc/BackgroundParent.h"
     16 #include "nsCOMPtr.h"
     17 #include "nsDebug.h"
     18 #include "nsError.h"
     19 #include "nsThreadUtils.h"
     20 
     21 namespace mozilla {
     22 
     23 using namespace ipc;
     24 
     25 namespace dom {
     26 
     27 RemoteWorkerControllerParent::RemoteWorkerControllerParent(
     28    const RemoteWorkerData& aRemoteWorkerData)
     29    : mRemoteWorkerController(RemoteWorkerController::Create(
     30          aRemoteWorkerData, this, 0 /* random process ID */)) {
     31  AssertIsInMainProcess();
     32  AssertIsOnBackgroundThread();
     33  MOZ_ASSERT(mRemoteWorkerController);
     34 }
     35 
     36 RefPtr<RemoteWorkerParent> RemoteWorkerControllerParent::GetRemoteWorkerParent()
     37    const {
     38  AssertIsOnBackgroundThread();
     39  MOZ_ASSERT(mRemoteWorkerController);
     40 
     41  return mRemoteWorkerController->mActor;
     42 }
     43 
     44 void RemoteWorkerControllerParent::MaybeSendSetServiceWorkerSkipWaitingFlag(
     45    std::function<void(bool)>&& aCallback) {
     46  AssertIsOnBackgroundThread();
     47  MOZ_ASSERT(aCallback);
     48 
     49  if (!mIPCActive) {
     50    aCallback(false);
     51    return;
     52  }
     53 
     54  SendSetServiceWorkerSkipWaitingFlag()->Then(
     55      GetCurrentSerialEventTarget(), __func__,
     56      [callback = std::move(aCallback)](
     57          const SetServiceWorkerSkipWaitingFlagPromise::ResolveOrRejectValue&
     58              aResult) {
     59        callback(aResult.IsResolve() ? aResult.ResolveValue() : false);
     60      });
     61 }
     62 
     63 RemoteWorkerControllerParent::~RemoteWorkerControllerParent() {
     64  AssertIsOnBackgroundThread();
     65  MOZ_ASSERT(!mIPCActive);
     66  MOZ_ASSERT(!mRemoteWorkerController);
     67 }
     68 
     69 PFetchEventOpParent* RemoteWorkerControllerParent::AllocPFetchEventOpParent(
     70    const ParentToParentServiceWorkerFetchEventOpArgs& aArgs) {
     71  AssertIsOnBackgroundThread();
     72 
     73  RefPtr<FetchEventOpParent> actor = new FetchEventOpParent();
     74  return actor.forget().take();
     75 }
     76 
     77 IPCResult RemoteWorkerControllerParent::RecvPFetchEventOpConstructor(
     78    PFetchEventOpParent* aActor,
     79    const ParentToParentServiceWorkerFetchEventOpArgs& aArgs) {
     80  AssertIsOnBackgroundThread();
     81  MOZ_ASSERT(aActor);
     82 
     83  RefPtr<FetchEventOpParent> realFetchOp =
     84      static_cast<FetchEventOpParent*>(aActor);
     85  mRemoteWorkerController->ExecServiceWorkerFetchEventOp(aArgs, realFetchOp)
     86      ->Then(GetCurrentSerialEventTarget(), __func__,
     87             [fetchOp = std::move(realFetchOp)](
     88                 ServiceWorkerFetchEventOpPromise::ResolveOrRejectValue&&
     89                     aResult) {
     90               if (NS_WARN_IF(aResult.IsReject())) {
     91                 MOZ_ASSERT(NS_FAILED(aResult.RejectValue()));
     92                 (void)fetchOp->Send__delete__(fetchOp, aResult.RejectValue());
     93                 return;
     94               }
     95 
     96               (void)fetchOp->Send__delete__(fetchOp, aResult.ResolveValue());
     97             });
     98 
     99  return IPC_OK();
    100 }
    101 
    102 bool RemoteWorkerControllerParent::DeallocPFetchEventOpParent(
    103    PFetchEventOpParent* aActor) {
    104  AssertIsOnBackgroundThread();
    105  MOZ_ASSERT(aActor);
    106 
    107  RefPtr<FetchEventOpParent> actor =
    108      dont_AddRef(static_cast<FetchEventOpParent*>(aActor));
    109  return true;
    110 }
    111 
    112 IPCResult RemoteWorkerControllerParent::RecvExecServiceWorkerOp(
    113    ServiceWorkerOpArgs&& aArgs, ExecServiceWorkerOpResolver&& aResolve) {
    114  AssertIsOnBackgroundThread();
    115  MOZ_ASSERT(mIPCActive);
    116  MOZ_ASSERT(mRemoteWorkerController);
    117 
    118  mRemoteWorkerController->ExecServiceWorkerOp(std::move(aArgs))
    119      ->Then(GetCurrentSerialEventTarget(), __func__,
    120             [resolve = std::move(aResolve)](
    121                 ServiceWorkerOpPromise::ResolveOrRejectValue&& aResult) {
    122               if (NS_WARN_IF(aResult.IsReject())) {
    123                 MOZ_ASSERT(NS_FAILED(aResult.RejectValue()));
    124                 resolve(aResult.RejectValue());
    125                 return;
    126               }
    127 
    128               resolve(aResult.ResolveValue());
    129             });
    130 
    131  return IPC_OK();
    132 }
    133 
    134 IPCResult RemoteWorkerControllerParent::RecvShutdown(
    135    ShutdownResolver&& aResolve) {
    136  AssertIsOnBackgroundThread();
    137  MOZ_ASSERT(mIPCActive);
    138  MOZ_ASSERT(mRemoteWorkerController);
    139 
    140  mIPCActive = false;
    141 
    142  mRemoteWorkerController->Shutdown();
    143  mRemoteWorkerController = nullptr;
    144 
    145  aResolve(true);
    146 
    147  return IPC_OK();
    148 }
    149 
    150 IPCResult RemoteWorkerControllerParent::Recv__delete__() {
    151  AssertIsOnBackgroundThread();
    152  MOZ_ASSERT(!mIPCActive);
    153  MOZ_ASSERT(!mRemoteWorkerController);
    154 
    155  return IPC_OK();
    156 }
    157 
    158 void RemoteWorkerControllerParent::ActorDestroy(ActorDestroyReason aReason) {
    159  AssertIsOnBackgroundThread();
    160 
    161  if (NS_WARN_IF(mIPCActive)) {
    162    mIPCActive = false;
    163  }
    164 
    165  if (NS_WARN_IF(mRemoteWorkerController)) {
    166    mRemoteWorkerController->Shutdown();
    167    mRemoteWorkerController = nullptr;
    168  }
    169 }
    170 
    171 void RemoteWorkerControllerParent::CreationFailed() {
    172  AssertIsOnBackgroundThread();
    173 
    174  if (!mIPCActive) {
    175    return;
    176  }
    177 
    178  (void)SendCreationFailed();
    179 }
    180 
    181 void RemoteWorkerControllerParent::CreationSucceeded() {
    182  AssertIsOnBackgroundThread();
    183 
    184  if (!mIPCActive) {
    185    return;
    186  }
    187 
    188  (void)SendCreationSucceeded();
    189 }
    190 
    191 void RemoteWorkerControllerParent::ErrorReceived(const ErrorValue& aValue) {
    192  AssertIsOnBackgroundThread();
    193 
    194  if (!mIPCActive) {
    195    return;
    196  }
    197 
    198  (void)SendErrorReceived(aValue);
    199 }
    200 
    201 void RemoteWorkerControllerParent::Terminated() {
    202  AssertIsOnBackgroundThread();
    203 
    204  if (!mIPCActive) {
    205    return;
    206  }
    207 
    208  (void)SendTerminated();
    209 }
    210 
    211 }  // namespace dom
    212 }  // namespace mozilla