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

Side by Side Diff: components/ntp_snippets/ntp_snippets_service.cc

Issue 2158883002: Change NTPSnippetsBridge to read from ContentSuggestionsService (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@offlinepagesprovider
Patch Set: Marc's comments; Rebase Created 4 years, 4 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 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 262 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 GetFetchingIntervalWifiCharging(), GetFetchingIntervalWifi(now), 273 GetFetchingIntervalWifiCharging(), GetFetchingIntervalWifi(now),
274 GetFetchingIntervalFallback(), GetRescheduleTime(now)); 274 GetFetchingIntervalFallback(), GetRescheduleTime(now));
275 } else { 275 } else {
276 scheduler_->Unschedule(); 276 scheduler_->Unschedule();
277 } 277 }
278 } 278 }
279 279
280 void NTPSnippetsService::FetchSuggestionImage( 280 void NTPSnippetsService::FetchSuggestionImage(
281 const std::string& suggestion_id, 281 const std::string& suggestion_id,
282 const ImageFetchedCallback& callback) { 282 const ImageFetchedCallback& callback) {
283 std::string snippet_id = GetWithinCategoryIDFromUniqueID(suggestion_id);
283 database_->LoadImage( 284 database_->LoadImage(
284 suggestion_id, 285 snippet_id,
285 base::Bind(&NTPSnippetsService::OnSnippetImageFetchedFromDatabase, 286 base::Bind(&NTPSnippetsService::OnSnippetImageFetchedFromDatabase,
286 base::Unretained(this), suggestion_id, callback)); 287 base::Unretained(this), snippet_id, callback));
287 } 288 }
288 289
289 void NTPSnippetsService::ClearCachedSuggestionsForDebugging() { 290 void NTPSnippetsService::ClearCachedSuggestionsForDebugging() {
290 if (!initialized()) 291 if (!initialized())
291 return; 292 return;
292 293
293 if (snippets_.empty()) 294 if (snippets_.empty())
294 return; 295 return;
295 296
296 database_->DeleteSnippets(snippets_); 297 database_->DeleteSnippets(snippets_);
297 snippets_.clear(); 298 snippets_.clear();
298 299
299 NotifyNewSuggestions(); 300 NotifyNewSuggestions();
300 } 301 }
301 302
302 std::set<std::string> NTPSnippetsService::GetSuggestionsHosts() const { 303 std::set<std::string> NTPSnippetsService::GetSuggestionsHosts() const {
303 // |suggestions_service_| can be null in tests. 304 // |suggestions_service_| can be null in tests.
304 if (!suggestions_service_) 305 if (!suggestions_service_)
305 return std::set<std::string>(); 306 return std::set<std::string>();
306 307
307 // TODO(treib): This should just call GetSnippetHostsFromPrefs. 308 // TODO(treib): This should just call GetSnippetHostsFromPrefs.
308 return GetSuggestionsHostsImpl( 309 return GetSuggestionsHostsImpl(
309 suggestions_service_->GetSuggestionsDataFromCache()); 310 suggestions_service_->GetSuggestionsDataFromCache());
310 } 311 }
311 312
312 void NTPSnippetsService::DiscardSuggestion(const std::string& suggestion_id) { 313 void NTPSnippetsService::DiscardSuggestion(const std::string& suggestion_id) {
313 if (!ready()) 314 if (!ready())
314 return; 315 return;
315 316
316 auto it = std::find_if( 317 std::string snippet_id = GetWithinCategoryIDFromUniqueID(suggestion_id);
317 snippets_.begin(), snippets_.end(), 318
318 [&suggestion_id](const std::unique_ptr<NTPSnippet>& snippet) { 319 auto it =
319 return snippet->id() == suggestion_id; 320 std::find_if(snippets_.begin(), snippets_.end(),
320 }); 321 [&snippet_id](const std::unique_ptr<NTPSnippet>& snippet) {
322 return snippet->id() == snippet_id;
323 });
321 if (it == snippets_.end()) 324 if (it == snippets_.end())
322 return; 325 return;
323 326
324 (*it)->set_discarded(true); 327 (*it)->set_discarded(true);
325 328
326 database_->SaveSnippet(**it); 329 database_->SaveSnippet(**it);
327 database_->DeleteImage((*it)->id()); 330 database_->DeleteImage((*it)->id());
328 331
329 discarded_snippets_.push_back(std::move(*it)); 332 discarded_snippets_.push_back(std::move(*it));
330 snippets_.erase(it); 333 snippets_.erase(it);
331
332 NotifyNewSuggestions();
333 } 334 }
334 335
335 void NTPSnippetsService::ClearDiscardedSuggestionsForDebugging() { 336 void NTPSnippetsService::ClearDiscardedSuggestionsForDebugging() {
336 if (!initialized()) 337 if (!initialized())
337 return; 338 return;
338 339
339 if (discarded_snippets_.empty()) 340 if (discarded_snippets_.empty())
340 return; 341 return;
341 342
342 database_->DeleteSnippets(discarded_snippets_); 343 database_->DeleteSnippets(discarded_snippets_);
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
615 616
616 // Fetching from the DB failed; start a network fetch. 617 // Fetching from the DB failed; start a network fetch.
617 FetchSnippetImageFromNetwork(snippet_id, callback); 618 FetchSnippetImageFromNetwork(snippet_id, callback);
618 } 619 }
619 620
620 void NTPSnippetsService::OnSnippetImageDecoded( 621 void NTPSnippetsService::OnSnippetImageDecoded(
621 const std::string& snippet_id, 622 const std::string& snippet_id,
622 const ImageFetchedCallback& callback, 623 const ImageFetchedCallback& callback,
623 const gfx::Image& image) { 624 const gfx::Image& image) {
624 if (!image.IsEmpty()) { 625 if (!image.IsEmpty()) {
625 callback.Run(snippet_id, image); 626 callback.Run(MakeUniqueID(ContentSuggestionsCategory::ARTICLES, snippet_id),
627 image);
626 return; 628 return;
627 } 629 }
628 630
629 // If decoding the image failed, delete the DB entry. 631 // If decoding the image failed, delete the DB entry.
630 database_->DeleteImage(snippet_id); 632 database_->DeleteImage(snippet_id);
631 633
632 FetchSnippetImageFromNetwork(snippet_id, callback); 634 FetchSnippetImageFromNetwork(snippet_id, callback);
633 } 635 }
634 636
635 void NTPSnippetsService::FetchSnippetImageFromNetwork( 637 void NTPSnippetsService::FetchSnippetImageFromNetwork(
636 const std::string& snippet_id, 638 const std::string& snippet_id,
637 const ImageFetchedCallback& callback) { 639 const ImageFetchedCallback& callback) {
638 auto it = 640 auto it =
639 std::find_if(snippets_.begin(), snippets_.end(), 641 std::find_if(snippets_.begin(), snippets_.end(),
640 [&snippet_id](const std::unique_ptr<NTPSnippet>& snippet) { 642 [&snippet_id](const std::unique_ptr<NTPSnippet>& snippet) {
641 return snippet->id() == snippet_id; 643 return snippet->id() == snippet_id;
642 }); 644 });
643 if (it == snippets_.end()) { 645 if (it == snippets_.end()) {
644 callback.Run(snippet_id, gfx::Image()); 646 callback.Run(MakeUniqueID(ContentSuggestionsCategory::ARTICLES, snippet_id),
647 gfx::Image());
645 return; 648 return;
646 } 649 }
647 650
648 const NTPSnippet& snippet = *it->get(); 651 const NTPSnippet& snippet = *it->get();
649 image_fetcher_->StartOrQueueNetworkRequest( 652 image_fetcher_->StartOrQueueNetworkRequest(
650 snippet.id(), snippet.salient_image_url(), callback); 653 snippet.id(), snippet.salient_image_url(), callback);
651 } 654 }
652 655
653 void NTPSnippetsService::EnterStateEnabled(bool fetch_snippets) { 656 void NTPSnippetsService::EnterStateEnabled(bool fetch_snippets) {
654 if (fetch_snippets) 657 if (fetch_snippets)
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
822 } 825 }
823 826
824 void NTPSnippetsService::NotifyCategoryStatusChanged() { 827 void NTPSnippetsService::NotifyCategoryStatusChanged() {
825 if (observer_) { 828 if (observer_) {
826 observer_->OnCategoryStatusChanged(ContentSuggestionsCategory::ARTICLES, 829 observer_->OnCategoryStatusChanged(ContentSuggestionsCategory::ARTICLES,
827 category_status_); 830 category_status_);
828 } 831 }
829 } 832 }
830 833
831 } // namespace ntp_snippets 834 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/ntp_snippets_service.h ('k') | components/ntp_snippets/ntp_snippets_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698