1 // Copyright 2013 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.
5 #include "content/browser/renderer_host/input/timeout_monitor.h"
7 #include "base/trace_event/trace_event.h"
10 using base::TimeTicks
;
14 TimeoutMonitor::TimeoutMonitor(const TimeoutHandler
& timeout_handler
)
15 : timeout_handler_(timeout_handler
) {
16 DCHECK(!timeout_handler_
.is_null());
19 TimeoutMonitor::~TimeoutMonitor() {
23 void TimeoutMonitor::Start(TimeDelta delay
) {
25 TRACE_EVENT_ASYNC_BEGIN0("renderer_host", "TimeoutMonitor", this);
26 TRACE_EVENT_INSTANT0("renderer_host", "TimeoutMonitor::Start",
27 TRACE_EVENT_SCOPE_THREAD
);
33 void TimeoutMonitor::Restart(TimeDelta delay
) {
39 TRACE_EVENT_INSTANT0("renderer_host", "TimeoutMonitor::Restart",
40 TRACE_EVENT_SCOPE_THREAD
);
41 // Setting to null will cause StartTimeoutMonitor to restart the timer.
42 time_when_considered_timed_out_
= TimeTicks();
46 void TimeoutMonitor::Stop() {
50 // We do not bother to stop the timeout_timer_ here in case it will be
51 // started again shortly, which happens to be the common use case.
52 TRACE_EVENT_INSTANT0("renderer_host", "TimeoutMonitor::Stop",
53 TRACE_EVENT_SCOPE_THREAD
);
54 TRACE_EVENT_ASYNC_END1("renderer_host", "TimeoutMonitor", this,
56 time_when_considered_timed_out_
= TimeTicks();
59 void TimeoutMonitor::StartImpl(base::TimeDelta delay
) {
60 // Set time_when_considered_timed_out_ if it's null. Also, update
61 // time_when_considered_timed_out_ if the caller's request is sooner than the
62 // existing one. This will have the side effect that the existing timeout will
64 TimeTicks requested_end_time
= TimeTicks::Now() + delay
;
65 if (time_when_considered_timed_out_
.is_null() ||
66 time_when_considered_timed_out_
> requested_end_time
)
67 time_when_considered_timed_out_
= requested_end_time
;
69 // If we already have a timer with the same or shorter duration, then we can
70 // wait for it to finish.
71 if (timeout_timer_
.IsRunning() && timeout_timer_
.GetCurrentDelay() <= delay
) {
72 // If time_when_considered_timed_out_ was null, this timer may fire early.
73 // CheckTimedOut handles that that by calling Start with the remaining time.
74 // If time_when_considered_timed_out_ was non-null, it means we still
75 // haven't been stopped, so we leave time_when_considered_timed_out_ as is.
79 // Either the timer is not yet running, or we need to adjust the timer to
81 time_when_considered_timed_out_
= requested_end_time
;
82 timeout_timer_
.Stop();
83 timeout_timer_
.Start(FROM_HERE
, delay
, this, &TimeoutMonitor::CheckTimedOut
);
86 void TimeoutMonitor::CheckTimedOut() {
87 // If we received a call to |Stop()|.
88 if (time_when_considered_timed_out_
.is_null())
91 // If we have not waited long enough, then wait some more.
92 TimeTicks now
= TimeTicks::Now();
93 if (now
< time_when_considered_timed_out_
) {
94 TRACE_EVENT_INSTANT0("renderer_host", "TimeoutMonitor::Reschedule",
95 TRACE_EVENT_SCOPE_THREAD
);
96 StartImpl(time_when_considered_timed_out_
- now
);
100 TRACE_EVENT_ASYNC_END1("renderer_host", "TimeoutMonitor", this,
101 "result", "timed_out");
102 TRACE_EVENT0("renderer_host", "TimeoutMonitor::TimeOutHandler");
103 time_when_considered_timed_out_
= TimeTicks();
104 timeout_handler_
.Run();
107 bool TimeoutMonitor::IsRunning() const {
108 return timeout_timer_
.IsRunning() &&
109 !time_when_considered_timed_out_
.is_null();
112 } // namespace content