Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit 07c822e

Browse files
committed
Bug 1231213 - Implement PRemoteWorkerController IPDL protocol and RemoteWorkerController{Parent,Child}. r=asuth
Differential Revision: https://phabricator.services.mozilla.com/D26168 --HG-- extra : moz-landing-system : lando
1 parent deae3fa commit 07c822e

13 files changed

Lines changed: 470 additions & 4 deletions
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 file,
3+
* You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
include protocol PBackground;
6+
7+
include RemoteWorkerTypes;
8+
include ServiceWorkerOpArgs;
9+
10+
namespace mozilla {
11+
namespace dom {
12+
13+
protocol PRemoteWorkerController {
14+
manager PBackground;
15+
16+
child:
17+
async CreationFailed();
18+
19+
async CreationSucceeded();
20+
21+
async ErrorReceived(ErrorValue aError);
22+
23+
async Terminated();
24+
25+
parent:
26+
async __delete__();
27+
28+
async Shutdown() returns (bool aOk);
29+
30+
};
31+
32+
} // namespace dom
33+
} // namespace mozilla

dom/workers/remoteworkers/RemoteWorkerController.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "mozilla/dom/MessagePort.h"
88
#include "mozilla/dom/MessagePortParent.h"
9+
#include "mozilla/dom/RemoteWorkerTypes.h"
910
#include "mozilla/ipc/BackgroundParent.h"
1011
#include "RemoteWorkerController.h"
1112
#include "RemoteWorkerManager.h"
@@ -26,7 +27,7 @@ already_AddRefed<RemoteWorkerController> RemoteWorkerController::Create(
2627
MOZ_ASSERT(aObserver);
2728

2829
RefPtr<RemoteWorkerController> controller =
29-
new RemoteWorkerController(aObserver);
30+
new RemoteWorkerController(aData, aObserver);
3031

3132
RefPtr<RemoteWorkerManager> manager = RemoteWorkerManager::GetOrCreate();
3233
MOZ_ASSERT(manager);
@@ -36,8 +37,13 @@ already_AddRefed<RemoteWorkerController> RemoteWorkerController::Create(
3637
return controller.forget();
3738
}
3839

39-
RemoteWorkerController::RemoteWorkerController(RemoteWorkerObserver* aObserver)
40-
: mObserver(aObserver), mState(ePending) {
40+
RemoteWorkerController::RemoteWorkerController(const RemoteWorkerData& aData,
41+
RemoteWorkerObserver* aObserver)
42+
: mObserver(aObserver),
43+
mState(ePending),
44+
mIsServiceWorker(aData.serviceWorkerData().type() ==
45+
OptionalServiceWorkerData::TServiceWorkerData) {
46+
AssertIsInMainProcess();
4147
AssertIsOnBackgroundThread();
4248
MOZ_ASSERT(XRE_IsParentProcess());
4349
}

dom/workers/remoteworkers/RemoteWorkerController.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ namespace dom {
8080

8181
class ErrorValue;
8282
class MessagePortIdentifier;
83+
class RemoteWorkerControllerParent;
84+
class RemoteWorkerData;
8385
class RemoteWorkerManager;
8486
class RemoteWorkerParent;
8587

@@ -97,6 +99,7 @@ class RemoteWorkerObserver {
9799
};
98100

99101
class RemoteWorkerController final {
102+
friend class RemoteWorkerControllerParent;
100103
friend class RemoteWorkerManager;
101104
friend class RemoteWorkerParent;
102105

@@ -124,7 +127,9 @@ class RemoteWorkerController final {
124127
void Thaw();
125128

126129
private:
127-
explicit RemoteWorkerController(RemoteWorkerObserver* aObserver);
130+
RemoteWorkerController(const RemoteWorkerData& aData,
131+
RemoteWorkerObserver* aObserver);
132+
128133
~RemoteWorkerController();
129134

130135
void SetWorkerActor(RemoteWorkerParent* aActor);
@@ -150,6 +155,8 @@ class RemoteWorkerController final {
150155
eTerminated,
151156
} mState;
152157

158+
const bool mIsServiceWorker;
159+
153160
struct Op {
154161
enum Type {
155162
eTerminate,
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 "RemoteWorkerControllerChild.h"
8+
9+
#include <utility>
10+
11+
#include "MainThreadUtils.h"
12+
#include "nsError.h"
13+
#include "nsThreadUtils.h"
14+
15+
#include "mozilla/Assertions.h"
16+
#include "mozilla/RefPtr.h"
17+
#include "mozilla/Unused.h"
18+
19+
namespace mozilla {
20+
21+
using ipc::IPCResult;
22+
23+
namespace dom {
24+
25+
RemoteWorkerControllerChild::RemoteWorkerControllerChild(
26+
RefPtr<RemoteWorkerObserver> aObserver)
27+
: mObserver(std::move(aObserver)) {
28+
AssertIsOnMainThread();
29+
MOZ_ASSERT(mObserver);
30+
}
31+
32+
void RemoteWorkerControllerChild::ActorDestroy(ActorDestroyReason aReason) {
33+
AssertIsOnMainThread();
34+
35+
mIPCActive = false;
36+
37+
if (NS_WARN_IF(mObserver)) {
38+
mObserver->ErrorReceived(NS_ERROR_DOM_ABORT_ERR);
39+
}
40+
}
41+
42+
IPCResult RemoteWorkerControllerChild::RecvCreationFailed() {
43+
AssertIsOnMainThread();
44+
45+
if (mObserver) {
46+
mObserver->CreationFailed();
47+
}
48+
49+
return IPC_OK();
50+
}
51+
52+
IPCResult RemoteWorkerControllerChild::RecvCreationSucceeded() {
53+
AssertIsOnMainThread();
54+
55+
if (mObserver) {
56+
mObserver->CreationSucceeded();
57+
}
58+
59+
return IPC_OK();
60+
}
61+
62+
IPCResult RemoteWorkerControllerChild::RecvErrorReceived(
63+
const ErrorValue& aError) {
64+
AssertIsOnMainThread();
65+
66+
if (mObserver) {
67+
mObserver->ErrorReceived(aError);
68+
}
69+
70+
return IPC_OK();
71+
}
72+
73+
IPCResult RemoteWorkerControllerChild::RecvTerminated() {
74+
AssertIsOnMainThread();
75+
76+
if (mObserver) {
77+
mObserver->Terminated();
78+
}
79+
80+
return IPC_OK();
81+
}
82+
83+
void RemoteWorkerControllerChild::RevokeObserver(
84+
RemoteWorkerObserver* aObserver) {
85+
AssertIsOnMainThread();
86+
MOZ_ASSERT(aObserver);
87+
MOZ_ASSERT(aObserver == mObserver);
88+
89+
mObserver = nullptr;
90+
}
91+
92+
void RemoteWorkerControllerChild::MaybeSendDelete() {
93+
AssertIsOnMainThread();
94+
95+
if (!mIPCActive) {
96+
return;
97+
}
98+
99+
RefPtr<RemoteWorkerControllerChild> self = this;
100+
101+
SendShutdown()->Then(
102+
GetCurrentThreadSerialEventTarget(), __func__,
103+
[self = std::move(self)](const ShutdownPromise::ResolveOrRejectValue&) {
104+
if (self->mIPCActive) {
105+
Unused << self->Send__delete__(self);
106+
}
107+
});
108+
}
109+
110+
} // namespace dom
111+
} // namespace mozilla
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
#ifndef mozilla_dom_remoteworkercontrollerchild_h__
8+
#define mozilla_dom_remoteworkercontrollerchild_h__
9+
10+
#include "nsISupportsImpl.h"
11+
12+
#include "RemoteWorkerController.h"
13+
#include "mozilla/RefPtr.h"
14+
#include "mozilla/dom/PRemoteWorkerControllerChild.h"
15+
16+
namespace mozilla {
17+
namespace dom {
18+
19+
class RemoteWorkerControllerChild final : public PRemoteWorkerControllerChild {
20+
friend class PRemoteWorkerControllerChild;
21+
22+
public:
23+
NS_INLINE_DECL_REFCOUNTING(RemoteWorkerControllerChild)
24+
25+
explicit RemoteWorkerControllerChild(RefPtr<RemoteWorkerObserver> aObserver);
26+
27+
void Initialize();
28+
29+
void RevokeObserver(RemoteWorkerObserver* aObserver);
30+
31+
void MaybeSendDelete();
32+
33+
private:
34+
~RemoteWorkerControllerChild() = default;
35+
36+
void ActorDestroy(ActorDestroyReason aReason) override;
37+
38+
mozilla::ipc::IPCResult RecvCreationFailed();
39+
40+
mozilla::ipc::IPCResult RecvCreationSucceeded();
41+
42+
mozilla::ipc::IPCResult RecvErrorReceived(const ErrorValue& aError);
43+
44+
mozilla::ipc::IPCResult RecvTerminated();
45+
46+
RefPtr<RemoteWorkerObserver> mObserver;
47+
48+
bool mIPCActive = true;
49+
};
50+
51+
} // namespace dom
52+
} // namespace mozilla
53+
54+
#endif // mozilla_dom_remoteworkercontrollerchild_h__
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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 "nsCOMPtr.h"
12+
#include "nsDebug.h"
13+
#include "nsError.h"
14+
#include "nsThreadUtils.h"
15+
16+
#include "mozilla/Assertions.h"
17+
#include "mozilla/Unused.h"
18+
#include "mozilla/ipc/BackgroundParent.h"
19+
20+
namespace mozilla {
21+
22+
using namespace ipc;
23+
24+
namespace dom {
25+
26+
RemoteWorkerControllerParent::RemoteWorkerControllerParent(
27+
const RemoteWorkerData& aRemoteWorkerData)
28+
: mRemoteWorkerController(RemoteWorkerController::Create(
29+
aRemoteWorkerData, this, 0 /* random process ID */)) {
30+
AssertIsInMainProcess();
31+
AssertIsOnBackgroundThread();
32+
MOZ_ASSERT(mRemoteWorkerController);
33+
}
34+
35+
RefPtr<RemoteWorkerParent> RemoteWorkerControllerParent::GetRemoteWorkerParent()
36+
const {
37+
AssertIsOnBackgroundThread();
38+
MOZ_ASSERT(mRemoteWorkerController);
39+
40+
return mRemoteWorkerController->mActor;
41+
}
42+
43+
RemoteWorkerControllerParent::~RemoteWorkerControllerParent() {
44+
AssertIsOnBackgroundThread();
45+
MOZ_ASSERT(!mIPCActive);
46+
MOZ_ASSERT(!mRemoteWorkerController);
47+
}
48+
49+
IPCResult RemoteWorkerControllerParent::RecvShutdown(
50+
ShutdownResolver&& aResolve) {
51+
AssertIsOnBackgroundThread();
52+
MOZ_ASSERT(mIPCActive);
53+
MOZ_ASSERT(mRemoteWorkerController);
54+
55+
mIPCActive = false;
56+
57+
mRemoteWorkerController->Shutdown();
58+
mRemoteWorkerController = nullptr;
59+
60+
aResolve(true);
61+
62+
return IPC_OK();
63+
}
64+
65+
IPCResult RemoteWorkerControllerParent::Recv__delete__() {
66+
AssertIsOnBackgroundThread();
67+
MOZ_ASSERT(!mIPCActive);
68+
MOZ_ASSERT(!mRemoteWorkerController);
69+
70+
return IPC_OK();
71+
}
72+
73+
void RemoteWorkerControllerParent::ActorDestroy(ActorDestroyReason aReason) {
74+
AssertIsOnBackgroundThread();
75+
76+
if (NS_WARN_IF(mIPCActive)) {
77+
mIPCActive = false;
78+
}
79+
80+
if (NS_WARN_IF(mRemoteWorkerController)) {
81+
mRemoteWorkerController->Shutdown();
82+
mRemoteWorkerController = nullptr;
83+
}
84+
}
85+
86+
void RemoteWorkerControllerParent::CreationFailed() {
87+
AssertIsOnBackgroundThread();
88+
89+
if (!mIPCActive) {
90+
return;
91+
}
92+
93+
Unused << SendCreationFailed();
94+
}
95+
96+
void RemoteWorkerControllerParent::CreationSucceeded() {
97+
AssertIsOnBackgroundThread();
98+
99+
if (!mIPCActive) {
100+
return;
101+
}
102+
103+
Unused << SendCreationSucceeded();
104+
}
105+
106+
void RemoteWorkerControllerParent::ErrorReceived(const ErrorValue& aValue) {
107+
AssertIsOnBackgroundThread();
108+
109+
if (!mIPCActive) {
110+
return;
111+
}
112+
113+
Unused << SendErrorReceived(aValue);
114+
}
115+
116+
void RemoteWorkerControllerParent::Terminated() {
117+
AssertIsOnBackgroundThread();
118+
119+
if (!mIPCActive) {
120+
return;
121+
}
122+
123+
Unused << SendTerminated();
124+
}
125+
126+
} // namespace dom
127+
} // namespace mozilla

0 commit comments

Comments
 (0)