| 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 SyncState old_sync_state = sync_state_; |
| 295 sync_state_ = new_sync_state; | 306 sync_state_ = new_sync_state; |
| 296 return true; | 307 |
| 308 switch (new_sync_state) { |
| 309 case NOT_INITIALIZED_ENABLED: |
| 310 break; |
| 311 case INITIALIZED_ENABLED_HISTORY: |
| 312 // If the user just signed in, we fetch suggestions, so that hopefully the |
| 313 // next NTP will already get them. |
| 314 if (old_sync_state == SYNC_OR_HISTORY_SYNC_DISABLED) { |
| 315 return FETCH_SUGGESTIONS; |
| 316 } |
| 317 break; |
| 318 case SYNC_OR_HISTORY_SYNC_DISABLED: |
| 319 // If the user signed out (or disabled history sync), we have to clear |
| 320 // everything. |
| 321 return CLEAR_SUGGESTIONS; |
| 322 } |
| 323 // Otherwise, there's nothing to do. |
| 324 return NO_ACTION; |
| 297 } | 325 } |
| 298 | 326 |
| 299 void SuggestionsServiceImpl::OnStateChanged(syncer::SyncService* sync) { | 327 void SuggestionsServiceImpl::OnStateChanged(syncer::SyncService* sync) { |
| 300 DCHECK(sync_service_ == sync); | 328 DCHECK(sync_service_ == sync); |
| 301 | 329 |
| 302 if (!RefreshSyncState()) { | 330 switch (RefreshSyncState()) { |
| 303 return; | 331 case NO_ACTION: |
| 304 } | 332 break; |
| 305 | 333 case CLEAR_SUGGESTIONS: |
| 306 switch (sync_state_) { | |
| 307 case SYNC_OR_HISTORY_SYNC_DISABLED: | |
| 308 // Cancel any ongoing request, to stop interacting with the server. | 334 // Cancel any ongoing request, to stop interacting with the server. |
| 309 pending_request_.reset(nullptr); | 335 pending_request_.reset(nullptr); |
| 310 suggestions_store_->ClearSuggestions(); | 336 suggestions_store_->ClearSuggestions(); |
| 311 callback_list_.Notify(SuggestionsProfile()); | 337 callback_list_.Notify(SuggestionsProfile()); |
| 312 break; | 338 break; |
| 313 case NOT_INITIALIZED_ENABLED: | 339 case FETCH_SUGGESTIONS: |
| 314 // Keep the cache (if any), but don't refresh. | 340 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; | 341 break; |
| 322 } | 342 } |
| 323 } | 343 } |
| 324 | 344 |
| 325 void SuggestionsServiceImpl::SetDefaultExpiryTimestamp( | 345 void SuggestionsServiceImpl::SetDefaultExpiryTimestamp( |
| 326 SuggestionsProfile* suggestions, | 346 SuggestionsProfile* suggestions, |
| 327 int64_t default_timestamp_usec) { | 347 int64_t default_timestamp_usec) { |
| 328 for (int i = 0; i < suggestions->suggestions_size(); ++i) { | 348 for (int i = 0; i < suggestions->suggestions_size(); ++i) { |
| 329 ChromeSuggestion* suggestion = suggestions->mutable_suggestions(i); | 349 ChromeSuggestion* suggestion = suggestions->mutable_suggestions(i); |
| 330 // Do not set expiry if the server has already provided a more specific | 350 // Do not set expiry if the server has already provided a more specific |
| 331 // expiry time for this suggestion. | 351 // expiry time for this suggestion. |
| 332 if (!suggestion->has_expiry_ts()) { | 352 if (!suggestion->has_expiry_ts()) { |
| 333 suggestion->set_expiry_ts(default_timestamp_usec); | 353 suggestion->set_expiry_ts(default_timestamp_usec); |
| 334 } | 354 } |
| 335 } | 355 } |
| 336 } | 356 } |
| 337 | 357 |
| 338 void SuggestionsServiceImpl::IssueRequestIfNoneOngoing(const GURL& url) { | 358 void SuggestionsServiceImpl::IssueRequestIfNoneOngoing(const GURL& url) { |
| 339 // If there is an ongoing request, let it complete. | 359 // If there is an ongoing request, let it complete. |
| 360 // This will silently swallow blacklist and clearblacklist requests if a |
| 361 // request happens to be ongoing. |
| 362 // TODO(treib): Queue such requests and send them after the current one |
| 363 // completes. |
| 340 if (pending_request_.get()) { | 364 if (pending_request_.get()) { |
| 341 return; | 365 return; |
| 342 } | 366 } |
| 343 // If there is an ongoing token request, also wait for that. | 367 // If there is an ongoing token request, also wait for that. |
| 344 if (token_fetcher_) { | 368 if (token_fetcher_) { |
| 345 return; | 369 return; |
| 346 } | 370 } |
| 347 | 371 |
| 348 OAuth2TokenService::ScopeSet scopes{GaiaConstants::kChromeSyncOAuth2Scope}; | 372 OAuth2TokenService::ScopeSet scopes{GaiaConstants::kChromeSyncOAuth2Scope}; |
| 349 token_fetcher_ = base::MakeUnique<AccessTokenFetcher>( | 373 token_fetcher_ = base::MakeUnique<AccessTokenFetcher>( |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 561 scheduling_delay_ = TimeDelta::FromSeconds(kDefaultSchedulingDelaySec); | 585 scheduling_delay_ = TimeDelta::FromSeconds(kDefaultSchedulingDelaySec); |
| 562 } else { | 586 } else { |
| 563 TimeDelta candidate_delay = | 587 TimeDelta candidate_delay = |
| 564 scheduling_delay_ * kSchedulingBackoffMultiplier; | 588 scheduling_delay_ * kSchedulingBackoffMultiplier; |
| 565 if (candidate_delay < TimeDelta::FromSeconds(kSchedulingMaxDelaySec)) | 589 if (candidate_delay < TimeDelta::FromSeconds(kSchedulingMaxDelaySec)) |
| 566 scheduling_delay_ = candidate_delay; | 590 scheduling_delay_ = candidate_delay; |
| 567 } | 591 } |
| 568 } | 592 } |
| 569 | 593 |
| 570 } // namespace suggestions | 594 } // namespace suggestions |
| OLD | NEW |