Bug 1634779 - pt 2. Partially revert Bug 1603006 r=kmag
[gecko.git] / dom / ipc / ContentProcessManager.cpp
blob23ebbd026eba8890be3538ef30ac6a511465f623
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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "ContentProcessManager.h"
8 #include "ContentParent.h"
9 #include "mozilla/dom/BrowserParent.h"
11 #include "mozilla/StaticPtr.h"
12 #include "mozilla/ClearOnShutdown.h"
14 #include "nsPrintfCString.h"
16 // XXX need another bug to move this to a common header.
17 #ifdef DISABLE_ASSERTS_FOR_FUZZING
18 # define ASSERT_UNLESS_FUZZING(...) \
19 do { \
20 } while (0)
21 #else
22 # define ASSERT_UNLESS_FUZZING(...) MOZ_ASSERT(false, __VA_ARGS__)
23 #endif
25 namespace mozilla {
26 namespace dom {
28 /* static */
29 StaticAutoPtr<ContentProcessManager> ContentProcessManager::sSingleton;
31 /* static */
32 ContentProcessManager* ContentProcessManager::GetSingleton() {
33 MOZ_ASSERT(XRE_IsParentProcess());
35 if (!sSingleton) {
36 sSingleton = new ContentProcessManager();
37 ClearOnShutdown(&sSingleton);
39 return sSingleton;
42 void ContentProcessManager::AddContentProcess(ContentParent* aChildCp) {
43 MOZ_ASSERT(NS_IsMainThread());
44 MOZ_ASSERT(aChildCp);
46 auto entry = mContentParentMap.LookupForAdd(aChildCp->ChildID());
47 MOZ_ASSERT_IF(entry, entry.Data() == aChildCp);
48 entry.OrInsert([&] { return aChildCp; });
51 void ContentProcessManager::RemoveContentProcess(
52 const ContentParentId& aChildCpId) {
53 MOZ_ASSERT(NS_IsMainThread());
55 MOZ_ALWAYS_TRUE(mContentParentMap.Remove(aChildCpId));
58 ContentParent* ContentProcessManager::GetContentProcessById(
59 const ContentParentId& aChildCpId) {
60 MOZ_ASSERT(NS_IsMainThread());
62 ContentParent* contentParent = mContentParentMap.Get(aChildCpId);
63 if (NS_WARN_IF(!contentParent)) {
64 ASSERT_UNLESS_FUZZING();
65 return nullptr;
67 return contentParent;
70 bool ContentProcessManager::RegisterRemoteFrame(BrowserParent* aChildBp) {
71 MOZ_ASSERT(NS_IsMainThread());
72 MOZ_ASSERT(aChildBp);
74 auto entry = mBrowserParentMap.LookupForAdd(aChildBp->GetTabId());
75 MOZ_ASSERT_IF(entry, entry.Data() == aChildBp);
76 entry.OrInsert([&] { return aChildBp; });
77 return !entry;
80 void ContentProcessManager::UnregisterRemoteFrame(const TabId& aChildTabId) {
81 MOZ_ASSERT(NS_IsMainThread());
83 MOZ_ALWAYS_TRUE(mBrowserParentMap.Remove(aChildTabId));
86 ContentParentId ContentProcessManager::GetTabProcessId(
87 const TabId& aChildTabId) {
88 MOZ_ASSERT(NS_IsMainThread());
90 if (BrowserParent* browserParent = mBrowserParentMap.Get(aChildTabId)) {
91 return browserParent->Manager()->ChildID();
93 return ContentParentId(0);
96 uint32_t ContentProcessManager::GetBrowserParentCountByProcessId(
97 const ContentParentId& aChildCpId) {
98 MOZ_ASSERT(NS_IsMainThread());
100 ContentParent* contentParent = mContentParentMap.Get(aChildCpId);
101 if (NS_WARN_IF(!contentParent)) {
102 return 0;
104 return contentParent->ManagedPBrowserParent().Count();
107 already_AddRefed<BrowserParent>
108 ContentProcessManager::GetBrowserParentByProcessAndTabId(
109 const ContentParentId& aChildCpId, const TabId& aChildTabId) {
110 RefPtr<BrowserParent> browserParent = mBrowserParentMap.Get(aChildTabId);
111 if (NS_WARN_IF(!browserParent)) {
112 return nullptr;
115 if (NS_WARN_IF(browserParent->Manager()->ChildID() != aChildCpId)) {
116 return nullptr;
119 return browserParent.forget();
122 already_AddRefed<BrowserParent>
123 ContentProcessManager::GetTopLevelBrowserParentByProcessAndTabId(
124 const ContentParentId& aChildCpId, const TabId& aChildTabId) {
125 RefPtr<BrowserParent> browserParent =
126 GetBrowserParentByProcessAndTabId(aChildCpId, aChildTabId);
127 while (browserParent && browserParent->GetBrowserBridgeParent()) {
128 browserParent = browserParent->GetBrowserBridgeParent()->Manager();
131 return browserParent.forget();
134 } // namespace dom
135 } // namespace mozilla