ContentProcessMessageManager.cpp (4265B)
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/. */ 6 7 #include "ContentProcessMessageManager.h" 8 9 #include "mozilla/HoldDropJSObjects.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 16 using namespace mozilla; 17 using namespace mozilla::dom; 18 19 bool ContentProcessMessageManager::sWasCreated = false; 20 21 ContentProcessMessageManager::ContentProcessMessageManager( 22 nsFrameMessageManager* aMessageManager) 23 : MessageManagerGlobal(aMessageManager), mInitialized(false) { 24 mozilla::HoldJSObjects(this); 25 } 26 27 ContentProcessMessageManager::~ContentProcessMessageManager() { 28 mozilla::DropJSObjects(this); 29 } 30 31 ContentProcessMessageManager* ContentProcessMessageManager::Get() { 32 nsCOMPtr<nsIMessageSender> service = 33 do_GetService("@mozilla.org/childprocessmessagemanager;1"); 34 if (!service) { 35 return nullptr; 36 } 37 sWasCreated = true; 38 return static_cast<ContentProcessMessageManager*>(service.get()); 39 } 40 41 already_AddRefed<mozilla::dom::ipc::SharedMap> 42 ContentProcessMessageManager::GetSharedData() { 43 if (ContentChild* child = ContentChild::GetSingleton()) { 44 return do_AddRef(child->SharedData()); 45 } 46 auto* ppmm = nsFrameMessageManager::sParentProcessManager; 47 return do_AddRef(ppmm->SharedData()->GetReadOnly()); 48 } 49 50 bool ContentProcessMessageManager::WasCreated() { return sWasCreated; } 51 52 void ContentProcessMessageManager::MarkForCC() { 53 MarkScopesForCC(); 54 MessageManagerGlobal::MarkForCC(); 55 } 56 57 NS_IMPL_CYCLE_COLLECTION_CLASS(ContentProcessMessageManager) 58 59 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(ContentProcessMessageManager) 60 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMessageManager) 61 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END 62 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 67 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 74 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 81 82 NS_IMPL_CYCLE_COLLECTING_ADDREF(ContentProcessMessageManager) 83 NS_IMPL_CYCLE_COLLECTING_RELEASE(ContentProcessMessageManager) 84 85 bool ContentProcessMessageManager::Init() { 86 if (mInitialized) { 87 return true; 88 } 89 mInitialized = true; 90 91 return nsMessageManagerScriptExecutor::Init(); 92 } 93 94 JSObject* ContentProcessMessageManager::WrapObject( 95 JSContext* aCx, JS::Handle<JSObject*> aGivenProto) { 96 return ContentProcessMessageManager_Binding::Wrap(aCx, this, aGivenProto); 97 } 98 99 JSObject* ContentProcessMessageManager::GetOrCreateWrapper() { 100 JS::Rooted<JS::Value> val(RootingCx()); 101 { 102 // Scope to run ~AutoJSAPI before working with a raw JSObject*. 103 AutoJSAPI jsapi; 104 jsapi.Init(); 105 106 if (!GetOrCreateDOMReflectorNoWrap(jsapi.cx(), this, &val)) { 107 JS_ClearPendingException(jsapi.cx()); 108 return nullptr; 109 } 110 } 111 MOZ_ASSERT(val.isObject()); 112 return &val.toObject(); 113 } 114 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; 122 } 123 return false; 124 } 125 126 void ContentProcessMessageManager::SetInitialProcessData( 127 JS::Handle<JS::Value> aInitialData) { 128 mMessageManager->SetInitialProcessData(aInitialData); 129 }