summaryrefslogtreecommitdiffstats
path: root/dom/workers/remoteworkers/RemoteWorkerControllerChild.cpp
blob: 5a1db77cf00ac1a611582554c6aca86c7ad93138 (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
/* -*- 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 "RemoteWorkerControllerChild.h"

#include <utility>

#include "MainThreadUtils.h"
#include "nsError.h"
#include "nsThreadUtils.h"

#include "ServiceWorkerPrivate.h"
#include "mozilla/Assertions.h"
#include "mozilla/RefPtr.h"
#include "mozilla/Unused.h"
#include "mozilla/dom/PFetchEventOpChild.h"

namespace mozilla {

using ipc::IPCResult;

namespace dom {

RemoteWorkerControllerChild::RemoteWorkerControllerChild(
    RefPtr<RemoteWorkerObserver> aObserver)
    : mObserver(std::move(aObserver)) {
  AssertIsOnMainThread();
  mRemoteWorkerLaunchStart = TimeStamp::Now();
  MOZ_ASSERT(mObserver);
}

PFetchEventOpChild* RemoteWorkerControllerChild::AllocPFetchEventOpChild(
    const ParentToParentServiceWorkerFetchEventOpArgs& aArgs) {
  MOZ_CRASH("PFetchEventOpChild actors must be manually constructed!");
  return nullptr;
}

TimeStamp RemoteWorkerControllerChild::GetRemoteWorkerLaunchStart() {
  MOZ_ASSERT(mRemoteWorkerLaunchStart);
  return mRemoteWorkerLaunchStart;
}

TimeStamp RemoteWorkerControllerChild::GetRemoteWorkerLaunchEnd() {
  MOZ_ASSERT(mRemoteWorkerLaunchEnd);
  return mRemoteWorkerLaunchEnd;
}

bool RemoteWorkerControllerChild::DeallocPFetchEventOpChild(
    PFetchEventOpChild* aActor) {
  AssertIsOnMainThread();
  MOZ_ASSERT(aActor);

  delete aActor;
  return true;
}

void RemoteWorkerControllerChild::ActorDestroy(ActorDestroyReason aReason) {
  AssertIsOnMainThread();

  mIPCActive = false;

  if (NS_WARN_IF(mObserver)) {
    mObserver->ErrorReceived(NS_ERROR_DOM_ABORT_ERR);
  }
}

IPCResult RemoteWorkerControllerChild::RecvCreationFailed() {
  AssertIsOnMainThread();

  if (mObserver) {
    mObserver->CreationFailed();
  }

  return IPC_OK();
}

IPCResult RemoteWorkerControllerChild::RecvCreationSucceeded() {
  AssertIsOnMainThread();
  mRemoteWorkerLaunchEnd = TimeStamp::Now();

  if (mObserver) {
    mObserver->CreationSucceeded();
  }

  return IPC_OK();
}

IPCResult RemoteWorkerControllerChild::RecvErrorReceived(
    const ErrorValue& aError) {
  AssertIsOnMainThread();
  mRemoteWorkerLaunchEnd = TimeStamp::Now();

  if (mObserver) {
    mObserver->ErrorReceived(aError);
  }

  return IPC_OK();
}

IPCResult RemoteWorkerControllerChild::RecvTerminated() {
  AssertIsOnMainThread();

  if (mObserver) {
    mObserver->Terminated();
  }

  return IPC_OK();
}

IPCResult RemoteWorkerControllerChild::RecvSetServiceWorkerSkipWaitingFlag(
    SetServiceWorkerSkipWaitingFlagResolver&& aResolve) {
  AssertIsOnMainThread();

  if (mObserver) {
    static_cast<ServiceWorkerPrivate*>(mObserver.get())
        ->SetSkipWaitingFlag()
        ->Then(GetCurrentSerialEventTarget(), __func__,
               [resolve = std::move(aResolve)](
                   const GenericPromise::ResolveOrRejectValue& aResult) {
                 resolve(aResult.IsResolve() ? aResult.ResolveValue() : false);
               });

    return IPC_OK();
  }

  aResolve(false);

  return IPC_OK();
}

void RemoteWorkerControllerChild::RevokeObserver(
    RemoteWorkerObserver* aObserver) {
  AssertIsOnMainThread();
  MOZ_ASSERT(aObserver);
  MOZ_ASSERT(aObserver == mObserver);

  mObserver = nullptr;
}

void RemoteWorkerControllerChild::MaybeSendDelete() {
  AssertIsOnMainThread();

  if (!mIPCActive) {
    return;
  }

  RefPtr<RemoteWorkerControllerChild> self = this;

  SendShutdown()->Then(
      GetCurrentSerialEventTarget(), __func__,
      [self = std::move(self)](const ShutdownPromise::ResolveOrRejectValue&) {
        if (self->mIPCActive) {
          Unused << self->Send__delete__(self);
        }
      });
}

}  // namespace dom
}  // namespace mozilla