diff --git a/dom/base/nsGlobalWindow.cpp b/dom/base/nsGlobalWindow.cpp index c5ed38dc27..744b3ab8d3 100644 --- a/dom/base/nsGlobalWindow.cpp +++ b/dom/base/nsGlobalWindow.cpp @@ -318,6 +318,8 @@ nsGlobalWindow::DOMMinTimeoutValue() const { // nsTimerImpl.h for details. #define DOM_MAX_TIMEOUT_VALUE DELAY_INTERVAL_LIMIT +#define MEMORY_PRESSURE_OBSERVER_TOPIC "memory-pressure" + // The interval at which we execute idle callbacks static uint32_t gThrottledIdlePeriodLength; @@ -1525,6 +1527,8 @@ nsGlobalWindow::nsGlobalWindow(nsGlobalWindow *aOuterWindow) // Watch for dom-storage2-changed so we can fire storage // events. Use a strong reference. os->AddObserver(mObserver, "dom-storage2-changed", false); + + os->AddObserver(mObserver, MEMORY_PRESSURE_OBSERVER_TOPIC, false); } Preferences::AddStrongObserver(mObserver, "intl.accept_languages"); @@ -1840,6 +1844,7 @@ nsGlobalWindow::CleanUp() if (os) { os->RemoveObserver(mObserver, NS_IOSERVICE_OFFLINE_STATUS_TOPIC); os->RemoveObserver(mObserver, "dom-storage2-changed"); + os->RemoveObserver(mObserver, MEMORY_PRESSURE_OBSERVER_TOPIC); } if (mIdleService) { @@ -11702,6 +11707,13 @@ nsGlobalWindow::Observe(nsISupports* aSubject, const char* aTopic, return NS_OK; } + if (!nsCRT::strcmp(aTopic, MEMORY_PRESSURE_OBSERVER_TOPIC)) { + if (mPerformance) { + mPerformance->MemoryPressure(); + } + return NS_OK; + } + if (!nsCRT::strcmp(aTopic, OBSERVER_TOPIC_IDLE)) { mCurrentlyIdle = true; if (IsFrozen()) { diff --git a/dom/performance/Performance.cpp b/dom/performance/Performance.cpp index 9a1d134626..09c38fef42 100755 --- a/dom/performance/Performance.cpp +++ b/dom/performance/Performance.cpp @@ -262,11 +262,6 @@ already_AddRefed Performance::Mark( const PerformanceMarkOptions& aMarkOptions, ErrorResult& aRv) { - // Don't add the entry if the buffer is full. XXX should be removed by bug 1159003. - if (mUserEntries.Length() >= mResourceTimingBufferSize) { - return nullptr; - } - nsCOMPtr parent = GetParentObject(); if (!parent || parent->IsDying() || !parent->GetGlobalJSObject()) { aRv.Throw(NS_ERROR_DOM_UT_UNAVAILABLE_GLOBAL_OBJECT); @@ -490,12 +485,6 @@ Performance::Measure(JSContext* aCx, const Optional& aEndMark, ErrorResult& aRv) { - // Don't add the entry if the buffer is full. XXX should be removed by bug - // 1159003. - if (mUserEntries.Length() >= mResourceTimingBufferSize) { - return nullptr; - } - const PerformanceMeasureOptions* options = nullptr; if (aStartOrMeasureOptions.IsPerformanceMeasureOptions()) { options = &aStartOrMeasureOptions.GetAsPerformanceMeasureOptions(); @@ -553,7 +542,7 @@ Performance::Measure(JSContext* aCx, } RefPtr performanceMeasure = new PerformanceMeasure( - GetAsISupports(), aName, startTime, endTime, detail); + GetParentObject(), aName, startTime, endTime, detail); InsertUserEntry(performanceMeasure); return performanceMeasure.forget(); @@ -753,5 +742,11 @@ Performance::IsObserverEnabled(JSContext* aCx, JSObject* aGlobal) return runnable->Dispatch() && runnable->IsEnabled(); } +void +Performance::MemoryPressure() +{ + mUserEntries.Clear(); +} + } // dom namespace } // mozilla namespace diff --git a/dom/performance/Performance.h b/dom/performance/Performance.h index 2108e1e394..a8ec1c484c 100644 --- a/dom/performance/Performance.h +++ b/dom/performance/Performance.h @@ -124,6 +124,8 @@ public: return false; } + void MemoryPressure(); + protected: Performance(); explicit Performance(nsPIDOMWindowInner* aWindow); @@ -136,8 +138,6 @@ protected: void ClearUserEntries(const Optional& aEntryName, const nsAString& aEntryType); - virtual nsISupports* GetAsISupports() = 0; - virtual void DispatchBufferFullEvent() = 0; virtual TimeStamp CreationTimeStamp() const = 0; @@ -167,11 +167,14 @@ protected: nsTObserverArray mObservers; protected: - nsTArray> mUserEntries; - nsTArray> mResourceEntries; + static const uint64_t kDefaultResourceTimingBufferSize = 1500; + + // When kDefaultResourceTimingBufferSize is increased or removed, these should + // be changed to use SegmentedVector + AutoTArray, kDefaultResourceTimingBufferSize> mUserEntries; + AutoTArray, kDefaultResourceTimingBufferSize> mResourceEntries; uint64_t mResourceTimingBufferSize; - static const uint64_t kDefaultResourceTimingBufferSize = 1500; bool mPendingNotificationObserversTask; RefPtr mPerformanceService; diff --git a/dom/performance/PerformanceMainThread.h b/dom/performance/PerformanceMainThread.h index 702483e9de..735522c502 100644 --- a/dom/performance/PerformanceMainThread.h +++ b/dom/performance/PerformanceMainThread.h @@ -65,11 +65,6 @@ public: protected: ~PerformanceMainThread(); - nsISupports* GetAsISupports() override - { - return this; - } - void InsertUserEntry(PerformanceEntry* aEntry) override; DOMHighResTimeStamp diff --git a/dom/performance/PerformanceWorker.h b/dom/performance/PerformanceWorker.h index 3604e6874f..8dcbf1f202 100644 --- a/dom/performance/PerformanceWorker.h +++ b/dom/performance/PerformanceWorker.h @@ -65,11 +65,6 @@ public: protected: ~PerformanceWorker(); - nsISupports* GetAsISupports() override - { - return nullptr; - } - void InsertUserEntry(PerformanceEntry* aEntry) override; void DispatchBufferFullEvent() override diff --git a/dom/workers/WorkerPrivate.cpp b/dom/workers/WorkerPrivate.cpp index a7a4929f3b..29c2263cd1 100644 --- a/dom/workers/WorkerPrivate.cpp +++ b/dom/workers/WorkerPrivate.cpp @@ -6441,14 +6441,23 @@ WorkerPrivate::MemoryPressureInternal() { AssertIsOnWorkerThread(); - RefPtr console = mScope ? mScope->GetConsoleIfExists() : nullptr; - if (console) { - console->ClearStorage(); + if (mScope) { + RefPtr console = mScope->GetConsoleIfExists(); + if (console) { + console->ClearStorage(); + } + + RefPtr performance = mScope->GetPerformanceIfExists(); + if (performance) { + performance->MemoryPressure(); + } } - console = mDebuggerScope ? mDebuggerScope->GetConsoleIfExists() : nullptr; - if (console) { - console->ClearStorage(); + if (mDebuggerScope) { + RefPtr console = mDebuggerScope->GetConsoleIfExists(); + if (console) { + console->ClearStorage(); + } } for (uint32_t index = 0; index < mChildWorkers.Length(); index++) { diff --git a/dom/workers/WorkerScope.h b/dom/workers/WorkerScope.h index 01bae5cc11..8e886f6cb2 100644 --- a/dom/workers/WorkerScope.h +++ b/dom/workers/WorkerScope.h @@ -157,6 +157,11 @@ public: Performance* GetPerformance(); + Performance* GetPerformanceIfExists() const + { + return mPerformance; + } + already_AddRefed Fetch(const RequestOrUSVString& aInput, const RequestInit& aInit, ErrorResult& aRv);