Bug 1492036 - Reporting API - part 8 - memory-pressure, r=smaug

This commit is contained in:
Andrea Marchesini 2018-11-14 20:02:34 +01:00
Родитель bbf68a5d96
Коммит 6183e66d60
6 изменённых файлов: 88 добавлений и 4 удалений

Просмотреть файл

@ -5672,6 +5672,7 @@ nsGlobalWindowInner::Observe(nsISupports* aSubject, const char* aTopic,
if (!nsCRT::strcmp(aTopic, MEMORY_PRESSURE_OBSERVER_TOPIC)) {
if (mPerformance) {
mPerformance->MemoryPressure();
mReportRecords.Clear();
}
return NS_OK;
}

Просмотреть файл

@ -14,7 +14,7 @@ namespace dom {
NS_IMPL_CYCLE_COLLECTION_CLASS(ReportingObserver)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ReportingObserver)
tmp->Disconnect();
tmp->Shutdown();
NS_IMPL_CYCLE_COLLECTION_UNLINK(mReports)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mCallback)
@ -32,8 +32,10 @@ NS_IMPL_CYCLE_COLLECTING_ADDREF(ReportingObserver)
NS_IMPL_CYCLE_COLLECTING_RELEASE(ReportingObserver)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ReportingObserver)
NS_INTERFACE_MAP_ENTRY(nsIObserver)
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIObserver)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
/* static */ already_AddRefed<ReportingObserver>
@ -52,6 +54,18 @@ ReportingObserver::Constructor(const GlobalObject& aGlobal,
RefPtr<ReportingObserver> ro =
new ReportingObserver(window, aCallback, types, aOptions.mBuffered);
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;
}
return ro.forget();
}
@ -68,8 +82,19 @@ ReportingObserver::ReportingObserver(nsPIDOMWindowInner* aWindow,
}
ReportingObserver::~ReportingObserver()
{
Shutdown();
}
void
ReportingObserver::Shutdown()
{
Disconnect();
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
if (obs) {
obs->RemoveObserver(this, "memory-pressure");
}
}
JSObject*
@ -159,5 +184,14 @@ ReportingObserver::MaybeNotify()
mCallback->Call(reports, *this);
}
NS_IMETHODIMP
ReportingObserver::Observe(nsISupports* aSubject, const char* aTopic,
const char16_t* aData)
{
MOZ_ASSERT(!strcmp(aTopic, "memory-pressure"));
mReports.Clear();
return NS_OK;
}
} // dom namespace
} // mozilla namespace

Просмотреть файл

@ -10,6 +10,8 @@
#include "mozilla/Attributes.h"
#include "mozilla/dom/BindingUtils.h"
#include "nsCycleCollectionParticipant.h"
#include "nsIObserver.h"
#include "nsWeakReference.h"
#include "nsWrapperCache.h"
#include "nsTArray.h"
@ -22,12 +24,15 @@ class Report;
class ReportingObserverCallback;
struct ReportingObserverOptions;
class ReportingObserver final : public nsISupports
class ReportingObserver final : public nsIObserver
, public nsWrapperCache
, public nsSupportsWeakReference
{
public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(ReportingObserver)
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(ReportingObserver,
nsIObserver)
NS_DECL_NSIOBSERVER
static already_AddRefed<ReportingObserver>
Constructor(const GlobalObject& aGlobal,
@ -67,6 +72,9 @@ public:
private:
~ReportingObserver();
void
Shutdown();
nsTArray<RefPtr<Report>> mReports;
nsCOMPtr<nsPIDOMWindowInner> mWindow;

Просмотреть файл

@ -0,0 +1,7 @@
"use strict";
module.exports = {
"extends": [
"plugin:mozilla/mochitest-test",
],
};

Просмотреть файл

@ -4,3 +4,4 @@ prefs =
dom.reporting.testing.enabled=true
[test_deprecated.html]
[test_memoryPressure.html]

Просмотреть файл

@ -0,0 +1,33 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test for ReportingObserver + memory-pressure</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<script type="application/javascript">
info("Testing TakeRecords() without memory-pressure");
let o = new ReportingObserver(() => {});
o.observe();
new TestingDeprecatedInterface();
let r = o.takeRecords();
is(r.length, 1, "We have 1 report");
r = o.takeRecords();
is(r.length, 0, "We have 0 reports after a takeRecords()");
info("Testing DeprecatedTestingMethod report");
new TestingDeprecatedInterface();
SpecialPowers.Services.obs.notifyObservers(null, "memory-pressure", "heap-minimize");
r = o.takeRecords();
is(r.length, 0, "We have 0 reports after a memory-pressure");
</script>
</body>
</html>