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 "ContentProcessMessageManager.h"
9 #include "nsContentCID.h"
10 #include "mozilla/dom/ContentChild.h"
11 #include "mozilla/dom/MessageManagerBinding.h"
12 #include "mozilla/dom/ParentProcessMessageManager.h"
13 #include "mozilla/dom/ScriptSettings.h"
14 #include "mozilla/dom/ipc/SharedMap.h"
15 #include "mozilla/HoldDropJSObjects.h"
17 using namespace mozilla
;
18 using namespace mozilla::dom
;
20 bool ContentProcessMessageManager::sWasCreated
= false;
22 ContentProcessMessageManager::ContentProcessMessageManager(
23 nsFrameMessageManager
* aMessageManager
)
24 : MessageManagerGlobal(aMessageManager
), mInitialized(false) {
25 mozilla::HoldJSObjects(this);
28 ContentProcessMessageManager::~ContentProcessMessageManager() {
29 mAnonymousGlobalScopes
.Clear();
30 mozilla::DropJSObjects(this);
33 ContentProcessMessageManager
* ContentProcessMessageManager::Get() {
34 nsCOMPtr
<nsIMessageSender
> service
=
35 do_GetService(NS_CHILDPROCESSMESSAGEMANAGER_CONTRACTID
);
40 return static_cast<ContentProcessMessageManager
*>(service
.get());
43 already_AddRefed
<mozilla::dom::ipc::SharedMap
>
44 ContentProcessMessageManager::GetSharedData() {
45 if (ContentChild
* child
= ContentChild::GetSingleton()) {
46 return do_AddRef(child
->SharedData());
48 auto* ppmm
= nsFrameMessageManager::sParentProcessManager
;
49 return do_AddRef(ppmm
->SharedData()->GetReadOnly());
52 bool ContentProcessMessageManager::WasCreated() { return sWasCreated
; }
54 void ContentProcessMessageManager::MarkForCC() {
56 MessageManagerGlobal::MarkForCC();
59 NS_IMPL_CYCLE_COLLECTION_CLASS(ContentProcessMessageManager
)
61 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(ContentProcessMessageManager
)
62 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMessageManager
)
63 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
65 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(ContentProcessMessageManager
)
66 NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
67 tmp
->nsMessageManagerScriptExecutor::Trace(aCallbacks
, aClosure
);
68 NS_IMPL_CYCLE_COLLECTION_TRACE_END
70 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ContentProcessMessageManager
)
71 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
72 NS_IMPL_CYCLE_COLLECTION_UNLINK(mMessageManager
)
73 tmp
->nsMessageManagerScriptExecutor::Unlink();
74 NS_IMPL_CYCLE_COLLECTION_UNLINK_WEAK_REFERENCE
75 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
77 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ContentProcessMessageManager
)
78 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
79 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports
, nsIMessageSender
)
80 NS_INTERFACE_MAP_ENTRY(nsIMessageSender
)
81 NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference
)
84 NS_IMPL_CYCLE_COLLECTING_ADDREF(ContentProcessMessageManager
)
85 NS_IMPL_CYCLE_COLLECTING_RELEASE(ContentProcessMessageManager
)
87 bool ContentProcessMessageManager::Init() {
93 return nsMessageManagerScriptExecutor::Init();
96 JSObject
* ContentProcessMessageManager::WrapObject(
97 JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
) {
98 return ContentProcessMessageManager_Binding::Wrap(aCx
, this, aGivenProto
);
101 JSObject
* ContentProcessMessageManager::GetOrCreateWrapper() {
102 JS::RootedValue
val(RootingCx());
104 // Scope to run ~AutoJSAPI before working with a raw JSObject*.
108 if (!GetOrCreateDOMReflectorNoWrap(jsapi
.cx(), this, &val
)) {
112 MOZ_ASSERT(val
.isObject());
113 return &val
.toObject();
116 void ContentProcessMessageManager::LoadScript(const nsAString
& aURL
) {
118 JS::Rooted
<JSObject
*> messageManager(mozilla::dom::RootingCx(),
119 GetOrCreateWrapper());
120 LoadScriptInternal(messageManager
, aURL
, true);
123 void ContentProcessMessageManager::SetInitialProcessData(
124 JS::HandleValue aInitialData
) {
125 mMessageManager
->SetInitialProcessData(aInitialData
);