OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/host/backoff_timer.h" | 5 #include "remoting/host/backoff_timer.h" |
6 | 6 |
| 7 #include <utility> |
| 8 |
7 namespace remoting { | 9 namespace remoting { |
8 | 10 |
9 BackoffTimer::BackoffTimer() : timer_(new base::Timer(false, false)) { | 11 BackoffTimer::BackoffTimer() : timer_(new base::Timer(false, false)) {} |
10 } | |
11 | 12 |
12 BackoffTimer::~BackoffTimer() { | 13 BackoffTimer::~BackoffTimer() {} |
13 } | |
14 | 14 |
15 void BackoffTimer::Start(const tracked_objects::Location& posted_from, | 15 void BackoffTimer::Start(const tracked_objects::Location& posted_from, |
16 base::TimeDelta delay, | 16 base::TimeDelta delay, |
17 base::TimeDelta max_delay, | 17 base::TimeDelta max_delay, |
18 const base::Closure& user_task) { | 18 const base::Closure& user_task) { |
19 backoff_policy_.multiply_factor = 2; | 19 backoff_policy_.multiply_factor = 2; |
20 backoff_policy_.initial_delay_ms = delay.InMilliseconds(); | 20 backoff_policy_.initial_delay_ms = delay.InMilliseconds(); |
21 backoff_policy_.maximum_backoff_ms = max_delay.InMilliseconds(); | 21 backoff_policy_.maximum_backoff_ms = max_delay.InMilliseconds(); |
22 backoff_policy_.entry_lifetime_ms = -1; | 22 backoff_policy_.entry_lifetime_ms = -1; |
23 backoff_entry_.reset(new net::BackoffEntry(&backoff_policy_)); | 23 backoff_entry_.reset(new net::BackoffEntry(&backoff_policy_)); |
24 | 24 |
25 posted_from_ = posted_from; | 25 posted_from_ = posted_from; |
26 user_task_ = user_task; | 26 user_task_ = user_task; |
27 StartTimer(); | 27 StartTimer(); |
28 } | 28 } |
29 | 29 |
30 void BackoffTimer::Stop() { | 30 void BackoffTimer::Stop() { |
31 timer_->Stop(); | 31 timer_->Stop(); |
32 user_task_.Reset(); | 32 user_task_.Reset(); |
33 backoff_entry_.reset(); | 33 backoff_entry_.reset(); |
34 }; | 34 }; |
35 | 35 |
| 36 void BackoffTimer::SetTimerForTest(scoped_ptr<base::Timer> timer) { |
| 37 timer_ = std::move(timer); |
| 38 } |
| 39 |
36 void BackoffTimer::StartTimer() { | 40 void BackoffTimer::StartTimer() { |
37 timer_->Start( | 41 timer_->Start( |
38 posted_from_, backoff_entry_->GetTimeUntilRelease(), | 42 posted_from_, backoff_entry_->GetTimeUntilRelease(), |
39 base::Bind(&BackoffTimer::OnTimerFired, base::Unretained(this))); | 43 base::Bind(&BackoffTimer::OnTimerFired, base::Unretained(this))); |
40 } | 44 } |
41 | 45 |
42 void BackoffTimer::OnTimerFired() { | 46 void BackoffTimer::OnTimerFired() { |
43 DCHECK(IsRunning()); | 47 DCHECK(IsRunning()); |
44 DCHECK(!user_task_.is_null()); | 48 DCHECK(!user_task_.is_null()); |
45 backoff_entry_->InformOfRequest(false); | 49 backoff_entry_->InformOfRequest(false); |
46 StartTimer(); | 50 StartTimer(); |
47 | 51 |
48 // Running the user task may destroy this object, so don't reference | 52 // Running the user task may destroy this object, so don't reference |
49 // any fields of this object after running it. | 53 // any fields of this object after running it. |
50 base::Closure user_task(user_task_); | 54 base::Closure user_task(user_task_); |
51 user_task.Run(); | 55 user_task.Run(); |
52 } | 56 } |
53 | 57 |
54 } // namespace remoting | 58 } // namespace remoting |
OLD | NEW |