2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2014-01-07 22:53:31 +04:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "nsIGlobalObject.h"
|
2019-05-24 11:27:03 +03:00
|
|
|
#include "mozilla/CycleCollectedJSContext.h"
|
2018-06-02 16:51:42 +03:00
|
|
|
#include "mozilla/dom/BlobURLProtocolHandler.h"
|
2019-05-24 11:27:03 +03:00
|
|
|
#include "mozilla/dom/FunctionBinding.h"
|
2020-04-18 15:25:57 +03:00
|
|
|
#include "mozilla/dom/Report.h"
|
|
|
|
#include "mozilla/dom/ReportingObserver.h"
|
2018-01-31 20:10:26 +03:00
|
|
|
#include "mozilla/dom/ServiceWorker.h"
|
2018-03-03 00:02:49 +03:00
|
|
|
#include "mozilla/dom/ServiceWorkerRegistration.h"
|
2014-01-07 22:53:31 +04:00
|
|
|
#include "nsContentUtils.h"
|
2015-07-01 16:56:00 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2018-04-04 15:57:17 +03:00
|
|
|
#include "nsGlobalWindowInner.h"
|
2014-01-07 22:53:31 +04:00
|
|
|
|
2020-04-18 15:25:57 +03:00
|
|
|
// Max number of Report objects
|
|
|
|
constexpr auto MAX_REPORT_RECORDS = 100;
|
|
|
|
|
2019-05-24 11:27:03 +03:00
|
|
|
using mozilla::AutoSlowOperation;
|
|
|
|
using mozilla::CycleCollectedJSContext;
|
2018-02-21 21:53:22 +03:00
|
|
|
using mozilla::DOMEventTargetHelper;
|
2019-05-24 11:27:03 +03:00
|
|
|
using mozilla::ErrorResult;
|
|
|
|
using mozilla::IgnoredErrorResult;
|
2018-02-21 21:53:22 +03:00
|
|
|
using mozilla::MallocSizeOf;
|
2018-01-12 04:46:08 +03:00
|
|
|
using mozilla::Maybe;
|
2019-05-24 11:27:03 +03:00
|
|
|
using mozilla::MicroTaskRunnable;
|
2018-04-13 21:04:47 +03:00
|
|
|
using mozilla::dom::BlobURLProtocolHandler;
|
2018-01-12 04:46:08 +03:00
|
|
|
using mozilla::dom::ClientInfo;
|
2020-04-29 12:02:05 +03:00
|
|
|
using mozilla::dom::Report;
|
|
|
|
using mozilla::dom::ReportingObserver;
|
2018-01-31 20:10:26 +03:00
|
|
|
using mozilla::dom::ServiceWorker;
|
2018-01-12 04:46:08 +03:00
|
|
|
using mozilla::dom::ServiceWorkerDescriptor;
|
2018-03-03 00:02:49 +03:00
|
|
|
using mozilla::dom::ServiceWorkerRegistration;
|
|
|
|
using mozilla::dom::ServiceWorkerRegistrationDescriptor;
|
2019-05-24 11:27:03 +03:00
|
|
|
using mozilla::dom::VoidFunction;
|
2018-01-12 04:46:08 +03:00
|
|
|
|
2020-04-18 15:25:57 +03:00
|
|
|
nsIGlobalObject::nsIGlobalObject()
|
|
|
|
: mIsDying(false), mIsScriptForbidden(false), mIsInnerWindow(false) {}
|
|
|
|
|
2020-03-28 01:40:58 +03:00
|
|
|
bool nsIGlobalObject::IsScriptForbidden(JSObject* aCallback,
|
|
|
|
bool aIsJSImplementedWebIDL) const {
|
|
|
|
if (mIsScriptForbidden) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
if (aIsJSImplementedWebIDL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!xpc::Scriptability::Get(aCallback).Allowed()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-05-07 10:05:43 +03:00
|
|
|
nsIGlobalObject::~nsIGlobalObject() {
|
2020-04-18 15:25:57 +03:00
|
|
|
UnlinkObjectsInGlobal();
|
2018-02-21 21:53:22 +03:00
|
|
|
DisconnectEventTargetObjects();
|
2018-05-04 19:25:05 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mEventTargetObjects.isEmpty());
|
2015-05-07 10:05:43 +03:00
|
|
|
}
|
|
|
|
|
2014-01-07 22:53:31 +04:00
|
|
|
nsIPrincipal* nsIGlobalObject::PrincipalOrNull() {
|
2019-06-10 22:32:04 +03:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-05-03 18:15:31 +03:00
|
|
|
JSObject* global = GetGlobalJSObjectPreserveColor();
|
2014-01-07 22:53:31 +04:00
|
|
|
if (NS_WARN_IF(!global)) return nullptr;
|
|
|
|
|
2014-05-13 13:58:00 +04:00
|
|
|
return nsContentUtils::ObjectPrincipal(global);
|
2014-01-07 22:53:31 +04:00
|
|
|
}
|
2015-05-07 10:05:43 +03:00
|
|
|
|
|
|
|
void nsIGlobalObject::RegisterHostObjectURI(const nsACString& aURI) {
|
|
|
|
MOZ_ASSERT(!mHostObjectURIs.Contains(aURI));
|
|
|
|
mHostObjectURIs.AppendElement(aURI);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsIGlobalObject::UnregisterHostObjectURI(const nsACString& aURI) {
|
|
|
|
mHostObjectURIs.RemoveElement(aURI);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class UnlinkHostObjectURIsRunnable final : public mozilla::Runnable {
|
2015-05-07 10:05:43 +03:00
|
|
|
public:
|
|
|
|
explicit UnlinkHostObjectURIsRunnable(nsTArray<nsCString>& aURIs)
|
2017-06-12 22:34:10 +03:00
|
|
|
: mozilla::Runnable("UnlinkHostObjectURIsRunnable") {
|
2015-05-07 10:05:43 +03:00
|
|
|
mURIs.SwapElements(aURIs);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() override {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
for (uint32_t index = 0; index < mURIs.Length(); ++index) {
|
2018-06-02 16:51:42 +03:00
|
|
|
BlobURLProtocolHandler::RemoveDataEntry(mURIs[index]);
|
2015-05-07 10:05:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-02-21 13:41:47 +03:00
|
|
|
~UnlinkHostObjectURIsRunnable() = default;
|
2015-05-07 10:05:43 +03:00
|
|
|
|
|
|
|
nsTArray<nsCString> mURIs;
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2015-05-07 10:05:43 +03:00
|
|
|
|
2020-04-18 15:25:57 +03:00
|
|
|
void nsIGlobalObject::UnlinkObjectsInGlobal() {
|
|
|
|
if (!mHostObjectURIs.IsEmpty()) {
|
|
|
|
// BlobURLProtocolHandler is main-thread only.
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
for (uint32_t index = 0; index < mHostObjectURIs.Length(); ++index) {
|
|
|
|
BlobURLProtocolHandler::RemoveDataEntry(mHostObjectURIs[index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
mHostObjectURIs.Clear();
|
|
|
|
} else {
|
|
|
|
RefPtr<UnlinkHostObjectURIsRunnable> runnable =
|
|
|
|
new UnlinkHostObjectURIsRunnable(mHostObjectURIs);
|
|
|
|
MOZ_ASSERT(mHostObjectURIs.IsEmpty());
|
|
|
|
|
|
|
|
nsresult rv = NS_DispatchToMainThread(runnable);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch a runnable to the main-thread.");
|
|
|
|
}
|
2015-05-07 10:05:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-18 15:25:57 +03:00
|
|
|
mReportRecords.Clear();
|
|
|
|
mReportingObservers.Clear();
|
2015-05-07 10:05:43 +03:00
|
|
|
}
|
|
|
|
|
2020-04-18 15:25:57 +03:00
|
|
|
void nsIGlobalObject::TraverseObjectsInGlobal(
|
|
|
|
nsCycleCollectionTraversalCallback& cb) {
|
2015-05-12 15:11:03 +03:00
|
|
|
// Currently we only store BlobImpl objects off the the main-thread and they
|
2015-05-07 10:05:43 +03:00
|
|
|
// are not CCed.
|
2020-04-18 15:25:57 +03:00
|
|
|
if (!mHostObjectURIs.IsEmpty() && NS_IsMainThread()) {
|
|
|
|
for (uint32_t index = 0; index < mHostObjectURIs.Length(); ++index) {
|
|
|
|
BlobURLProtocolHandler::Traverse(mHostObjectURIs[index], cb);
|
|
|
|
}
|
2015-05-07 10:05:43 +03:00
|
|
|
}
|
|
|
|
|
2020-04-18 15:25:57 +03:00
|
|
|
nsIGlobalObject* tmp = this;
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mReportRecords)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mReportingObservers)
|
2015-05-07 10:05:43 +03:00
|
|
|
}
|
2018-01-12 04:46:08 +03:00
|
|
|
|
2018-02-21 21:53:22 +03:00
|
|
|
void nsIGlobalObject::AddEventTargetObject(DOMEventTargetHelper* aObject) {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aObject);
|
2018-05-04 19:25:05 +03:00
|
|
|
MOZ_ASSERT(!aObject->isInList());
|
|
|
|
mEventTargetObjects.insertBack(aObject);
|
2018-02-21 21:53:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsIGlobalObject::RemoveEventTargetObject(DOMEventTargetHelper* aObject) {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aObject);
|
2018-05-04 19:25:05 +03:00
|
|
|
MOZ_ASSERT(aObject->isInList());
|
|
|
|
MOZ_ASSERT(aObject->GetOwnerGlobal() == this);
|
|
|
|
aObject->remove();
|
2018-02-21 21:53:22 +03:00
|
|
|
}
|
|
|
|
|
2018-02-21 21:53:53 +03:00
|
|
|
void nsIGlobalObject::ForEachEventTargetObject(
|
|
|
|
const std::function<void(DOMEventTargetHelper*, bool* aDoneOut)>& aFunc)
|
|
|
|
const {
|
2018-05-04 19:25:05 +03:00
|
|
|
// Protect against the function call triggering a mutation of the list
|
2018-02-21 21:53:22 +03:00
|
|
|
// while we are iterating by copying the DETH references to a temporary
|
|
|
|
// list.
|
2018-06-24 18:16:32 +03:00
|
|
|
AutoTArray<RefPtr<DOMEventTargetHelper>, 64> targetList;
|
2018-05-04 19:25:05 +03:00
|
|
|
for (const DOMEventTargetHelper* deth = mEventTargetObjects.getFirst(); deth;
|
|
|
|
deth = deth->getNext()) {
|
|
|
|
targetList.AppendElement(const_cast<DOMEventTargetHelper*>(deth));
|
2018-02-21 21:53:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate the target list and call the function on each one.
|
2018-02-21 21:53:53 +03:00
|
|
|
bool done = false;
|
2018-02-21 21:53:22 +03:00
|
|
|
for (auto target : targetList) {
|
2018-03-30 21:25:48 +03:00
|
|
|
// Check to see if a previous iteration's callback triggered the removal
|
|
|
|
// of this target as a side-effect. If it did, then just ignore it.
|
2018-05-04 19:25:05 +03:00
|
|
|
if (target->GetOwnerGlobal() != this) {
|
2018-03-30 21:25:48 +03:00
|
|
|
continue;
|
|
|
|
}
|
2018-02-21 21:53:53 +03:00
|
|
|
aFunc(target, &done);
|
|
|
|
if (done) {
|
|
|
|
break;
|
|
|
|
}
|
2018-02-21 21:53:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsIGlobalObject::DisconnectEventTargetObjects() {
|
2018-02-21 21:53:53 +03:00
|
|
|
ForEachEventTargetObject([&](DOMEventTargetHelper* aTarget, bool* aDoneOut) {
|
2018-02-21 21:53:22 +03:00
|
|
|
aTarget->DisconnectFromOwner();
|
|
|
|
|
|
|
|
// Calling DisconnectFromOwner() should result in
|
|
|
|
// RemoveEventTargetObject() being called.
|
2018-05-04 19:25:05 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aTarget->GetOwnerGlobal() != this);
|
2018-02-21 21:53:22 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-12 04:46:08 +03:00
|
|
|
Maybe<ClientInfo> nsIGlobalObject::GetClientInfo() const {
|
|
|
|
// By default globals do not expose themselves as a client. Only real
|
|
|
|
// window and worker globals are currently considered clients.
|
|
|
|
return Maybe<ClientInfo>();
|
|
|
|
}
|
|
|
|
|
2020-02-06 16:45:47 +03:00
|
|
|
Maybe<nsID> nsIGlobalObject::GetAgentClusterId() const {
|
|
|
|
Maybe<ClientInfo> ci = GetClientInfo();
|
|
|
|
if (ci.isSome()) {
|
|
|
|
return ci.value().AgentClusterId();
|
|
|
|
}
|
2020-04-29 12:02:05 +03:00
|
|
|
return mozilla::Nothing();
|
2020-02-06 16:45:47 +03:00
|
|
|
}
|
|
|
|
|
2018-01-12 04:46:08 +03:00
|
|
|
Maybe<ServiceWorkerDescriptor> nsIGlobalObject::GetController() const {
|
|
|
|
// By default globals do not have a service worker controller. Only real
|
|
|
|
// window and worker globals can currently be controlled as a client.
|
|
|
|
return Maybe<ServiceWorkerDescriptor>();
|
|
|
|
}
|
2018-01-31 20:10:26 +03:00
|
|
|
|
|
|
|
RefPtr<ServiceWorker> nsIGlobalObject::GetOrCreateServiceWorker(
|
|
|
|
const ServiceWorkerDescriptor& aDescriptor) {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(false,
|
|
|
|
"this global should not have any service workers");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:44:18 +03:00
|
|
|
RefPtr<ServiceWorkerRegistration> nsIGlobalObject::GetServiceWorkerRegistration(
|
|
|
|
const mozilla::dom::ServiceWorkerRegistrationDescriptor& aDescriptor)
|
|
|
|
const {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(false,
|
|
|
|
"this global should not have any service workers");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-03-03 00:02:49 +03:00
|
|
|
RefPtr<ServiceWorkerRegistration>
|
|
|
|
nsIGlobalObject::GetOrCreateServiceWorkerRegistration(
|
|
|
|
const ServiceWorkerRegistrationDescriptor& aDescriptor) {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(
|
|
|
|
false, "this global should not have any service worker registrations");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-04-04 15:57:17 +03:00
|
|
|
nsPIDOMWindowInner* nsIGlobalObject::AsInnerWindow() {
|
|
|
|
if (MOZ_LIKELY(mIsInnerWindow)) {
|
|
|
|
return static_cast<nsPIDOMWindowInner*>(
|
|
|
|
static_cast<nsGlobalWindowInner*>(this));
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-02-21 21:53:22 +03:00
|
|
|
size_t nsIGlobalObject::ShallowSizeOfExcludingThis(MallocSizeOf aSizeOf) const {
|
|
|
|
size_t rtn = mHostObjectURIs.ShallowSizeOfExcludingThis(aSizeOf);
|
|
|
|
return rtn;
|
|
|
|
}
|
2019-05-24 11:27:03 +03:00
|
|
|
|
|
|
|
class QueuedMicrotask : public MicroTaskRunnable {
|
|
|
|
public:
|
|
|
|
QueuedMicrotask(nsIGlobalObject* aGlobal, VoidFunction& aCallback)
|
|
|
|
: mGlobal(aGlobal), mCallback(&aCallback) {}
|
|
|
|
|
|
|
|
MOZ_CAN_RUN_SCRIPT_BOUNDARY void Run(AutoSlowOperation& aAso) final {
|
|
|
|
IgnoredErrorResult rv;
|
|
|
|
MOZ_KnownLive(mCallback)->Call(static_cast<ErrorResult&>(rv));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Suppressed() final { return mGlobal->IsInSyncOperation(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIGlobalObject> mGlobal;
|
|
|
|
RefPtr<VoidFunction> mCallback;
|
|
|
|
};
|
|
|
|
|
|
|
|
void nsIGlobalObject::QueueMicrotask(VoidFunction& aCallback) {
|
|
|
|
CycleCollectedJSContext* context = CycleCollectedJSContext::Get();
|
|
|
|
if (context) {
|
|
|
|
RefPtr<MicroTaskRunnable> mt = new QueuedMicrotask(this, aCallback);
|
|
|
|
context->DispatchToMicroTask(mt.forget());
|
|
|
|
}
|
|
|
|
}
|
2020-04-18 15:25:57 +03:00
|
|
|
|
|
|
|
void nsIGlobalObject::RegisterReportingObserver(ReportingObserver* aObserver,
|
|
|
|
bool aBuffered) {
|
|
|
|
MOZ_ASSERT(aObserver);
|
|
|
|
|
|
|
|
if (mReportingObservers.Contains(aObserver)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-29 12:02:05 +03:00
|
|
|
if (NS_WARN_IF(
|
|
|
|
!mReportingObservers.AppendElement(aObserver, mozilla::fallible))) {
|
2020-04-18 15:25:57 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aBuffered) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Report* report : mReportRecords) {
|
|
|
|
aObserver->MaybeReport(report);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsIGlobalObject::UnregisterReportingObserver(
|
|
|
|
ReportingObserver* aObserver) {
|
|
|
|
MOZ_ASSERT(aObserver);
|
|
|
|
mReportingObservers.RemoveElement(aObserver);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsIGlobalObject::BroadcastReport(Report* aReport) {
|
|
|
|
MOZ_ASSERT(aReport);
|
|
|
|
|
|
|
|
for (ReportingObserver* observer : mReportingObservers) {
|
|
|
|
observer->MaybeReport(aReport);
|
|
|
|
}
|
|
|
|
|
2020-04-29 12:02:05 +03:00
|
|
|
if (NS_WARN_IF(!mReportRecords.AppendElement(aReport, mozilla::fallible))) {
|
2020-04-18 15:25:57 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (mReportRecords.Length() > MAX_REPORT_RECORDS) {
|
|
|
|
mReportRecords.RemoveElementAt(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsIGlobalObject::NotifyReportingObservers() {
|
|
|
|
const nsTArray<RefPtr<ReportingObserver>> reportingObservers(
|
|
|
|
mReportingObservers);
|
|
|
|
for (auto& observer : reportingObservers) {
|
|
|
|
// MOZ_KnownLive because 'reportingObservers' is guaranteed to
|
|
|
|
// keep it alive.
|
|
|
|
//
|
|
|
|
// This can go away once
|
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=1620312 is fixed.
|
|
|
|
MOZ_KnownLive(observer)->MaybeNotify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsIGlobalObject::RemoveReportRecords() {
|
|
|
|
mReportRecords.Clear();
|
|
|
|
|
|
|
|
for (auto& observer : mReportingObservers) {
|
|
|
|
observer->ForgetReports();
|
|
|
|
}
|
|
|
|
}
|