Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/ntp_snippets/ntp_snippets_service.h" | 5 #include "components/ntp_snippets/ntp_snippets_service.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <iterator> | 8 #include <iterator> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 180 NTPSnippetsService::NTPSnippetsService( | 180 NTPSnippetsService::NTPSnippetsService( |
| 181 bool enabled, | 181 bool enabled, |
| 182 PrefService* pref_service, | 182 PrefService* pref_service, |
| 183 sync_driver::SyncService* sync_service, | 183 sync_driver::SyncService* sync_service, |
| 184 SuggestionsService* suggestions_service, | 184 SuggestionsService* suggestions_service, |
| 185 const std::string& application_language_code, | 185 const std::string& application_language_code, |
| 186 NTPSnippetsScheduler* scheduler, | 186 NTPSnippetsScheduler* scheduler, |
| 187 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, | 187 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, |
| 188 std::unique_ptr<ImageFetcher> image_fetcher, | 188 std::unique_ptr<ImageFetcher> image_fetcher, |
| 189 std::unique_ptr<NTPSnippetsDatabase> database) | 189 std::unique_ptr<NTPSnippetsDatabase> database) |
| 190 : state_(State::INITED), | 190 : state_(State::NOT_INITED), |
| 191 enabled_(enabled), | 191 explicitly_disabled_(!enabled), |
| 192 pref_service_(pref_service), | 192 pref_service_(pref_service), |
| 193 sync_service_(sync_service), | 193 sync_service_(sync_service), |
| 194 sync_service_observer_(this), | 194 sync_service_observer_(this), |
| 195 suggestions_service_(suggestions_service), | 195 suggestions_service_(suggestions_service), |
| 196 application_language_code_(application_language_code), | 196 application_language_code_(application_language_code), |
| 197 scheduler_(scheduler), | 197 scheduler_(scheduler), |
| 198 snippets_fetcher_(std::move(snippets_fetcher)), | 198 snippets_fetcher_(std::move(snippets_fetcher)), |
| 199 image_fetcher_(std::move(image_fetcher)), | 199 image_fetcher_(std::move(image_fetcher)), |
| 200 database_(std::move(database)), | 200 database_(std::move(database)), |
| 201 fetch_after_load_(false) { | 201 fetch_after_load_(false) { |
| 202 // TODO(dgn) should that be removed after branch point? | |
|
Marc Treib
2016/06/06 08:38:32
Yup - seems I forgot to put in a TODO for that?
dgn
2016/06/06 15:44:38
Acknowledged.
| |
| 203 ClearDeprecatedPrefs(); | |
| 204 | |
| 205 if (explicitly_disabled_) { | |
| 206 EnterState(State::DISABLED); | |
| 207 return; | |
| 208 } | |
| 209 | |
| 202 snippets_fetcher_->SetCallback(base::Bind( | 210 snippets_fetcher_->SetCallback(base::Bind( |
| 203 &NTPSnippetsService::OnFetchFinished, base::Unretained(this))); | 211 &NTPSnippetsService::OnFetchFinished, base::Unretained(this))); |
| 204 | 212 |
| 205 // |sync_service_| can be null in tests or if sync is disabled. | 213 // |sync_service_| can be null in tests or if sync is disabled. |
| 214 // This is a service we want to keep listening to all the time, independently | |
| 215 // from the state, since it will allow us to enable the snippets service. | |
|
Marc Treib
2016/06/06 08:38:32
enable or disable, even!
dgn
2016/06/06 15:44:38
Done.
| |
| 206 if (sync_service_) | 216 if (sync_service_) |
| 207 sync_service_observer_.Add(sync_service_); | 217 sync_service_observer_.Add(sync_service_); |
| 208 | 218 |
| 209 if (enabled_) { | 219 // Will trigger the transition to the READY state. |
| 210 database_->Load(base::Bind(&NTPSnippetsService::OnDatabaseLoaded, | 220 database_->Load(base::Bind(&NTPSnippetsService::OnDatabaseLoaded, |
| 211 base::Unretained(this))); | 221 base::Unretained(this))); |
| 212 } | |
| 213 | |
| 214 RescheduleFetching(); | |
| 215 | |
| 216 ClearDeprecatedPrefs(); | |
| 217 } | 222 } |
| 218 | 223 |
| 219 NTPSnippetsService::~NTPSnippetsService() { | 224 NTPSnippetsService::~NTPSnippetsService() { |
| 220 DCHECK(state_ == State::SHUT_DOWN); | 225 DCHECK(state_ == State::SHUT_DOWN); |
| 221 } | 226 } |
| 222 | 227 |
| 223 // static | 228 // static |
| 224 void NTPSnippetsService::RegisterProfilePrefs(PrefRegistrySimple* registry) { | 229 void NTPSnippetsService::RegisterProfilePrefs(PrefRegistrySimple* registry) { |
| 225 registry->RegisterListPref(prefs::kDeprecatedSnippets); | 230 registry->RegisterListPref(prefs::kDeprecatedSnippets); |
| 226 registry->RegisterListPref(prefs::kDeprecatedDiscardedSnippets); | 231 registry->RegisterListPref(prefs::kDeprecatedDiscardedSnippets); |
| 227 registry->RegisterListPref(prefs::kSnippetHosts); | 232 registry->RegisterListPref(prefs::kSnippetHosts); |
| 228 } | 233 } |
| 229 | 234 |
| 235 // Inherited from KeyedService. | |
| 230 void NTPSnippetsService::Shutdown() { | 236 void NTPSnippetsService::Shutdown() { |
| 231 DCHECK(state_ == State::INITED || state_ == State::LOADED); | 237 EnterState(State::SHUT_DOWN); |
| 232 state_ = State::SHUT_DOWN; | |
| 233 | 238 |
| 234 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 239 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
| 235 NTPSnippetsServiceShutdown()); | 240 NTPSnippetsServiceShutdown()); |
| 236 expiry_timer_.Stop(); | 241 expiry_timer_.Stop(); |
| 237 suggestions_service_subscription_.reset(); | 242 suggestions_service_subscription_.reset(); |
|
Marc Treib
2016/06/06 08:38:32
Maybe move this into a helper method (EnterStateSh
dgn
2016/06/06 15:44:39
Done.
| |
| 238 enabled_ = false; | |
| 239 } | 243 } |
| 240 | 244 |
| 241 void NTPSnippetsService::FetchSnippets() { | 245 void NTPSnippetsService::FetchSnippets() { |
| 242 if (loaded()) | 246 if (ready()) |
| 243 FetchSnippetsFromHosts(GetSuggestionsHosts()); | 247 FetchSnippetsFromHosts(GetSuggestionsHosts()); |
| 244 else | 248 else |
| 245 fetch_after_load_ = true; | 249 fetch_after_load_ = true; |
| 246 } | 250 } |
| 247 | 251 |
| 248 void NTPSnippetsService::FetchSnippetsFromHosts( | 252 void NTPSnippetsService::FetchSnippetsFromHosts( |
| 249 const std::set<std::string>& hosts) { | 253 const std::set<std::string>& hosts) { |
| 250 if (!loaded()) | 254 if (!ready()) |
| 251 return; | 255 return; |
| 252 snippets_fetcher_->FetchSnippetsFromHosts(hosts, application_language_code_, | 256 snippets_fetcher_->FetchSnippetsFromHosts(hosts, application_language_code_, |
| 253 kMaxSnippetCount); | 257 kMaxSnippetCount); |
| 254 } | 258 } |
| 255 | 259 |
| 256 void NTPSnippetsService::RescheduleFetching() { | 260 void NTPSnippetsService::RescheduleFetching() { |
| 257 // The scheduler only exists on Android so far, it's null on other platforms. | 261 // The scheduler only exists on Android so far, it's null on other platforms. |
| 258 if (!scheduler_) | 262 if (!scheduler_) |
| 259 return; | 263 return; |
| 260 | 264 |
| 261 if (enabled_) { | 265 if (ready()) { |
| 262 base::Time now = base::Time::Now(); | 266 base::Time now = base::Time::Now(); |
| 263 scheduler_->Schedule( | 267 scheduler_->Schedule( |
| 264 GetFetchingIntervalWifiCharging(), GetFetchingIntervalWifi(now), | 268 GetFetchingIntervalWifiCharging(), GetFetchingIntervalWifi(now), |
| 265 GetFetchingIntervalFallback(), GetRescheduleTime(now)); | 269 GetFetchingIntervalFallback(), GetRescheduleTime(now)); |
| 266 } else { | 270 } else { |
| 267 scheduler_->Unschedule(); | 271 scheduler_->Unschedule(); |
| 268 } | 272 } |
| 269 } | 273 } |
| 270 | 274 |
| 271 void NTPSnippetsService::FetchSnippetImage( | 275 void NTPSnippetsService::FetchSnippetImage( |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 282 return; | 286 return; |
| 283 } | 287 } |
| 284 | 288 |
| 285 const NTPSnippet& snippet = *it->get(); | 289 const NTPSnippet& snippet = *it->get(); |
| 286 image_fetcher_->StartOrQueueNetworkRequest( | 290 image_fetcher_->StartOrQueueNetworkRequest( |
| 287 snippet.id(), snippet.salient_image_url(), callback); | 291 snippet.id(), snippet.salient_image_url(), callback); |
| 288 // TODO(treib): Cache/persist the snippet image. | 292 // TODO(treib): Cache/persist the snippet image. |
| 289 } | 293 } |
| 290 | 294 |
| 291 void NTPSnippetsService::ClearSnippets() { | 295 void NTPSnippetsService::ClearSnippets() { |
| 292 if (!loaded()) | 296 if (!initialized()) |
| 297 return; | |
| 298 | |
| 299 if (snippets_.empty()) | |
| 293 return; | 300 return; |
| 294 | 301 |
| 295 database_->Delete(snippets_); | 302 database_->Delete(snippets_); |
| 296 snippets_.clear(); | 303 snippets_.clear(); |
| 297 | 304 |
| 298 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 305 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
| 299 NTPSnippetsServiceLoaded()); | 306 NTPSnippetsServiceLoaded()); |
| 300 } | 307 } |
| 301 | 308 |
| 302 std::set<std::string> NTPSnippetsService::GetSuggestionsHosts() const { | 309 std::set<std::string> NTPSnippetsService::GetSuggestionsHosts() const { |
| 303 // |suggestions_service_| can be null in tests. | 310 // |suggestions_service_| can be null in tests. |
| 304 if (!suggestions_service_) | 311 if (!suggestions_service_) |
| 305 return std::set<std::string>(); | 312 return std::set<std::string>(); |
| 306 | 313 |
| 307 // TODO(treib): This should just call GetSnippetHostsFromPrefs. | 314 // TODO(treib): This should just call GetSnippetHostsFromPrefs. |
| 308 return GetSuggestionsHostsImpl( | 315 return GetSuggestionsHostsImpl( |
| 309 suggestions_service_->GetSuggestionsDataFromCache()); | 316 suggestions_service_->GetSuggestionsDataFromCache()); |
| 310 } | 317 } |
| 311 | 318 |
| 312 bool NTPSnippetsService::DiscardSnippet(const std::string& snippet_id) { | 319 bool NTPSnippetsService::DiscardSnippet(const std::string& snippet_id) { |
| 313 if (!loaded()) | 320 if (!ready()) |
| 314 return false; | 321 return false; |
| 315 | 322 |
| 316 auto it = | 323 auto it = |
| 317 std::find_if(snippets_.begin(), snippets_.end(), | 324 std::find_if(snippets_.begin(), snippets_.end(), |
| 318 [&snippet_id](const std::unique_ptr<NTPSnippet>& snippet) { | 325 [&snippet_id](const std::unique_ptr<NTPSnippet>& snippet) { |
| 319 return snippet->id() == snippet_id; | 326 return snippet->id() == snippet_id; |
| 320 }); | 327 }); |
| 321 if (it == snippets_.end()) | 328 if (it == snippets_.end()) |
| 322 return false; | 329 return false; |
| 323 | 330 |
| 324 (*it)->set_discarded(true); | 331 (*it)->set_discarded(true); |
| 325 | 332 |
| 326 database_->Save(**it); | 333 database_->Save(**it); |
| 327 | 334 |
| 328 discarded_snippets_.push_back(std::move(*it)); | 335 discarded_snippets_.push_back(std::move(*it)); |
| 329 snippets_.erase(it); | 336 snippets_.erase(it); |
| 330 | 337 |
| 331 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 338 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
| 332 NTPSnippetsServiceLoaded()); | 339 NTPSnippetsServiceLoaded()); |
| 333 return true; | 340 return true; |
| 334 } | 341 } |
| 335 | 342 |
| 336 void NTPSnippetsService::ClearDiscardedSnippets() { | 343 void NTPSnippetsService::ClearDiscardedSnippets() { |
| 337 if (!loaded()) | 344 if (!initialized()) |
| 338 return; | 345 return; |
| 339 | 346 |
| 340 database_->Delete(discarded_snippets_); | 347 database_->Delete(discarded_snippets_); |
| 341 discarded_snippets_.clear(); | 348 discarded_snippets_.clear(); |
| 342 | 349 |
| 343 FetchSnippets(); | 350 FetchSnippets(); |
|
dgn
2016/06/06 15:44:39
Removed so that we don't fetch while disabling the
Marc Treib
2016/06/07 08:55:38
Acknowledged.
| |
| 344 } | 351 } |
| 345 | 352 |
| 346 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { | 353 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { |
| 347 observers_.AddObserver(observer); | 354 observers_.AddObserver(observer); |
| 348 } | 355 } |
| 349 | 356 |
| 350 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { | 357 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { |
| 351 observers_.RemoveObserver(observer); | 358 observers_.RemoveObserver(observer); |
| 352 } | 359 } |
| 353 | 360 |
| 361 DisabledReason NTPSnippetsService::GetDisabledReason() { | |
| 362 if (explicitly_disabled_) | |
| 363 return DisabledReason::EXPLICITLY_DISABLED; | |
| 364 | |
| 365 if (!sync_service_ || !sync_service_->CanSyncStart()) { | |
| 366 DVLOG(1) << "[GetDisabledReason] Sync disabled"; | |
| 367 return DisabledReason::HISTORY_SYNC_DISABLED; | |
| 368 } | |
| 369 | |
| 370 // !IsSyncActive in cases where CanSyncStart is true hints at the backend not | |
| 371 // being initialized. | |
| 372 // ConfigurationDone() verifies that the sync service has properly loaded its | |
| 373 // configuration and is aware of the different components to sync. | |
|
Marc Treib
2016/06/06 08:38:32
s/components/data types
?
dgn
2016/06/06 15:44:38
Done.
| |
| 374 if (!sync_service_->IsSyncActive() || !sync_service_->ConfigurationDone()) { | |
| 375 DVLOG(1) << "[GetDisabledReason] Sync initialization is not complete."; | |
| 376 return DisabledReason::HISTORY_SYNC_STATE_UNKNOWN; | |
| 377 } | |
| 378 | |
| 379 if (!sync_service_->GetActiveDataTypes().Has( | |
| 380 syncer::HISTORY_DELETE_DIRECTIVES)) { | |
| 381 DVLOG(1) << "[GetDisabledReason] History sync disabled"; | |
| 382 return DisabledReason::HISTORY_SYNC_DISABLED; | |
| 383 } | |
| 384 | |
| 385 DVLOG(1) << "[GetDisabledReason] Enabled"; | |
| 386 return DisabledReason::NONE; | |
| 387 } | |
| 388 | |
| 354 // static | 389 // static |
| 355 int NTPSnippetsService::GetMaxSnippetCountForTesting() { | 390 int NTPSnippetsService::GetMaxSnippetCountForTesting() { |
| 356 return kMaxSnippetCount; | 391 return kMaxSnippetCount; |
| 357 } | 392 } |
| 358 | 393 |
| 359 //////////////////////////////////////////////////////////////////////////////// | 394 //////////////////////////////////////////////////////////////////////////////// |
| 360 // Private methods | 395 // Private methods |
| 361 | 396 |
| 362 // sync_driver::SyncServiceObserver implementation. | |
| 363 void NTPSnippetsService::OnStateChanged() { | 397 void NTPSnippetsService::OnStateChanged() { |
| 364 if (IsSyncStateIncompatible()) { | 398 if (!initialized()) |
| 365 ClearSnippets(); | |
| 366 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | |
| 367 NTPSnippetsServiceDisabled()); | |
| 368 return; | 399 return; |
| 369 } | |
| 370 | 400 |
| 371 // TODO(dgn): When the data sources change, we may want to not fetch here, | 401 DVLOG(1) << "[OnStateChanged]"; |
| 372 // as we will get notified of changes from the snippet sources as well, and | 402 EnterState(GetStateForDependenciesStatus()); |
| 373 // start multiple fetches. | |
| 374 FetchSnippets(); | |
| 375 } | 403 } |
| 376 | 404 |
| 377 void NTPSnippetsService::OnDatabaseLoaded(NTPSnippet::PtrVector snippets) { | 405 void NTPSnippetsService::OnDatabaseLoaded(NTPSnippet::PtrVector snippets) { |
| 378 DCHECK(state_ == State::INITED || state_ == State::SHUT_DOWN); | 406 DCHECK(state_ == State::NOT_INITED || state_ == State::SHUT_DOWN); |
| 379 if (state_ == State::SHUT_DOWN) | 407 if (state_ == State::SHUT_DOWN) |
| 380 return; | 408 return; |
| 381 state_ = State::LOADED; | |
| 382 | 409 |
| 383 DCHECK(snippets_.empty()); | 410 DCHECK(snippets_.empty()); |
| 384 DCHECK(discarded_snippets_.empty()); | 411 DCHECK(discarded_snippets_.empty()); |
| 385 for (std::unique_ptr<NTPSnippet>& snippet : snippets) { | 412 for (std::unique_ptr<NTPSnippet>& snippet : snippets) { |
| 386 if (snippet->is_discarded()) | 413 if (snippet->is_discarded()) |
| 387 discarded_snippets_.emplace_back(std::move(snippet)); | 414 discarded_snippets_.emplace_back(std::move(snippet)); |
| 388 else | 415 else |
| 389 snippets_.emplace_back(std::move(snippet)); | 416 snippets_.emplace_back(std::move(snippet)); |
| 390 } | 417 } |
| 391 std::sort(snippets_.begin(), snippets_.end(), | 418 std::sort(snippets_.begin(), snippets_.end(), |
| 392 [](const std::unique_ptr<NTPSnippet>& lhs, | 419 [](const std::unique_ptr<NTPSnippet>& lhs, |
| 393 const std::unique_ptr<NTPSnippet>& rhs) { | 420 const std::unique_ptr<NTPSnippet>& rhs) { |
| 394 return lhs->score() > rhs->score(); | 421 return lhs->score() > rhs->score(); |
| 395 }); | 422 }); |
| 423 | |
| 396 LoadingSnippetsFinished(); | 424 LoadingSnippetsFinished(); |
| 397 | 425 |
| 398 // If host restrictions are enabled, register for host list updates. | 426 EnterState(GetStateForDependenciesStatus()); |
| 399 // |suggestions_service_| can be null in tests. | |
| 400 if (snippets_fetcher_->UsesHostRestrictions() && suggestions_service_) { | |
| 401 suggestions_service_subscription_ = | |
| 402 suggestions_service_->AddCallback(base::Bind( | |
| 403 &NTPSnippetsService::OnSuggestionsChanged, base::Unretained(this))); | |
| 404 } | |
| 405 | |
| 406 // Start a fetch if we don't have any snippets yet, or a fetch was requested | |
| 407 // earlier. | |
| 408 if (snippets_.empty() || fetch_after_load_) { | |
| 409 fetch_after_load_ = false; | |
| 410 FetchSnippets(); | |
| 411 } | |
| 412 } | 427 } |
| 413 | 428 |
| 414 void NTPSnippetsService::OnSuggestionsChanged( | 429 void NTPSnippetsService::OnSuggestionsChanged( |
| 415 const SuggestionsProfile& suggestions) { | 430 const SuggestionsProfile& suggestions) { |
| 416 DCHECK(loaded()); | 431 DCHECK(initialized()); |
| 417 | 432 |
| 418 std::set<std::string> hosts = GetSuggestionsHostsImpl(suggestions); | 433 std::set<std::string> hosts = GetSuggestionsHostsImpl(suggestions); |
| 419 if (hosts == GetSnippetHostsFromPrefs()) | 434 if (hosts == GetSnippetHostsFromPrefs()) |
| 420 return; | 435 return; |
| 421 | 436 |
| 422 // Remove existing snippets that aren't in the suggestions anymore. | 437 // Remove existing snippets that aren't in the suggestions anymore. |
| 423 // TODO(treib,maybelle): If there is another source with an allowed host, | 438 // TODO(treib,maybelle): If there is another source with an allowed host, |
| 424 // then we should fall back to that. | 439 // then we should fall back to that. |
| 425 // First, move them over into |to_delete|. | 440 // First, move them over into |to_delete|. |
| 426 NTPSnippet::PtrVector to_delete; | 441 NTPSnippet::PtrVector to_delete; |
| 427 for (std::unique_ptr<NTPSnippet>& snippet : snippets_) { | 442 for (std::unique_ptr<NTPSnippet>& snippet : snippets_) { |
| 428 if (!hosts.count(snippet->best_source().url.host())) | 443 if (!hosts.count(snippet->best_source().url.host())) |
| 429 to_delete.emplace_back(std::move(snippet)); | 444 to_delete.emplace_back(std::move(snippet)); |
| 430 } | 445 } |
| 431 Compact(&snippets_); | 446 Compact(&snippets_); |
| 432 // Then delete the removed snippets from the database. | 447 // Then delete the removed snippets from the database. |
| 433 database_->Delete(to_delete); | 448 database_->Delete(to_delete); |
| 434 | 449 |
| 435 StoreSnippetHostsToPrefs(hosts); | 450 StoreSnippetHostsToPrefs(hosts); |
| 436 | 451 |
| 437 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 452 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
| 438 NTPSnippetsServiceLoaded()); | 453 NTPSnippetsServiceLoaded()); |
| 439 | 454 |
| 440 FetchSnippetsFromHosts(hosts); | 455 FetchSnippetsFromHosts(hosts); |
| 441 } | 456 } |
| 442 | 457 |
| 443 void NTPSnippetsService::OnFetchFinished( | 458 void NTPSnippetsService::OnFetchFinished( |
| 444 NTPSnippetsFetcher::OptionalSnippets snippets) { | 459 NTPSnippetsFetcher::OptionalSnippets snippets) { |
| 445 if (!loaded()) | 460 if (!ready()) |
| 446 return; | 461 return; |
| 447 | 462 |
| 448 if (snippets) { | 463 if (snippets) { |
| 449 // Sparse histogram used because the number of snippets is small (bound by | 464 // Sparse histogram used because the number of snippets is small (bound by |
| 450 // kMaxSnippetCount). | 465 // kMaxSnippetCount). |
| 451 DCHECK_LE(snippets->size(), static_cast<size_t>(kMaxSnippetCount)); | 466 DCHECK_LE(snippets->size(), static_cast<size_t>(kMaxSnippetCount)); |
| 452 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticlesFetched", | 467 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticlesFetched", |
| 453 snippets->size()); | 468 snippets->size()); |
| 454 MergeSnippets(std::move(*snippets)); | 469 MergeSnippets(std::move(*snippets)); |
| 455 } | 470 } |
| 456 LoadingSnippetsFinished(); | 471 LoadingSnippetsFinished(); |
| 457 } | 472 } |
| 458 | 473 |
| 459 void NTPSnippetsService::MergeSnippets(NTPSnippet::PtrVector new_snippets) { | 474 void NTPSnippetsService::MergeSnippets(NTPSnippet::PtrVector new_snippets) { |
| 460 DCHECK(loaded()); | 475 DCHECK(ready()); |
| 461 | 476 |
| 462 // Remove new snippets that we already have, or that have been discarded. | 477 // Remove new snippets that we already have, or that have been discarded. |
| 463 std::set<std::string> old_snippet_ids; | 478 std::set<std::string> old_snippet_ids; |
| 464 InsertAllIDs(discarded_snippets_, &old_snippet_ids); | 479 InsertAllIDs(discarded_snippets_, &old_snippet_ids); |
| 465 InsertAllIDs(snippets_, &old_snippet_ids); | 480 InsertAllIDs(snippets_, &old_snippet_ids); |
| 466 new_snippets.erase( | 481 new_snippets.erase( |
| 467 std::remove_if( | 482 std::remove_if( |
| 468 new_snippets.begin(), new_snippets.end(), | 483 new_snippets.begin(), new_snippets.end(), |
| 469 [&old_snippet_ids](const std::unique_ptr<NTPSnippet>& snippet) { | 484 [&old_snippet_ids](const std::unique_ptr<NTPSnippet>& snippet) { |
| 470 if (old_snippet_ids.count(snippet->id())) | 485 if (old_snippet_ids.count(snippet->id())) |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 533 | 548 |
| 534 void NTPSnippetsService::StoreSnippetHostsToPrefs( | 549 void NTPSnippetsService::StoreSnippetHostsToPrefs( |
| 535 const std::set<std::string>& hosts) { | 550 const std::set<std::string>& hosts) { |
| 536 base::ListValue list; | 551 base::ListValue list; |
| 537 for (const std::string& host : hosts) | 552 for (const std::string& host : hosts) |
| 538 list.AppendString(host); | 553 list.AppendString(host); |
| 539 pref_service_->Set(prefs::kSnippetHosts, list); | 554 pref_service_->Set(prefs::kSnippetHosts, list); |
| 540 } | 555 } |
| 541 | 556 |
| 542 void NTPSnippetsService::LoadingSnippetsFinished() { | 557 void NTPSnippetsService::LoadingSnippetsFinished() { |
| 543 DCHECK(loaded()); | 558 DCHECK(ready() || state_ == State::NOT_INITED /* for OnDatabaseLoaded */); |
|
Marc Treib
2016/06/06 08:38:32
In OnDatabaseLoaded, could we call EnterState befo
dgn
2016/06/06 15:44:38
Done.
| |
| 544 | 559 |
| 545 // Remove expired snippets. | 560 // Remove expired snippets. |
| 546 base::Time expiry = base::Time::Now(); | 561 base::Time expiry = base::Time::Now(); |
| 547 | 562 |
| 548 // Move expired snippets over into |to_delete|. | 563 // Move expired snippets over into |to_delete|. |
| 549 NTPSnippet::PtrVector to_delete; | 564 NTPSnippet::PtrVector to_delete; |
| 550 for (std::unique_ptr<NTPSnippet>& snippet : snippets_) { | 565 for (std::unique_ptr<NTPSnippet>& snippet : snippets_) { |
| 551 if (snippet->expiry_date() <= expiry) | 566 if (snippet->expiry_date() <= expiry) |
| 552 to_delete.emplace_back(std::move(snippet)); | 567 to_delete.emplace_back(std::move(snippet)); |
| 553 } | 568 } |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 595 for (const auto& snippet : discarded_snippets_) { | 610 for (const auto& snippet : discarded_snippets_) { |
| 596 if (snippet->expiry_date() < next_expiry) | 611 if (snippet->expiry_date() < next_expiry) |
| 597 next_expiry = snippet->expiry_date(); | 612 next_expiry = snippet->expiry_date(); |
| 598 } | 613 } |
| 599 DCHECK_GT(next_expiry, expiry); | 614 DCHECK_GT(next_expiry, expiry); |
| 600 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, | 615 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, |
| 601 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, | 616 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, |
| 602 base::Unretained(this))); | 617 base::Unretained(this))); |
| 603 } | 618 } |
| 604 | 619 |
| 605 bool NTPSnippetsService::IsSyncStateIncompatible() { | 620 void NTPSnippetsService::Enable() { |
| 606 if (!sync_service_ || !sync_service_->CanSyncStart()) | 621 // Start a fetch if we don't have any snippets yet, or a fetch was requested |
| 607 return true; | 622 // earlier. |
| 608 if (!sync_service_->IsSyncActive() || !sync_service_->ConfigurationDone()) | 623 if (snippets_.empty() || fetch_after_load_) { |
| 609 return false; // Sync service is not initialized, yet not disabled. | 624 fetch_after_load_ = false; |
| 610 return !sync_service_->GetActiveDataTypes().Has( | 625 FetchSnippets(); |
| 611 syncer::HISTORY_DELETE_DIRECTIVES); | 626 } |
| 627 | |
| 628 // If host restrictions are enabled, register for host list updates. | |
| 629 // |suggestions_service_| can be null in tests. | |
| 630 if (snippets_fetcher_->UsesHostRestrictions() && suggestions_service_) { | |
| 631 suggestions_service_subscription_ = | |
| 632 suggestions_service_->AddCallback(base::Bind( | |
| 633 &NTPSnippetsService::OnSuggestionsChanged, base::Unretained(this))); | |
| 634 } | |
| 635 | |
| 636 RescheduleFetching(); | |
| 637 } | |
| 638 | |
| 639 void NTPSnippetsService::Disable() { | |
| 640 // TODO(dgn) should we also clear the discarded snippets? | |
| 641 ClearSnippets(); | |
|
Marc Treib
2016/06/06 08:38:32
Just to make sure: We do not get here during the r
dgn
2016/06/06 15:44:38
No, we should not. It's guarded by a DCHECK. But w
| |
| 642 | |
| 643 suggestions_service_subscription_.reset(); | |
| 644 expiry_timer_.Stop(); | |
| 645 | |
| 646 RescheduleFetching(); | |
| 647 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | |
| 648 NTPSnippetsServiceDisabled()); | |
| 649 } | |
| 650 | |
| 651 NTPSnippetsService::State NTPSnippetsService::GetStateForDependenciesStatus() { | |
| 652 DisabledReason dr = GetDisabledReason(); | |
| 653 | |
| 654 if (dr == DisabledReason::NONE) | |
|
Marc Treib
2016/06/06 08:38:32
Make this a switch, so it's clear that all possibl
dgn
2016/06/06 15:44:38
Done.
| |
| 655 return State::READY; | |
| 656 | |
| 657 // HistorySync is not initialized yet, so we don't know what the actual state | |
| 658 // is. However, because we retreived the previous snippets from the database, | |
| 659 // if we got something, we know that the service was previously enabled, so | |
| 660 // we just restore that state. If things changed, |OnStateChanged| will call | |
| 661 // this function again to fix the state. | |
| 662 if (dr == DisabledReason::HISTORY_SYNC_STATE_UNKNOWN) { | |
| 663 DVLOG(1) << "Sync configuration not done, continuing based on the current " | |
| 664 "state."; | |
| 665 return snippets_.empty() ? State::DISABLED : State::READY; | |
| 666 } | |
| 667 | |
| 668 return State::DISABLED; | |
| 669 } | |
| 670 | |
| 671 void NTPSnippetsService::EnterState(State state) { | |
| 672 switch (state) { | |
| 673 case State::NOT_INITED: | |
| 674 // Initial state, it should not be possible to get back there. | |
| 675 NOTREACHED(); | |
| 676 return; | |
| 677 | |
| 678 case State::READY: | |
| 679 DCHECK(state_ == State::NOT_INITED || state_ == State::READY || | |
| 680 state_ == State::DISABLED); | |
| 681 if (state_ == State::READY) | |
| 682 return; | |
| 683 | |
| 684 DVLOG(1) << "Entering state: READY"; | |
| 685 state_ = State::READY; | |
| 686 Enable(); | |
| 687 return; | |
| 688 | |
| 689 case State::DISABLED: | |
| 690 DCHECK(state_ == State::NOT_INITED || state_ == State::READY || | |
| 691 state_ == State::DISABLED); | |
| 692 if (state_ == State::DISABLED) | |
| 693 return; | |
| 694 | |
| 695 DVLOG(1) << "Entering state: DISABLED"; | |
| 696 state_ = State::DISABLED; | |
| 697 Disable(); | |
| 698 return; | |
| 699 | |
| 700 case State::SHUT_DOWN: | |
| 701 DCHECK(state_ == State::NOT_INITED || state_ == State::DISABLED || | |
| 702 state_ == State::READY); | |
| 703 DVLOG(1) << "Entering state: SHUT_DOWN"; | |
| 704 state_ = State::SHUT_DOWN; | |
| 705 return; | |
| 706 } | |
| 612 } | 707 } |
| 613 | 708 |
| 614 void NTPSnippetsService::ClearDeprecatedPrefs() { | 709 void NTPSnippetsService::ClearDeprecatedPrefs() { |
| 615 pref_service_->ClearPref(prefs::kDeprecatedSnippets); | 710 pref_service_->ClearPref(prefs::kDeprecatedSnippets); |
| 616 pref_service_->ClearPref(prefs::kDeprecatedDiscardedSnippets); | 711 pref_service_->ClearPref(prefs::kDeprecatedDiscardedSnippets); |
| 617 } | 712 } |
| 618 | 713 |
| 619 } // namespace ntp_snippets | 714 } // namespace ntp_snippets |
| OLD | NEW |