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 "components/password_manager/core/browser/affiliation_fetch_throttler.h
" | 5 #include "components/password_manager/core/browser/affiliation_fetch_throttler.h
" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
11 #include "base/thread_task_runner_handle.h" | 11 #include "base/thread_task_runner_handle.h" |
12 #include "base/time/tick_clock.h" | 12 #include "base/time/tick_clock.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 #include "components/password_manager/core/browser/affiliation_fetch_throttler_d
elegate.h" | 14 #include "components/password_manager/core/browser/affiliation_fetch_throttler_d
elegate.h" |
15 | 15 |
16 namespace password_manager { | 16 namespace password_manager { |
17 | 17 |
18 namespace { | |
19 | |
20 // Implementation of net::BackoffEntry that allows mocking its tick source. | |
21 class BackoffEntryImpl : public net::BackoffEntry { | |
22 public: | |
23 // |tick_clock| must outlive this instance. | |
24 explicit BackoffEntryImpl(const net::BackoffEntry::Policy* const policy, | |
25 base::TickClock* tick_clock) | |
26 : BackoffEntry(policy), tick_clock_(tick_clock) {} | |
27 ~BackoffEntryImpl() override {} | |
28 | |
29 private: | |
30 // net::BackoffEntry: | |
31 base::TimeTicks ImplGetTimeNow() const override { | |
32 return tick_clock_->NowTicks(); | |
33 } | |
34 | |
35 base::TickClock* tick_clock_; | |
36 | |
37 DISALLOW_COPY_AND_ASSIGN(BackoffEntryImpl); | |
38 }; | |
39 | |
40 } // namespace | |
41 | |
42 // static | 18 // static |
43 const net::BackoffEntry::Policy AffiliationFetchThrottler::kBackoffPolicy = { | 19 const net::BackoffEntry::Policy AffiliationFetchThrottler::kBackoffPolicy = { |
44 // Number of initial errors (in sequence) to ignore before going into | 20 // Number of initial errors (in sequence) to ignore before going into |
45 // exponential backoff. | 21 // exponential backoff. |
46 0, | 22 0, |
47 | 23 |
48 // Initial delay (in ms) once backoff starts. | 24 // Initial delay (in ms) once backoff starts. |
49 10 * 1000, // 10 seconds | 25 10 * 1000, // 10 seconds |
50 | 26 |
51 // Factor by which the delay will be multiplied on each subsequent failure. | 27 // Factor by which the delay will be multiplied on each subsequent failure. |
(...skipping 23 matching lines...) Expand all Loading... |
75 AffiliationFetchThrottler::AffiliationFetchThrottler( | 51 AffiliationFetchThrottler::AffiliationFetchThrottler( |
76 AffiliationFetchThrottlerDelegate* delegate, | 52 AffiliationFetchThrottlerDelegate* delegate, |
77 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 53 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
78 base::TickClock* tick_clock) | 54 base::TickClock* tick_clock) |
79 : delegate_(delegate), | 55 : delegate_(delegate), |
80 task_runner_(task_runner), | 56 task_runner_(task_runner), |
81 tick_clock_(tick_clock), | 57 tick_clock_(tick_clock), |
82 state_(IDLE), | 58 state_(IDLE), |
83 has_network_connectivity_(false), | 59 has_network_connectivity_(false), |
84 is_fetch_scheduled_(false), | 60 is_fetch_scheduled_(false), |
85 exponential_backoff_(new BackoffEntryImpl(&kBackoffPolicy, tick_clock_)), | 61 exponential_backoff_(new net::BackoffEntry(&kBackoffPolicy, tick_clock_)), |
86 weak_ptr_factory_(this) { | 62 weak_ptr_factory_(this) { |
87 DCHECK(delegate); | 63 DCHECK(delegate); |
88 // Start observing before querying the current connectivity state, so that if | 64 // Start observing before querying the current connectivity state, so that if |
89 // the state changes concurrently in-between, it will not go unnoticed. | 65 // the state changes concurrently in-between, it will not go unnoticed. |
90 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); | 66 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); |
91 has_network_connectivity_ = !net::NetworkChangeNotifier::IsOffline(); | 67 has_network_connectivity_ = !net::NetworkChangeNotifier::IsOffline(); |
92 } | 68 } |
93 | 69 |
94 AffiliationFetchThrottler::~AffiliationFetchThrottler() { | 70 AffiliationFetchThrottler::~AffiliationFetchThrottler() { |
95 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); | 71 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 (1 - base::RandDouble() * kBackoffPolicy.jitter_factor); | 134 (1 - base::RandDouble() * kBackoffPolicy.jitter_factor); |
159 exponential_backoff_->SetCustomReleaseTime(std::max( | 135 exponential_backoff_->SetCustomReleaseTime(std::max( |
160 exponential_backoff_->GetReleaseTime(), | 136 exponential_backoff_->GetReleaseTime(), |
161 tick_clock_->NowTicks() + base::TimeDelta::FromMillisecondsD(grace_ms))); | 137 tick_clock_->NowTicks() + base::TimeDelta::FromMillisecondsD(grace_ms))); |
162 | 138 |
163 if (state_ == FETCH_NEEDED) | 139 if (state_ == FETCH_NEEDED) |
164 EnsureCallbackIsScheduled(); | 140 EnsureCallbackIsScheduled(); |
165 } | 141 } |
166 | 142 |
167 } // namespace password_manager | 143 } // namespace password_manager |
OLD | NEW |