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

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

Issue 2325473003: Support server-provided category names. (Closed)
Patch Set: Created 4 years, 3 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/ntp_snippets_fetcher.h" 5 #include "components/ntp_snippets/ntp_snippets_fetcher.h"
6 6
7 #include <stdlib.h> 7 #include <stdlib.h>
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"
11 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
12 #include "base/json/json_writer.h" 12 #include "base/json/json_writer.h"
13 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
14 #include "base/metrics/histogram_macros.h" 14 #include "base/metrics/histogram_macros.h"
15 #include "base/metrics/sparse_histogram.h" 15 #include "base/metrics/sparse_histogram.h"
16 #include "base/path_service.h" 16 #include "base/path_service.h"
17 #include "base/strings/string_number_conversions.h" 17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_util.h" 18 #include "base/strings/string_util.h"
19 #include "base/strings/stringprintf.h" 19 #include "base/strings/stringprintf.h"
20 #include "base/strings/utf_string_conversions.h"
20 #include "base/time/default_tick_clock.h" 21 #include "base/time/default_tick_clock.h"
21 #include "base/values.h" 22 #include "base/values.h"
22 #include "components/data_use_measurement/core/data_use_user_data.h" 23 #include "components/data_use_measurement/core/data_use_user_data.h"
23 #include "components/ntp_snippets/category_factory.h" 24 #include "components/ntp_snippets/category_factory.h"
24 #include "components/ntp_snippets/ntp_snippets_constants.h" 25 #include "components/ntp_snippets/ntp_snippets_constants.h"
25 #include "components/ntp_snippets/switches.h" 26 #include "components/ntp_snippets/switches.h"
26 #include "components/signin/core/browser/profile_oauth2_token_service.h" 27 #include "components/signin/core/browser/profile_oauth2_token_service.h"
27 #include "components/signin/core/browser/signin_manager.h" 28 #include "components/signin/core/browser/signin_manager.h"
28 #include "components/signin/core/browser/signin_manager_base.h" 29 #include "components/signin/core/browser/signin_manager_base.h"
29 #include "components/variations/net/variations_http_headers.h" 30 #include "components/variations/net/variations_http_headers.h"
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 // Translate the input to a posix locale. 168 // Translate the input to a posix locale.
168 uloc_forLanguageTag(language_code.c_str(), locale, ULOC_FULLNAME_CAPACITY, 169 uloc_forLanguageTag(language_code.c_str(), locale, ULOC_FULLNAME_CAPACITY,
169 nullptr, &error); 170 nullptr, &error);
170 DLOG_IF(WARNING, U_ZERO_ERROR != error) 171 DLOG_IF(WARNING, U_ZERO_ERROR != error)
171 << "Error in translating language code to a locale string: " << error; 172 << "Error in translating language code to a locale string: " << error;
172 return locale; 173 return locale;
173 } 174 }
174 175
175 } // namespace 176 } // namespace
176 177
178 NTPSnippetsFetcher::FetchedCategory::FetchedCategory(Category c)
179 : category(c) {}
180
181 NTPSnippetsFetcher::FetchedCategory::FetchedCategory(FetchedCategory&&) =
182 default;
183 NTPSnippetsFetcher::FetchedCategory::~FetchedCategory() = default;
184 NTPSnippetsFetcher::FetchedCategory& NTPSnippetsFetcher::FetchedCategory::
185 operator=(FetchedCategory&&) = default;
186
177 NTPSnippetsFetcher::NTPSnippetsFetcher( 187 NTPSnippetsFetcher::NTPSnippetsFetcher(
178 SigninManagerBase* signin_manager, 188 SigninManagerBase* signin_manager,
179 OAuth2TokenService* token_service, 189 OAuth2TokenService* token_service,
180 scoped_refptr<URLRequestContextGetter> url_request_context_getter, 190 scoped_refptr<URLRequestContextGetter> url_request_context_getter,
181 PrefService* pref_service, 191 PrefService* pref_service,
182 CategoryFactory* category_factory, 192 CategoryFactory* category_factory,
183 const ParseJSONCallback& parse_json_callback, 193 const ParseJSONCallback& parse_json_callback,
184 bool is_stable_channel) 194 bool is_stable_channel)
185 : OAuth2TokenService::Consumer("ntp_snippets"), 195 : OAuth2TokenService::Consumer("ntp_snippets"),
186 signin_manager_(signin_manager), 196 signin_manager_(signin_manager),
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after
556 566
557 parse_json_callback_.Run(last_fetch_json_, 567 parse_json_callback_.Run(last_fetch_json_,
558 base::Bind(&NTPSnippetsFetcher::OnJsonParsed, 568 base::Bind(&NTPSnippetsFetcher::OnJsonParsed,
559 weak_ptr_factory_.GetWeakPtr()), 569 weak_ptr_factory_.GetWeakPtr()),
560 base::Bind(&NTPSnippetsFetcher::OnJsonError, 570 base::Bind(&NTPSnippetsFetcher::OnJsonError,
561 weak_ptr_factory_.GetWeakPtr())); 571 weak_ptr_factory_.GetWeakPtr()));
562 } 572 }
563 } 573 }
564 574
565 bool NTPSnippetsFetcher::JsonToSnippets(const base::Value& parsed, 575 bool NTPSnippetsFetcher::JsonToSnippets(const base::Value& parsed,
566 NTPSnippet::CategoryMap* snippets) { 576 FetchedCategoriesVector* categories) {
567 const base::DictionaryValue* top_dict = nullptr; 577 const base::DictionaryValue* top_dict = nullptr;
568 if (!parsed.GetAsDictionary(&top_dict)) { 578 if (!parsed.GetAsDictionary(&top_dict)) {
569 return false; 579 return false;
570 } 580 }
571 581
572 switch (fetch_api_) { 582 switch (fetch_api_) {
573 case CHROME_READER_API: { 583 case CHROME_READER_API: {
574 Category category = 584 categories->push_back(FetchedCategory(
575 category_factory_->FromKnownCategory(KnownCategories::ARTICLES); 585 category_factory_->FromKnownCategory(KnownCategories::ARTICLES)));
576 NTPSnippet::PtrVector* articles = &(*snippets)[category];
577 const base::ListValue* recos = nullptr; 586 const base::ListValue* recos = nullptr;
578 return top_dict->GetList("recos", &recos) && 587 return top_dict->GetList("recos", &recos) &&
579 AddSnippetsFromListValue(/* content_suggestions_api = */ false, 588 AddSnippetsFromListValue(/* content_suggestions_api = */ false,
580 *recos, articles); 589 *recos, &categories->back().snippets);
581 } 590 }
582 591
583 case CHROME_CONTENT_SUGGESTIONS_API: { 592 case CHROME_CONTENT_SUGGESTIONS_API: {
584 const base::ListValue* categories = nullptr; 593 const base::ListValue* categories_value = nullptr;
585 if (!top_dict->GetList("categories", &categories)) { 594 if (!top_dict->GetList("categories", &categories_value)) {
586 return false; 595 return false;
587 } 596 }
588 597
589 for (const auto& v : *categories) { 598 for (const auto& v : *categories_value) {
599 std::string utf8_title;
590 int category_id = -1; 600 int category_id = -1;
591 const base::DictionaryValue* category_value = nullptr; 601 const base::DictionaryValue* category_value = nullptr;
592 if (!(v->GetAsDictionary(&category_value) && 602 if (!(v->GetAsDictionary(&category_value) &&
603 category_value->GetString("localizedTitle", &utf8_title) &&
593 category_value->GetInteger("id", &category_id) && 604 category_value->GetInteger("id", &category_id) &&
594 (category_id > 0))) { 605 (category_id > 0))) {
595 return false; 606 return false;
596 } 607 }
597 Category category = category_factory_->FromRemoteCategory(category_id); 608
609 categories->push_back(FetchedCategory(
610 category_factory_->FromRemoteCategory(category_id)));
611 categories->back().localized_title = base::UTF8ToUTF16(utf8_title);
612
598 const base::ListValue* suggestions = nullptr; 613 const base::ListValue* suggestions = nullptr;
599 NTPSnippet::PtrVector* articles = &(*snippets)[category];
600 if (!category_value->GetList("suggestions", &suggestions)) { 614 if (!category_value->GetList("suggestions", &suggestions)) {
601 // Absence of a list of suggestions is treated as an empty list, which 615 // Absence of a list of suggestions is treated as an empty list, which
602 // is permissible. 616 // is permissible.
603 continue; 617 continue;
604 } 618 }
605 if (!AddSnippetsFromListValue( 619 if (!AddSnippetsFromListValue(
606 /* content_suggestions_api = */ true, *suggestions, articles)) { 620 /* content_suggestions_api = */ true, *suggestions,
621 &categories->back().snippets)) {
607 return false; 622 return false;
608 } 623 }
609 } 624 }
610 return true; 625 return true;
611 } 626 }
612 } 627 }
613 NOTREACHED(); 628 NOTREACHED();
614 return false; 629 return false;
615 } 630 }
616 631
617 void NTPSnippetsFetcher::OnJsonParsed(std::unique_ptr<base::Value> parsed) { 632 void NTPSnippetsFetcher::OnJsonParsed(std::unique_ptr<base::Value> parsed) {
618 NTPSnippet::CategoryMap snippets; 633 FetchedCategoriesVector categories;
619 if (JsonToSnippets(*parsed, &snippets)) { 634 if (JsonToSnippets(*parsed, &categories)) {
620 FetchFinished(OptionalSnippets(std::move(snippets)), FetchResult::SUCCESS, 635 FetchFinished(OptionalSnippets(std::move(categories)), FetchResult::SUCCESS,
621 /*extra_message=*/std::string()); 636 /*extra_message=*/std::string());
622 } else { 637 } else {
623 LOG(WARNING) << "Received invalid snippets: " << last_fetch_json_; 638 LOG(WARNING) << "Received invalid snippets: " << last_fetch_json_;
624 FetchFinished(OptionalSnippets(), 639 FetchFinished(OptionalSnippets(),
625 FetchResult::INVALID_SNIPPET_CONTENT_ERROR, 640 FetchResult::INVALID_SNIPPET_CONTENT_ERROR,
626 /*extra_message=*/std::string()); 641 /*extra_message=*/std::string());
627 } 642 }
628 } 643 }
629 644
630 void NTPSnippetsFetcher::OnJsonError(const std::string& error) { 645 void NTPSnippetsFetcher::OnJsonError(const std::string& error) {
(...skipping 19 matching lines...) Expand all
650 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", 665 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult",
651 static_cast<int>(result), 666 static_cast<int>(result),
652 static_cast<int>(FetchResult::RESULT_MAX)); 667 static_cast<int>(FetchResult::RESULT_MAX));
653 668
654 DVLOG(1) << "Fetch finished: " << last_status_; 669 DVLOG(1) << "Fetch finished: " << last_status_;
655 if (!snippets_available_callback_.is_null()) 670 if (!snippets_available_callback_.is_null())
656 snippets_available_callback_.Run(std::move(snippets)); 671 snippets_available_callback_.Run(std::move(snippets));
657 } 672 }
658 673
659 } // namespace ntp_snippets 674 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/ntp_snippets_fetcher.h ('k') | components/ntp_snippets/ntp_snippets_fetcher_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698