Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(81)

Side by Side Diff: components/password_manager/core/browser/affiliation_fetch_throttler.h

Issue 807503002: Implement throttling logic for fetching affiliation information. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@aff_database
Patch Set: Fix ambiguity with pow() on Android. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
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/gtest_prod_util.h"
9 #include "base/macros.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "net/base/backoff_entry.h"
14 #include "net/base/network_change_notifier.h"
15
16 namespace base {
17 class TickClock;
18 class SingleThreadTaskRunner;
19 } // namespace base
20
21 namespace password_manager {
22
23 class AffiliationFetchThrottlerDelegate;
24
25 // Implements the throttling logic that the AffiliationBackend will use when it
26 // needs to issue requests over the network to fetch affiliation information.
27 //
28 // This class manages only the scheduling of the requests. It is up to the
29 // consumer (the AffiliationBackend) to actually assemble and send the requests,
30 // to report back about their success or failure, and to retry them if desired.
31 // The process goes like this:
32 // 1.) The consumer calls SignalNetworkRequestNeeded().
33 // 2.) Once appropriate, OnCanSendNetworkRequest() is called on the delegate.
34 // 3.) The consumer sends the request, and waits until it completes.
35 // 4.) The consumer calls InformOfNetworkRequestComplete().
36 // Note that only a single request at a time is supported.
37 //
38 // If the request fails in Step 3, the consumer should not automatically retry
39 // it. Instead it should always proceed to Step 4, and then -- if retrying the
40 // request is desired -- proceed immediately to Step 1. That is, it should act
41 // as if another request was needed right away.
42 //
43 // Essentially, this class implements exponential backoff in case of network and
44 // server errors with the additional constraint that no requests will be issued
45 // in the first place while there is known to be no network connectivity. This
46 // prevents the exponential backoff delay from growing huge during long offline
47 // periods, so that requests will not be held back for too long after
48 // connectivity is restored.
49 class AffiliationFetchThrottler
50 : public net::NetworkChangeNotifier::ConnectionTypeObserver {
51 public:
52 // Creates an instance that will use |tick_clock| as its tick source, and will
53 // post to |task_runner| to call the |delegate|'s OnSendNetworkRequest(). The
54 // |delegate| should outlive the throttler.
55 AffiliationFetchThrottler(
56 AffiliationFetchThrottlerDelegate* delegate,
57 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
58 scoped_ptr<base::TickClock> tick_clock);
59 ~AffiliationFetchThrottler() override;
60
61 // Signals to the throttling logic that a network request is needed, and that
62 // OnCanSendNetworkRequest() should be called as soon as the request can be
63 // sent. OnCanSendNetworkRequest() will always be called asynchronously.
64 //
65 // Calls to this method will be ignored when a request is already known to be
66 // needed or while a request is in flight. To signal that another request will
67 // be needed right away after the current one, call this method after calling
68 // InformOfNetworkRequestComplete().
69 void SignalNetworkRequestNeeded();
70
71 // Informs the back-off logic that the in-flight network request has been
72 // completed, either with |success| or not.
73 void InformOfNetworkRequestComplete(bool success);
74
75 private:
76 FRIEND_TEST_ALL_PREFIXES(AffiliationFetchThrottlerTest, FailedRequests);
77 FRIEND_TEST_ALL_PREFIXES(AffiliationFetchThrottlerTest,
78 GracePeriodAfterConnectivityIsRestored);
79 FRIEND_TEST_ALL_PREFIXES(AffiliationFetchThrottlerTest,
80 GracePeriodAfterConnectivityIsRestored2);
81 FRIEND_TEST_ALL_PREFIXES(AffiliationFetchThrottlerTest,
82 GracePeriodAfterConnectivityIsRestored3);
83 FRIEND_TEST_ALL_PREFIXES(AffiliationFetchThrottlerTest,
84 ConnectivityLostDuringBackoff);
85 FRIEND_TEST_ALL_PREFIXES(AffiliationFetchThrottlerTest,
86 ConnectivityLostAndRestoredDuringBackoff);
87 FRIEND_TEST_ALL_PREFIXES(AffiliationFetchThrottlerTest, FlakyConnectivity);
88 FRIEND_TEST_ALL_PREFIXES(AffiliationFetchThrottlerTest,
89 ConnectivityLostDuringRequest);
90 FRIEND_TEST_ALL_PREFIXES(AffiliationFetchThrottlerTest,
91 ConnectivityLostAndRestoredDuringRequest);
92 FRIEND_TEST_ALL_PREFIXES(AffiliationFetchThrottlerTest,
93 ConnectivityLostAndRestoredDuringRequest2);
94
95 enum State { IDLE, FETCH_NEEDED, FETCH_IN_FLIGHT };
96
97 // Exponential backoff parameters in case of network and server errors
98 static const net::BackoffEntry::Policy kBackoffPolicy;
99
100 // Minimum delay before sending the first request once network connectivity is
101 // restored. The fuzzing factor in |kBackoffParameters.jitter_factor| applies.
102 static const int64_t kGracePeriodAfterReconnectMs;
103
104 // Ensures that OnBackoffDelayExpiredCallback() is scheduled to be called back
105 // once the |exponential_backoff_| delay expires.
106 void EnsureCallbackIsScheduled();
107
108 // Called back when the |exponential_backoff_| delay expires.
109 void OnBackoffDelayExpiredCallback();
110
111 // net::NetworkChangeNotifier::ConnectionTypeObserver:
112 void OnConnectionTypeChanged(
113 net::NetworkChangeNotifier::ConnectionType type) override;
114
115 AffiliationFetchThrottlerDelegate* delegate_;
116 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
117 State state_;
118 bool has_network_connectivity_;
119 bool is_fetch_scheduled_;
120 scoped_ptr<base::TickClock> tick_clock_;
121 scoped_ptr<net::BackoffEntry> exponential_backoff_;
122
123 base::WeakPtrFactory<AffiliationFetchThrottler> weak_ptr_factory_;
124
125 DISALLOW_COPY_AND_ASSIGN(AffiliationFetchThrottler);
126 };
127
128 } // namespace password_manager
129
130 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_FETCH_THROTTLER_ H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698