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/. */
7 #include "mozilla/dom/JSOracleParent.h"
8 #include "mozilla/ClearOnShutdown.h"
9 #include "mozilla/RefPtr.h"
10 #include "mozilla/dom/PJSOracle.h"
11 #include "mozilla/ipc/Endpoint.h"
12 #include "mozilla/ipc/UtilityProcessManager.h"
14 using namespace mozilla
;
15 using namespace mozilla::dom
;
17 static StaticRefPtr
<JSOracleParent
> sOracleSingleton
;
20 void JSOracleParent::WithJSOracle(
21 const std::function
<void(JSOracleParent
* aParent
)>& aCallback
) {
22 GetSingleton()->StartJSOracle()->Then(
23 GetMainThreadSerialEventTarget(), __func__
,
24 [aCallback
](const JSOraclePromise::ResolveOrRejectValue
& aResult
) {
25 aCallback(aResult
.IsReject() || !aResult
.ResolveValue()
31 void JSOracleParent::ActorDestroy(ActorDestroyReason aReason
) {
32 // Given an actor can only be bound to one process,
33 // if the utility process crashes and we create a new one,
34 // we can't reuse the same JSOracleParent instance for it,
35 // so we always create a new JSOracleParent to replace
37 if (aReason
== ActorDestroyReason::AbnormalShutdown
) {
38 sOracleSingleton
= new JSOracleParent();
43 JSOracleParent
* JSOracleParent::GetSingleton() {
44 if (!sOracleSingleton
) {
45 sOracleSingleton
= new JSOracleParent();
46 ClearOnShutdown(&sOracleSingleton
);
49 return sOracleSingleton
;
52 RefPtr
<JSOracleParent::JSOraclePromise
> JSOracleParent::StartJSOracle() {
53 using namespace mozilla::ipc
;
54 RefPtr
<JSOracleParent
> parent
= JSOracleParent::GetSingleton();
55 return UtilityProcessManager::GetSingleton()->StartJSOracle(parent
);
58 nsresult
JSOracleParent::BindToUtilityProcess(
59 const RefPtr
<mozilla::ipc::UtilityProcessParent
>& aUtilityParent
) {
60 Endpoint
<PJSOracleParent
> parentEnd
;
61 Endpoint
<PJSOracleChild
> childEnd
;
62 MOZ_ASSERT(aUtilityParent
);
63 if (NS_FAILED(PJSOracle::CreateEndpoints(base::GetCurrentProcId(),
64 aUtilityParent
->OtherPid(),
65 &parentEnd
, &childEnd
))) {
66 return NS_ERROR_FAILURE
;
69 if (!aUtilityParent
->SendStartJSOracleService(std::move(childEnd
))) {
70 return NS_ERROR_FAILURE
;
73 Bind(std::move(parentEnd
));
78 void JSOracleParent::Bind(Endpoint
<PJSOracleParent
>&& aEndpoint
) {
79 DebugOnly
<bool> ok
= aEndpoint
.Bind(this);