Backed out 4 changesets (bug 1825722) for causing reftest failures CLOSED TREE
[gecko.git] / dom / base / ContentProcessMessageManager.cpp
blob7fe9f4c2c70b25684d98f4c75163f36c5a09a922
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 mozilla::DropJSObjects(this);
32 ContentProcessMessageManager* ContentProcessMessageManager::Get() {
33 nsCOMPtr<nsIMessageSender> service =
34 do_GetService(NS_CHILDPROCESSMESSAGEMANAGER_CONTRACTID);
35 if (!service) {
36 return nullptr;
38 sWasCreated = true;
39 return static_cast<ContentProcessMessageManager*>(service.get());
42 already_AddRefed<mozilla::dom::ipc::SharedMap>
43 ContentProcessMessageManager::GetSharedData() {
44 if (ContentChild* child = ContentChild::GetSingleton()) {
45 return do_AddRef(child->SharedData());
47 auto* ppmm = nsFrameMessageManager::sParentProcessManager;
48 return do_AddRef(ppmm->SharedData()->GetReadOnly());
51 bool ContentProcessMessageManager::WasCreated() { return sWasCreated; }
53 void ContentProcessMessageManager::MarkForCC() {
54 MarkScopesForCC();
55 MessageManagerGlobal::MarkForCC();
58 NS_IMPL_CYCLE_COLLECTION_CLASS(ContentProcessMessageManager)
60 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(ContentProcessMessageManager)
61 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMessageManager)
62 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
64 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(ContentProcessMessageManager)
65 NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
66 tmp->nsMessageManagerScriptExecutor::Trace(aCallbacks, aClosure);
67 NS_IMPL_CYCLE_COLLECTION_TRACE_END
69 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ContentProcessMessageManager)
70 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
71 NS_IMPL_CYCLE_COLLECTION_UNLINK(mMessageManager)
72 tmp->nsMessageManagerScriptExecutor::Unlink();
73 NS_IMPL_CYCLE_COLLECTION_UNLINK_WEAK_REFERENCE
74 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
76 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ContentProcessMessageManager)
77 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
78 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIMessageSender)
79 NS_INTERFACE_MAP_ENTRY(nsIMessageSender)
80 NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
81 NS_INTERFACE_MAP_END
83 NS_IMPL_CYCLE_COLLECTING_ADDREF(ContentProcessMessageManager)
84 NS_IMPL_CYCLE_COLLECTING_RELEASE(ContentProcessMessageManager)
86 bool ContentProcessMessageManager::Init() {
87 if (mInitialized) {
88 return true;
90 mInitialized = true;
92 return nsMessageManagerScriptExecutor::Init();
95 JSObject* ContentProcessMessageManager::WrapObject(
96 JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
97 return ContentProcessMessageManager_Binding::Wrap(aCx, this, aGivenProto);
100 JSObject* ContentProcessMessageManager::GetOrCreateWrapper() {
101 JS::Rooted<JS::Value> val(RootingCx());
103 // Scope to run ~AutoJSAPI before working with a raw JSObject*.
104 AutoJSAPI jsapi;
105 jsapi.Init();
107 if (!GetOrCreateDOMReflectorNoWrap(jsapi.cx(), this, &val)) {
108 return nullptr;
111 MOZ_ASSERT(val.isObject());
112 return &val.toObject();
115 void ContentProcessMessageManager::LoadScript(const nsAString& aURL) {
116 Init();
117 JS::Rooted<JSObject*> messageManager(mozilla::dom::RootingCx(),
118 GetOrCreateWrapper());
119 LoadScriptInternal(messageManager, aURL, true);
122 void ContentProcessMessageManager::SetInitialProcessData(
123 JS::Handle<JS::Value> aInitialData) {
124 mMessageManager->SetInitialProcessData(aInitialData);