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 "mozilla/dom/ContentChild.h"
10 #include "mozilla/dom/MessageManagerBinding.h"
11 #include "mozilla/dom/ParentProcessMessageManager.h"
12 #include "mozilla/dom/ScriptSettings.h"
13 #include "mozilla/dom/ipc/SharedMap.h"
14 #include "mozilla/HoldDropJSObjects.h"
16 using namespace mozilla
;
17 using namespace mozilla::dom
;
19 bool ContentProcessMessageManager::sWasCreated
= false;
21 ContentProcessMessageManager::ContentProcessMessageManager(
22 nsFrameMessageManager
* aMessageManager
)
23 : MessageManagerGlobal(aMessageManager
), mInitialized(false) {
24 mozilla::HoldJSObjects(this);
27 ContentProcessMessageManager::~ContentProcessMessageManager() {
28 mozilla::DropJSObjects(this);
31 ContentProcessMessageManager
* ContentProcessMessageManager::Get() {
32 nsCOMPtr
<nsIMessageSender
> service
=
33 do_GetService("@mozilla.org/childprocessmessagemanager;1");
38 return static_cast<ContentProcessMessageManager
*>(service
.get());
41 already_AddRefed
<mozilla::dom::ipc::SharedMap
>
42 ContentProcessMessageManager::GetSharedData() {
43 if (ContentChild
* child
= ContentChild::GetSingleton()) {
44 return do_AddRef(child
->SharedData());
46 auto* ppmm
= nsFrameMessageManager::sParentProcessManager
;
47 return do_AddRef(ppmm
->SharedData()->GetReadOnly());
50 bool ContentProcessMessageManager::WasCreated() { return sWasCreated
; }
52 void ContentProcessMessageManager::MarkForCC() {
54 MessageManagerGlobal::MarkForCC();
57 NS_IMPL_CYCLE_COLLECTION_CLASS(ContentProcessMessageManager
)
59 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(ContentProcessMessageManager
)
60 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMessageManager
)
61 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
63 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(ContentProcessMessageManager
)
64 NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
65 tmp
->nsMessageManagerScriptExecutor::Trace(aCallbacks
, aClosure
);
66 NS_IMPL_CYCLE_COLLECTION_TRACE_END
68 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ContentProcessMessageManager
)
69 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
70 NS_IMPL_CYCLE_COLLECTION_UNLINK(mMessageManager
)
71 tmp
->nsMessageManagerScriptExecutor::Unlink();
72 NS_IMPL_CYCLE_COLLECTION_UNLINK_WEAK_REFERENCE
73 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
75 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ContentProcessMessageManager
)
76 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
77 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports
, nsIMessageSender
)
78 NS_INTERFACE_MAP_ENTRY(nsIMessageSender
)
79 NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference
)
82 NS_IMPL_CYCLE_COLLECTING_ADDREF(ContentProcessMessageManager
)
83 NS_IMPL_CYCLE_COLLECTING_RELEASE(ContentProcessMessageManager
)
85 bool ContentProcessMessageManager::Init() {
91 return nsMessageManagerScriptExecutor::Init();
94 JSObject
* ContentProcessMessageManager::WrapObject(
95 JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
) {
96 return ContentProcessMessageManager_Binding::Wrap(aCx
, this, aGivenProto
);
99 JSObject
* ContentProcessMessageManager::GetOrCreateWrapper() {
100 JS::Rooted
<JS::Value
> val(RootingCx());
102 // Scope to run ~AutoJSAPI before working with a raw JSObject*.
106 if (!GetOrCreateDOMReflectorNoWrap(jsapi
.cx(), this, &val
)) {
107 JS_ClearPendingException(jsapi
.cx());
111 MOZ_ASSERT(val
.isObject());
112 return &val
.toObject();
115 bool ContentProcessMessageManager::LoadScript(const nsAString
& aURL
) {
117 JSObject
* wrapper
= GetOrCreateWrapper();
119 JS::Rooted
<JSObject
*> messageManager(mozilla::dom::RootingCx(), wrapper
);
120 LoadScriptInternal(messageManager
, aURL
, true);
126 void ContentProcessMessageManager::SetInitialProcessData(
127 JS::Handle
<JS::Value
> aInitialData
) {
128 mMessageManager
->SetInitialProcessData(aInitialData
);