tor-browser

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

RemoteWorkerNonLifeCycleOpControllerChild.cpp (3204B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 #include "RemoteWorkerNonLifeCycleOpControllerChild.h"
      6 
      7 #include "mozilla/dom/ServiceWorkerOp.h"
      8 #include "mozilla/dom/SharedWorkerOp.h"
      9 #include "mozilla/dom/WorkerCommon.h"
     10 #include "mozilla/dom/WorkerPrivate.h"
     11 #include "mozilla/dom/WorkerRef.h"
     12 
     13 namespace mozilla::dom {
     14 
     15 using remoteworker::Canceled;
     16 using remoteworker::Killed;
     17 using remoteworker::Running;
     18 
     19 /* static */
     20 RefPtr<RemoteWorkerNonLifeCycleOpControllerChild>
     21 RemoteWorkerNonLifeCycleOpControllerChild::Create() {
     22  MOZ_ASSERT(!NS_IsMainThread());
     23  MOZ_ASSERT(GetCurrentThreadWorkerPrivate());
     24 
     25  RefPtr<RemoteWorkerNonLifeCycleOpControllerChild> actor =
     26      MakeAndAddRef<RemoteWorkerNonLifeCycleOpControllerChild>();
     27  return actor;
     28 }
     29 
     30 RemoteWorkerNonLifeCycleOpControllerChild::
     31    RemoteWorkerNonLifeCycleOpControllerChild()
     32    : mState(VariantType<remoteworker::Running>(),
     33             "RemoteWorkerNonLifeCycleOpControllerChild") {}
     34 
     35 RemoteWorkerNonLifeCycleOpControllerChild::
     36    ~RemoteWorkerNonLifeCycleOpControllerChild() = default;
     37 
     38 void RemoteWorkerNonLifeCycleOpControllerChild::TransistionStateToCanceled() {
     39  auto lock = mState.Lock();
     40  MOZ_ASSERT(lock->is<Running>());
     41 
     42  *lock = VariantType<Canceled>();
     43 }
     44 
     45 void RemoteWorkerNonLifeCycleOpControllerChild::TransistionStateToKilled() {
     46  auto lock = mState.Lock();
     47  MOZ_ASSERT(lock->is<Canceled>());
     48  *lock = VariantType<Killed>();
     49 
     50  if (!CanSend()) {
     51    return;
     52  }
     53  (void)SendTerminated();
     54  if (GetIPCChannel()) {
     55    GetIPCChannel()->Close();
     56  }
     57 }
     58 
     59 void RemoteWorkerNonLifeCycleOpControllerChild::ErrorPropagation(
     60    nsresult aError) {
     61  if (!CanSend()) {
     62    return;
     63  }
     64  (void)SendError(aError);
     65 }
     66 
     67 void RemoteWorkerNonLifeCycleOpControllerChild::StartOp(
     68    RefPtr<RemoteWorkerOp>&& aOp) {
     69  MOZ_ASSERT(aOp);
     70  auto lock = mState.Lock();
     71  // ServiceWorkerOp/SharedWorkerOp handles the Canceled/Killed state cases.
     72  aOp->Start(this, lock.ref());
     73 }
     74 
     75 IPCResult RemoteWorkerNonLifeCycleOpControllerChild::RecvExecOp(
     76    SharedWorkerOpArgs&& aOpArgs) {
     77  MOZ_ASSERT(aOpArgs.type() ==
     78             SharedWorkerOpArgs::TSharedWorkerPortIdentifierOpArgs);
     79  StartOp(new SharedWorkerOp(std::move(aOpArgs)));
     80 
     81  return IPC_OK();
     82 }
     83 
     84 IPCResult RemoteWorkerNonLifeCycleOpControllerChild::RecvExecServiceWorkerOp(
     85    ServiceWorkerOpArgs&& aOpArgs, ExecServiceWorkerOpResolver&& aResolve) {
     86  MOZ_ASSERT(
     87      aOpArgs.type() !=
     88          ServiceWorkerOpArgs::TParentToChildServiceWorkerFetchEventOpArgs,
     89      "FetchEvent operations should be sent via PFetchEventOp(Proxy) actors!");
     90 
     91  MOZ_ASSERT(aOpArgs.type() !=
     92                 ServiceWorkerOpArgs::TServiceWorkerTerminateWorkerOpArgs,
     93             "Terminate operations should be sent via PRemoteWorker actros!");
     94 
     95  StartOp(ServiceWorkerOp::Create(std::move(aOpArgs), std::move(aResolve)));
     96  return IPC_OK();
     97 }
     98 
     99 IPCResult RemoteWorkerNonLifeCycleOpControllerChild::RecvShutdown() {
    100  if (GetIPCChannel()) {
    101    GetIPCChannel()->Close();
    102  }
    103  return IPC_OK();
    104 }
    105 
    106 }  // namespace mozilla::dom