Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / base / ContentProcessMessageManager.cpp
blob972334178228eda6ffceefe8ea296649c67be749
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");
34 if (!service) {
35 return nullptr;
37 sWasCreated = true;
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() {
53 MarkScopesForCC();
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)
80 NS_INTERFACE_MAP_END
82 NS_IMPL_CYCLE_COLLECTING_ADDREF(ContentProcessMessageManager)
83 NS_IMPL_CYCLE_COLLECTING_RELEASE(ContentProcessMessageManager)
85 bool ContentProcessMessageManager::Init() {
86 if (mInitialized) {
87 return true;
89 mInitialized = true;
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*.
103 AutoJSAPI jsapi;
104 jsapi.Init();
106 if (!GetOrCreateDOMReflectorNoWrap(jsapi.cx(), this, &val)) {
107 JS_ClearPendingException(jsapi.cx());
108 return nullptr;
111 MOZ_ASSERT(val.isObject());
112 return &val.toObject();
115 bool ContentProcessMessageManager::LoadScript(const nsAString& aURL) {
116 Init();
117 JSObject* wrapper = GetOrCreateWrapper();
118 if (wrapper) {
119 JS::Rooted<JSObject*> messageManager(mozilla::dom::RootingCx(), wrapper);
120 LoadScriptInternal(messageManager, aURL, true);
121 return true;
123 return false;
126 void ContentProcessMessageManager::SetInitialProcessData(
127 JS::Handle<JS::Value> aInitialData) {
128 mMessageManager->SetInitialProcessData(aInitialData);