Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 #ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_FETCH_THROTTLER_H_ | |
| 6 #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_FETCH_THROTTLER_H_ | |
| 7 | |
| 8 #include "base/macros.h" | |
| 9 #include "base/memory/ref_counted.h" | |
| 10 #include "base/memory/weak_ptr.h" | |
| 11 #include "net/base/backoff_entry.h" | |
|
mmenke
2015/01/14 19:25:07
Think we can forward declare this?
engedy
2015/01/15 19:16:42
Done.
| |
| 12 #include "net/base/network_change_notifier.h" | |
| 13 | |
| 14 namespace base { | |
| 15 class TickClock; | |
| 16 class SingleThreadTaskRunner; | |
| 17 } | |
| 18 | |
| 19 namespace password_manager { | |
| 20 | |
| 21 // A delegate to receive a callback once it is okay to issue a network request. | |
|
mmenke
2015/01/14 19:25:07
This needs clearer documentation, since it's the f
engedy
2015/01/15 19:16:42
I have moved the delegate to its own file.
| |
| 22 class AffiliationFetchThrottlerDelegate { | |
|
mmenke
2015/01/14 19:25:07
option: Believe it's currently recommended these
engedy
2015/01/15 19:16:42
Done.
| |
| 23 public: | |
| 24 // Will be called once the throttling policy allows issuing a network request, | |
| 25 // provided there has been at least one SignalNetworkRequestNeeded() call | |
| 26 // since the last request. The implementor should return whether a request was | |
|
mmenke
2015/01/14 19:25:07
nit: implementor -> implemention
Should avoid am
engedy
2015/01/15 19:16:42
Done.
| |
| 27 // actually initiated, and if so, should call InformOfNetworkRequestComplete() | |
|
mmenke
2015/01/14 19:25:07
nit: should -> must (Maybe I've been reading too
engedy
2015/01/15 19:16:42
Done.
| |
| 28 // when the request completes. | |
| 29 virtual bool OnCanSendNetworkRequest() = 0; | |
| 30 | |
| 31 protected: | |
| 32 virtual ~AffiliationFetchThrottlerDelegate() {} | |
| 33 }; | |
| 34 | |
| 35 // Implements the throttling logic that AffiliationBackend will use when it | |
| 36 // needs to issue requests to fetch affiliation information over the network. | |
| 37 // | |
| 38 // Essentially, the logic implements exponential backoff on network and server | |
| 39 // errors, plus that no requests will be issued in the first place while there | |
| 40 // is known to be no network connectivity. | |
| 41 class AffiliationFetchThrottler | |
| 42 : public net::NetworkChangeNotifier::ConnectionTypeObserver { | |
|
mmenke
2015/01/14 19:25:07
I wonder about whether we really should be using N
engedy
2015/01/15 19:16:42
Yes, even as a signal that is known to produce fal
| |
| 43 public: | |
| 44 // Creates an instance that will use |tick_clock| as its tick source, and will | |
| 45 // post to |task_runner| to call the |delegate|'s OnSendNetworkRequest(). | |
| 46 AffiliationFetchThrottler( | |
| 47 AffiliationFetchThrottlerDelegate* delegate, | |
| 48 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
| 49 scoped_ptr<base::TickClock> tick_clock); | |
| 50 ~AffiliationFetchThrottler() override; | |
| 51 | |
| 52 // Signals to the throttling logic that a network request is needed, and that | |
| 53 // OnCanSendNetworkRequest() should be called as soon as the request can be | |
| 54 // sent. Calls to this method will be ignored while a request is in flight. | |
| 55 void SignalNetworkRequestNeeded(); | |
| 56 | |
| 57 // Informs the back-off logic that a network request has been completed, with | |
| 58 // either |success| or not. | |
| 59 void InformOfNetworkRequestComplete(bool success); | |
| 60 | |
| 61 protected: | |
| 62 enum State { | |
| 63 IDLE, | |
| 64 FETCH_NEEDED, | |
| 65 FETCH_IN_FLIGHT | |
| 66 }; | |
| 67 | |
| 68 // Ensures that OnBackoffDelayExpiredCallback() is scheduled to be called back | |
| 69 // once the |exponential_backoff_| delay expires. | |
| 70 void EnsureCallbackIsScheduled(); | |
| 71 | |
| 72 // Called back when the |exponential_backoff_| delay expires. | |
| 73 void OnBackoffDelayExpiredCallback(); | |
| 74 | |
| 75 // net::NetworkChangeNotifier::ConnectionTypeObserver: | |
| 76 virtual void OnConnectionTypeChanged( | |
|
mmenke
2015/01/14 19:25:07
nit: --virtual
engedy
2015/01/15 19:16:42
Done.
| |
| 77 net::NetworkChangeNotifier::ConnectionType type) override; | |
| 78 | |
| 79 private: | |
| 80 AffiliationFetchThrottlerDelegate* delegate_; | |
| 81 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
| 82 | |
| 83 State state_; | |
| 84 bool has_network_connectivity_; | |
| 85 bool is_fetch_scheduled_; | |
| 86 scoped_ptr<base::TickClock> tick_clock_; | |
| 87 scoped_ptr<net::BackoffEntry> exponential_backoff_; | |
|
mmenke
2015/01/14 19:25:07
Need to include scoped_ptr.h
engedy
2015/01/15 19:16:42
Done.
| |
| 88 base::WeakPtrFactory<AffiliationFetchThrottler> weak_ptr_factory_; | |
| 89 | |
| 90 DISALLOW_COPY_AND_ASSIGN(AffiliationFetchThrottler); | |
| 91 }; | |
| 92 | |
| 93 } // namespace password_manager | |
| 94 | |
| 95 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_FETCH_THROTTLER_ H_ | |
| OLD | NEW |