Bug 1586807 - Make pseudoclass locking work with Fission. r=pbro
[gecko.git] / xpcom / threads / IdleTaskRunner.h
blobb400e19778acc201a74ef188db754ec628074e39
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"
13 #include <functional>
15 namespace mozilla {
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
21 // the runner.
22 class IdleTaskRunner final : public IdleRunnable {
23 public:
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()>;
32 public:
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);
47 private:
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);
53 ~IdleTaskRunner();
54 void CancelTimer();
55 void SetTimerInternal(uint32_t aDelay);
57 nsCOMPtr<nsITimer> mTimer;
58 nsCOMPtr<nsITimer> mScheduleTimer;
59 CallbackType mCallback;
60 uint32_t mDelay;
61 TimeStamp mDeadline;
62 TimeDuration mBudget;
63 bool mRepeating;
64 bool mTimerActive;
65 MayStopProcessingCallbackType mMayStopProcessing;
66 const TaskCategory mTaskCategory;
67 const char* mName;
70 } // end of namespace mozilla.
72 #endif