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

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: Created 4 years, 5 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 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 GetFetchingIntervalWifiCharging(), GetFetchingIntervalWifi(now), 270 GetFetchingIntervalWifiCharging(), GetFetchingIntervalWifi(now),
271 GetFetchingIntervalFallback(), GetRescheduleTime(now)); 271 GetFetchingIntervalFallback(), GetRescheduleTime(now));
272 } else { 272 } else {
273 scheduler_->Unschedule(); 273 scheduler_->Unschedule();
274 } 274 }
275 } 275 }
276 276
277 void NTPSnippetsService::FetchSuggestionImage( 277 void NTPSnippetsService::FetchSuggestionImage(
278 const std::string& suggestion_id, 278 const std::string& suggestion_id,
279 const ImageFetchedCallback& callback) { 279 const ImageFetchedCallback& callback) {
280 std::string snippet_id = GetWithinCategoryIDFromUniqueID(suggestion_id);
280 database_->LoadImage( 281 database_->LoadImage(
281 suggestion_id, 282 snippet_id,
Marc Treib 2016/07/19 09:30:25 Wait, so this only worked because suggestion_id wa
Philipp Keck 2016/07/19 12:21:50 Yes. This method implements two different interfac
282 base::Bind(&NTPSnippetsService::OnSnippetImageFetchedFromDatabase, 283 base::Bind(&NTPSnippetsService::OnSnippetImageFetchedFromDatabase,
283 base::Unretained(this), suggestion_id, callback)); 284 base::Unretained(this), snippet_id, callback));
284 } 285 }
285 286
286 void NTPSnippetsService::ClearCachedSuggestionsForDebugging() { 287 void NTPSnippetsService::ClearCachedSuggestionsForDebugging() {
287 if (!initialized()) 288 if (!initialized())
288 return; 289 return;
289 290
290 if (snippets_.empty()) 291 if (snippets_.empty())
291 return; 292 return;
292 293
293 database_->DeleteSnippets(snippets_); 294 database_->DeleteSnippets(snippets_);
294 snippets_.clear(); 295 snippets_.clear();
295 296
296 NotifyNewSuggestions(); 297 NotifyNewSuggestions();
297 } 298 }
298 299
299 std::set<std::string> NTPSnippetsService::GetSuggestionsHosts() const { 300 std::set<std::string> NTPSnippetsService::GetSuggestionsHosts() const {
300 // |suggestions_service_| can be null in tests. 301 // |suggestions_service_| can be null in tests.
301 if (!suggestions_service_) 302 if (!suggestions_service_)
302 return std::set<std::string>(); 303 return std::set<std::string>();
303 304
304 // TODO(treib): This should just call GetSnippetHostsFromPrefs. 305 // TODO(treib): This should just call GetSnippetHostsFromPrefs.
305 return GetSuggestionsHostsImpl( 306 return GetSuggestionsHostsImpl(
306 suggestions_service_->GetSuggestionsDataFromCache()); 307 suggestions_service_->GetSuggestionsDataFromCache());
307 } 308 }
308 309
309 void NTPSnippetsService::DiscardSuggestion(const std::string& suggestion_id) { 310 void NTPSnippetsService::DiscardSuggestion(const std::string& suggestion_id) {
310 if (!ready()) 311 if (!ready())
311 return; 312 return;
312 313
313 auto it = std::find_if( 314 std::string snippet_id = GetWithinCategoryIDFromUniqueID(suggestion_id);
314 snippets_.begin(), snippets_.end(), 315
315 [&suggestion_id](const std::unique_ptr<NTPSnippet>& snippet) { 316 auto it =
316 return snippet->id() == suggestion_id; 317 std::find_if(snippets_.begin(), snippets_.end(),
317 }); 318 [&snippet_id](const std::unique_ptr<NTPSnippet>& snippet) {
319 return snippet->id() == snippet_id;
320 });
318 if (it == snippets_.end()) 321 if (it == snippets_.end())
319 return; 322 return;
320 323
321 (*it)->set_discarded(true); 324 (*it)->set_discarded(true);
322 325
323 database_->SaveSnippet(**it); 326 database_->SaveSnippet(**it);
324 database_->DeleteImage((*it)->id()); 327 database_->DeleteImage((*it)->id());
325 328
326 discarded_snippets_.push_back(std::move(*it)); 329 discarded_snippets_.push_back(std::move(*it));
327 snippets_.erase(it); 330 snippets_.erase(it);
328
329 NotifyNewSuggestions();
330 } 331 }
331 332
332 void NTPSnippetsService::ClearDiscardedSuggestionsForDebugging() { 333 void NTPSnippetsService::ClearDiscardedSuggestionsForDebugging() {
333 if (!initialized()) 334 if (!initialized())
334 return; 335 return;
335 336
336 if (discarded_snippets_.empty()) 337 if (discarded_snippets_.empty())
337 return; 338 return;
338 339
339 database_->DeleteSnippets(discarded_snippets_); 340 database_->DeleteSnippets(discarded_snippets_);
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
612 613
613 // Fetching from the DB failed; start a network fetch. 614 // Fetching from the DB failed; start a network fetch.
614 FetchSnippetImageFromNetwork(snippet_id, callback); 615 FetchSnippetImageFromNetwork(snippet_id, callback);
615 } 616 }
616 617
617 void NTPSnippetsService::OnSnippetImageDecoded( 618 void NTPSnippetsService::OnSnippetImageDecoded(
618 const std::string& snippet_id, 619 const std::string& snippet_id,
619 const ImageFetchedCallback& callback, 620 const ImageFetchedCallback& callback,
620 const gfx::Image& image) { 621 const gfx::Image& image) {
621 if (!image.IsEmpty()) { 622 if (!image.IsEmpty()) {
622 callback.Run(snippet_id, image); 623 callback.Run(MakeUniqueID(ContentSuggestionsCategory::ARTICLES, snippet_id),
624 image);
623 return; 625 return;
624 } 626 }
625 627
626 // If decoding the image failed, delete the DB entry. 628 // If decoding the image failed, delete the DB entry.
627 database_->DeleteImage(snippet_id); 629 database_->DeleteImage(snippet_id);
628 630
629 FetchSnippetImageFromNetwork(snippet_id, callback); 631 FetchSnippetImageFromNetwork(snippet_id, callback);
630 } 632 }
631 633
632 void NTPSnippetsService::FetchSnippetImageFromNetwork( 634 void NTPSnippetsService::FetchSnippetImageFromNetwork(
633 const std::string& snippet_id, 635 const std::string& snippet_id,
634 const ImageFetchedCallback& callback) { 636 const ImageFetchedCallback& callback) {
635 auto it = 637 auto it =
636 std::find_if(snippets_.begin(), snippets_.end(), 638 std::find_if(snippets_.begin(), snippets_.end(),
637 [&snippet_id](const std::unique_ptr<NTPSnippet>& snippet) { 639 [&snippet_id](const std::unique_ptr<NTPSnippet>& snippet) {
638 return snippet->id() == snippet_id; 640 return snippet->id() == snippet_id;
639 }); 641 });
640 if (it == snippets_.end()) { 642 if (it == snippets_.end()) {
641 callback.Run(snippet_id, gfx::Image()); 643 callback.Run(MakeUniqueID(ContentSuggestionsCategory::ARTICLES, snippet_id),
644 gfx::Image());
642 return; 645 return;
643 } 646 }
644 647
645 const NTPSnippet& snippet = *it->get(); 648 const NTPSnippet& snippet = *it->get();
646 image_fetcher_->StartOrQueueNetworkRequest( 649 image_fetcher_->StartOrQueueNetworkRequest(
647 snippet.id(), snippet.salient_image_url(), callback); 650 snippet.id(), snippet.salient_image_url(), callback);
648 } 651 }
649 652
650 void NTPSnippetsService::EnterStateEnabled(bool fetch_snippets) { 653 void NTPSnippetsService::EnterStateEnabled(bool fetch_snippets) {
651 if (fetch_snippets) 654 if (fetch_snippets)
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
815 } 818 }
816 819
817 void NTPSnippetsService::NotifyCategoryStatusChanged() { 820 void NTPSnippetsService::NotifyCategoryStatusChanged() {
818 if (observer_) { 821 if (observer_) {
819 observer_->OnCategoryStatusChanged(ContentSuggestionsCategory::ARTICLES, 822 observer_->OnCategoryStatusChanged(ContentSuggestionsCategory::ARTICLES,
820 category_status_); 823 category_status_);
821 } 824 }
822 } 825 }
823 826
824 } // namespace ntp_snippets 827 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698