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

Side by Side Diff: components/suggestions/suggestions_service.cc

Issue 527943002: [Most Visited] Check for Sync state when using SuggestionsService. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@2125
Patch Set: Created 6 years, 3 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 2014 The Chromium Authors. All rights reserved. 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 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/suggestions/suggestions_service.h" 5 #include "components/suggestions/suggestions_service.h"
6 6
7 #include <sstream> 7 #include <sstream>
8 #include <string> 8 #include <string>
9 9
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 kSuggestionsFieldTrialStateEnabled; 149 kSuggestionsFieldTrialStateEnabled;
150 } 150 }
151 151
152 // static 152 // static
153 bool SuggestionsService::IsControlGroup() { 153 bool SuggestionsService::IsControlGroup() {
154 return GetExperimentParam(kSuggestionsFieldTrialControlParam) == 154 return GetExperimentParam(kSuggestionsFieldTrialControlParam) ==
155 kSuggestionsFieldTrialStateEnabled; 155 kSuggestionsFieldTrialStateEnabled;
156 } 156 }
157 157
158 void SuggestionsService::FetchSuggestionsData( 158 void SuggestionsService::FetchSuggestionsData(
159 SyncState sync_state,
159 SuggestionsService::ResponseCallback callback) { 160 SuggestionsService::ResponseCallback callback) {
160 DCHECK(thread_checker_.CalledOnValidThread()); 161 DCHECK(thread_checker_.CalledOnValidThread());
162 if (sync_state == NOT_INITIALIZED_ENABLED) {
163 // Sync is not initialized yet, but enabled. Serve previously cached
164 // suggestions if available.
165 waiting_requestors_.push_back(callback);
166 ServeFromCache();
167 return;
168 } else if (sync_state == SYNC_OR_HISTORY_SYNC_DISABLED) {
169 // Cancel any ongoing request (and the timeout closure). We must no longer
170 // interact with the server.
171 pending_request_.reset(NULL);
172 pending_timeout_closure_.reset(NULL);
173 suggestions_store_->ClearSuggestions();
174 callback.Run(SuggestionsProfile());
175 DispatchRequestsAndClear(SuggestionsProfile(), &waiting_requestors_);
176 return;
177 }
161 178
162 FetchSuggestionsDataNoTimeout(callback); 179 FetchSuggestionsDataNoTimeout(callback);
163 180
164 // Post a task to serve the cached suggestions if the request hasn't completed 181 // Post a task to serve the cached suggestions if the request hasn't completed
165 // after some time. Cancels the previous such task, if one existed. 182 // after some time. Cancels the previous such task, if one existed.
166 pending_timeout_closure_.reset(new CancelableClosure(base::Bind( 183 pending_timeout_closure_.reset(new CancelableClosure(base::Bind(
167 &SuggestionsService::OnRequestTimeout, weak_ptr_factory_.GetWeakPtr()))); 184 &SuggestionsService::OnRequestTimeout, weak_ptr_factory_.GetWeakPtr())));
168 base::MessageLoopProxy::current()->PostDelayedTask( 185 base::MessageLoopProxy::current()->PostDelayedTask(
169 FROM_HERE, pending_timeout_closure_->callback(), 186 FROM_HERE, pending_timeout_closure_->callback(),
170 base::TimeDelta::FromMilliseconds(request_timeout_ms_)); 187 base::TimeDelta::FromMilliseconds(request_timeout_ms_));
171 } 188 }
172 189
173 void SuggestionsService::FetchSuggestionsDataNoTimeout(
174 SuggestionsService::ResponseCallback callback) {
175 DCHECK(thread_checker_.CalledOnValidThread());
176 if (pending_request_.get()) {
177 // Request already exists, so just add requestor to queue.
178 waiting_requestors_.push_back(callback);
179 return;
180 }
181
182 // Form new request.
183 DCHECK(waiting_requestors_.empty());
184 waiting_requestors_.push_back(callback);
185 IssueRequest(suggestions_url_);
186 }
187
188 void SuggestionsService::GetPageThumbnail( 190 void SuggestionsService::GetPageThumbnail(
189 const GURL& url, 191 const GURL& url,
190 base::Callback<void(const GURL&, const SkBitmap*)> callback) { 192 base::Callback<void(const GURL&, const SkBitmap*)> callback) {
191 thumbnail_manager_->GetImageForURL(url, callback); 193 thumbnail_manager_->GetImageForURL(url, callback);
192 } 194 }
193 195
194 void SuggestionsService::BlacklistURL( 196 void SuggestionsService::BlacklistURL(
195 const GURL& candidate_url, 197 const GURL& candidate_url,
196 const SuggestionsService::ResponseCallback& callback) { 198 const SuggestionsService::ResponseCallback& callback) {
197 DCHECK(thread_checker_.CalledOnValidThread()); 199 DCHECK(thread_checker_.CalledOnValidThread());
(...skipping 30 matching lines...) Expand all
228 return true; 230 return true;
229 } 231 }
230 232
231 // static 233 // static
232 void SuggestionsService::RegisterProfilePrefs( 234 void SuggestionsService::RegisterProfilePrefs(
233 user_prefs::PrefRegistrySyncable* registry) { 235 user_prefs::PrefRegistrySyncable* registry) {
234 SuggestionsStore::RegisterProfilePrefs(registry); 236 SuggestionsStore::RegisterProfilePrefs(registry);
235 BlacklistStore::RegisterProfilePrefs(registry); 237 BlacklistStore::RegisterProfilePrefs(registry);
236 } 238 }
237 239
240 void SuggestionsService::SetDefaultExpiryTimestamp(
241 SuggestionsProfile* suggestions, int64 default_timestamp_usec) {
242 for (int i = 0; i < suggestions->suggestions_size(); ++i) {
243 ChromeSuggestion* suggestion = suggestions->mutable_suggestions(i);
244 // Do not set expiry if the server has already provided a more specific
245 // expiry time for this suggestion.
246 if (!suggestion->has_expiry_ts()) {
247 suggestion->set_expiry_ts(default_timestamp_usec);
248 }
249 }
250 }
251
252 void SuggestionsService::FetchSuggestionsDataNoTimeout(
253 SuggestionsService::ResponseCallback callback) {
254 DCHECK(thread_checker_.CalledOnValidThread());
255 if (pending_request_.get()) {
256 // Request already exists, so just add requestor to queue.
257 waiting_requestors_.push_back(callback);
258 return;
259 }
260
261 // Form new request.
262 DCHECK(waiting_requestors_.empty());
263 waiting_requestors_.push_back(callback);
264 IssueRequest(suggestions_url_);
265 }
266
238 void SuggestionsService::IssueRequest(const GURL& url) { 267 void SuggestionsService::IssueRequest(const GURL& url) {
239 pending_request_.reset(CreateSuggestionsRequest(url)); 268 pending_request_.reset(CreateSuggestionsRequest(url));
240 pending_request_->Start(); 269 pending_request_->Start();
241 last_request_started_time_ = base::TimeTicks::Now(); 270 last_request_started_time_ = base::TimeTicks::Now();
242 } 271 }
243 272
244 net::URLFetcher* SuggestionsService::CreateSuggestionsRequest(const GURL& url) { 273 net::URLFetcher* SuggestionsService::CreateSuggestionsRequest(const GURL& url) {
245 net::URLFetcher* request = 274 net::URLFetcher* request =
246 net::URLFetcher::Create(0, url, net::URLFetcher::GET, this); 275 net::URLFetcher::Create(0, url, net::URLFetcher::GET, this);
247 request->SetLoadFlags(net::LOAD_DISABLE_CACHE); 276 request->SetLoadFlags(net::LOAD_DISABLE_CACHE);
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 } else { 352 } else {
324 LogResponseState(RESPONSE_INVALID); 353 LogResponseState(RESPONSE_INVALID);
325 suggestions_store_->LoadSuggestions(&suggestions); 354 suggestions_store_->LoadSuggestions(&suggestions);
326 thumbnail_manager_->Initialize(suggestions); 355 thumbnail_manager_->Initialize(suggestions);
327 } 356 }
328 357
329 FilterAndServe(&suggestions); 358 FilterAndServe(&suggestions);
330 ScheduleBlacklistUpload(true); 359 ScheduleBlacklistUpload(true);
331 } 360 }
332 361
333 void SuggestionsService::SetDefaultExpiryTimestamp(
334 SuggestionsProfile* suggestions, int64 default_timestamp_usec) {
335 for (int i = 0; i < suggestions->suggestions_size(); ++i) {
336 ChromeSuggestion* suggestion = suggestions->mutable_suggestions(i);
337 // Do not set expiry if the server has already provided a more specific
338 // expiry time for this suggestion.
339 if (!suggestion->has_expiry_ts()) {
340 suggestion->set_expiry_ts(default_timestamp_usec);
341 }
342 }
343 }
344
345 void SuggestionsService::Shutdown() { 362 void SuggestionsService::Shutdown() {
346 // Cancel pending request and timeout closure, then serve existing requestors 363 // Cancel pending request and timeout closure, then serve existing requestors
347 // from cache. 364 // from cache.
348 pending_request_.reset(NULL); 365 pending_request_.reset(NULL);
349 pending_timeout_closure_.reset(NULL); 366 pending_timeout_closure_.reset(NULL);
350 ServeFromCache(); 367 ServeFromCache();
351 } 368 }
352 369
353 void SuggestionsService::ServeFromCache() { 370 void SuggestionsService::ServeFromCache() {
354 SuggestionsProfile suggestions; 371 SuggestionsProfile suggestions;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 if (last_request_successful) { 416 if (last_request_successful) {
400 blacklist_delay_sec_ = kBlacklistDefaultDelaySec; 417 blacklist_delay_sec_ = kBlacklistDefaultDelaySec;
401 } else { 418 } else {
402 int candidate_delay = blacklist_delay_sec_ * kBlacklistBackoffMultiplier; 419 int candidate_delay = blacklist_delay_sec_ * kBlacklistBackoffMultiplier;
403 if (candidate_delay < kBlacklistMaxDelaySec) 420 if (candidate_delay < kBlacklistMaxDelaySec)
404 blacklist_delay_sec_ = candidate_delay; 421 blacklist_delay_sec_ = candidate_delay;
405 } 422 }
406 } 423 }
407 424
408 } // namespace suggestions 425 } // namespace suggestions
OLDNEW
« no previous file with comments | « components/suggestions/suggestions_service.h ('k') | components/suggestions/suggestions_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698