| 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 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 [](const std::unique_ptr<NTPSnippet>& snippet) { return !snippet; }), | 184 [](const std::unique_ptr<NTPSnippet>& snippet) { return !snippet; }), |
| 185 snippets->end()); | 185 snippets->end()); |
| 186 } | 186 } |
| 187 | 187 |
| 188 } // namespace | 188 } // namespace |
| 189 | 189 |
| 190 NTPSnippetsService::NTPSnippetsService( | 190 NTPSnippetsService::NTPSnippetsService( |
| 191 Observer* observer, | 191 Observer* observer, |
| 192 CategoryFactory* category_factory, | 192 CategoryFactory* category_factory, |
| 193 PrefService* pref_service, | 193 PrefService* pref_service, |
| 194 history::HistoryService* history_service, | |
| 195 SuggestionsService* suggestions_service, | 194 SuggestionsService* suggestions_service, |
| 196 const std::string& application_language_code, | 195 const std::string& application_language_code, |
| 197 NTPSnippetsScheduler* scheduler, | 196 NTPSnippetsScheduler* scheduler, |
| 198 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, | 197 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, |
| 199 std::unique_ptr<ImageFetcher> image_fetcher, | 198 std::unique_ptr<ImageFetcher> image_fetcher, |
| 200 std::unique_ptr<ImageDecoder> image_decoder, | 199 std::unique_ptr<ImageDecoder> image_decoder, |
| 201 std::unique_ptr<NTPSnippetsDatabase> database, | 200 std::unique_ptr<NTPSnippetsDatabase> database, |
| 202 std::unique_ptr<NTPSnippetsStatusService> status_service) | 201 std::unique_ptr<NTPSnippetsStatusService> status_service) |
| 203 : ContentSuggestionsProvider(observer, category_factory), | 202 : ContentSuggestionsProvider(observer, category_factory), |
| 204 state_(State::NOT_INITED), | 203 state_(State::NOT_INITED), |
| 205 category_status_(CategoryStatus::INITIALIZING), | 204 category_status_(CategoryStatus::INITIALIZING), |
| 206 pref_service_(pref_service), | 205 pref_service_(pref_service), |
| 207 suggestions_service_(suggestions_service), | 206 suggestions_service_(suggestions_service), |
| 208 application_language_code_(application_language_code), | 207 application_language_code_(application_language_code), |
| 209 scheduler_(scheduler), | 208 scheduler_(scheduler), |
| 210 history_service_observer_(this), | |
| 211 snippets_fetcher_(std::move(snippets_fetcher)), | 209 snippets_fetcher_(std::move(snippets_fetcher)), |
| 212 image_fetcher_(std::move(image_fetcher)), | 210 image_fetcher_(std::move(image_fetcher)), |
| 213 image_decoder_(std::move(image_decoder)), | 211 image_decoder_(std::move(image_decoder)), |
| 214 database_(std::move(database)), | 212 database_(std::move(database)), |
| 215 snippets_status_service_(std::move(status_service)), | 213 snippets_status_service_(std::move(status_service)), |
| 216 fetch_after_load_(false), | 214 fetch_after_load_(false), |
| 217 nuke_after_load_(false), | 215 nuke_after_load_(false), |
| 218 provided_category_( | 216 provided_category_( |
| 219 category_factory->FromKnownCategory(KnownCategories::ARTICLES)), | 217 category_factory->FromKnownCategory(KnownCategories::ARTICLES)), |
| 220 thumbnail_requests_throttler_( | 218 thumbnail_requests_throttler_( |
| 221 pref_service, | 219 pref_service, |
| 222 RequestThrottler::RequestType::CONTENT_SUGGESTION_THUMBNAIL) { | 220 RequestThrottler::RequestType::CONTENT_SUGGESTION_THUMBNAIL) { |
| 223 observer->OnCategoryStatusChanged(this, provided_category_, category_status_); | 221 observer->OnCategoryStatusChanged(this, provided_category_, category_status_); |
| 224 if (database_->IsErrorState()) { | 222 if (database_->IsErrorState()) { |
| 225 EnterState(State::ERROR_OCCURRED, CategoryStatus::LOADING_ERROR); | 223 EnterState(State::ERROR_OCCURRED, CategoryStatus::LOADING_ERROR); |
| 226 return; | 224 return; |
| 227 } | 225 } |
| 228 | 226 |
| 229 // Can be null in tests. | |
| 230 if (history_service) | |
| 231 history_service_observer_.Add(history_service); | |
| 232 | |
| 233 database_->SetErrorCallback(base::Bind(&NTPSnippetsService::OnDatabaseError, | 227 database_->SetErrorCallback(base::Bind(&NTPSnippetsService::OnDatabaseError, |
| 234 base::Unretained(this))); | 228 base::Unretained(this))); |
| 235 | 229 |
| 236 // We transition to other states while finalizing the initialization, when the | 230 // We transition to other states while finalizing the initialization, when the |
| 237 // database is done loading. | 231 // database is done loading. |
| 238 database_->LoadSnippets(base::Bind(&NTPSnippetsService::OnDatabaseLoaded, | 232 database_->LoadSnippets(base::Bind(&NTPSnippetsService::OnDatabaseLoaded, |
| 239 base::Unretained(this))); | 233 base::Unretained(this))); |
| 240 } | 234 } |
| 241 | 235 |
| 242 NTPSnippetsService::~NTPSnippetsService() { | 236 NTPSnippetsService::~NTPSnippetsService() { |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 void NTPSnippetsService::FetchSuggestionImage( | 322 void NTPSnippetsService::FetchSuggestionImage( |
| 329 const std::string& suggestion_id, | 323 const std::string& suggestion_id, |
| 330 const ImageFetchedCallback& callback) { | 324 const ImageFetchedCallback& callback) { |
| 331 std::string snippet_id = GetWithinCategoryIDFromUniqueID(suggestion_id); | 325 std::string snippet_id = GetWithinCategoryIDFromUniqueID(suggestion_id); |
| 332 database_->LoadImage( | 326 database_->LoadImage( |
| 333 snippet_id, | 327 snippet_id, |
| 334 base::Bind(&NTPSnippetsService::OnSnippetImageFetchedFromDatabase, | 328 base::Bind(&NTPSnippetsService::OnSnippetImageFetchedFromDatabase, |
| 335 base::Unretained(this), callback, snippet_id)); | 329 base::Unretained(this), callback, snippet_id)); |
| 336 } | 330 } |
| 337 | 331 |
| 332 void NTPSnippetsService::RemoveURLsFromHistory( |
| 333 bool all_history, |
| 334 const std::vector<GURL>& deleted_urls) { |
| 335 // |deleted_urls| are ignored and all suggestions are removed, because it is |
| 336 // not known which history entries were used for the suggestions |
| 337 // personalization. |
| 338 if (!ready()) |
| 339 nuke_after_load_ = true; |
| 340 else |
| 341 NukeAllSnippets(); |
| 342 } |
| 343 |
| 338 void NTPSnippetsService::ClearHistory( | 344 void NTPSnippetsService::ClearHistory( |
| 339 base::Time begin, | 345 base::Time begin, |
| 340 base::Time end, | 346 base::Time end, |
| 341 const base::Callback<bool(const GURL& url)>& filter) { | 347 const base::Callback<bool(const GURL& url)>& filter) { |
| 342 // Both time range and the filter are ignored and all suggestions are removed, | 348 // Both time range and the filter are ignored and all suggestions are removed, |
| 343 // because it is not known which history entries were used for the suggestions | 349 // because it is not known which history entries were used for the suggestions |
| 344 // personalization. | 350 // personalization. |
| 345 if (!ready()) | 351 if (!ready()) |
| 346 nuke_after_load_ = true; | 352 nuke_after_load_ = true; |
| 347 else | 353 else |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 } | 415 } |
| 410 | 416 |
| 411 // static | 417 // static |
| 412 int NTPSnippetsService::GetMaxSnippetCountForTesting() { | 418 int NTPSnippetsService::GetMaxSnippetCountForTesting() { |
| 413 return kMaxSnippetCount; | 419 return kMaxSnippetCount; |
| 414 } | 420 } |
| 415 | 421 |
| 416 //////////////////////////////////////////////////////////////////////////////// | 422 //////////////////////////////////////////////////////////////////////////////// |
| 417 // Private methods | 423 // Private methods |
| 418 | 424 |
| 419 // history::HistoryServiceObserver implementation. | |
| 420 void NTPSnippetsService::OnURLsDeleted( | |
| 421 history::HistoryService* history_service, | |
| 422 bool all_history, | |
| 423 bool expired, | |
| 424 const history::URLRows& deleted_rows, | |
| 425 const std::set<GURL>& favicon_urls) { | |
| 426 // We don't care about expired entries. | |
| 427 if (expired) | |
| 428 return; | |
| 429 | |
| 430 if (!ready()) | |
| 431 nuke_after_load_ = true; | |
| 432 else | |
| 433 NukeAllSnippets(); | |
| 434 } | |
| 435 | |
| 436 void NTPSnippetsService::HistoryServiceBeingDeleted( | |
| 437 history::HistoryService* history_service) { | |
| 438 history_service_observer_.RemoveAll(); | |
| 439 } | |
| 440 | |
| 441 // image_fetcher::ImageFetcherDelegate implementation. | 425 // image_fetcher::ImageFetcherDelegate implementation. |
| 442 void NTPSnippetsService::OnImageDataFetched(const std::string& snippet_id, | 426 void NTPSnippetsService::OnImageDataFetched(const std::string& snippet_id, |
| 443 const std::string& image_data) { | 427 const std::string& image_data) { |
| 444 if (image_data.empty()) | 428 if (image_data.empty()) |
| 445 return; | 429 return; |
| 446 | 430 |
| 447 // Only save the image if the corresponding snippet still exists. | 431 // Only save the image if the corresponding snippet still exists. |
| 448 auto it = | 432 auto it = |
| 449 std::find_if(snippets_.begin(), snippets_.end(), | 433 std::find_if(snippets_.begin(), snippets_.end(), |
| 450 [&snippet_id](const std::unique_ptr<NTPSnippet>& snippet) { | 434 [&snippet_id](const std::unique_ptr<NTPSnippet>& snippet) { |
| (...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 905 void NTPSnippetsService::UpdateCategoryStatus(CategoryStatus status) { | 889 void NTPSnippetsService::UpdateCategoryStatus(CategoryStatus status) { |
| 906 if (status == category_status_) | 890 if (status == category_status_) |
| 907 return; | 891 return; |
| 908 | 892 |
| 909 category_status_ = status; | 893 category_status_ = status; |
| 910 observer()->OnCategoryStatusChanged(this, provided_category_, | 894 observer()->OnCategoryStatusChanged(this, provided_category_, |
| 911 category_status_); | 895 category_status_); |
| 912 } | 896 } |
| 913 | 897 |
| 914 } // namespace ntp_snippets | 898 } // namespace ntp_snippets |
| OLD | NEW |