File: SharedWorkerOp.cpp

package info (click to toggle)
firefox 144.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,637,504 kB
  • sloc: cpp: 7,576,692; javascript: 6,430,831; ansic: 3,748,119; python: 1,398,978; xml: 628,810; asm: 438,679; java: 186,194; sh: 63,212; makefile: 19,159; objc: 13,086; perl: 12,986; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (245 lines) | stat: -rw-r--r-- 7,682 bytes parent folder | download | duplicates (4)
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
/* 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 "SharedWorkerOp.h"

#include "mozilla/dom/MessagePort.h"
#include "mozilla/dom/RemoteWorkerChild.h"
#include "mozilla/dom/RemoteWorkerNonLifeCycleOpControllerChild.h"
#include "mozilla/dom/WorkerPrivate.h"
#include "mozilla/dom/WorkerRunnable.h"
#include "mozilla/dom/WorkerScope.h"

namespace mozilla::dom {

using remoteworker::Canceled;
using remoteworker::Killed;
using remoteworker::Pending;
using remoteworker::Running;

namespace {

// Normal runnable because AddPortIdentifier() is going to exec JS code.
class MessagePortIdentifierRunnable final : public WorkerSameThreadRunnable {
 public:
  MessagePortIdentifierRunnable(
      RemoteWorkerNonLifeCycleOpControllerChild* aActor,
      const MessagePortIdentifier& aPortIdentifier)
      : WorkerSameThreadRunnable("MessagePortIdentifierRunnable"),
        mActor(aActor),
        mPortIdentifier(aPortIdentifier) {}

 private:
  bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
    if (aWorkerPrivate->GlobalScope()->IsDying()) {
      mPortIdentifier.ForceClose();
      return true;
    }
    if (!aWorkerPrivate->ConnectMessagePort(aCx, mPortIdentifier)) {
      mActor->ErrorPropagation(NS_ERROR_FAILURE);
    }
    return true;
  }

  RefPtr<RemoteWorkerNonLifeCycleOpControllerChild> mActor;
  UniqueMessagePortId mPortIdentifier;
};

class UpdateWindowIDRunnable final : public WorkerThreadRunnable {
 public:
  UpdateWindowIDRunnable(const uint64_t& aWindowID, const bool& aIsAdd)
      : WorkerThreadRunnable("UpdateWindowIDRunnable"),
        mWindowID(aWindowID),
        mIsAdd(aIsAdd) {}

 private:
  bool PreDispatch(WorkerPrivate* aWorkerPrivate) final { return true; }
  void PostDispatch(WorkerPrivate* aWorkerPrivate, bool aDispatchResult) final {
  }

  bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
    aWorkerPrivate->UpdateWindowIDToDebugger(mWindowID, mIsAdd);
    return true;
  }

  uint64_t mWindowID;
  bool mIsAdd;
};

}  // namespace

SharedWorkerOp::SharedWorkerOp(SharedWorkerOpArgs&& aArgs)
    : mOpArgs(std::move(aArgs)) {}

SharedWorkerOp::~SharedWorkerOp() { MOZ_ASSERT(mStarted); }

void SharedWorkerOp::Cancel() {
#ifdef DEBUG
  mStarted = true;
#endif
}

bool SharedWorkerOp::MaybeStart(RemoteWorkerChild* aOwner,
                                RemoteWorkerState& aState) {
  MOZ_ASSERT(!mStarted);
  MOZ_ASSERT(aOwner);
  // Thread: We are on the Worker Launcher thread.

  // Return false, indicating we should queue this op if our current state is
  // pending and this isn't a termination op (which should skip the line).
  if (aState.is<Pending>() && !IsTerminationOp()) {
    return false;
  }

  // If the worker is already shutting down (which should be unexpected
  // because we should be told new operations after a termination op), just
  // return true to indicate the op should be discarded.
  if (aState.is<Canceled>() || aState.is<Killed>()) {
#ifdef DEBUG
    mStarted = true;
#endif
    return true;
  }

  MOZ_ASSERT(aState.is<Running>() || IsTerminationOp());

  if (mOpArgs.type() == SharedWorkerOpArgs::TSharedWorkerThawOpArgs) {
    aOwner->SetIsThawing(true);
  }

  RefPtr<SharedWorkerOp> self = this;
  RefPtr<RemoteWorkerChild> owner = aOwner;

  nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
      __func__, [self = std::move(self), owner = std::move(owner)]() mutable {
        {
          auto lock = owner->mState.Lock();

          if (NS_WARN_IF(lock->is<Canceled>() || lock->is<Killed>())) {
            self->Cancel();
            return;
          }
        }

        self->StartOnMainThread(owner);
        if (self->mOpArgs.type() ==
            SharedWorkerOpArgs::TSharedWorkerThawOpArgs) {
          owner->SetIsThawing(false);
          owner->RunAllPendingOpsOnMainThread();
        }
      });

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

#ifdef DEBUG
  mStarted = true;
#endif

  return true;
}

void SharedWorkerOp::StartOnMainThread(RefPtr<RemoteWorkerChild>& aOwner) {
  AssertIsOnMainThread();

  if (IsTerminationOp()) {
    aOwner->CloseWorkerOnMainThread();
    return;
  }

  auto lock = aOwner->mState.Lock();
  MOZ_ASSERT(lock->is<Running>());
  if (!lock->is<Running>()) {
    aOwner->ErrorPropagationDispatch(NS_ERROR_DOM_INVALID_STATE_ERR);
    return;
  }

  RefPtr<WorkerPrivate> workerPrivate = lock->as<Running>().mWorkerPrivate;

  MOZ_ASSERT(workerPrivate);

  if (mOpArgs.type() == SharedWorkerOpArgs::TSharedWorkerSuspendOpArgs) {
    workerPrivate->ParentWindowPaused();
  } else if (mOpArgs.type() == SharedWorkerOpArgs::TSharedWorkerResumeOpArgs) {
    workerPrivate->ParentWindowResumed();
  } else if (mOpArgs.type() == SharedWorkerOpArgs::TSharedWorkerFreezeOpArgs) {
    workerPrivate->Freeze(nullptr);
  } else if (mOpArgs.type() == SharedWorkerOpArgs::TSharedWorkerThawOpArgs) {
    workerPrivate->Thaw(nullptr);
  } else if (mOpArgs.type() ==
             SharedWorkerOpArgs::TSharedWorkerPortIdentifierOpArgs) {
    MOZ_CRASH(
        "PortIdentifierOpArgs should not be processed by "
        "StartOnMainThread!!!");
  } else if (mOpArgs.type() ==
             SharedWorkerOpArgs::TSharedWorkerAddWindowIDOpArgs) {
    aOwner->mWindowIDs.AppendElement(
        mOpArgs.get_SharedWorkerAddWindowIDOpArgs().windowID());
    RefPtr<UpdateWindowIDRunnable> r = new UpdateWindowIDRunnable(
        mOpArgs.get_SharedWorkerAddWindowIDOpArgs().windowID(), true);
    Unused << r->Dispatch(workerPrivate);
  } else if (mOpArgs.type() ==
             SharedWorkerOpArgs::TSharedWorkerRemoveWindowIDOpArgs) {
    aOwner->mWindowIDs.RemoveElement(
        mOpArgs.get_SharedWorkerRemoveWindowIDOpArgs().windowID());
    RefPtr<UpdateWindowIDRunnable> r = new UpdateWindowIDRunnable(
        mOpArgs.get_SharedWorkerRemoveWindowIDOpArgs().windowID(), false);
    Unused << r->Dispatch(workerPrivate);
  } else {
    MOZ_CRASH("Unknown SharedWorkerOpArgs type!");
  }

#ifdef DEBUG
  if (!mStarted) {
    mStarted = true;
  }
#endif
}

void SharedWorkerOp::Start(RemoteWorkerNonLifeCycleOpControllerChild* aOwner,
                           RemoteWorkerState& aState) {
  MOZ_ASSERT(!mStarted);
  MOZ_ASSERT(aOwner);
  // Thread: We are on the Worker thread.

  // Only PortIdentifierOp is NonLifeCycle related opertaion.
  MOZ_ASSERT(mOpArgs.type() ==
             SharedWorkerOpArgs::TSharedWorkerPortIdentifierOpArgs);

  // Should never be Pending state.
  MOZ_ASSERT(!aState.is<Pending>());

  // If the worker is already shutting down (which should be unexpected
  // because we should be told new operations after a termination op), just
  // return directly.
  if (aState.is<Canceled>() || aState.is<Killed>()) {
#ifdef DEBUG
    mStarted = true;
#endif
    MessagePort::ForceClose(
        mOpArgs.get_SharedWorkerPortIdentifierOpArgs().portIdentifier());
    return;
  }

  MOZ_ASSERT(aState.is<Running>());

  // RefPtr<WorkerPrivate> workerPrivate = aState.as<Running>().mWorkerPrivate;
  WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();

  RefPtr<MessagePortIdentifierRunnable> r = new MessagePortIdentifierRunnable(
      aOwner, mOpArgs.get_SharedWorkerPortIdentifierOpArgs().portIdentifier());

  if (NS_WARN_IF(!r->Dispatch(workerPrivate))) {
    aOwner->ErrorPropagation(NS_ERROR_FAILURE);
  }

#ifdef DEBUG
  mStarted = true;
#endif
}

bool SharedWorkerOp::IsTerminationOp() const {
  return mOpArgs.type() == SharedWorkerOpArgs::TSharedWorkerTerminateOpArgs;
}

}  // namespace mozilla::dom