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

Side by Side Diff: components/ntp_snippets/remote/ntp_snippets_fetcher.cc

Issue 2402323002: [NTP Snippets] Persist non-article remote suggestions in the DB (Closed)
Patch Set: review Created 4 years, 2 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/remote/ntp_snippets_fetcher.h" 5 #include "components/ntp_snippets/remote/ntp_snippets_fetcher.h"
6 6
7 #include <cstdlib> 7 #include <cstdlib>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 endpoint != GURL(kContentSuggestionsDevServer) && 145 endpoint != GURL(kContentSuggestionsDevServer) &&
146 endpoint != GURL(kContentSuggestionsAlphaServer)) { 146 endpoint != GURL(kContentSuggestionsAlphaServer)) {
147 LOG(WARNING) << "Unknown value for " << kContentSuggestionsBackend << ": " 147 LOG(WARNING) << "Unknown value for " << kContentSuggestionsBackend << ": "
148 << "assuming chromecontentsuggestions-style API"; 148 << "assuming chromecontentsuggestions-style API";
149 } 149 }
150 return true; 150 return true;
151 } 151 }
152 152
153 // Creates snippets from dictionary values in |list| and adds them to 153 // Creates snippets from dictionary values in |list| and adds them to
154 // |snippets|. Returns true on success, false if anything went wrong. 154 // |snippets|. Returns true on success, false if anything went wrong.
155 // |remote_category_id| is only used if |content_suggestions_api| is true.
155 bool AddSnippetsFromListValue(bool content_suggestions_api, 156 bool AddSnippetsFromListValue(bool content_suggestions_api,
157 int remote_category_id,
156 const base::ListValue& list, 158 const base::ListValue& list,
157 NTPSnippet::PtrVector* snippets) { 159 NTPSnippet::PtrVector* snippets) {
158 for (const auto& value : list) { 160 for (const auto& value : list) {
159 const base::DictionaryValue* dict = nullptr; 161 const base::DictionaryValue* dict = nullptr;
160 if (!value->GetAsDictionary(&dict)) { 162 if (!value->GetAsDictionary(&dict)) {
161 return false; 163 return false;
162 } 164 }
163 165
164 std::unique_ptr<NTPSnippet> snippet; 166 std::unique_ptr<NTPSnippet> snippet;
165 if (content_suggestions_api) { 167 if (content_suggestions_api) {
166 snippet = NTPSnippet::CreateFromContentSuggestionsDictionary(*dict); 168 snippet = NTPSnippet::CreateFromContentSuggestionsDictionary(
169 *dict, remote_category_id);
167 } else { 170 } else {
168 snippet = NTPSnippet::CreateFromChromeReaderDictionary(*dict); 171 snippet = NTPSnippet::CreateFromChromeReaderDictionary(*dict);
169 } 172 }
170 if (!snippet) { 173 if (!snippet) {
171 return false; 174 return false;
172 } 175 }
173 176
174 snippets->push_back(std::move(snippet)); 177 snippets->push_back(std::move(snippet));
175 } 178 }
176 return true; 179 return true;
(...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after
622 625
623 bool NTPSnippetsFetcher::JsonToSnippets(const base::Value& parsed, 626 bool NTPSnippetsFetcher::JsonToSnippets(const base::Value& parsed,
624 FetchedCategoriesVector* categories) { 627 FetchedCategoriesVector* categories) {
625 const base::DictionaryValue* top_dict = nullptr; 628 const base::DictionaryValue* top_dict = nullptr;
626 if (!parsed.GetAsDictionary(&top_dict)) { 629 if (!parsed.GetAsDictionary(&top_dict)) {
627 return false; 630 return false;
628 } 631 }
629 632
630 switch (fetch_api_) { 633 switch (fetch_api_) {
631 case CHROME_READER_API: { 634 case CHROME_READER_API: {
635 const int kUnusedRemoteCategoryId = -1;
632 categories->push_back(FetchedCategory( 636 categories->push_back(FetchedCategory(
633 category_factory_->FromKnownCategory(KnownCategories::ARTICLES))); 637 category_factory_->FromKnownCategory(KnownCategories::ARTICLES)));
634 const base::ListValue* recos = nullptr; 638 const base::ListValue* recos = nullptr;
635 return top_dict->GetList("recos", &recos) && 639 return top_dict->GetList("recos", &recos) &&
636 AddSnippetsFromListValue(/* content_suggestions_api = */ false, 640 AddSnippetsFromListValue(/*content_suggestions_api=*/false,
641 kUnusedRemoteCategoryId,
637 *recos, &categories->back().snippets); 642 *recos, &categories->back().snippets);
638 } 643 }
639 644
640 case CHROME_CONTENT_SUGGESTIONS_API: { 645 case CHROME_CONTENT_SUGGESTIONS_API: {
641 const base::ListValue* categories_value = nullptr; 646 const base::ListValue* categories_value = nullptr;
642 if (!top_dict->GetList("categories", &categories_value)) { 647 if (!top_dict->GetList("categories", &categories_value)) {
643 return false; 648 return false;
644 } 649 }
645 650
646 for (const auto& v : *categories_value) { 651 for (const auto& v : *categories_value) {
647 std::string utf8_title; 652 std::string utf8_title;
648 int category_id = -1; 653 int remote_category_id = -1;
649 const base::DictionaryValue* category_value = nullptr; 654 const base::DictionaryValue* category_value = nullptr;
650 if (!(v->GetAsDictionary(&category_value) && 655 if (!(v->GetAsDictionary(&category_value) &&
651 category_value->GetString("localizedTitle", &utf8_title) && 656 category_value->GetString("localizedTitle", &utf8_title) &&
652 category_value->GetInteger("id", &category_id) && 657 category_value->GetInteger("id", &remote_category_id) &&
653 (category_id > 0))) { 658 (remote_category_id > 0))) {
654 return false; 659 return false;
655 } 660 }
656 661
657 categories->push_back(FetchedCategory( 662 categories->push_back(FetchedCategory(
658 category_factory_->FromRemoteCategory(category_id))); 663 category_factory_->FromRemoteCategory(remote_category_id)));
659 categories->back().localized_title = base::UTF8ToUTF16(utf8_title); 664 categories->back().localized_title = base::UTF8ToUTF16(utf8_title);
660 665
661 const base::ListValue* suggestions = nullptr; 666 const base::ListValue* suggestions = nullptr;
662 if (!category_value->GetList("suggestions", &suggestions)) { 667 if (!category_value->GetList("suggestions", &suggestions)) {
663 // Absence of a list of suggestions is treated as an empty list, which 668 // Absence of a list of suggestions is treated as an empty list, which
664 // is permissible. 669 // is permissible.
665 continue; 670 continue;
666 } 671 }
667 if (!AddSnippetsFromListValue( 672 if (!AddSnippetsFromListValue(
668 /* content_suggestions_api = */ true, *suggestions, 673 /*content_suggestions_api=*/true, remote_category_id,
669 &categories->back().snippets)) { 674 *suggestions, &categories->back().snippets)) {
670 return false; 675 return false;
671 } 676 }
672 } 677 }
673 return true; 678 return true;
674 } 679 }
675 } 680 }
676 NOTREACHED(); 681 NOTREACHED();
677 return false; 682 return false;
678 } 683 }
679 684
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
715 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", 720 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult",
716 static_cast<int>(result), 721 static_cast<int>(result),
717 static_cast<int>(FetchResult::RESULT_MAX)); 722 static_cast<int>(FetchResult::RESULT_MAX));
718 723
719 DVLOG(1) << "Fetch finished: " << last_status_; 724 DVLOG(1) << "Fetch finished: " << last_status_;
720 if (!snippets_available_callback_.is_null()) 725 if (!snippets_available_callback_.is_null())
721 snippets_available_callback_.Run(std::move(fetched_categories)); 726 snippets_available_callback_.Run(std::move(fetched_categories));
722 } 727 }
723 728
724 } // namespace ntp_snippets 729 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/remote/ntp_snippets_fetcher.h ('k') | components/ntp_snippets/remote/ntp_snippets_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698