Chromium Code Reviews| OLD | NEW |
|---|---|
| 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_impl.h" | 5 #include "components/suggestions/suggestions_service_impl.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/feature_list.h" | 10 #include "base/feature_list.h" |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 173 const GURL& url, | 173 const GURL& url, |
| 174 const GURL& thumbnail_url, | 174 const GURL& thumbnail_url, |
| 175 const BitmapCallback& callback) { | 175 const BitmapCallback& callback) { |
| 176 thumbnail_manager_->AddImageURL(url, thumbnail_url); | 176 thumbnail_manager_->AddImageURL(url, thumbnail_url); |
| 177 GetPageThumbnail(url, callback); | 177 GetPageThumbnail(url, callback); |
| 178 } | 178 } |
| 179 | 179 |
| 180 bool SuggestionsServiceImpl::BlacklistURL(const GURL& candidate_url) { | 180 bool SuggestionsServiceImpl::BlacklistURL(const GURL& candidate_url) { |
| 181 DCHECK(thread_checker_.CalledOnValidThread()); | 181 DCHECK(thread_checker_.CalledOnValidThread()); |
| 182 | 182 |
| 183 // TODO(treib): Do we need to check |sync_state_| here? | |
| 184 | |
| 183 if (!blacklist_store_->BlacklistUrl(candidate_url)) | 185 if (!blacklist_store_->BlacklistUrl(candidate_url)) |
| 184 return false; | 186 return false; |
| 185 | 187 |
| 186 callback_list_.Notify( | 188 callback_list_.Notify( |
| 187 GetSuggestionsDataFromCache().value_or(SuggestionsProfile())); | 189 GetSuggestionsDataFromCache().value_or(SuggestionsProfile())); |
| 188 | 190 |
| 189 // Blacklist uploads are scheduled on any request completion, so only schedule | 191 // Blacklist uploads are scheduled on any request completion, so only schedule |
| 190 // an upload if there is no ongoing request. | 192 // an upload if there is no ongoing request. |
| 191 if (!pending_request_.get()) | 193 if (!pending_request_.get()) |
| 192 ScheduleBlacklistUpload(); | 194 ScheduleBlacklistUpload(); |
| 193 | 195 |
| 194 return true; | 196 return true; |
| 195 } | 197 } |
| 196 | 198 |
| 197 bool SuggestionsServiceImpl::UndoBlacklistURL(const GURL& url) { | 199 bool SuggestionsServiceImpl::UndoBlacklistURL(const GURL& url) { |
| 198 DCHECK(thread_checker_.CalledOnValidThread()); | 200 DCHECK(thread_checker_.CalledOnValidThread()); |
| 201 | |
| 202 // TODO(treib): Do we need to check |sync_state_| here? | |
| 203 | |
| 199 TimeDelta time_delta; | 204 TimeDelta time_delta; |
| 200 if (blacklist_store_->GetTimeUntilURLReadyForUpload(url, &time_delta) && | 205 if (blacklist_store_->GetTimeUntilURLReadyForUpload(url, &time_delta) && |
| 201 time_delta > TimeDelta::FromSeconds(0) && | 206 time_delta > TimeDelta::FromSeconds(0) && |
| 202 blacklist_store_->RemoveUrl(url)) { | 207 blacklist_store_->RemoveUrl(url)) { |
| 203 // The URL was not yet candidate for upload to the server and could be | 208 // The URL was not yet candidate for upload to the server and could be |
| 204 // removed from the blacklist. | 209 // removed from the blacklist. |
| 205 callback_list_.Notify( | 210 callback_list_.Notify( |
| 206 GetSuggestionsDataFromCache().value_or(SuggestionsProfile())); | 211 GetSuggestionsDataFromCache().value_or(SuggestionsProfile())); |
| 207 return true; | 212 return true; |
| 208 } | 213 } |
| 209 return false; | 214 return false; |
| 210 } | 215 } |
| 211 | 216 |
| 212 void SuggestionsServiceImpl::ClearBlacklist() { | 217 void SuggestionsServiceImpl::ClearBlacklist() { |
| 213 DCHECK(thread_checker_.CalledOnValidThread()); | 218 DCHECK(thread_checker_.CalledOnValidThread()); |
| 219 | |
| 220 // TODO(treib): Do we need to check |sync_state_| here? | |
| 221 | |
| 214 blacklist_store_->ClearBlacklist(); | 222 blacklist_store_->ClearBlacklist(); |
| 215 callback_list_.Notify( | 223 callback_list_.Notify( |
| 216 GetSuggestionsDataFromCache().value_or(SuggestionsProfile())); | 224 GetSuggestionsDataFromCache().value_or(SuggestionsProfile())); |
| 217 IssueRequestIfNoneOngoing(BuildSuggestionsBlacklistClearURL()); | 225 IssueRequestIfNoneOngoing(BuildSuggestionsBlacklistClearURL()); |
| 218 } | 226 } |
| 219 | 227 |
| 220 // static | 228 // static |
| 221 bool SuggestionsServiceImpl::GetBlacklistedUrl(const net::URLFetcher& request, | 229 bool SuggestionsServiceImpl::GetBlacklistedUrl(const net::URLFetcher& request, |
| 222 GURL* url) { | 230 GURL* url) { |
| 223 bool is_blacklist_request = base::StartsWith( | 231 bool is_blacklist_request = base::StartsWith( |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 280 } | 288 } |
| 281 if (!sync_service_->IsSyncActive() || !sync_service_->ConfigurationDone()) { | 289 if (!sync_service_->IsSyncActive() || !sync_service_->ConfigurationDone()) { |
| 282 return NOT_INITIALIZED_ENABLED; | 290 return NOT_INITIALIZED_ENABLED; |
| 283 } | 291 } |
| 284 return sync_service_->GetActiveDataTypes().Has( | 292 return sync_service_->GetActiveDataTypes().Has( |
| 285 syncer::HISTORY_DELETE_DIRECTIVES) | 293 syncer::HISTORY_DELETE_DIRECTIVES) |
| 286 ? INITIALIZED_ENABLED_HISTORY | 294 ? INITIALIZED_ENABLED_HISTORY |
| 287 : SYNC_OR_HISTORY_SYNC_DISABLED; | 295 : SYNC_OR_HISTORY_SYNC_DISABLED; |
| 288 } | 296 } |
| 289 | 297 |
| 290 bool SuggestionsServiceImpl::RefreshSyncState() { | 298 SuggestionsServiceImpl::RefreshAction |
| 299 SuggestionsServiceImpl::RefreshSyncState() { | |
| 291 SyncState new_sync_state = ComputeSyncState(); | 300 SyncState new_sync_state = ComputeSyncState(); |
| 292 if (sync_state_ == new_sync_state) { | 301 if (sync_state_ == new_sync_state) { |
| 293 return false; | 302 return NO_ACTION; |
| 294 } | 303 } |
| 304 | |
| 305 RefreshAction result = NO_ACTION; | |
| 306 // If the user signed out (or disabled history sync), we have to clear | |
| 307 // everything. | |
| 308 if (new_sync_state == SYNC_OR_HISTORY_SYNC_DISABLED) { | |
|
mastiz
2017/05/08 14:18:32
Optional: How about using a switch with |new_sync_
Marc Treib
2017/05/08 14:57:46
Done.
| |
| 309 result = CLEAR_SUGGESTIONS; | |
| 310 } | |
| 311 // If the user just signed in, we fetch suggestions, so that hopefully the | |
| 312 // next NTP will already get them. | |
| 313 if (sync_state_ == SYNC_OR_HISTORY_SYNC_DISABLED && | |
| 314 new_sync_state == INITIALIZED_ENABLED_HISTORY) { | |
| 315 result = FETCH_SUGGESTIONS; | |
| 316 } | |
| 317 // Otherwise, there's nothing to do. | |
| 318 | |
| 295 sync_state_ = new_sync_state; | 319 sync_state_ = new_sync_state; |
| 296 return true; | 320 return result; |
| 297 } | 321 } |
| 298 | 322 |
| 299 void SuggestionsServiceImpl::OnStateChanged(syncer::SyncService* sync) { | 323 void SuggestionsServiceImpl::OnStateChanged(syncer::SyncService* sync) { |
| 300 DCHECK(sync_service_ == sync); | 324 DCHECK(sync_service_ == sync); |
| 301 | 325 |
| 302 if (!RefreshSyncState()) { | 326 RefreshAction action = RefreshSyncState(); |
|
mastiz
2017/05/08 14:18:32
Optional: avoid the temporary variable.
Marc Treib
2017/05/08 14:57:46
Done.
| |
| 303 return; | |
| 304 } | |
| 305 | 327 |
| 306 switch (sync_state_) { | 328 switch (action) { |
| 307 case SYNC_OR_HISTORY_SYNC_DISABLED: | 329 case NO_ACTION: |
| 330 break; | |
| 331 case CLEAR_SUGGESTIONS: | |
| 308 // Cancel any ongoing request, to stop interacting with the server. | 332 // Cancel any ongoing request, to stop interacting with the server. |
| 309 pending_request_.reset(nullptr); | 333 pending_request_.reset(nullptr); |
| 310 suggestions_store_->ClearSuggestions(); | 334 suggestions_store_->ClearSuggestions(); |
| 311 callback_list_.Notify(SuggestionsProfile()); | 335 callback_list_.Notify(SuggestionsProfile()); |
| 312 break; | 336 break; |
| 313 case NOT_INITIALIZED_ENABLED: | 337 case FETCH_SUGGESTIONS: |
| 314 // Keep the cache (if any), but don't refresh. | 338 IssueRequestIfNoneOngoing(BuildSuggestionsURL()); |
| 315 break; | |
| 316 case INITIALIZED_ENABLED_HISTORY: | |
| 317 // If we have any observers, issue a network request to refresh the | |
| 318 // suggestions in the cache. | |
| 319 if (!callback_list_.empty()) | |
| 320 IssueRequestIfNoneOngoing(BuildSuggestionsURL()); | |
| 321 break; | 339 break; |
| 322 } | 340 } |
| 323 } | 341 } |
| 324 | 342 |
| 325 void SuggestionsServiceImpl::SetDefaultExpiryTimestamp( | 343 void SuggestionsServiceImpl::SetDefaultExpiryTimestamp( |
| 326 SuggestionsProfile* suggestions, | 344 SuggestionsProfile* suggestions, |
| 327 int64_t default_timestamp_usec) { | 345 int64_t default_timestamp_usec) { |
| 328 for (int i = 0; i < suggestions->suggestions_size(); ++i) { | 346 for (int i = 0; i < suggestions->suggestions_size(); ++i) { |
| 329 ChromeSuggestion* suggestion = suggestions->mutable_suggestions(i); | 347 ChromeSuggestion* suggestion = suggestions->mutable_suggestions(i); |
| 330 // Do not set expiry if the server has already provided a more specific | 348 // Do not set expiry if the server has already provided a more specific |
| 331 // expiry time for this suggestion. | 349 // expiry time for this suggestion. |
| 332 if (!suggestion->has_expiry_ts()) { | 350 if (!suggestion->has_expiry_ts()) { |
| 333 suggestion->set_expiry_ts(default_timestamp_usec); | 351 suggestion->set_expiry_ts(default_timestamp_usec); |
| 334 } | 352 } |
| 335 } | 353 } |
| 336 } | 354 } |
| 337 | 355 |
| 338 void SuggestionsServiceImpl::IssueRequestIfNoneOngoing(const GURL& url) { | 356 void SuggestionsServiceImpl::IssueRequestIfNoneOngoing(const GURL& url) { |
| 339 // If there is an ongoing request, let it complete. | 357 // If there is an ongoing request, let it complete. |
| 358 // TODO(treib): This will silently swallow blacklist and clearblacklist | |
| 359 // requests if a request happens to be ongoing. | |
|
mastiz
2017/05/08 14:18:32
Nit: I'm not a very fan of this TODO phrasing, sin
Marc Treib
2017/05/08 14:57:46
Done.
| |
| 340 if (pending_request_.get()) { | 360 if (pending_request_.get()) { |
| 341 return; | 361 return; |
| 342 } | 362 } |
| 343 // If there is an ongoing token request, also wait for that. | 363 // If there is an ongoing token request, also wait for that. |
| 344 if (token_fetcher_) { | 364 if (token_fetcher_) { |
| 345 return; | 365 return; |
| 346 } | 366 } |
| 347 | 367 |
| 348 OAuth2TokenService::ScopeSet scopes{GaiaConstants::kChromeSyncOAuth2Scope}; | 368 OAuth2TokenService::ScopeSet scopes{GaiaConstants::kChromeSyncOAuth2Scope}; |
| 349 token_fetcher_ = base::MakeUnique<AccessTokenFetcher>( | 369 token_fetcher_ = base::MakeUnique<AccessTokenFetcher>( |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 561 scheduling_delay_ = TimeDelta::FromSeconds(kDefaultSchedulingDelaySec); | 581 scheduling_delay_ = TimeDelta::FromSeconds(kDefaultSchedulingDelaySec); |
| 562 } else { | 582 } else { |
| 563 TimeDelta candidate_delay = | 583 TimeDelta candidate_delay = |
| 564 scheduling_delay_ * kSchedulingBackoffMultiplier; | 584 scheduling_delay_ * kSchedulingBackoffMultiplier; |
| 565 if (candidate_delay < TimeDelta::FromSeconds(kSchedulingMaxDelaySec)) | 585 if (candidate_delay < TimeDelta::FromSeconds(kSchedulingMaxDelaySec)) |
| 566 scheduling_delay_ = candidate_delay; | 586 scheduling_delay_ = candidate_delay; |
| 567 } | 587 } |
| 568 } | 588 } |
| 569 | 589 |
| 570 } // namespace suggestions | 590 } // namespace suggestions |
| OLD | NEW |