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

Side by Side Diff: components/timers/alarm_timer.cc

Issue 641943002: components: Introduce AlarmTimer class and use it for GCM heartbeat (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix build error in mcs_probe Created 6 years, 1 month 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 unified diff | Download patch
« no previous file with comments | « components/timers/alarm_timer.h ('k') | components/timers/alarm_timer_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/timers/alarm_timer.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/files/file_util.h"
10 #include "base/logging.h"
11 #include "base/pending_task.h"
12 #include "components/timers/rtc_alarm.h"
13
14 namespace timers {
15
16 AlarmTimer::AlarmTimer(bool retain_user_task, bool is_repeating)
17 : base::Timer(retain_user_task, is_repeating),
18 delegate_(new RtcAlarm()),
19 can_wake_from_suspend_(false),
20 origin_message_loop_(NULL),
21 weak_factory_(this) {
22 can_wake_from_suspend_ = delegate_->Init(weak_factory_.GetWeakPtr());
23 }
24
25 AlarmTimer::AlarmTimer(const tracked_objects::Location& posted_from,
26 base::TimeDelta delay,
27 const base::Closure& user_task,
28 bool is_repeating)
29 : base::Timer(posted_from, delay, user_task, is_repeating),
30 delegate_(new RtcAlarm()),
31 can_wake_from_suspend_(false),
32 origin_message_loop_(NULL),
33 weak_factory_(this) {
34 can_wake_from_suspend_ = delegate_->Init(weak_factory_.GetWeakPtr());
35 }
36
37 AlarmTimer::~AlarmTimer() {
38 Stop();
39 }
40
41 void AlarmTimer::Stop() {
42 if (!can_wake_from_suspend_) {
43 base::Timer::Stop();
44 return;
45 }
46
47 // Clear the running flag, stop the delegate, and delete the pending task.
48 base::Timer::set_is_running(false);
49 delegate_->Stop();
50 pending_task_.reset();
51
52 // Stop is called when the AlarmTimer is destroyed so we need to remove
53 // ourselves as a MessageLoop::DestructionObserver to prevent a segfault
54 // later.
55 if (origin_message_loop_) {
56 origin_message_loop_->RemoveDestructionObserver(this);
57 origin_message_loop_ = NULL;
58 }
59
60 if (!base::Timer::retain_user_task())
61 base::Timer::set_user_task(base::Closure());
62 }
63
64 void AlarmTimer::Reset() {
65 if (!can_wake_from_suspend_) {
66 base::Timer::Reset();
67 return;
68 }
69
70 DCHECK(!base::Timer::user_task().is_null());
71 DCHECK(!origin_message_loop_ ||
72 origin_message_loop_->task_runner()->RunsTasksOnCurrentThread());
73
74 // Make sure that the timer will stop if the underlying message loop is
75 // destroyed.
76 if (!origin_message_loop_) {
77 origin_message_loop_ = base::MessageLoop::current();
78 origin_message_loop_->AddDestructionObserver(this);
79 }
80
81 // Set up the pending task.
82 if (base::Timer::GetCurrentDelay() > base::TimeDelta::FromMicroseconds(0)) {
83 base::Timer::set_desired_run_time(
84 base::TimeTicks::Now() + base::Timer::GetCurrentDelay());
85 pending_task_.reset(new base::PendingTask(base::Timer::posted_from(),
86 base::Timer::user_task(),
87 base::Timer::desired_run_time(),
88 true /* nestable */));
89 } else {
90 base::Timer::set_desired_run_time(base::TimeTicks());
91 pending_task_.reset(new base::PendingTask(base::Timer::posted_from(),
92 base::Timer::user_task()));
93 }
94 base::MessageLoop::current()->task_annotator()->DidQueueTask(
95 "AlarmTimer::Reset", *pending_task_);
96
97 // Now start up the timer.
98 delegate_->Reset(base::Timer::GetCurrentDelay());
99 base::Timer::set_is_running(true);
100 }
101
102 void AlarmTimer::WillDestroyCurrentMessageLoop() {
103 Stop();
104 }
105
106 void AlarmTimer::OnTimerFired() {
107 if (!base::Timer::is_running())
108 return;
109
110 DCHECK(pending_task_.get());
111
112 // Take ownership of the pending user task, which is going to be cleared by
113 // the Stop() or Reset() functions below.
114 scoped_ptr<base::PendingTask> pending_user_task(pending_task_.Pass());
115
116 // Re-schedule or stop the timer as requested.
117 if (base::Timer::is_repeating())
118 Reset();
119 else
120 Stop();
121
122 // Now run the user task.
123 base::MessageLoop::current()->task_annotator()->RunTask(
124 "AlarmTimer::Reset", "AlarmTimer::OnTimerFired", *pending_user_task);
125 }
126
127 } // namespace timers
OLDNEW
« no previous file with comments | « components/timers/alarm_timer.h ('k') | components/timers/alarm_timer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698