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 #ifndef IdleTaskRunner_h
8 #define IdleTaskRunner_h
10 #include "mozilla/TimeStamp.h"
11 #include "mozilla/TaskCategory.h"
12 #include "nsThreadUtils.h"
17 // A general purpose repeating callback runner (it can be configured
18 // to a one-time runner, too.) If it is running repeatedly,
19 // one has to either explicitly Cancel() the runner or have
20 // MayContinueProcessing() callback return false to completely remove
22 class IdleTaskRunner final
: public IdleRunnable
{
24 // Return true if some meaningful work was done.
25 using CallbackType
= std::function
<bool(TimeStamp aDeadline
)>;
27 // A callback for "stop processing" decision. Return true to
28 // stop processing. This can be an alternative to Cancel() or
29 // work together in different way.
30 using MayStopProcessingCallbackType
= std::function
<bool()>;
33 static already_AddRefed
<IdleTaskRunner
> Create(
34 const CallbackType
& aCallback
, const char* aRunnableName
, uint32_t aDelay
,
35 int64_t aBudget
, bool aRepeating
,
36 const MayStopProcessingCallbackType
& aMayStopProcessing
,
37 TaskCategory aTaskCategory
= TaskCategory::Count
);
39 NS_IMETHOD
Run() override
;
41 void SetDeadline(mozilla::TimeStamp aDeadline
) override
;
42 void SetTimer(uint32_t aDelay
, nsIEventTarget
* aTarget
) override
;
44 nsresult
Cancel() override
;
45 void Schedule(bool aAllowIdleDispatch
);
48 explicit IdleTaskRunner(
49 const CallbackType
& aCallback
, const char* aRunnableName
, uint32_t aDelay
,
50 int64_t aBudget
, bool aRepeating
,
51 const MayStopProcessingCallbackType
& aMayStopProcessing
,
52 TaskCategory aTaskCategory
);
55 void SetTimerInternal(uint32_t aDelay
);
57 nsCOMPtr
<nsITimer
> mTimer
;
58 nsCOMPtr
<nsITimer
> mScheduleTimer
;
59 CallbackType mCallback
;
65 MayStopProcessingCallbackType mMayStopProcessing
;
66 const TaskCategory mTaskCategory
;
70 } // end of namespace mozilla.