summaryrefslogtreecommitdiffstats
path: root/components/timers/alarm_timer.cc
blob: 8b46020af5496fe191e405218098defaf1166a75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/timers/alarm_timer.h"

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/pending_task.h"
#include "components/timers/rtc_alarm.h"

namespace timers {

AlarmTimer::AlarmTimer(bool retain_user_task, bool is_repeating)
    : base::Timer(retain_user_task, is_repeating),
      delegate_(new RtcAlarm()),
      can_wake_from_suspend_(false),
      origin_message_loop_(NULL),
      weak_factory_(this) {
  can_wake_from_suspend_ = delegate_->Init(weak_factory_.GetWeakPtr());
}

AlarmTimer::AlarmTimer(const tracked_objects::Location& posted_from,
                       base::TimeDelta delay,
                       const base::Closure& user_task,
                       bool is_repeating)
    : base::Timer(posted_from, delay, user_task, is_repeating),
      delegate_(new RtcAlarm()),
      can_wake_from_suspend_(false),
      origin_message_loop_(NULL),
      weak_factory_(this) {
  can_wake_from_suspend_ = delegate_->Init(weak_factory_.GetWeakPtr());
}

AlarmTimer::~AlarmTimer() {
  Stop();
}

void AlarmTimer::OnTimerFired() {
  if (!base::Timer::IsRunning())
    return;

  DCHECK(pending_task_.get());

  // Take ownership of the pending user task, which is going to be cleared by
  // the Stop() or Reset() functions below.
  scoped_ptr<base::PendingTask> pending_user_task(pending_task_.Pass());

  // Re-schedule or stop the timer as requested.
  if (base::Timer::is_repeating())
    Reset();
  else
    Stop();

  // Now run the user task.
  base::MessageLoop::current()->task_annotator()->RunTask(
      "AlarmTimer::Reset", "AlarmTimer::OnTimerFired", *pending_user_task);
}

void AlarmTimer::Stop() {
  if (!can_wake_from_suspend_) {
    base::Timer::Stop();
    return;
  }

  // Clear the running flag, stop the delegate, and delete the pending task.
  base::Timer::set_is_running(false);
  delegate_->Stop();
  pending_task_.reset();

  // Stop is called when the AlarmTimer is destroyed so we need to remove
  // ourselves as a MessageLoop::DestructionObserver to prevent a segfault
  // later.
  if (origin_message_loop_) {
    origin_message_loop_->RemoveDestructionObserver(this);
    origin_message_loop_ = NULL;
  }

  if (!base::Timer::retain_user_task())
    base::Timer::set_user_task(base::Closure());
}

void AlarmTimer::Reset() {
  if (!can_wake_from_suspend_) {
    base::Timer::Reset();
    return;
  }

  DCHECK(!base::Timer::user_task().is_null());
  DCHECK(!origin_message_loop_ ||
         origin_message_loop_->task_runner()->RunsTasksOnCurrentThread());

  // Make sure that the timer will stop if the underlying message loop is
  // destroyed.
  if (!origin_message_loop_) {
    origin_message_loop_ = base::MessageLoop::current();
    origin_message_loop_->AddDestructionObserver(this);
  }

  // Set up the pending task.
  if (base::Timer::GetCurrentDelay() > base::TimeDelta::FromMicroseconds(0)) {
    base::Timer::set_desired_run_time(
        base::TimeTicks::Now() + base::Timer::GetCurrentDelay());
    pending_task_.reset(new base::PendingTask(base::Timer::posted_from(),
                                              base::Timer::user_task(),
                                              base::Timer::desired_run_time(),
                                              true  /* nestable */));
  } else {
    base::Timer::set_desired_run_time(base::TimeTicks());
    pending_task_.reset(new base::PendingTask(base::Timer::posted_from(),
                                              base::Timer::user_task()));
  }
  base::MessageLoop::current()->task_annotator()->DidQueueTask(
      "AlarmTimer::Reset", *pending_task_);

  // Now start up the timer.
  delegate_->Reset(base::Timer::GetCurrentDelay());
  base::Timer::set_is_running(true);
}

void AlarmTimer::WillDestroyCurrentMessageLoop() {
  Stop();
}

}  // namespace timers