mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-25 22:01:30 +00:00
9717c27492
Because DelayedRunnables are fire-and-forget, there is no way for a targeted EventTarget to clean them up on shutdown. Thus if a timer fires after EventTarget shutdown it will fail to dispatch the timer event, and avoid releasing the timer callback because it's not on the targeted thread. This causes a leak as there is a ref-cycle between nsTimerImpl::mCallback and DelayedRunnable::mTimer. This patch adds nsIDelayedRunnableObserver for a target to observe which DelayedRunnables are relying on their timer to run them. This allows the target to schedule a shutdown task to cancel those timers and release the runnables on the target thread. Supported DelayedRunnable targets with this patch are TaskQueues, eventqueue-based nsThreads and XPCOMThreadWrappers that wrap a supported nsThread. An assertion makes sure at runtime that future new uses of DelayedRunnable target nsIDelayedRunnableObserver-supported event targets. Differential Revision: https://phabricator.services.mozilla.com/D109781
74 lines
2.1 KiB
C++
74 lines
2.1 KiB
C++
/* -*- 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 "DelayedRunnable.h"
|
|
|
|
namespace mozilla {
|
|
|
|
DelayedRunnable::DelayedRunnable(already_AddRefed<nsIEventTarget> aTarget,
|
|
already_AddRefed<nsIRunnable> aRunnable,
|
|
uint32_t aDelay)
|
|
: mozilla::Runnable("DelayedRunnable"),
|
|
mTarget(aTarget),
|
|
mObserver(do_QueryInterface(mTarget)),
|
|
mWrappedRunnable(aRunnable),
|
|
mDelayedFrom(TimeStamp::NowLoRes()),
|
|
mDelay(aDelay) {
|
|
MOZ_DIAGNOSTIC_ASSERT(mObserver,
|
|
"Target must implement nsIDelayedRunnableObserver");
|
|
}
|
|
|
|
nsresult DelayedRunnable::Init() {
|
|
mObserver->OnDelayedRunnableCreated(this);
|
|
return NS_NewTimerWithCallback(getter_AddRefs(mTimer), this, mDelay,
|
|
nsITimer::TYPE_ONE_SHOT, mTarget);
|
|
}
|
|
|
|
void DelayedRunnable::CancelTimer() {
|
|
MOZ_ASSERT(mTarget->IsOnCurrentThread());
|
|
mTimer->Cancel();
|
|
}
|
|
|
|
NS_IMETHODIMP DelayedRunnable::Run() {
|
|
MOZ_ASSERT(mTimer, "DelayedRunnable without Init?");
|
|
|
|
// Already ran?
|
|
if (!mWrappedRunnable) {
|
|
return NS_OK;
|
|
}
|
|
|
|
// Are we too early?
|
|
if ((mozilla::TimeStamp::NowLoRes() - mDelayedFrom).ToMilliseconds() <
|
|
mDelay) {
|
|
if (mObserver) {
|
|
mObserver->OnDelayedRunnableScheduled(this);
|
|
}
|
|
return NS_OK; // Let the nsITimer run us.
|
|
}
|
|
|
|
mTimer->Cancel();
|
|
return DoRun();
|
|
}
|
|
|
|
NS_IMETHODIMP DelayedRunnable::Notify(nsITimer* aTimer) {
|
|
// If we already ran, the timer should have been canceled.
|
|
MOZ_ASSERT(mWrappedRunnable);
|
|
|
|
if (mObserver) {
|
|
mObserver->OnDelayedRunnableRan(this);
|
|
}
|
|
return DoRun();
|
|
}
|
|
|
|
nsresult DelayedRunnable::DoRun() {
|
|
nsCOMPtr<nsIRunnable> r = std::move(mWrappedRunnable);
|
|
return r->Run();
|
|
}
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(DelayedRunnable, Runnable, nsITimerCallback)
|
|
|
|
} // namespace mozilla
|