2018-11-14 22:02:32 +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/. */
|
|
|
|
|
|
|
|
#include "mozilla/dom/ReportingObserver.h"
|
|
|
|
#include "mozilla/dom/ReportingBinding.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsGlobalWindowInner.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(ReportingObserver)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ReportingObserver)
|
2018-11-14 22:02:34 +03:00
|
|
|
tmp->Shutdown();
|
2018-11-14 22:02:33 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mReports)
|
2018-11-14 22:02:32 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(ReportingObserver)
|
2018-11-14 22:02:33 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mReports)
|
2018-11-14 22:02:32 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(ReportingObserver)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(ReportingObserver)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(ReportingObserver)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ReportingObserver)
|
2018-11-14 22:02:34 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIObserver)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIObserver)
|
2018-11-14 22:02:32 +03:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<ReportingObserver> ReportingObserver::Constructor(
|
|
|
|
const GlobalObject& aGlobal, ReportingObserverCallback& aCallback,
|
|
|
|
const ReportingObserverOptions& aOptions, ErrorResult& aRv) {
|
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window =
|
|
|
|
do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
MOZ_ASSERT(window);
|
|
|
|
|
|
|
|
nsTArray<nsString> types;
|
|
|
|
if (aOptions.mTypes.WasPassed()) {
|
|
|
|
types = aOptions.mTypes.Value();
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<ReportingObserver> ro =
|
|
|
|
new ReportingObserver(window, aCallback, types, aOptions.mBuffered);
|
2018-11-14 22:02:34 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (NS_WARN_IF(!obs)) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
aRv = obs->AddObserver(ro, "memory-pressure", true);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-11-14 22:02:32 +03:00
|
|
|
return ro.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
ReportingObserver::ReportingObserver(nsPIDOMWindowInner* aWindow,
|
|
|
|
ReportingObserverCallback& aCallback,
|
|
|
|
const nsTArray<nsString>& aTypes,
|
|
|
|
bool aBuffered)
|
|
|
|
: mWindow(aWindow),
|
|
|
|
mCallback(&aCallback),
|
|
|
|
mTypes(aTypes),
|
|
|
|
mBuffered(aBuffered) {
|
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
}
|
|
|
|
|
|
|
|
ReportingObserver::~ReportingObserver() { Shutdown(); }
|
2018-11-14 22:02:34 +03:00
|
|
|
|
|
|
|
void ReportingObserver::Shutdown() {
|
2018-11-14 22:02:32 +03:00
|
|
|
Disconnect();
|
2018-11-14 22:02:34 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (obs) {
|
|
|
|
obs->RemoveObserver(this, "memory-pressure");
|
|
|
|
}
|
2018-11-14 22:02:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* ReportingObserver::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
|
|
|
return ReportingObserver_Binding::Wrap(aCx, this, aGivenProto);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReportingObserver::Observe() {
|
2018-11-14 22:02:33 +03:00
|
|
|
mWindow->RegisterReportingObserver(this, mBuffered);
|
2018-11-14 22:02:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ReportingObserver::Disconnect() {
|
2018-11-14 22:02:33 +03:00
|
|
|
if (mWindow) {
|
|
|
|
mWindow->UnregisterReportingObserver(this);
|
|
|
|
}
|
2018-11-14 22:02:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ReportingObserver::TakeRecords(nsTArray<RefPtr<Report>>& aRecords) {
|
2018-11-14 22:02:33 +03:00
|
|
|
mReports.SwapElements(aRecords);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReportingObserver::MaybeReport(Report* aReport) {
|
|
|
|
MOZ_ASSERT(aReport);
|
|
|
|
|
|
|
|
if (!mTypes.IsEmpty()) {
|
|
|
|
nsAutoString type;
|
|
|
|
aReport->GetType(type);
|
|
|
|
|
|
|
|
if (!mTypes.Contains(type)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool wasEmpty = mReports.IsEmpty();
|
|
|
|
|
|
|
|
RefPtr<Report> report = aReport->Clone();
|
|
|
|
MOZ_ASSERT(report);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!mReports.AppendElement(report, fallible))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!wasEmpty) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = mWindow;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
|
|
|
|
"ReportingObserver::MaybeReport",
|
|
|
|
[window]() { window->NotifyReportingObservers(); });
|
|
|
|
|
|
|
|
NS_DispatchToCurrentThread(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReportingObserver::MaybeNotify() {
|
|
|
|
if (mReports.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's take the ownership of the reports.
|
|
|
|
nsTArray<RefPtr<Report>> list;
|
|
|
|
list.SwapElements(mReports);
|
|
|
|
|
|
|
|
Sequence<OwningNonNull<Report>> reports;
|
|
|
|
for (Report* report : list) {
|
|
|
|
if (NS_WARN_IF(!reports.AppendElement(*report, fallible))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should report if this throws exception. But where?
|
|
|
|
mCallback->Call(reports, *this);
|
2018-11-14 22:02:32 +03:00
|
|
|
}
|
|
|
|
|
2018-11-14 22:02:34 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ReportingObserver::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* aData) {
|
|
|
|
MOZ_ASSERT(!strcmp(aTopic, "memory-pressure"));
|
|
|
|
mReports.Clear();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-11-14 22:02:32 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|