/src/mozilla-central/dom/serviceworkers/RemoteServiceWorkerRegistrationImpl.cpp
Line | Count | Source (jump to first uncovered line) |
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 "RemoteServiceWorkerRegistrationImpl.h" |
8 | | |
9 | | #include "ServiceWorkerRegistrationChild.h" |
10 | | |
11 | | namespace mozilla { |
12 | | namespace dom { |
13 | | |
14 | | using mozilla::ipc::IPCResult; |
15 | | using mozilla::ipc::ResponseRejectReason; |
16 | | |
17 | | RemoteServiceWorkerRegistrationImpl::~RemoteServiceWorkerRegistrationImpl() |
18 | 0 | { |
19 | 0 | MOZ_DIAGNOSTIC_ASSERT(!mOuter); |
20 | 0 | Shutdown(); |
21 | 0 | } |
22 | | |
23 | | void |
24 | | RemoteServiceWorkerRegistrationImpl::Shutdown() |
25 | 0 | { |
26 | 0 | if (mShutdown) { |
27 | 0 | return; |
28 | 0 | } |
29 | 0 | mShutdown = true; |
30 | 0 |
|
31 | 0 | if (mActor) { |
32 | 0 | mActor->RevokeOwner(this); |
33 | 0 | mActor->MaybeStartTeardown(); |
34 | 0 | mActor = nullptr; |
35 | 0 | } |
36 | 0 | } |
37 | | |
38 | | void |
39 | | RemoteServiceWorkerRegistrationImpl::SetServiceWorkerRegistration(ServiceWorkerRegistration* aReg) |
40 | 0 | { |
41 | 0 | NS_ASSERT_OWNINGTHREAD(RemoteServiceWorkerRegistrationImpl); |
42 | 0 | MOZ_DIAGNOSTIC_ASSERT(!mOuter); |
43 | 0 | MOZ_DIAGNOSTIC_ASSERT(aReg); |
44 | 0 | mOuter = aReg; |
45 | 0 | } |
46 | | |
47 | | void |
48 | | RemoteServiceWorkerRegistrationImpl::ClearServiceWorkerRegistration(ServiceWorkerRegistration* aReg) |
49 | 0 | { |
50 | 0 | NS_ASSERT_OWNINGTHREAD(RemoteServiceWorkerRegistrationImpl); |
51 | 0 | MOZ_DIAGNOSTIC_ASSERT(mOuter); |
52 | 0 | MOZ_DIAGNOSTIC_ASSERT(aReg == mOuter); |
53 | 0 | mOuter = nullptr; |
54 | 0 | } |
55 | | |
56 | | void |
57 | | RemoteServiceWorkerRegistrationImpl::Update(ServiceWorkerRegistrationCallback&& aSuccessCB, |
58 | | ServiceWorkerFailureCallback&& aFailureCB) |
59 | 0 | { |
60 | 0 | if (!mActor) { |
61 | 0 | aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR)); |
62 | 0 | return; |
63 | 0 | } |
64 | 0 | |
65 | 0 | mActor->SendUpdate( |
66 | 0 | [successCB = std::move(aSuccessCB), aFailureCB] |
67 | 0 | (const IPCServiceWorkerRegistrationDescriptorOrCopyableErrorResult& aResult) { |
68 | 0 | if (aResult.type() == IPCServiceWorkerRegistrationDescriptorOrCopyableErrorResult::TCopyableErrorResult) { |
69 | 0 | // application layer error |
70 | 0 | auto& rv = aResult.get_CopyableErrorResult(); |
71 | 0 | MOZ_DIAGNOSTIC_ASSERT(rv.Failed()); |
72 | 0 | aFailureCB(CopyableErrorResult(rv)); |
73 | 0 | return; |
74 | 0 | } |
75 | 0 | // success |
76 | 0 | auto& ipcDesc = aResult.get_IPCServiceWorkerRegistrationDescriptor(); |
77 | 0 | successCB(ServiceWorkerRegistrationDescriptor(ipcDesc)); |
78 | 0 | }, [aFailureCB] (ResponseRejectReason aReason) { |
79 | 0 | // IPC layer error |
80 | 0 | aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR)); |
81 | 0 | }); |
82 | 0 | } |
83 | | |
84 | | void |
85 | | RemoteServiceWorkerRegistrationImpl::Unregister(ServiceWorkerBoolCallback&& aSuccessCB, |
86 | | ServiceWorkerFailureCallback&& aFailureCB) |
87 | 0 | { |
88 | 0 | if (!mActor) { |
89 | 0 | aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR)); |
90 | 0 | return; |
91 | 0 | } |
92 | 0 | |
93 | 0 | mActor->SendUnregister( |
94 | 0 | [successCB = std::move(aSuccessCB), aFailureCB] |
95 | 0 | (Tuple<bool, CopyableErrorResult>&& aResult) { |
96 | 0 | if (Get<1>(aResult).Failed()) { |
97 | 0 | // application layer error |
98 | 0 | aFailureCB(Get<1>(aResult)); |
99 | 0 | return; |
100 | 0 | } |
101 | 0 | // success |
102 | 0 | successCB(Get<0>(aResult)); |
103 | 0 | }, [aFailureCB] (ResponseRejectReason aReason) { |
104 | 0 | // IPC layer error |
105 | 0 | aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR)); |
106 | 0 | }); |
107 | 0 | } |
108 | | |
109 | | RemoteServiceWorkerRegistrationImpl::RemoteServiceWorkerRegistrationImpl(const ServiceWorkerRegistrationDescriptor& aDescriptor) |
110 | | : mActor(nullptr) |
111 | | , mOuter(nullptr) |
112 | | , mShutdown(false) |
113 | 0 | { |
114 | 0 | PBackgroundChild* parentActor = BackgroundChild::GetOrCreateForCurrentThread(); |
115 | 0 | if (NS_WARN_IF(!parentActor)) { |
116 | 0 | Shutdown(); |
117 | 0 | return; |
118 | 0 | } |
119 | 0 | |
120 | 0 | RefPtr<WorkerHolderToken> workerHolderToken; |
121 | 0 | if (!NS_IsMainThread()) { |
122 | 0 | WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate(); |
123 | 0 | MOZ_DIAGNOSTIC_ASSERT(workerPrivate); |
124 | 0 |
|
125 | 0 | workerHolderToken = |
126 | 0 | WorkerHolderToken::Create(workerPrivate, Canceling, |
127 | 0 | WorkerHolderToken::AllowIdleShutdownStart); |
128 | 0 |
|
129 | 0 | if (NS_WARN_IF(!workerHolderToken)) { |
130 | 0 | Shutdown(); |
131 | 0 | return; |
132 | 0 | } |
133 | 0 | } |
134 | 0 | |
135 | 0 | ServiceWorkerRegistrationChild* actor = |
136 | 0 | new ServiceWorkerRegistrationChild(workerHolderToken); |
137 | 0 | PServiceWorkerRegistrationChild* sentActor = |
138 | 0 | parentActor->SendPServiceWorkerRegistrationConstructor(actor, aDescriptor.ToIPC()); |
139 | 0 | if (NS_WARN_IF(!sentActor)) { |
140 | 0 | Shutdown(); |
141 | 0 | return; |
142 | 0 | } |
143 | 0 | MOZ_DIAGNOSTIC_ASSERT(sentActor == actor); |
144 | 0 |
|
145 | 0 | mActor = actor; |
146 | 0 | mActor->SetOwner(this); |
147 | 0 | } |
148 | | |
149 | | void |
150 | | RemoteServiceWorkerRegistrationImpl::RevokeActor(ServiceWorkerRegistrationChild* aActor) |
151 | 0 | { |
152 | 0 | MOZ_DIAGNOSTIC_ASSERT(mActor); |
153 | 0 | MOZ_DIAGNOSTIC_ASSERT(mActor == aActor); |
154 | 0 | mActor->RevokeOwner(this); |
155 | 0 | mActor = nullptr; |
156 | 0 |
|
157 | 0 | mShutdown = true; |
158 | 0 |
|
159 | 0 | if (mOuter) { |
160 | 0 | mOuter->RegistrationRemoved(); |
161 | 0 | } |
162 | 0 | } |
163 | | |
164 | | void |
165 | | RemoteServiceWorkerRegistrationImpl::UpdateState(const ServiceWorkerRegistrationDescriptor& aDescriptor) |
166 | 0 | { |
167 | 0 | if (mOuter) { |
168 | 0 | mOuter->UpdateState(aDescriptor); |
169 | 0 | } |
170 | 0 | } |
171 | | |
172 | | } // namespace dom |
173 | | } // namespace mozilla |