Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 bool AddSnippetsFromListValue(bool content_suggestions_api, | 155 bool AddSnippetsFromListValue(bool content_suggestions_api, |
| 156 int remote_category_id, | |
| 156 const base::ListValue& list, | 157 const base::ListValue& list, |
| 157 NTPSnippet::PtrVector* snippets) { | 158 NTPSnippet::PtrVector* snippets) { |
| 158 for (const auto& value : list) { | 159 for (const auto& value : list) { |
| 159 const base::DictionaryValue* dict = nullptr; | 160 const base::DictionaryValue* dict = nullptr; |
| 160 if (!value->GetAsDictionary(&dict)) { | 161 if (!value->GetAsDictionary(&dict)) { |
| 161 return false; | 162 return false; |
| 162 } | 163 } |
| 163 | 164 |
| 164 std::unique_ptr<NTPSnippet> snippet; | 165 std::unique_ptr<NTPSnippet> snippet; |
| 165 if (content_suggestions_api) { | 166 if (content_suggestions_api) { |
| 166 snippet = NTPSnippet::CreateFromContentSuggestionsDictionary(*dict); | 167 snippet = NTPSnippet::CreateFromContentSuggestionsDictionary( |
| 168 *dict, remote_category_id); | |
| 167 } else { | 169 } else { |
| 168 snippet = NTPSnippet::CreateFromChromeReaderDictionary(*dict); | 170 snippet = NTPSnippet::CreateFromChromeReaderDictionary(*dict); |
| 169 } | 171 } |
| 170 if (!snippet) { | 172 if (!snippet) { |
| 171 return false; | 173 return false; |
| 172 } | 174 } |
| 173 | 175 |
| 174 snippets->push_back(std::move(snippet)); | 176 snippets->push_back(std::move(snippet)); |
| 175 } | 177 } |
| 176 return true; | 178 return true; |
| (...skipping 449 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 626 if (!parsed.GetAsDictionary(&top_dict)) { | 628 if (!parsed.GetAsDictionary(&top_dict)) { |
| 627 return false; | 629 return false; |
| 628 } | 630 } |
| 629 | 631 |
| 630 switch (fetch_api_) { | 632 switch (fetch_api_) { |
| 631 case CHROME_READER_API: { | 633 case CHROME_READER_API: { |
| 632 categories->push_back(FetchedCategory( | 634 categories->push_back(FetchedCategory( |
| 633 category_factory_->FromKnownCategory(KnownCategories::ARTICLES))); | 635 category_factory_->FromKnownCategory(KnownCategories::ARTICLES))); |
| 634 const base::ListValue* recos = nullptr; | 636 const base::ListValue* recos = nullptr; |
| 635 return top_dict->GetList("recos", &recos) && | 637 return top_dict->GetList("recos", &recos) && |
| 636 AddSnippetsFromListValue(/* content_suggestions_api = */ false, | 638 AddSnippetsFromListValue(/*content_suggestions_api=*/false, |
| 639 /*remote_category_id=*/-1, // unused | |
|
Bernhard Bauer
2016/10/10 13:54:54
Nit: Maybe put the value into a variable kUnusedRe
Marc Treib
2016/10/10 15:54:48
Done.
| |
| 637 *recos, &categories->back().snippets); | 640 *recos, &categories->back().snippets); |
| 638 } | 641 } |
| 639 | 642 |
| 640 case CHROME_CONTENT_SUGGESTIONS_API: { | 643 case CHROME_CONTENT_SUGGESTIONS_API: { |
| 641 const base::ListValue* categories_value = nullptr; | 644 const base::ListValue* categories_value = nullptr; |
| 642 if (!top_dict->GetList("categories", &categories_value)) { | 645 if (!top_dict->GetList("categories", &categories_value)) { |
| 643 return false; | 646 return false; |
| 644 } | 647 } |
| 645 | 648 |
| 646 for (const auto& v : *categories_value) { | 649 for (const auto& v : *categories_value) { |
| 647 std::string utf8_title; | 650 std::string utf8_title; |
| 648 int category_id = -1; | 651 int remote_category_id = -1; |
| 649 const base::DictionaryValue* category_value = nullptr; | 652 const base::DictionaryValue* category_value = nullptr; |
| 650 if (!(v->GetAsDictionary(&category_value) && | 653 if (!(v->GetAsDictionary(&category_value) && |
| 651 category_value->GetString("localizedTitle", &utf8_title) && | 654 category_value->GetString("localizedTitle", &utf8_title) && |
| 652 category_value->GetInteger("id", &category_id) && | 655 category_value->GetInteger("id", &remote_category_id) && |
| 653 (category_id > 0))) { | 656 (remote_category_id > 0))) { |
| 654 return false; | 657 return false; |
| 655 } | 658 } |
| 656 | 659 |
| 657 categories->push_back(FetchedCategory( | 660 categories->push_back(FetchedCategory( |
| 658 category_factory_->FromRemoteCategory(category_id))); | 661 category_factory_->FromRemoteCategory(remote_category_id))); |
| 659 categories->back().localized_title = base::UTF8ToUTF16(utf8_title); | 662 categories->back().localized_title = base::UTF8ToUTF16(utf8_title); |
| 660 | 663 |
| 661 const base::ListValue* suggestions = nullptr; | 664 const base::ListValue* suggestions = nullptr; |
| 662 if (!category_value->GetList("suggestions", &suggestions)) { | 665 if (!category_value->GetList("suggestions", &suggestions)) { |
| 663 // Absence of a list of suggestions is treated as an empty list, which | 666 // Absence of a list of suggestions is treated as an empty list, which |
| 664 // is permissible. | 667 // is permissible. |
| 665 continue; | 668 continue; |
| 666 } | 669 } |
| 667 if (!AddSnippetsFromListValue( | 670 if (!AddSnippetsFromListValue( |
| 668 /* content_suggestions_api = */ true, *suggestions, | 671 /*content_suggestions_api=*/true, remote_category_id, |
| 669 &categories->back().snippets)) { | 672 *suggestions, &categories->back().snippets)) { |
| 670 return false; | 673 return false; |
| 671 } | 674 } |
| 672 } | 675 } |
| 673 return true; | 676 return true; |
| 674 } | 677 } |
| 675 } | 678 } |
| 676 NOTREACHED(); | 679 NOTREACHED(); |
| 677 return false; | 680 return false; |
| 678 } | 681 } |
| 679 | 682 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 715 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", | 718 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", |
| 716 static_cast<int>(result), | 719 static_cast<int>(result), |
| 717 static_cast<int>(FetchResult::RESULT_MAX)); | 720 static_cast<int>(FetchResult::RESULT_MAX)); |
| 718 | 721 |
| 719 DVLOG(1) << "Fetch finished: " << last_status_; | 722 DVLOG(1) << "Fetch finished: " << last_status_; |
| 720 if (!snippets_available_callback_.is_null()) | 723 if (!snippets_available_callback_.is_null()) |
| 721 snippets_available_callback_.Run(std::move(fetched_categories)); | 724 snippets_available_callback_.Run(std::move(fetched_categories)); |
| 722 } | 725 } |
| 723 | 726 |
| 724 } // namespace ntp_snippets | 727 } // namespace ntp_snippets |
| OLD | NEW |