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"
|
2018-01-31 20:10:26 +03:00
|
|
|
|
2018-06-02 16:51:42 +03:00
|
|
|
#include "mozilla/dom/BlobURLProtocolHandler.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"
|
2014-01-07 22:53:31 +04:00
|
|
|
|
2018-02-21 21:53:22 +03:00
|
|
|
using mozilla::MallocSizeOf;
|
2018-01-12 04:46:08 +03:00
|
|
|
using mozilla::Maybe;
|
2018-02-21 21:53:22 +03:00
|
|
|
using mozilla::DOMEventTargetHelper;
|
2018-04-13 21:04:47 +03:00
|
|
|
using mozilla::dom::BlobURLProtocolHandler;
|
2018-01-12 04:46:08 +03:00
|
|
|
using mozilla::dom::ClientInfo;
|
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;
|
2018-01-12 04:46:08 +03:00
|
|
|
|
2015-05-07 10:05:43 +03:00
|
|
|
nsIGlobalObject::~nsIGlobalObject()
|
|
|
|
{
|
|
|
|
UnlinkHostObjectURIs();
|
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()
|
|
|
|
{
|
|
|
|
JSObject *global = GetGlobalJSObject();
|
|
|
|
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:
|
|
|
|
~UnlinkHostObjectURIsRunnable() {}
|
|
|
|
|
|
|
|
nsTArray<nsCString> mURIs;
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2015-05-07 10:05:43 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
nsIGlobalObject::UnlinkHostObjectURIs()
|
|
|
|
{
|
|
|
|
if (mHostObjectURIs.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
for (uint32_t index = 0; index < mHostObjectURIs.Length(); ++index) {
|
2018-06-02 16:51:42 +03:00
|
|
|
BlobURLProtocolHandler::RemoveDataEntry(mHostObjectURIs[index]);
|
2015-05-07 10:05:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mHostObjectURIs.Clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-02 16:51:42 +03:00
|
|
|
// BlobURLProtocolHandler is main-thread only.
|
2015-05-07 10:05:43 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<UnlinkHostObjectURIsRunnable> runnable =
|
2015-05-07 10:05:43 +03:00
|
|
|
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.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsIGlobalObject::TraverseHostObjectURIs(nsCycleCollectionTraversalCallback &aCb)
|
|
|
|
{
|
|
|
|
if (mHostObjectURIs.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t index = 0; index < mHostObjectURIs.Length(); ++index) {
|
2018-06-02 16:51:42 +03:00
|
|
|
BlobURLProtocolHandler::Traverse(mHostObjectURIs[index], aCb);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-02-21 21:53:53 +03:00
|
|
|
nsIGlobalObject::ForEachEventTargetObject(const std::function<void(DOMEventTargetHelper*, bool* aDoneOut)>& aFunc) const
|
2018-02-21 21:53:22 +03:00
|
|
|
{
|
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>();
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-21 21:53:22 +03:00
|
|
|
size_t
|
|
|
|
nsIGlobalObject::ShallowSizeOfExcludingThis(MallocSizeOf aSizeOf) const
|
|
|
|
{
|
|
|
|
size_t rtn = mHostObjectURIs.ShallowSizeOfExcludingThis(aSizeOf);
|
|
|
|
return rtn;
|
|
|
|
}
|