1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "SocketProcessBackgroundChild.h"
7 #include "SocketProcessLogging.h"
9 #include "mozilla/ipc/Endpoint.h"
10 #include "nsThreadUtils.h"
12 namespace mozilla::net
{
14 StaticMutex
SocketProcessBackgroundChild::sMutex
;
15 StaticRefPtr
<SocketProcessBackgroundChild
>
16 SocketProcessBackgroundChild::sInstance
;
17 StaticRefPtr
<nsISerialEventTarget
> SocketProcessBackgroundChild::sTaskQueue
;
20 RefPtr
<SocketProcessBackgroundChild
>
21 SocketProcessBackgroundChild::GetSingleton() {
22 StaticMutexAutoLock
lock(sMutex
);
27 void SocketProcessBackgroundChild::Create(
28 ipc::Endpoint
<PSocketProcessBackgroundChild
>&& aEndpoint
) {
29 if (NS_WARN_IF(!aEndpoint
.IsValid())) {
30 MOZ_ASSERT_UNREACHABLE(
31 "Can't create SocketProcessBackgroundChild with invalid endpoint");
35 nsCOMPtr
<nsISerialEventTarget
> transportQueue
;
36 if (NS_WARN_IF(NS_FAILED(NS_CreateBackgroundTaskQueue(
37 "SocketBackgroundChildQueue", getter_AddRefs(transportQueue
))))) {
41 RefPtr
<SocketProcessBackgroundChild
> actor
=
42 new SocketProcessBackgroundChild();
44 transportQueue
->Dispatch(NS_NewRunnableFunction(
45 "BindSocketBackgroundChild",
46 [endpoint
= std::move(aEndpoint
), actor
]() mutable {
47 // We checked endpoint validity before the dispatch, so this cannot
49 MOZ_ALWAYS_TRUE(endpoint
.Bind(actor
));
52 // Immediately store the actor and queue into the global.
53 // Any messages dispatched to the queue will arrive after it has been bound.
54 LOG(("SocketProcessBackgroundChild::Create"));
55 StaticMutexAutoLock
lock(sMutex
);
56 MOZ_ASSERT(!sInstance
&& !sTaskQueue
,
57 "Cannot initialize SocketProcessBackgroundChild twice!");
59 sTaskQueue
= transportQueue
;
63 void SocketProcessBackgroundChild::Shutdown() {
64 nsCOMPtr
<nsISerialEventTarget
> taskQueue
= TaskQueue();
70 NS_NewRunnableFunction("SocketProcessBackgroundChild::Shutdown", []() {
71 LOG(("SocketProcessBackgroundChild::Shutdown"));
72 StaticMutexAutoLock
lock(sMutex
);
80 already_AddRefed
<nsISerialEventTarget
>
81 SocketProcessBackgroundChild::TaskQueue() {
82 StaticMutexAutoLock
lock(sMutex
);
83 return do_AddRef(sTaskQueue
);
87 nsresult
SocketProcessBackgroundChild::WithActor(
89 MoveOnlyFunction
<void(SocketProcessBackgroundChild
*)> aCallback
) {
90 nsCOMPtr
<nsISerialEventTarget
> taskQueue
= TaskQueue();
92 return NS_ERROR_NOT_AVAILABLE
;
95 return taskQueue
->Dispatch(NS_NewRunnableFunction(
96 aName
, [callback
= std::move(aCallback
)]() mutable {
97 RefPtr
<SocketProcessBackgroundChild
> actor
=
98 SocketProcessBackgroundChild::GetSingleton();
105 SocketProcessBackgroundChild::SocketProcessBackgroundChild() {
106 LOG(("SocketProcessBackgroundChild ctor"));
109 SocketProcessBackgroundChild::~SocketProcessBackgroundChild() {
110 LOG(("SocketProcessBackgroundChild dtor"));
113 } // namespace mozilla::net