Bug 1345464 - Add an optional EventTarget to nsExpirationTracker to support Labeling for Quantum-DOM. r=froydnj

--HG--
extra : rebase_source : f05d3a31543ea8ed35a756a1721adc272c792a08
This commit is contained in:
Bevis Tseng 2017-03-30 09:23:36 -04:00
Родитель 87fae2d807
Коммит 5daae11f33
1 изменённых файлов: 24 добавлений и 4 удалений

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

@ -98,16 +98,29 @@ public:
* provided by the tracker are defined in terms of this period. If the * provided by the tracker are defined in terms of this period. If the
* period is zero, then we don't use a timer and rely on someone calling * period is zero, then we don't use a timer and rely on someone calling
* AgeOneGenerationLocked explicitly. * AgeOneGenerationLocked explicitly.
* @param aName the name of the subclass for telemetry.
* @param aEventTarget the optional event target on main thread to label the
* runnable of the asynchronous invocation to NotifyExpired().
*/ */
ExpirationTrackerImpl(uint32_t aTimerPeriod, const char* aName) ExpirationTrackerImpl(uint32_t aTimerPeriod,
const char* aName,
nsIEventTarget* aEventTarget = nullptr)
: mTimerPeriod(aTimerPeriod) : mTimerPeriod(aTimerPeriod)
, mNewestGeneration(0) , mNewestGeneration(0)
, mInAgeOneGeneration(false) , mInAgeOneGeneration(false)
, mName(aName) , mName(aName)
, mEventTarget(aEventTarget)
{ {
static_assert(K >= 2 && K <= nsExpirationState::NOT_TRACKED, static_assert(K >= 2 && K <= nsExpirationState::NOT_TRACKED,
"Unsupported number of generations (must be 2 <= K <= 15)"); "Unsupported number of generations (must be 2 <= K <= 15)");
MOZ_ASSERT(NS_IsMainThread()); MOZ_ASSERT(NS_IsMainThread());
if (mEventTarget) {
bool current = false;
MOZ_RELEASE_ASSERT(
NS_SUCCEEDED(mEventTarget->IsOnCurrentThread(&current)) && current,
"Provided event target must be on the main thread");
}
mObserver = new ExpirationTrackerObserver(); mObserver = new ExpirationTrackerObserver();
mObserver->Init(this); mObserver->Init(this);
} }
@ -334,6 +347,7 @@ private:
uint32_t mNewestGeneration; uint32_t mNewestGeneration;
bool mInAgeOneGeneration; bool mInAgeOneGeneration;
const char* const mName; // Used for timer firing profiling. const char* const mName; // Used for timer firing profiling.
const nsCOMPtr<nsIEventTarget> mEventTarget;
/** /**
* Whenever "memory-pressure" is observed, it calls AgeAllGenerationsLocked() * Whenever "memory-pressure" is observed, it calls AgeAllGenerationsLocked()
@ -394,7 +408,9 @@ private:
if (!mTimer) { if (!mTimer) {
return NS_ERROR_OUT_OF_MEMORY; return NS_ERROR_OUT_OF_MEMORY;
} }
if (!NS_IsMainThread()) { if (mEventTarget) {
mTimer->SetTarget(mEventTarget);
} else if (!NS_IsMainThread()) {
// TimerCallback should always be run on the main thread to prevent races // TimerCallback should always be run on the main thread to prevent races
// to the destruction of the tracker. // to the destruction of the tracker.
nsCOMPtr<nsIEventTarget> target = do_GetMainThread(); nsCOMPtr<nsIEventTarget> target = do_GetMainThread();
@ -454,8 +470,12 @@ protected:
virtual void NotifyExpired(T* aObj) = 0; virtual void NotifyExpired(T* aObj) = 0;
public: public:
nsExpirationTracker(uint32_t aTimerPeriod, const char* aName) nsExpirationTracker(uint32_t aTimerPeriod,
: ::detail::SingleThreadedExpirationTracker<T, K>(aTimerPeriod, aName) const char* aName,
nsIEventTarget* aEventTarget = nullptr)
: ::detail::SingleThreadedExpirationTracker<T, K>(aTimerPeriod,
aName,
aEventTarget)
{ } { }
virtual ~nsExpirationTracker() virtual ~nsExpirationTracker()