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

Side by Side Diff: components/password_manager/core/browser/affiliation_backend.cc

Issue 1008373003: Integrate throttling logic into AffiliationBackend. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comment. Created 5 years, 9 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
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_backend.h" 5 #include "components/password_manager/core/browser/affiliation_backend.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/location.h" 10 #include "base/location.h"
11 #include "base/single_thread_task_runner.h" 11 #include "base/single_thread_task_runner.h"
12 #include "base/task_runner.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "base/threading/thread_checker.h" 12 #include "base/threading/thread_checker.h"
15 #include "base/time/clock.h" 13 #include "base/time/clock.h"
14 #include "base/time/tick_clock.h"
16 #include "base/time/time.h" 15 #include "base/time/time.h"
17 #include "components/password_manager/core/browser/affiliation_database.h" 16 #include "components/password_manager/core/browser/affiliation_database.h"
17 #include "components/password_manager/core/browser/affiliation_fetch_throttler.h "
18 #include "components/password_manager/core/browser/affiliation_fetcher.h" 18 #include "components/password_manager/core/browser/affiliation_fetcher.h"
19 #include "components/password_manager/core/browser/facet_manager.h" 19 #include "components/password_manager/core/browser/facet_manager.h"
20 #include "net/url_request/url_request_context_getter.h" 20 #include "net/url_request/url_request_context_getter.h"
21 21
22 namespace password_manager { 22 namespace password_manager {
23 23
24 AffiliationBackend::AffiliationBackend( 24 AffiliationBackend::AffiliationBackend(
25 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, 25 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter,
26 scoped_ptr<base::Clock> time_source) 26 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
27 scoped_ptr<base::Clock> time_source,
28 scoped_ptr<base::TickClock> time_tick_source)
27 : request_context_getter_(request_context_getter), 29 : request_context_getter_(request_context_getter),
30 task_runner_(task_runner),
28 clock_(time_source.Pass()), 31 clock_(time_source.Pass()),
32 tick_clock_(time_tick_source.Pass()),
29 weak_ptr_factory_(this) { 33 weak_ptr_factory_(this) {
30 DCHECK_LT(base::Time(), clock_->Now()); 34 DCHECK_LT(base::Time(), clock_->Now());
31 } 35 }
32 36
33 AffiliationBackend::~AffiliationBackend() { 37 AffiliationBackend::~AffiliationBackend() {
34 } 38 }
35 39
36 void AffiliationBackend::Initialize(const base::FilePath& db_path) { 40 void AffiliationBackend::Initialize(const base::FilePath& db_path) {
37 thread_checker_.reset(new base::ThreadChecker); 41 thread_checker_.reset(new base::ThreadChecker);
42
43 DCHECK(!throttler_);
44 throttler_.reset(
45 new AffiliationFetchThrottler(this, task_runner_, tick_clock_.get()));
46
38 cache_.reset(new AffiliationDatabase()); 47 cache_.reset(new AffiliationDatabase());
39 if (!cache_->Init(db_path)) { 48 if (!cache_->Init(db_path)) {
40 // TODO(engedy): Implement this. crbug.com/437865. 49 // TODO(engedy): Implement this. crbug.com/437865.
41 NOTIMPLEMENTED(); 50 NOTIMPLEMENTED();
42 } 51 }
43 } 52 }
44 53
45 void AffiliationBackend::GetAffiliations( 54 void AffiliationBackend::GetAffiliations(
46 const FacetURI& facet_uri, 55 const FacetURI& facet_uri,
47 bool cached_only, 56 bool cached_only,
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 FacetManager* AffiliationBackend::GetOrCreateFacetManager( 101 FacetManager* AffiliationBackend::GetOrCreateFacetManager(
93 const FacetURI& facet_uri) { 102 const FacetURI& facet_uri) {
94 if (!facet_managers_.contains(facet_uri)) { 103 if (!facet_managers_.contains(facet_uri)) {
95 scoped_ptr<FacetManager> new_manager( 104 scoped_ptr<FacetManager> new_manager(
96 new FacetManager(facet_uri, this, clock_.get())); 105 new FacetManager(facet_uri, this, clock_.get()));
97 facet_managers_.add(facet_uri, new_manager.Pass()); 106 facet_managers_.add(facet_uri, new_manager.Pass());
98 } 107 }
99 return facet_managers_.get(facet_uri); 108 return facet_managers_.get(facet_uri);
100 } 109 }
101 110
102 void AffiliationBackend::SendNetworkRequest() {
103 DCHECK(!fetcher_);
104
105 std::vector<FacetURI> requested_facet_uris;
106 for (const auto& facet_manager_pair : facet_managers_) {
107 if (facet_manager_pair.second->DoesRequireFetch())
108 requested_facet_uris.push_back(facet_manager_pair.first);
109 }
110 DCHECK(!requested_facet_uris.empty());
111 fetcher_.reset(AffiliationFetcher::Create(request_context_getter_.get(),
112 requested_facet_uris, this));
113 fetcher_->StartRequest();
114 }
115
116 void AffiliationBackend::OnSendNotification(const FacetURI& facet_uri) { 111 void AffiliationBackend::OnSendNotification(const FacetURI& facet_uri) {
117 DCHECK(thread_checker_ && thread_checker_->CalledOnValidThread()); 112 DCHECK(thread_checker_ && thread_checker_->CalledOnValidThread());
118 113
119 FacetManager* facet_manager = facet_managers_.get(facet_uri); 114 FacetManager* facet_manager = facet_managers_.get(facet_uri);
120 if (!facet_manager) 115 if (!facet_manager)
121 return; 116 return;
122 facet_manager->NotifyAtRequestedTime(); 117 facet_manager->NotifyAtRequestedTime();
123 118
124 if (facet_manager->CanBeDiscarded()) 119 if (facet_manager->CanBeDiscarded())
125 facet_managers_.erase(facet_uri); 120 facet_managers_.erase(facet_uri);
126 } 121 }
127 122
128 bool AffiliationBackend::ReadAffiliationsFromDatabase( 123 bool AffiliationBackend::ReadAffiliationsFromDatabase(
129 const FacetURI& facet_uri, 124 const FacetURI& facet_uri,
130 AffiliatedFacetsWithUpdateTime* affiliations) { 125 AffiliatedFacetsWithUpdateTime* affiliations) {
131 return cache_->GetAffiliationsForFacet(facet_uri, affiliations); 126 return cache_->GetAffiliationsForFacet(facet_uri, affiliations);
132 } 127 }
133 128
134 void AffiliationBackend::SignalNeedNetworkRequest() { 129 void AffiliationBackend::SignalNeedNetworkRequest() {
135 // TODO(engedy): Add more sophisticated throttling logic. crbug.com/437865. 130 throttler_->SignalNetworkRequestNeeded();
136 if (fetcher_)
137 return;
138 SendNetworkRequest();
139 } 131 }
140 132
141 void AffiliationBackend::RequestNotificationAtTime(const FacetURI& facet_uri, 133 void AffiliationBackend::RequestNotificationAtTime(const FacetURI& facet_uri,
142 base::Time time) { 134 base::Time time) {
143 // TODO(engedy): Avoid spamming the task runner; only ever schedule the first 135 // TODO(engedy): Avoid spamming the task runner; only ever schedule the first
144 // callback. crbug.com/437865. 136 // callback. crbug.com/437865.
145 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 137 task_runner_->PostDelayedTask(
146 FROM_HERE, base::Bind(&AffiliationBackend::OnSendNotification, 138 FROM_HERE, base::Bind(&AffiliationBackend::OnSendNotification,
147 weak_ptr_factory_.GetWeakPtr(), facet_uri), 139 weak_ptr_factory_.GetWeakPtr(), facet_uri),
148 time - clock_->Now()); 140 time - clock_->Now());
149 } 141 }
150 142
151 void AffiliationBackend::OnFetchSucceeded( 143 void AffiliationBackend::OnFetchSucceeded(
152 scoped_ptr<AffiliationFetcherDelegate::Result> result) { 144 scoped_ptr<AffiliationFetcherDelegate::Result> result) {
153 DCHECK(thread_checker_ && thread_checker_->CalledOnValidThread()); 145 DCHECK(thread_checker_ && thread_checker_->CalledOnValidThread());
146
154 fetcher_.reset(); 147 fetcher_.reset();
148 throttler_->InformOfNetworkRequestComplete(true);
155 149
156 for (const AffiliatedFacets& affiliated_facets : *result) { 150 for (const AffiliatedFacets& affiliated_facets : *result) {
157 AffiliatedFacetsWithUpdateTime affiliation; 151 AffiliatedFacetsWithUpdateTime affiliation;
158 affiliation.facets = affiliated_facets; 152 affiliation.facets = affiliated_facets;
159 affiliation.last_update_time = clock_->Now(); 153 affiliation.last_update_time = clock_->Now();
160 154
161 std::vector<AffiliatedFacetsWithUpdateTime> obsoleted_affiliations; 155 std::vector<AffiliatedFacetsWithUpdateTime> obsoleted_affiliations;
162 cache_->StoreAndRemoveConflicting(affiliation, &obsoleted_affiliations); 156 cache_->StoreAndRemoveConflicting(affiliation, &obsoleted_affiliations);
163 157
164 // Cached data in contradiction with newly stored data automatically gets 158 // Cached data in contradiction with newly stored data automatically gets
(...skipping 10 matching lines...) Expand all
175 facet_manager->OnFetchSucceeded(affiliation); 169 facet_manager->OnFetchSucceeded(affiliation);
176 if (facet_manager->CanBeDiscarded()) 170 if (facet_manager->CanBeDiscarded())
177 facet_managers_.erase(facet_uri); 171 facet_managers_.erase(facet_uri);
178 } 172 }
179 } 173 }
180 174
181 // A subsequent fetch may be needed if any additional GetAffiliations() 175 // A subsequent fetch may be needed if any additional GetAffiliations()
182 // requests came in while the current fetch was in flight. 176 // requests came in while the current fetch was in flight.
183 for (const auto& facet_manager_pair : facet_managers_) { 177 for (const auto& facet_manager_pair : facet_managers_) {
184 if (facet_manager_pair.second->DoesRequireFetch()) { 178 if (facet_manager_pair.second->DoesRequireFetch()) {
185 SendNetworkRequest(); 179 throttler_->SignalNetworkRequestNeeded();
186 return; 180 return;
187 } 181 }
188 } 182 }
189 } 183 }
190 184
191 void AffiliationBackend::OnFetchFailed() { 185 void AffiliationBackend::OnFetchFailed() {
192 DCHECK(thread_checker_ && thread_checker_->CalledOnValidThread()); 186 DCHECK(thread_checker_ && thread_checker_->CalledOnValidThread());
193 187
194 // TODO(engedy): Implement this. crbug.com/437865. 188 fetcher_.reset();
195 NOTIMPLEMENTED(); 189 throttler_->InformOfNetworkRequestComplete(false);
190
191 // Trigger a retry if a fetch is still needed.
192 for (const auto& facet_manager_pair : facet_managers_) {
193 if (facet_manager_pair.second->DoesRequireFetch()) {
194 throttler_->SignalNetworkRequestNeeded();
195 return;
196 }
197 }
196 } 198 }
197 199
198 void AffiliationBackend::OnMalformedResponse() { 200 void AffiliationBackend::OnMalformedResponse() {
199 DCHECK(thread_checker_ && thread_checker_->CalledOnValidThread()); 201 DCHECK(thread_checker_ && thread_checker_->CalledOnValidThread());
200 202
201 // TODO(engedy): Implement this. crbug.com/437865. 203 // TODO(engedy): Potentially handle this case differently. crbug.com/437865.
202 NOTIMPLEMENTED(); 204 OnFetchFailed();
205 }
206
207 bool AffiliationBackend::OnCanSendNetworkRequest() {
208 DCHECK(!fetcher_);
209 std::vector<FacetURI> requested_facet_uris;
210 for (const auto& facet_manager_pair : facet_managers_) {
211 if (facet_manager_pair.second->DoesRequireFetch())
212 requested_facet_uris.push_back(facet_manager_pair.first);
213 }
214
215 // In case a request is no longer needed, return false to indicate this.
216 if (requested_facet_uris.empty())
217 return false;
218
219 fetcher_.reset(AffiliationFetcher::Create(request_context_getter_.get(),
220 requested_facet_uris, this));
221 fetcher_->StartRequest();
222 return true;
223 }
224
225 void AffiliationBackend::SetThrottlerForTesting(
226 scoped_ptr<AffiliationFetchThrottler> throttler) {
227 throttler_ = throttler.Pass();
203 } 228 }
204 229
205 } // namespace password_manager 230 } // namespace password_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698