Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set sw=2 ts=8 et tw=80 : */
3 :
4 : /* This Source Code Form is subject to the terms of the Mozilla Public
5 : * License, v. 2.0. If a copy of the MPL was not distributed with this
6 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 :
8 : // HttpLog.h should generally be included first
9 : #include "HttpLog.h"
10 :
11 : #include "HttpBackgroundChannelParent.h"
12 :
13 : #include "HttpChannelParent.h"
14 : #include "mozilla/ipc/BackgroundParent.h"
15 : #include "mozilla/IntegerPrintfMacros.h"
16 : #include "mozilla/Unused.h"
17 : #include "nsIBackgroundChannelRegistrar.h"
18 : #include "nsNetCID.h"
19 : #include "nsQueryObject.h"
20 : #include "nsThreadUtils.h"
21 :
22 : using mozilla::dom::ContentParent;
23 : using mozilla::ipc::AssertIsInMainProcess;
24 : using mozilla::ipc::AssertIsOnBackgroundThread;
25 : using mozilla::ipc::BackgroundParent;
26 : using mozilla::ipc::IPCResult;
27 : using mozilla::ipc::IsOnBackgroundThread;
28 :
29 : namespace mozilla {
30 : namespace net {
31 :
32 : /*
33 : * Helper class for continuing the AsyncOpen procedure on main thread.
34 : */
35 9 : class ContinueAsyncOpenRunnable final : public Runnable
36 : {
37 : public:
38 3 : ContinueAsyncOpenRunnable(HttpBackgroundChannelParent* aActor,
39 : const uint64_t& aChannelId)
40 3 : : Runnable("net::ContinueAsyncOpenRunnable")
41 : , mActor(aActor)
42 3 : , mChannelId(aChannelId)
43 : {
44 3 : AssertIsInMainProcess();
45 3 : AssertIsOnBackgroundThread();
46 3 : MOZ_ASSERT(mActor);
47 3 : }
48 :
49 3 : NS_IMETHOD Run() override
50 : {
51 3 : LOG(("HttpBackgroundChannelParent::ContinueAsyncOpen [this=%p channelId=%"
52 : PRIu64 "]\n", mActor.get(), mChannelId));
53 3 : AssertIsInMainProcess();
54 3 : MOZ_ASSERT(NS_IsMainThread());
55 :
56 : nsCOMPtr<nsIBackgroundChannelRegistrar> registrar =
57 6 : do_GetService(NS_BACKGROUNDCHANNELREGISTRAR_CONTRACTID);
58 3 : MOZ_ASSERT(registrar);
59 :
60 3 : registrar->LinkBackgroundChannel(mChannelId, mActor);
61 6 : return NS_OK;
62 : }
63 :
64 : private:
65 : RefPtr<HttpBackgroundChannelParent> mActor;
66 : const uint64_t mChannelId;
67 : };
68 :
69 3 : HttpBackgroundChannelParent::HttpBackgroundChannelParent()
70 : : mIPCOpened(true)
71 3 : , mBackgroundThread(NS_GetCurrentThread())
72 : {
73 3 : AssertIsInMainProcess();
74 3 : AssertIsOnBackgroundThread();
75 3 : }
76 :
77 9 : HttpBackgroundChannelParent::~HttpBackgroundChannelParent()
78 : {
79 3 : MOZ_ASSERT(NS_IsMainThread() || IsOnBackgroundThread());
80 3 : MOZ_ASSERT(!mIPCOpened);
81 9 : }
82 :
83 : nsresult
84 3 : HttpBackgroundChannelParent::Init(const uint64_t& aChannelId)
85 : {
86 3 : LOG(("HttpBackgroundChannelParent::Init [this=%p channelId=%" PRIu64 "]\n",
87 : this, aChannelId));
88 3 : AssertIsInMainProcess();
89 3 : AssertIsOnBackgroundThread();
90 :
91 : RefPtr<ContinueAsyncOpenRunnable> runnable =
92 6 : new ContinueAsyncOpenRunnable(this, aChannelId);
93 :
94 6 : return NS_DispatchToMainThread(runnable);
95 : }
96 :
97 : void
98 3 : HttpBackgroundChannelParent::LinkToChannel(HttpChannelParent* aChannelParent)
99 : {
100 3 : LOG(("HttpBackgroundChannelParent::LinkToChannel [this=%p channel=%p]\n",
101 : this, aChannelParent));
102 3 : AssertIsInMainProcess();
103 3 : MOZ_ASSERT(NS_IsMainThread());
104 :
105 3 : if (!mIPCOpened) {
106 0 : return;
107 : }
108 :
109 3 : mChannelParent = aChannelParent;
110 : }
111 :
112 : void
113 3 : HttpBackgroundChannelParent::OnChannelClosed()
114 : {
115 3 : LOG(("HttpBackgroundChannelParent::OnChannelClosed [this=%p]\n", this));
116 3 : AssertIsInMainProcess();
117 3 : MOZ_ASSERT(NS_IsMainThread());
118 :
119 3 : if (!mIPCOpened) {
120 0 : return;
121 : }
122 :
123 : nsresult rv;
124 :
125 6 : RefPtr<HttpBackgroundChannelParent> self = this;
126 9 : rv = mBackgroundThread->Dispatch(
127 6 : NS_NewRunnableFunction(
128 : "net::HttpBackgroundChannelParent::OnChannelClosed",
129 12 : [self]() {
130 3 : LOG(("HttpBackgroundChannelParent::DeleteRunnable [this=%p]\n",
131 : self.get()));
132 3 : AssertIsOnBackgroundThread();
133 :
134 3 : if (!self->mIPCOpened.compareExchange(true, false)) {
135 0 : return;
136 : }
137 :
138 3 : Unused << self->Send__delete__(self);
139 : }),
140 6 : NS_DISPATCH_NORMAL);
141 :
142 3 : Unused << NS_WARN_IF(NS_FAILED(rv));
143 : }
144 :
145 : bool
146 6 : HttpBackgroundChannelParent::OnStartRequestSent()
147 : {
148 6 : LOG(("HttpBackgroundChannelParent::OnStartRequestSent [this=%p]\n", this));
149 6 : AssertIsInMainProcess();
150 :
151 6 : if (NS_WARN_IF(!mIPCOpened)) {
152 0 : return false;
153 : }
154 :
155 6 : if (!IsOnBackgroundThread()) {
156 9 : nsresult rv = mBackgroundThread->Dispatch(
157 6 : NewRunnableMethod("net::HttpBackgroundChannelParent::OnStartRequestSent",
158 : this,
159 : &HttpBackgroundChannelParent::OnStartRequestSent),
160 6 : NS_DISPATCH_NORMAL);
161 :
162 3 : MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
163 :
164 3 : return NS_SUCCEEDED(rv);
165 : }
166 :
167 3 : return SendOnStartRequestSent();
168 : }
169 :
170 : bool
171 6 : HttpBackgroundChannelParent::OnTransportAndData(
172 : const nsresult& aChannelStatus,
173 : const nsresult& aTransportStatus,
174 : const uint64_t& aOffset,
175 : const uint32_t& aCount,
176 : const nsCString& aData)
177 : {
178 6 : LOG(("HttpBackgroundChannelParent::OnTransportAndData [this=%p]\n", this));
179 6 : AssertIsInMainProcess();
180 :
181 6 : if (NS_WARN_IF(!mIPCOpened)) {
182 0 : return false;
183 : }
184 :
185 6 : if (!IsOnBackgroundThread()) {
186 9 : nsresult rv = mBackgroundThread->Dispatch(
187 : NewRunnableMethod<const nsresult,
188 : const nsresult,
189 : const uint64_t,
190 : const uint32_t,
191 6 : const nsCString>(
192 : "net::HttpBackgroundChannelParent::OnTransportAndData",
193 : this,
194 : &HttpBackgroundChannelParent::OnTransportAndData,
195 : aChannelStatus,
196 : aTransportStatus,
197 : aOffset,
198 : aCount,
199 : aData),
200 6 : NS_DISPATCH_NORMAL);
201 :
202 3 : MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
203 :
204 3 : return NS_SUCCEEDED(rv);
205 : }
206 :
207 3 : return SendOnTransportAndData(aChannelStatus, aTransportStatus,
208 3 : aOffset, aCount, aData);
209 : }
210 :
211 : bool
212 6 : HttpBackgroundChannelParent::OnStopRequest(const nsresult& aChannelStatus,
213 : const ResourceTimingStruct& aTiming)
214 : {
215 6 : LOG(("HttpBackgroundChannelParent::OnStopRequest [this=%p "
216 : "status=%" PRIx32 "]\n", this, static_cast<uint32_t>(aChannelStatus)));
217 6 : AssertIsInMainProcess();
218 :
219 6 : if (NS_WARN_IF(!mIPCOpened)) {
220 0 : return false;
221 : }
222 :
223 6 : if (!IsOnBackgroundThread()) {
224 9 : nsresult rv = mBackgroundThread->Dispatch(
225 6 : NewRunnableMethod<const nsresult, const ResourceTimingStruct>(
226 : "net::HttpBackgroundChannelParent::OnStopRequest",
227 : this,
228 : &HttpBackgroundChannelParent::OnStopRequest,
229 : aChannelStatus,
230 : aTiming),
231 6 : NS_DISPATCH_NORMAL);
232 :
233 3 : MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
234 :
235 3 : return NS_SUCCEEDED(rv);
236 : }
237 :
238 3 : return SendOnStopRequest(aChannelStatus, aTiming);
239 : }
240 :
241 : bool
242 0 : HttpBackgroundChannelParent::OnProgress(const int64_t& aProgress,
243 : const int64_t& aProgressMax)
244 : {
245 0 : LOG(("HttpBackgroundChannelParent::OnProgress [this=%p progress=%" PRId64
246 : " max=%" PRId64 "]\n", this, aProgress, aProgressMax));
247 0 : AssertIsInMainProcess();
248 :
249 0 : if (NS_WARN_IF(!mIPCOpened)) {
250 0 : return false;
251 : }
252 :
253 0 : if (!IsOnBackgroundThread()) {
254 0 : nsresult rv = mBackgroundThread->Dispatch(
255 0 : NewRunnableMethod<const int64_t, const int64_t>(
256 : "net::HttpBackgroundChannelParent::OnProgress",
257 : this,
258 : &HttpBackgroundChannelParent::OnProgress,
259 : aProgress,
260 : aProgressMax),
261 0 : NS_DISPATCH_NORMAL);
262 :
263 0 : MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
264 :
265 0 : return NS_SUCCEEDED(rv);
266 : }
267 :
268 0 : return SendOnProgress(aProgress, aProgressMax);
269 : }
270 :
271 : bool
272 10 : HttpBackgroundChannelParent::OnStatus(const nsresult& aStatus)
273 : {
274 10 : LOG(("HttpBackgroundChannelParent::OnStatus [this=%p stauts=%" PRIx32
275 : "]\n", this, static_cast<uint32_t>(aStatus)));
276 10 : AssertIsInMainProcess();
277 :
278 10 : if (NS_WARN_IF(!mIPCOpened)) {
279 0 : return false;
280 : }
281 :
282 10 : if (!IsOnBackgroundThread()) {
283 15 : nsresult rv = mBackgroundThread->Dispatch(
284 10 : NewRunnableMethod<const nsresult>(
285 : "net::HttpBackgroundChannelParent::OnStatus",
286 : this,
287 : &HttpBackgroundChannelParent::OnStatus,
288 : aStatus),
289 10 : NS_DISPATCH_NORMAL);
290 :
291 5 : MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
292 :
293 5 : return NS_SUCCEEDED(rv);
294 : }
295 :
296 5 : return SendOnStatus(aStatus);
297 : }
298 :
299 : bool
300 0 : HttpBackgroundChannelParent::OnDiversion()
301 : {
302 0 : LOG(("HttpBackgroundChannelParent::OnDiversion [this=%p]\n", this));
303 0 : AssertIsInMainProcess();
304 :
305 0 : if (NS_WARN_IF(!mIPCOpened)) {
306 0 : return false;
307 : }
308 :
309 0 : if (!IsOnBackgroundThread()) {
310 0 : nsresult rv = mBackgroundThread->Dispatch(
311 0 : NewRunnableMethod("net::HttpBackgroundChannelParent::OnDiversion",
312 : this,
313 : &HttpBackgroundChannelParent::OnDiversion),
314 0 : NS_DISPATCH_NORMAL);
315 :
316 0 : MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
317 :
318 0 : return NS_SUCCEEDED(rv);
319 : }
320 :
321 0 : if (!SendFlushedForDiversion()) {
322 0 : return false;
323 : }
324 :
325 : // The listener chain should now be setup; tell HttpChannelChild to divert
326 : // the OnDataAvailables and OnStopRequest to associated HttpChannelParent.
327 0 : if (!SendDivertMessages()) {
328 0 : return false;
329 : }
330 :
331 0 : return true;
332 : }
333 :
334 : bool
335 0 : HttpBackgroundChannelParent::OnNotifyTrackingProtectionDisabled()
336 : {
337 0 : LOG(("HttpBackgroundChannelParent::OnNotifyTrackingProtectionDisabled [this=%p]\n", this));
338 0 : AssertIsInMainProcess();
339 :
340 0 : if (NS_WARN_IF(!mIPCOpened)) {
341 0 : return false;
342 : }
343 :
344 0 : if (!IsOnBackgroundThread()) {
345 0 : nsresult rv = mBackgroundThread->Dispatch(
346 0 : NewRunnableMethod(
347 : "net::HttpBackgroundChannelParent::OnNotifyTrackingProtectionDisabled",
348 : this,
349 : &HttpBackgroundChannelParent::OnNotifyTrackingProtectionDisabled),
350 0 : NS_DISPATCH_NORMAL);
351 :
352 0 : MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
353 :
354 0 : return NS_SUCCEEDED(rv);
355 : }
356 :
357 0 : return SendNotifyTrackingProtectionDisabled();
358 : }
359 :
360 : bool
361 0 : HttpBackgroundChannelParent::OnNotifyTrackingResource()
362 : {
363 0 : LOG(("HttpBackgroundChannelParent::OnNotifyTrackingResource [this=%p]\n", this));
364 0 : AssertIsInMainProcess();
365 :
366 0 : if (NS_WARN_IF(!mIPCOpened)) {
367 0 : return false;
368 : }
369 :
370 0 : if (!IsOnBackgroundThread()) {
371 0 : nsresult rv = mBackgroundThread->Dispatch(
372 0 : NewRunnableMethod(
373 : "net::HttpBackgroundChannelParent::OnNotifyTrackingResource",
374 : this,
375 : &HttpBackgroundChannelParent::OnNotifyTrackingResource),
376 0 : NS_DISPATCH_NORMAL);
377 :
378 0 : MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
379 :
380 0 : return NS_SUCCEEDED(rv);
381 : }
382 :
383 0 : return SendNotifyTrackingResource();
384 : }
385 :
386 : bool
387 0 : HttpBackgroundChannelParent::OnSetClassifierMatchedInfo(
388 : const nsACString& aList,
389 : const nsACString& aProvider,
390 : const nsACString& aPrefix)
391 : {
392 0 : LOG(("HttpBackgroundChannelParent::OnSetClassifierMatchedInfo [this=%p]\n", this));
393 0 : AssertIsInMainProcess();
394 :
395 0 : if (NS_WARN_IF(!mIPCOpened)) {
396 0 : return false;
397 : }
398 :
399 0 : if (!IsOnBackgroundThread()) {
400 0 : nsresult rv = mBackgroundThread->Dispatch(
401 0 : NewRunnableMethod<const nsCString, const nsCString, const nsCString>(
402 : "net::HttpBackgroundChannelParent::OnSetClassifierMatchedInfo",
403 : this,
404 : &HttpBackgroundChannelParent::OnSetClassifierMatchedInfo,
405 : aList,
406 : aProvider,
407 : aPrefix),
408 0 : NS_DISPATCH_NORMAL);
409 :
410 0 : MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
411 :
412 0 : return NS_SUCCEEDED(rv);
413 : }
414 :
415 0 : ClassifierInfo info;
416 0 : info.list() = aList;
417 0 : info.prefix() = aPrefix;
418 0 : info.provider() = aProvider;
419 :
420 0 : return SendSetClassifierMatchedInfo(info);
421 : }
422 :
423 : void
424 3 : HttpBackgroundChannelParent::ActorDestroy(ActorDestroyReason aWhy)
425 : {
426 3 : LOG(("HttpBackgroundChannelParent::ActorDestroy [this=%p]\n", this));
427 3 : AssertIsInMainProcess();
428 3 : AssertIsOnBackgroundThread();
429 :
430 3 : mIPCOpened = false;
431 :
432 6 : RefPtr<HttpBackgroundChannelParent> self = this;
433 9 : DebugOnly<nsresult> rv = NS_DispatchToMainThread(NS_NewRunnableFunction(
434 12 : "net::HttpBackgroundChannelParent::ActorDestroy", [self]() {
435 3 : MOZ_ASSERT(NS_IsMainThread());
436 :
437 : RefPtr<HttpChannelParent> channelParent =
438 6 : self->mChannelParent.forget();
439 :
440 3 : if (channelParent) {
441 3 : channelParent->OnBackgroundParentDestroyed();
442 : }
443 12 : }));
444 3 : MOZ_ASSERT(NS_SUCCEEDED(rv));
445 3 : }
446 :
447 : } // namespace net
448 : } // namespace mozilla
|