2016-11-07 23:30:17 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
|
2016-10-28 21:25:43 +03:00
|
|
|
#include "mozilla/dom/TabGroup.h"
|
2016-11-07 23:30:17 +03:00
|
|
|
|
2016-11-04 21:13:52 +03:00
|
|
|
#include "mozilla/dom/ContentChild.h"
|
|
|
|
#include "mozilla/dom/TabChild.h"
|
2016-10-28 21:25:43 +03:00
|
|
|
#include "mozilla/dom/DocGroup.h"
|
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2016-11-07 23:30:17 +03:00
|
|
|
#include "mozilla/StaticPtr.h"
|
|
|
|
#include "mozilla/Telemetry.h"
|
|
|
|
#include "mozilla/ThrottledEventQueue.h"
|
2016-10-28 21:25:43 +03:00
|
|
|
#include "nsIDocShell.h"
|
2016-11-07 23:30:17 +03:00
|
|
|
#include "nsIEffectiveTLDService.h"
|
|
|
|
#include "nsIURI.h"
|
2016-10-28 21:25:43 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2016-11-07 23:30:17 +03:00
|
|
|
static StaticRefPtr<TabGroup> sChromeTabGroup;
|
|
|
|
|
|
|
|
TabGroup::TabGroup(bool aIsChrome)
|
2016-11-23 08:14:39 +03:00
|
|
|
: mLastWindowLeft(false)
|
2016-11-04 21:13:52 +03:00
|
|
|
, mThrottledQueuesInitialized(false)
|
2016-11-07 23:30:17 +03:00
|
|
|
{
|
2016-11-23 08:14:39 +03:00
|
|
|
for (size_t i = 0; i < size_t(TaskCategory::Count); i++) {
|
|
|
|
TaskCategory category = static_cast<TaskCategory>(i);
|
2017-01-19 01:25:07 +03:00
|
|
|
if (aIsChrome) {
|
|
|
|
// The chrome TabGroup dispatches directly to the main thread. This means
|
|
|
|
// that we don't have to worry about cyclical references when cleaning up
|
|
|
|
// the chrome TabGroup.
|
|
|
|
mEventTargets[i] = do_GetMainThread();
|
|
|
|
} else {
|
|
|
|
mEventTargets[i] = CreateEventTargetFor(category);
|
|
|
|
}
|
2016-11-23 08:14:39 +03:00
|
|
|
}
|
|
|
|
|
2016-11-07 23:30:17 +03:00
|
|
|
// Do not throttle runnables from chrome windows. In theory we should
|
|
|
|
// not have abuse issues from these windows and many browser chrome
|
|
|
|
// tests have races that fail if we do throttle chrome runnables.
|
|
|
|
if (aIsChrome) {
|
|
|
|
MOZ_ASSERT(!sChromeTabGroup);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-04 21:13:52 +03:00
|
|
|
// This constructor can be called from the IPC I/O thread. In that case, we
|
|
|
|
// won't actually use the TabGroup on the main thread until GetFromWindowActor
|
|
|
|
// is called, so we initialize the throttled queues there.
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
EnsureThrottledEventQueues();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TabGroup::~TabGroup()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mDocGroups.IsEmpty());
|
|
|
|
MOZ_ASSERT(mWindows.IsEmpty());
|
2016-12-28 00:35:07 +03:00
|
|
|
MOZ_RELEASE_ASSERT(mLastWindowLeft);
|
2016-11-04 21:13:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TabGroup::EnsureThrottledEventQueues()
|
|
|
|
{
|
|
|
|
if (mThrottledQueuesInitialized) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mThrottledQueuesInitialized = true;
|
|
|
|
|
2016-11-27 22:24:34 +03:00
|
|
|
for (size_t i = 0; i < size_t(TaskCategory::Count); i++) {
|
|
|
|
TaskCategory category = static_cast<TaskCategory>(i);
|
|
|
|
if (category == TaskCategory::Worker || category == TaskCategory::Timer) {
|
|
|
|
nsCOMPtr<nsIEventTarget> target = ThrottledEventQueue::Create(mEventTargets[i]);
|
|
|
|
if (target) {
|
|
|
|
// This may return nullptr during xpcom shutdown. This is ok as we
|
|
|
|
// do not guarantee a ThrottledEventQueue will be present.
|
|
|
|
mEventTargets[i] = target;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-07 23:30:17 +03:00
|
|
|
}
|
2016-10-28 21:25:43 +03:00
|
|
|
|
2017-01-19 01:25:07 +03:00
|
|
|
/* static */ TabGroup*
|
2016-10-28 21:25:43 +03:00
|
|
|
TabGroup::GetChromeTabGroup()
|
|
|
|
{
|
|
|
|
if (!sChromeTabGroup) {
|
2016-11-07 23:30:17 +03:00
|
|
|
sChromeTabGroup = new TabGroup(true /* chrome tab group */);
|
2016-10-28 21:25:43 +03:00
|
|
|
ClearOnShutdown(&sChromeTabGroup);
|
|
|
|
}
|
|
|
|
return sChromeTabGroup;
|
|
|
|
}
|
|
|
|
|
2016-11-04 21:13:52 +03:00
|
|
|
/* static */ TabGroup*
|
|
|
|
TabGroup::GetFromWindowActor(mozIDOMWindowProxy* aWindow)
|
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
TabChild* tabChild = TabChild::GetFrom(aWindow);
|
|
|
|
if (!tabChild) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ContentChild* cc = ContentChild::GetSingleton();
|
|
|
|
nsCOMPtr<nsIEventTarget> target = cc->GetActorEventTarget(tabChild);
|
|
|
|
if (!target) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have an event target. We assume the IPC code created it via
|
|
|
|
// TabGroup::CreateEventTarget.
|
|
|
|
RefPtr<Dispatcher> dispatcher = Dispatcher::FromEventTarget(target);
|
|
|
|
MOZ_RELEASE_ASSERT(dispatcher);
|
|
|
|
auto tabGroup = dispatcher->AsTabGroup();
|
|
|
|
MOZ_RELEASE_ASSERT(tabGroup);
|
|
|
|
|
|
|
|
// We delay creating the event targets until now since the TabGroup
|
|
|
|
// constructor ran off the main thread.
|
|
|
|
tabGroup->EnsureThrottledEventQueues();
|
|
|
|
|
|
|
|
return tabGroup;
|
|
|
|
}
|
|
|
|
|
2016-10-28 21:25:43 +03:00
|
|
|
already_AddRefed<DocGroup>
|
|
|
|
TabGroup::GetDocGroup(const nsACString& aKey)
|
|
|
|
{
|
|
|
|
RefPtr<DocGroup> docGroup(mDocGroups.GetEntry(aKey)->mDocGroup);
|
|
|
|
return docGroup.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<DocGroup>
|
|
|
|
TabGroup::AddDocument(const nsACString& aKey, nsIDocument* aDocument)
|
|
|
|
{
|
|
|
|
HashEntry* entry = mDocGroups.PutEntry(aKey);
|
|
|
|
RefPtr<DocGroup> docGroup;
|
|
|
|
if (entry->mDocGroup) {
|
|
|
|
docGroup = entry->mDocGroup;
|
|
|
|
} else {
|
|
|
|
docGroup = new DocGroup(this, aKey);
|
|
|
|
entry->mDocGroup = docGroup;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that the hashtable was updated and now contains the correct value
|
|
|
|
MOZ_ASSERT(RefPtr<DocGroup>(GetDocGroup(aKey)) == docGroup);
|
|
|
|
|
|
|
|
docGroup->mDocuments.AppendElement(aDocument);
|
|
|
|
|
|
|
|
return docGroup.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<TabGroup>
|
|
|
|
TabGroup::Join(nsPIDOMWindowOuter* aWindow, TabGroup* aTabGroup)
|
|
|
|
{
|
|
|
|
RefPtr<TabGroup> tabGroup = aTabGroup;
|
|
|
|
if (!tabGroup) {
|
|
|
|
tabGroup = new TabGroup();
|
|
|
|
}
|
2016-12-28 00:35:07 +03:00
|
|
|
MOZ_RELEASE_ASSERT(!tabGroup->mLastWindowLeft);
|
2016-10-28 21:25:43 +03:00
|
|
|
MOZ_ASSERT(!tabGroup->mWindows.Contains(aWindow));
|
|
|
|
tabGroup->mWindows.AppendElement(aWindow);
|
|
|
|
return tabGroup.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TabGroup::Leave(nsPIDOMWindowOuter* aWindow)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mWindows.Contains(aWindow));
|
|
|
|
mWindows.RemoveElement(aWindow);
|
2016-11-23 08:14:39 +03:00
|
|
|
|
2017-01-19 01:25:07 +03:00
|
|
|
// The Chrome TabGroup doesn't have cyclical references through mEventTargets
|
|
|
|
// to itself, meaning that we don't have to worry about nulling mEventTargets
|
|
|
|
// out after the last window leaves.
|
|
|
|
if (sChromeTabGroup != this && mWindows.IsEmpty()) {
|
2016-11-23 08:14:39 +03:00
|
|
|
mLastWindowLeft = true;
|
|
|
|
|
|
|
|
// There is a RefPtr cycle TabGroup -> DispatcherEventTarget -> TabGroup. To
|
|
|
|
// avoid leaks, we need to break the chain somewhere. We shouldn't be using
|
|
|
|
// the ThrottledEventQueue for this TabGroup when no windows belong to it,
|
|
|
|
// so it's safe to null out the queue here.
|
|
|
|
for (size_t i = 0; i < size_t(TaskCategory::Count); i++) {
|
|
|
|
mEventTargets[i] = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2016-10-28 21:25:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
TabGroup::FindItemWithName(const nsAString& aName,
|
|
|
|
nsIDocShellTreeItem* aRequestor,
|
|
|
|
nsIDocShellTreeItem* aOriginalRequestor,
|
|
|
|
nsIDocShellTreeItem** aFoundItem)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aFoundItem);
|
|
|
|
*aFoundItem = nullptr;
|
|
|
|
|
|
|
|
MOZ_ASSERT(!aName.LowerCaseEqualsLiteral("_blank") &&
|
|
|
|
!aName.LowerCaseEqualsLiteral("_top") &&
|
|
|
|
!aName.LowerCaseEqualsLiteral("_parent") &&
|
|
|
|
!aName.LowerCaseEqualsLiteral("_self"));
|
|
|
|
|
|
|
|
for (nsPIDOMWindowOuter* outerWindow : mWindows) {
|
|
|
|
// Ignore non-toplevel windows
|
|
|
|
if (outerWindow->GetScriptableParentOrNull()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocShellTreeItem> docshell = outerWindow->GetDocShell();
|
|
|
|
if (!docshell) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocShellTreeItem> root;
|
|
|
|
docshell->GetSameTypeRootTreeItem(getter_AddRefs(root));
|
|
|
|
MOZ_RELEASE_ASSERT(docshell == root);
|
|
|
|
if (root && aRequestor != root) {
|
|
|
|
root->FindItemWithName(aName, this, aOriginalRequestor, aFoundItem);
|
|
|
|
if (*aFoundItem) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-10-21 23:56:51 +03:00
|
|
|
nsTArray<nsPIDOMWindowOuter*>
|
|
|
|
TabGroup::GetTopLevelWindows()
|
|
|
|
{
|
|
|
|
nsTArray<nsPIDOMWindowOuter*> array;
|
|
|
|
|
|
|
|
for (nsPIDOMWindowOuter* outerWindow : mWindows) {
|
2016-11-29 19:50:13 +03:00
|
|
|
if (outerWindow->GetDocShell() &&
|
|
|
|
!outerWindow->GetScriptableParentOrNull()) {
|
2016-10-21 23:56:51 +03:00
|
|
|
array.AppendElement(outerWindow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2016-10-28 21:25:43 +03:00
|
|
|
NS_IMPL_ISUPPORTS(TabGroup, nsISupports)
|
|
|
|
|
|
|
|
TabGroup::HashEntry::HashEntry(const nsACString* aKey)
|
|
|
|
: nsCStringHashKey(aKey), mDocGroup(nullptr)
|
|
|
|
{}
|
|
|
|
|
2016-10-29 01:25:08 +03:00
|
|
|
nsresult
|
|
|
|
TabGroup::Dispatch(const char* aName,
|
|
|
|
TaskCategory aCategory,
|
|
|
|
already_AddRefed<nsIRunnable>&& aRunnable)
|
|
|
|
{
|
2016-11-23 08:14:39 +03:00
|
|
|
nsCOMPtr<nsIRunnable> runnable(aRunnable);
|
|
|
|
if (aName) {
|
|
|
|
if (nsCOMPtr<nsINamed> named = do_QueryInterface(runnable)) {
|
|
|
|
named->SetName(aName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
return NS_DispatchToCurrentThread(runnable.forget());
|
|
|
|
} else {
|
|
|
|
return NS_DispatchToMainThread(runnable.forget());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-16 00:48:14 +03:00
|
|
|
nsIEventTarget*
|
2016-11-23 08:14:39 +03:00
|
|
|
TabGroup::EventTargetFor(TaskCategory aCategory) const
|
|
|
|
{
|
2016-11-27 22:24:34 +03:00
|
|
|
if (aCategory == TaskCategory::Worker || aCategory == TaskCategory::Timer) {
|
|
|
|
MOZ_RELEASE_ASSERT(mThrottledQueuesInitialized || this == sChromeTabGroup);
|
|
|
|
}
|
|
|
|
|
2016-12-28 00:35:07 +03:00
|
|
|
if (NS_WARN_IF(mLastWindowLeft)) {
|
|
|
|
// Once we've disconnected everything, we still allow people to
|
|
|
|
// dispatch. We'll just go directly to the main thread.
|
|
|
|
nsCOMPtr<nsIEventTarget> main = do_GetMainThread();
|
|
|
|
return main;
|
|
|
|
}
|
|
|
|
|
2016-12-16 00:48:14 +03:00
|
|
|
return mEventTargets[size_t(aCategory)];
|
2016-10-29 01:25:08 +03:00
|
|
|
}
|
|
|
|
|
2016-10-28 21:25:43 +03:00
|
|
|
}
|
|
|
|
}
|