2017-08-07 08:50:47 +03:00
|
|
|
/* -*- 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/. */
|
|
|
|
|
|
|
|
#ifndef IdleTaskRunner_h
|
|
|
|
#define IdleTaskRunner_h
|
|
|
|
|
|
|
|
#include "mozilla/TimeStamp.h"
|
|
|
|
#include "mozilla/TaskCategory.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2017-07-12 04:23:15 +03:00
|
|
|
#include <functional>
|
2017-08-07 08:50:47 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2017-07-12 04:23:15 +03:00
|
|
|
// A general purpose repeating callback runner (it can be configured
|
|
|
|
// to a one-time runner, too.) If it is running repeatedly,
|
|
|
|
// one has to either explicitly Cancel() the runner or have
|
|
|
|
// MayContinueProcessing() callback return false to completely remove
|
|
|
|
// the runner.
|
2017-08-07 08:50:47 +03:00
|
|
|
class IdleTaskRunner final : public IdleRunnable
|
|
|
|
{
|
2017-07-12 04:23:15 +03:00
|
|
|
public:
|
|
|
|
// Return true if some meaningful work was done.
|
|
|
|
using CallbackType = std::function<bool(TimeStamp aDeadline)>;
|
|
|
|
|
|
|
|
// A callback for "stop processing" decision. Return true to
|
|
|
|
// stop processing. This can be an alternative to Cancel() or
|
|
|
|
// work together in different way.
|
|
|
|
using MayStopProcessingCallbackType = std::function<bool()>;
|
|
|
|
|
2017-08-07 08:50:47 +03:00
|
|
|
public:
|
|
|
|
static already_AddRefed<IdleTaskRunner>
|
2017-08-25 01:13:52 +03:00
|
|
|
Create(const CallbackType& aCallback,
|
|
|
|
const char* aRunnableName, uint32_t aDelay,
|
2017-07-12 04:23:15 +03:00
|
|
|
int64_t aBudget, bool aRepeating,
|
|
|
|
const MayStopProcessingCallbackType& aMayStopProcessing,
|
|
|
|
TaskCategory aTaskCategory = TaskCategory::Count);
|
2017-08-07 08:50:47 +03:00
|
|
|
|
|
|
|
NS_IMETHOD Run() override;
|
|
|
|
|
|
|
|
void SetDeadline(mozilla::TimeStamp aDeadline) override;
|
|
|
|
void SetTimer(uint32_t aDelay, nsIEventTarget* aTarget) override;
|
|
|
|
|
|
|
|
nsresult Cancel() override;
|
|
|
|
void Schedule(bool aAllowIdleDispatch);
|
|
|
|
|
|
|
|
private:
|
2017-07-12 04:23:15 +03:00
|
|
|
explicit IdleTaskRunner(const CallbackType& aCallback,
|
2017-08-25 01:13:52 +03:00
|
|
|
const char* aRunnableName,
|
2017-08-07 08:50:47 +03:00
|
|
|
uint32_t aDelay, int64_t aBudget,
|
2017-07-12 04:23:15 +03:00
|
|
|
bool aRepeating,
|
|
|
|
const MayStopProcessingCallbackType& aMayStopProcessing,
|
|
|
|
TaskCategory aTaskCategory);
|
2017-08-07 08:50:47 +03:00
|
|
|
~IdleTaskRunner();
|
|
|
|
void CancelTimer();
|
|
|
|
void SetTimerInternal(uint32_t aDelay);
|
|
|
|
|
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
|
|
|
nsCOMPtr<nsITimer> mScheduleTimer;
|
2017-07-12 04:23:15 +03:00
|
|
|
CallbackType mCallback;
|
2017-08-07 08:50:47 +03:00
|
|
|
uint32_t mDelay;
|
|
|
|
TimeStamp mDeadline;
|
|
|
|
TimeDuration mBudget;
|
|
|
|
bool mRepeating;
|
|
|
|
bool mTimerActive;
|
2017-07-12 04:23:15 +03:00
|
|
|
MayStopProcessingCallbackType mMayStopProcessing;
|
|
|
|
TaskCategory mTaskCategory;
|
2017-08-07 08:50:47 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end of namespace mozilla.
|
|
|
|
|
|
|
|
#endif
|