1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "DelayedRunnable.h"
9 #include "mozilla/ProfilerRunnable.h"
13 DelayedRunnable::DelayedRunnable(already_AddRefed
<nsIEventTarget
> aTarget
,
14 already_AddRefed
<nsIRunnable
> aRunnable
,
16 : mozilla::Runnable("DelayedRunnable"),
18 mObserver(do_QueryInterface(mTarget
)),
19 mWrappedRunnable(aRunnable
),
20 mDelayedFrom(TimeStamp::NowLoRes()),
22 MOZ_DIAGNOSTIC_ASSERT(mObserver
,
23 "Target must implement nsIDelayedRunnableObserver");
26 nsresult
DelayedRunnable::Init() {
27 mObserver
->OnDelayedRunnableCreated(this);
28 return NS_NewTimerWithCallback(getter_AddRefs(mTimer
), this, mDelay
,
29 nsITimer::TYPE_ONE_SHOT
, mTarget
);
32 void DelayedRunnable::CancelTimer() {
33 MOZ_ASSERT(mTarget
->IsOnCurrentThread());
37 NS_IMETHODIMP
DelayedRunnable::Run() {
38 MOZ_ASSERT(mTimer
, "DelayedRunnable without Init?");
41 if (!mWrappedRunnable
) {
46 if ((mozilla::TimeStamp::NowLoRes() - mDelayedFrom
).ToMilliseconds() <
49 mObserver
->OnDelayedRunnableScheduled(this);
51 return NS_OK
; // Let the nsITimer run us.
58 NS_IMETHODIMP
DelayedRunnable::Notify(nsITimer
* aTimer
) {
59 // If we already ran, the timer should have been canceled.
60 MOZ_ASSERT(mWrappedRunnable
);
63 mObserver
->OnDelayedRunnableRan(this);
68 nsresult
DelayedRunnable::DoRun() {
69 nsCOMPtr
<nsIRunnable
> r
= std::move(mWrappedRunnable
);
70 AUTO_PROFILE_FOLLOWING_RUNNABLE(r
);
74 NS_IMPL_ISUPPORTS_INHERITED(DelayedRunnable
, Runnable
, nsITimerCallback
)
76 } // namespace mozilla