Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(56)

Unified Diff: components/timers/alarm_timer_chromeos.h

Issue 2398753003: Use FileDescriptorWatcher in AlarmTimer. (Closed)
Patch Set: fix build error Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | components/timers/alarm_timer_chromeos.cc » ('j') | components/timers/alarm_timer_chromeos.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/timers/alarm_timer_chromeos.h
diff --git a/components/timers/alarm_timer_chromeos.h b/components/timers/alarm_timer_chromeos.h
index 313c9f9a936173b6c27e444d4b73f1730ca1c67a..d861aeeda0d926328b919cd35469035ae514dd14 100644
--- a/components/timers/alarm_timer_chromeos.h
+++ b/components/timers/alarm_timer_chromeos.h
@@ -7,85 +7,69 @@
#include <memory>
-#include "base/callback.h"
+#include "base/files/file_descriptor_watcher_posix.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
+#include "base/threading/sequenced_task_runner_handle.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
namespace base {
-class MessageLoop;
struct PendingTask;
}
namespace timers {
// The class implements a timer that is capable of waking the system up from a
-// suspended state. For example, this is useful for running tasks that are
+// suspended state. For example, this is useful for running tasks that are
// needed for maintaining network connectivity, like sending heartbeat messages.
// Currently, this feature is only available on Chrome OS systems running linux
-// version 3.11 or higher. On all other platforms, the AlarmTimer behaves
+// version 3.11 or higher. On all other platforms, the AlarmTimer behaves
// exactly the same way as a regular Timer.
+//
+// An AlarmTimer instance can only be used from the sequence on which it was
+// instantiated. Start() and Stop() must be called from a thread that supports
+// FileDescriptorWatcher.
class AlarmTimer : public base::Timer {
public:
~AlarmTimer() override;
- bool can_wake_from_suspend() const { return can_wake_from_suspend_; }
-
- // Sets a hook that will be called when the timer fires and a task has been
- // queued on |origin_message_loop_|. Used by tests to wait until a task is
- // pending in the MessageLoop.
- void SetTimerFiredCallbackForTest(base::Closure test_callback);
-
// Timer overrides.
void Stop() override;
void Reset() override;
protected:
- // The constructors for this class are protected because consumers should
- // instantiate one of the specialized sub-classes defined below instead.
AlarmTimer(bool retain_user_task, bool is_repeating);
- AlarmTimer(const tracked_objects::Location& posted_from,
- base::TimeDelta delay,
- const base::Closure& user_task,
- bool is_repeating);
private:
- // Common initialization that must be performed by both constructors. This
- // really should live in a delegated constructor but the way base::Timer's
- // constructors are written makes it really hard to do so.
- void Init();
+ // Called when |alarm_fd_| is readable without blocking. Reads data from
+ // |alarm_fd_| and calls OnTimerFired().
+ void OnAlarmFdReadableWithoutBlocking();
- // Will be called by the delegate to indicate that the timer has fired and
- // that the user task should be run.
+ // Called when the timer fires. Runs the callback.
void OnTimerFired();
- // Called when |origin_message_loop_| will be destroyed.
- void WillDestroyCurrentMessageLoop();
-
- // Delegate that will manage actually setting the timer.
- class Delegate;
- scoped_refptr<Delegate> delegate_;
+ // Tracks whether the timer has the ability to wake the system up from
+ // suspend. This is a runtime check because we won't know if the system
+ // supports being woken up from suspend until the constructor actually tries
+ // to set it up.
+ bool CanWakeFromSuspend() const;
- // Keeps track of the user task we want to run. A new one is constructed
- // every time Reset() is called.
- std::unique_ptr<base::PendingTask> pending_task_;
+ // Timer file descriptor.
+ const int alarm_fd_;
- // Tracks whether the timer has the ability to wake the system up from
- // suspend. This is a runtime check because we won't know if the system
- // supports being woken up from suspend until the delegate actually tries to
- // set it up.
- bool can_wake_from_suspend_;
+ // Watches |alarm_fd_|.
+ std::unique_ptr<base::FileDescriptorWatcher::Controller> alarm_fd_watcher_;
- // Pointer to the message loop that started the timer. Used to track the
- // destruction of that message loop.
- base::MessageLoop* origin_message_loop_;
+ // Posts tasks to the sequence on which this AlarmTimer was instantiated.
+ const scoped_refptr<base::SequencedTaskRunner> origin_task_runner_ =
+ base::SequencedTaskRunnerHandle::Get();
- // Observes |origin_message_loop_| and informs this class if it will be
- // destroyed.
- class MessageLoopObserver;
- std::unique_ptr<MessageLoopObserver> message_loop_observer_;
+ // Keeps track of the user task we want to run. A new one is constructed every
+ // time Reset() is called.
+ std::unique_ptr<base::PendingTask> pending_task_;
+ // Used to invalidate pending callbacks.
base::WeakPtrFactory<AlarmTimer> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(AlarmTimer);
@@ -96,8 +80,6 @@ class AlarmTimer : public base::Timer {
// repeat. Useful for fire-and-forget tasks.
class OneShotAlarmTimer : public AlarmTimer {
public:
- // Constructs a basic OneShotAlarmTimer. An AlarmTimer constructed this way
- // requires that Start() is called before Reset() is called.
OneShotAlarmTimer();
~OneShotAlarmTimer() override;
};
@@ -108,18 +90,7 @@ class OneShotAlarmTimer : public AlarmTimer {
// after it fires.
class RepeatingAlarmTimer : public AlarmTimer {
public:
- // Constructs a basic RepeatingAlarmTimer. An AlarmTimer constructed this way
- // requires that Start() is called before Reset() is called.
RepeatingAlarmTimer();
-
- // Constructs a RepeatingAlarmTimer with pre-populated parameters but does not
- // start it. Useful if |user_task| or |delay| are not going to change.
- // Reset() can be called immediately after constructing an AlarmTimer in this
- // way.
- RepeatingAlarmTimer(const tracked_objects::Location& posted_from,
- base::TimeDelta delay,
- const base::Closure& user_task);
-
~RepeatingAlarmTimer() override;
};
@@ -128,18 +99,7 @@ class RepeatingAlarmTimer : public AlarmTimer {
// times but not at a regular interval.
class SimpleAlarmTimer : public AlarmTimer {
public:
- // Constructs a basic SimpleAlarmTimer. An AlarmTimer constructed this way
- // requires that Start() is called before Reset() is called.
SimpleAlarmTimer();
-
- // Constructs a SimpleAlarmTimer with pre-populated parameters but does not
- // start it. Useful if |user_task| or |delay| are not going to change.
- // Reset() can be called immediately after constructing an AlarmTimer in this
- // way.
- SimpleAlarmTimer(const tracked_objects::Location& posted_from,
- base::TimeDelta delay,
- const base::Closure& user_task);
-
~SimpleAlarmTimer() override;
};
« no previous file with comments | « no previous file | components/timers/alarm_timer_chromeos.cc » ('j') | components/timers/alarm_timer_chromeos.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698