2014-10-29 21:11:00 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2015-05-03 22:32:37 +03:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2014-10-29 21:11:00 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "ContentProcessManager.h"
|
|
|
|
#include "ContentParent.h"
|
2015-02-03 11:03:29 +03:00
|
|
|
#include "mozilla/dom/TabParent.h"
|
2014-10-29 21:11:00 +03:00
|
|
|
|
|
|
|
#include "mozilla/StaticPtr.h"
|
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
|
|
|
|
|
|
|
#include "nsPrintfCString.h"
|
2015-08-28 10:18:00 +03:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2014-10-29 21:11:00 +03:00
|
|
|
|
|
|
|
// XXX need another bug to move this to a common header.
|
|
|
|
#ifdef DISABLE_ASSERTS_FOR_FUZZING
|
2015-02-07 02:05:08 +03:00
|
|
|
#define ASSERT_UNLESS_FUZZING(...) do { } while (0)
|
2014-10-29 21:11:00 +03:00
|
|
|
#else
|
2015-02-07 02:05:08 +03:00
|
|
|
#define ASSERT_UNLESS_FUZZING(...) MOZ_ASSERT(false, __VA_ARGS__)
|
2014-10-29 21:11:00 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
StaticAutoPtr<ContentProcessManager>
|
|
|
|
ContentProcessManager::sSingleton;
|
|
|
|
|
|
|
|
/* static */ ContentProcessManager*
|
|
|
|
ContentProcessManager::GetSingleton()
|
|
|
|
{
|
2015-07-04 04:29:00 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
2014-10-29 21:11:00 +03:00
|
|
|
|
|
|
|
if (!sSingleton) {
|
|
|
|
sSingleton = new ContentProcessManager();
|
|
|
|
ClearOnShutdown(&sSingleton);
|
|
|
|
}
|
|
|
|
return sSingleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ContentProcessManager::AddContentProcess(ContentParent* aChildCp,
|
|
|
|
const ContentParentId& aParentCpId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aChildCp);
|
|
|
|
|
2017-01-16 23:15:11 +03:00
|
|
|
ContentProcessInfo& info = mContentParentMap[aChildCp->ChildID()];
|
|
|
|
if (!info.mCp) {
|
|
|
|
info.mCp = aChildCp;
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(info.mCp == aChildCp);
|
|
|
|
MOZ_ASSERT_IF(!!info.mParentCpId, info.mParentCpId == aParentCpId);
|
|
|
|
}
|
2014-10-29 21:11:00 +03:00
|
|
|
info.mParentCpId = aParentCpId;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ContentProcessManager::RemoveContentProcess(const ContentParentId& aChildCpId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mContentParentMap.find(aChildCpId) != mContentParentMap.end());
|
|
|
|
|
|
|
|
mContentParentMap.erase(aChildCpId);
|
|
|
|
for (auto iter = mContentParentMap.begin();
|
|
|
|
iter != mContentParentMap.end();
|
|
|
|
++iter) {
|
|
|
|
if (!iter->second.mChildrenCpId.empty()) {
|
|
|
|
iter->second.mChildrenCpId.erase(aChildCpId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ContentProcessManager::AddGrandchildProcess(const ContentParentId& aParentCpId,
|
|
|
|
const ContentParentId& aChildCpId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
auto iter = mContentParentMap.find(aParentCpId);
|
|
|
|
if (NS_WARN_IF(iter == mContentParentMap.end())) {
|
2015-02-07 02:05:08 +03:00
|
|
|
ASSERT_UNLESS_FUZZING("Parent process should be already in map!");
|
2014-10-29 21:11:00 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
iter->second.mChildrenCpId.insert(aChildCpId);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ContentProcessManager::GetParentProcessId(const ContentParentId& aChildCpId,
|
|
|
|
/*out*/ ContentParentId* aParentCpId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
auto iter = mContentParentMap.find(aChildCpId);
|
|
|
|
if (NS_WARN_IF(iter == mContentParentMap.end())) {
|
|
|
|
ASSERT_UNLESS_FUZZING();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*aParentCpId = iter->second.mParentCpId;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ContentParent*
|
|
|
|
ContentProcessManager::GetContentProcessById(const ContentParentId& aChildCpId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
auto iter = mContentParentMap.find(aChildCpId);
|
|
|
|
if (NS_WARN_IF(iter == mContentParentMap.end())) {
|
|
|
|
ASSERT_UNLESS_FUZZING();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return iter->second.mCp;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<ContentParentId>
|
|
|
|
ContentProcessManager::GetAllChildProcessById(const ContentParentId& aParentCpId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsTArray<ContentParentId> cpIdArray;
|
|
|
|
auto iter = mContentParentMap.find(aParentCpId);
|
|
|
|
if (NS_WARN_IF(iter == mContentParentMap.end())) {
|
|
|
|
ASSERT_UNLESS_FUZZING();
|
2018-06-01 18:59:07 +03:00
|
|
|
return cpIdArray;
|
2014-10-29 21:11:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto cpIter = iter->second.mChildrenCpId.begin();
|
|
|
|
cpIter != iter->second.mChildrenCpId.end();
|
|
|
|
++cpIter) {
|
|
|
|
cpIdArray.AppendElement(*cpIter);
|
|
|
|
}
|
|
|
|
|
2018-06-01 18:59:07 +03:00
|
|
|
return cpIdArray;
|
2014-10-29 21:11:00 +03:00
|
|
|
}
|
|
|
|
|
2017-04-05 13:42:00 +03:00
|
|
|
bool
|
|
|
|
ContentProcessManager::RegisterRemoteFrame(const TabId& aTabId,
|
2017-01-16 23:15:11 +03:00
|
|
|
const ContentParentId& aOpenerCpId,
|
2017-04-05 13:42:00 +03:00
|
|
|
const TabId& aOpenerTabId,
|
|
|
|
const IPCTabContext& aContext,
|
|
|
|
const ContentParentId& aChildCpId)
|
2014-10-29 21:11:00 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
auto iter = mContentParentMap.find(aChildCpId);
|
|
|
|
if (NS_WARN_IF(iter == mContentParentMap.end())) {
|
|
|
|
ASSERT_UNLESS_FUZZING();
|
2017-04-05 13:42:00 +03:00
|
|
|
return false;
|
2014-10-29 21:11:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct RemoteFrameInfo info;
|
|
|
|
|
|
|
|
// If it's a PopupIPCTabContext, it's the case that a TabChild want to
|
|
|
|
// open a new tab. aOpenerTabId has to be it's parent frame's opener id.
|
2015-10-30 02:30:57 +03:00
|
|
|
if (aContext.type() == IPCTabContext::TPopupIPCTabContext) {
|
2014-10-29 21:11:00 +03:00
|
|
|
auto remoteFrameIter = iter->second.mRemoteFrames.find(aOpenerTabId);
|
|
|
|
if (remoteFrameIter == iter->second.mRemoteFrames.end()) {
|
2015-02-07 02:05:08 +03:00
|
|
|
ASSERT_UNLESS_FUZZING("Failed to find parent frame's opener id.");
|
2017-04-05 13:42:00 +03:00
|
|
|
return false;
|
2014-10-29 21:11:00 +03:00
|
|
|
}
|
|
|
|
|
2017-01-16 23:15:11 +03:00
|
|
|
info.mOpenerCpId = remoteFrameIter->second.mOpenerCpId;
|
2014-10-29 21:11:00 +03:00
|
|
|
info.mOpenerTabId = remoteFrameIter->second.mOpenerTabId;
|
|
|
|
info.mContext = remoteFrameIter->second.mContext;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
MaybeInvalidTabContext tc(aContext);
|
|
|
|
if (!tc.IsValid()) {
|
|
|
|
NS_ERROR(nsPrintfCString("Received an invalid TabContext from "
|
|
|
|
"the child process. (%s)",
|
|
|
|
tc.GetInvalidReason()).get());
|
2017-04-05 13:42:00 +03:00
|
|
|
return false;
|
2014-10-29 21:11:00 +03:00
|
|
|
}
|
2017-01-16 23:15:11 +03:00
|
|
|
info.mOpenerCpId = aOpenerCpId;
|
2014-10-29 21:11:00 +03:00
|
|
|
info.mOpenerTabId = aOpenerTabId;
|
|
|
|
info.mContext = tc.GetTabContext();
|
|
|
|
}
|
|
|
|
|
2017-04-05 13:42:00 +03:00
|
|
|
iter->second.mRemoteFrames[aTabId] = info;
|
|
|
|
return true;
|
2014-10-29 21:11:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-04-05 13:42:00 +03:00
|
|
|
ContentProcessManager::UnregisterRemoteFrame(const ContentParentId& aChildCpId,
|
|
|
|
const TabId& aChildTabId)
|
2014-10-29 21:11:00 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
auto iter = mContentParentMap.find(aChildCpId);
|
|
|
|
if (NS_WARN_IF(iter == mContentParentMap.end())) {
|
|
|
|
ASSERT_UNLESS_FUZZING();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto remoteFrameIter = iter->second.mRemoteFrames.find(aChildTabId);
|
|
|
|
if (remoteFrameIter != iter->second.mRemoteFrames.end()) {
|
|
|
|
iter->second.mRemoteFrames.erase(aChildTabId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-13 03:27:00 +03:00
|
|
|
bool
|
|
|
|
ContentProcessManager::GetTabContextByProcessAndTabId(const ContentParentId& aChildCpId,
|
|
|
|
const TabId& aChildTabId,
|
|
|
|
/*out*/ TabContext* aTabContext)
|
2014-10-29 21:11:00 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-11-13 03:27:00 +03:00
|
|
|
MOZ_ASSERT(aTabContext);
|
2014-10-29 21:11:00 +03:00
|
|
|
|
|
|
|
auto iter = mContentParentMap.find(aChildCpId);
|
|
|
|
if (NS_WARN_IF(iter == mContentParentMap.end())) {
|
|
|
|
ASSERT_UNLESS_FUZZING();
|
2014-11-13 03:27:00 +03:00
|
|
|
return false;
|
2014-10-29 21:11:00 +03:00
|
|
|
}
|
|
|
|
|
2014-11-13 03:27:00 +03:00
|
|
|
auto remoteFrameIter = iter->second.mRemoteFrames.find(aChildTabId);
|
|
|
|
if (NS_WARN_IF(remoteFrameIter == iter->second.mRemoteFrames.end())) {
|
|
|
|
ASSERT_UNLESS_FUZZING();
|
|
|
|
return false;
|
2014-10-29 21:11:00 +03:00
|
|
|
}
|
|
|
|
|
2014-11-13 03:27:00 +03:00
|
|
|
*aTabContext = remoteFrameIter->second.mContext;
|
|
|
|
|
|
|
|
return true;
|
2014-10-29 21:11:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<TabContext>
|
|
|
|
ContentProcessManager::GetTabContextByContentProcess(const ContentParentId& aChildCpId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsTArray<TabContext> tabContextArray;
|
|
|
|
auto iter = mContentParentMap.find(aChildCpId);
|
|
|
|
if (NS_WARN_IF(iter == mContentParentMap.end())) {
|
|
|
|
ASSERT_UNLESS_FUZZING();
|
2018-06-01 18:59:07 +03:00
|
|
|
return tabContextArray;
|
2014-10-29 21:11:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto remoteFrameIter = iter->second.mRemoteFrames.begin();
|
|
|
|
remoteFrameIter != iter->second.mRemoteFrames.end();
|
|
|
|
++remoteFrameIter) {
|
|
|
|
tabContextArray.AppendElement(remoteFrameIter->second.mContext);
|
|
|
|
}
|
|
|
|
|
2018-06-01 18:59:07 +03:00
|
|
|
return tabContextArray;
|
2014-10-29 21:11:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ContentProcessManager::GetRemoteFrameOpenerTabId(const ContentParentId& aChildCpId,
|
|
|
|
const TabId& aChildTabId,
|
2017-01-16 23:15:11 +03:00
|
|
|
/*out*/ContentParentId* aOpenerCpId,
|
2014-10-29 21:11:00 +03:00
|
|
|
/*out*/TabId* aOpenerTabId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
auto iter = mContentParentMap.find(aChildCpId);
|
|
|
|
if (NS_WARN_IF(iter == mContentParentMap.end())) {
|
|
|
|
ASSERT_UNLESS_FUZZING();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto remoteFrameIter = iter->second.mRemoteFrames.find(aChildTabId);
|
|
|
|
if (NS_WARN_IF(remoteFrameIter == iter->second.mRemoteFrames.end())) {
|
|
|
|
ASSERT_UNLESS_FUZZING();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-16 23:15:11 +03:00
|
|
|
*aOpenerCpId = remoteFrameIter->second.mOpenerCpId;
|
2014-10-29 21:11:00 +03:00
|
|
|
*aOpenerTabId = remoteFrameIter->second.mOpenerTabId;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-03 11:03:29 +03:00
|
|
|
already_AddRefed<TabParent>
|
|
|
|
ContentProcessManager::GetTabParentByProcessAndTabId(const ContentParentId& aChildCpId,
|
|
|
|
const TabId& aChildTabId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
auto iter = mContentParentMap.find(aChildCpId);
|
|
|
|
if (NS_WARN_IF(iter == mContentParentMap.end())) {
|
|
|
|
ASSERT_UNLESS_FUZZING();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-07 21:30:33 +03:00
|
|
|
const ManagedContainer<PBrowserParent>& browsers = iter->second.mCp->ManagedPBrowserParent();
|
|
|
|
for (auto iter = browsers.ConstIter(); !iter.Done(); iter.Next()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<TabParent> tab = TabParent::GetFrom(iter.Get()->GetKey());
|
2015-02-03 11:03:29 +03:00
|
|
|
if (tab->GetTabId() == aChildTabId) {
|
|
|
|
return tab.forget();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<TabParent>
|
|
|
|
ContentProcessManager::GetTopLevelTabParentByProcessAndTabId(const ContentParentId& aChildCpId,
|
|
|
|
const TabId& aChildTabId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
// Used to keep the current ContentParentId and the current TabId
|
|
|
|
// in the iteration(do-while loop below)
|
|
|
|
ContentParentId currentCpId;
|
|
|
|
TabId currentTabId;
|
|
|
|
|
|
|
|
// To get the ContentParentId and the TabParentId on upper level
|
|
|
|
ContentParentId parentCpId = aChildCpId;
|
|
|
|
TabId openerTabId = aChildTabId;
|
|
|
|
|
|
|
|
// Stop this loop when the upper ContentParentId of
|
|
|
|
// the current ContentParentId is chrome(ContentParentId = 0).
|
|
|
|
do {
|
|
|
|
// Update the current ContentParentId and TabId in iteration
|
|
|
|
currentCpId = parentCpId;
|
|
|
|
currentTabId = openerTabId;
|
|
|
|
|
|
|
|
// Get the ContentParentId and TabId on upper level
|
2017-01-16 23:15:11 +03:00
|
|
|
if (!GetRemoteFrameOpenerTabId(currentCpId, currentTabId, &parentCpId, &openerTabId)) {
|
2015-02-03 11:03:29 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
} while (parentCpId);
|
|
|
|
|
|
|
|
// Get the top level TabParent by the current ContentParentId and TabId
|
|
|
|
return GetTabParentByProcessAndTabId(currentCpId, currentTabId);
|
|
|
|
}
|
|
|
|
|
2015-08-28 10:18:00 +03:00
|
|
|
nsTArray<TabId>
|
|
|
|
ContentProcessManager::GetTabParentsByProcessId(const ContentParentId& aChildCpId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsTArray<TabId> tabIdList;
|
|
|
|
auto iter = mContentParentMap.find(aChildCpId);
|
|
|
|
if (NS_WARN_IF(iter == mContentParentMap.end())) {
|
|
|
|
ASSERT_UNLESS_FUZZING();
|
2018-06-01 18:59:07 +03:00
|
|
|
return tabIdList;
|
2015-08-28 10:18:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto remoteFrameIter = iter->second.mRemoteFrames.begin();
|
|
|
|
remoteFrameIter != iter->second.mRemoteFrames.end();
|
|
|
|
++remoteFrameIter) {
|
|
|
|
tabIdList.AppendElement(remoteFrameIter->first);
|
|
|
|
}
|
|
|
|
|
2018-06-01 18:59:07 +03:00
|
|
|
return tabIdList;
|
2015-08-28 10:18:00 +03:00
|
|
|
}
|
|
|
|
|
2017-03-07 22:00:28 +03:00
|
|
|
uint32_t
|
|
|
|
ContentProcessManager::GetTabParentCountByProcessId(const ContentParentId& aChildCpId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
auto iter = mContentParentMap.find(aChildCpId);
|
|
|
|
if (NS_WARN_IF(iter == mContentParentMap.end())) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return iter->second.mRemoteFrames.size();
|
|
|
|
}
|
|
|
|
|
2014-10-29 21:11:00 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|