| 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/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 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 // Translate the input to a posix locale. | 145 // Translate the input to a posix locale. |
| 145 uloc_forLanguageTag(language_code.c_str(), locale, ULOC_FULLNAME_CAPACITY, | 146 uloc_forLanguageTag(language_code.c_str(), locale, ULOC_FULLNAME_CAPACITY, |
| 146 nullptr, &error); | 147 nullptr, &error); |
| 147 DLOG_IF(WARNING, U_ZERO_ERROR != error) | 148 DLOG_IF(WARNING, U_ZERO_ERROR != error) |
| 148 << "Error in translating language code to a locale string: " << error; | 149 << "Error in translating language code to a locale string: " << error; |
| 149 return locale; | 150 return locale; |
| 150 } | 151 } |
| 151 | 152 |
| 152 } // namespace | 153 } // namespace |
| 153 | 154 |
| 155 NTPSnippetsFetcher::FetchedCategory::FetchedCategory(Category c) |
| 156 : category(c) {} |
| 157 |
| 158 NTPSnippetsFetcher::FetchedCategory::FetchedCategory(FetchedCategory&&) = |
| 159 default; |
| 160 NTPSnippetsFetcher::FetchedCategory::~FetchedCategory() = default; |
| 161 NTPSnippetsFetcher::FetchedCategory& NTPSnippetsFetcher::FetchedCategory:: |
| 162 operator=(FetchedCategory&&) = default; |
| 163 |
| 154 NTPSnippetsFetcher::NTPSnippetsFetcher( | 164 NTPSnippetsFetcher::NTPSnippetsFetcher( |
| 155 SigninManagerBase* signin_manager, | 165 SigninManagerBase* signin_manager, |
| 156 OAuth2TokenService* token_service, | 166 OAuth2TokenService* token_service, |
| 157 scoped_refptr<URLRequestContextGetter> url_request_context_getter, | 167 scoped_refptr<URLRequestContextGetter> url_request_context_getter, |
| 158 PrefService* pref_service, | 168 PrefService* pref_service, |
| 159 CategoryFactory* category_factory, | 169 CategoryFactory* category_factory, |
| 160 const ParseJSONCallback& parse_json_callback, | 170 const ParseJSONCallback& parse_json_callback, |
| 161 bool is_stable_channel) | 171 bool is_stable_channel) |
| 162 : OAuth2TokenService::Consumer("ntp_snippets"), | 172 : OAuth2TokenService::Consumer("ntp_snippets"), |
| 163 signin_manager_(signin_manager), | 173 signin_manager_(signin_manager), |
| (...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 519 parse_json_callback_.Run( | 529 parse_json_callback_.Run( |
| 520 last_fetch_json_, | 530 last_fetch_json_, |
| 521 base::Bind(&NTPSnippetsFetcher::OnJsonParsed, | 531 base::Bind(&NTPSnippetsFetcher::OnJsonParsed, |
| 522 weak_ptr_factory_.GetWeakPtr()), | 532 weak_ptr_factory_.GetWeakPtr()), |
| 523 base::Bind(&NTPSnippetsFetcher::OnJsonError, | 533 base::Bind(&NTPSnippetsFetcher::OnJsonError, |
| 524 weak_ptr_factory_.GetWeakPtr())); | 534 weak_ptr_factory_.GetWeakPtr())); |
| 525 } | 535 } |
| 526 } | 536 } |
| 527 | 537 |
| 528 bool NTPSnippetsFetcher::JsonToSnippets(const base::Value& parsed, | 538 bool NTPSnippetsFetcher::JsonToSnippets(const base::Value& parsed, |
| 529 NTPSnippet::CategoryMap* snippets) { | 539 FetchedCategoriesVector* categories) { |
| 530 const base::DictionaryValue* top_dict = nullptr; | 540 const base::DictionaryValue* top_dict = nullptr; |
| 531 if (!parsed.GetAsDictionary(&top_dict)) { | 541 if (!parsed.GetAsDictionary(&top_dict)) { |
| 532 return false; | 542 return false; |
| 533 } | 543 } |
| 534 | 544 |
| 535 switch (fetch_api_) { | 545 switch (fetch_api_) { |
| 536 case CHROME_READER_API: { | 546 case CHROME_READER_API: { |
| 537 Category category = | 547 categories->push_back(FetchedCategory( |
| 538 category_factory_->FromKnownCategory(KnownCategories::ARTICLES); | 548 category_factory_->FromKnownCategory(KnownCategories::ARTICLES))); |
| 539 NTPSnippet::PtrVector* articles = &(*snippets)[category]; | |
| 540 const base::ListValue* recos = nullptr; | 549 const base::ListValue* recos = nullptr; |
| 541 return top_dict->GetList("recos", &recos) && | 550 return top_dict->GetList("recos", &recos) && |
| 542 AddSnippetsFromListValue(/* content_suggestions_api = */ false, | 551 AddSnippetsFromListValue(/* content_suggestions_api = */ false, |
| 543 *recos, articles); | 552 *recos, &categories->back().snippets); |
| 544 } | 553 } |
| 545 | 554 |
| 546 case CHROME_CONTENT_SUGGESTIONS_API: { | 555 case CHROME_CONTENT_SUGGESTIONS_API: { |
| 547 const base::ListValue* categories = nullptr; | 556 const base::ListValue* categories_value = nullptr; |
| 548 if (!top_dict->GetList("categories", &categories)) { | 557 if (!top_dict->GetList("categories", &categories_value)) { |
| 549 return false; | 558 return false; |
| 550 } | 559 } |
| 551 | 560 |
| 552 for (const auto& v : *categories) { | 561 for (const auto& v : *categories_value) { |
| 562 std::string utf8_title; |
| 553 int category_id = -1; | 563 int category_id = -1; |
| 554 const base::DictionaryValue* category_value = nullptr; | 564 const base::DictionaryValue* category_value = nullptr; |
| 555 if (!(v->GetAsDictionary(&category_value) && | 565 if (!(v->GetAsDictionary(&category_value) && |
| 566 category_value->GetString("localizedTitle", &utf8_title) && |
| 556 category_value->GetInteger("id", &category_id) && | 567 category_value->GetInteger("id", &category_id) && |
| 557 (category_id > 0))) { | 568 (category_id > 0))) { |
| 558 return false; | 569 return false; |
| 559 } | 570 } |
| 560 Category category = category_factory_->FromRemoteCategory(category_id); | 571 |
| 572 categories->push_back(FetchedCategory( |
| 573 category_factory_->FromRemoteCategory(category_id))); |
| 574 categories->back().localized_title = base::UTF8ToUTF16(utf8_title); |
| 575 |
| 561 const base::ListValue* suggestions = nullptr; | 576 const base::ListValue* suggestions = nullptr; |
| 562 NTPSnippet::PtrVector* articles = &(*snippets)[category]; | |
| 563 if (!category_value->GetList("suggestions", &suggestions)) { | 577 if (!category_value->GetList("suggestions", &suggestions)) { |
| 564 // Absence of a list of suggestions is treated as an empty list, which | 578 // Absence of a list of suggestions is treated as an empty list, which |
| 565 // is permissible. | 579 // is permissible. |
| 566 continue; | 580 continue; |
| 567 } | 581 } |
| 568 if (!AddSnippetsFromListValue( | 582 if (!AddSnippetsFromListValue( |
| 569 /* content_suggestions_api = */ true, *suggestions, articles)) { | 583 /* content_suggestions_api = */ true, *suggestions, |
| 584 &categories->back().snippets)) { |
| 570 return false; | 585 return false; |
| 571 } | 586 } |
| 572 } | 587 } |
| 573 return true; | 588 return true; |
| 574 } | 589 } |
| 575 } | 590 } |
| 576 NOTREACHED(); | 591 NOTREACHED(); |
| 577 return false; | 592 return false; |
| 578 } | 593 } |
| 579 | 594 |
| 580 void NTPSnippetsFetcher::OnJsonParsed(std::unique_ptr<base::Value> parsed) { | 595 void NTPSnippetsFetcher::OnJsonParsed(std::unique_ptr<base::Value> parsed) { |
| 581 NTPSnippet::CategoryMap snippets; | 596 FetchedCategoriesVector categories; |
| 582 if (JsonToSnippets(*parsed, &snippets)) { | 597 if (JsonToSnippets(*parsed, &categories)) { |
| 583 FetchFinished(OptionalSnippets(std::move(snippets)), FetchResult::SUCCESS, | 598 FetchFinished(OptionalSnippets(std::move(categories)), FetchResult::SUCCESS, |
| 584 /*extra_message=*/std::string()); | 599 /*extra_message=*/std::string()); |
| 585 } else { | 600 } else { |
| 586 LOG(WARNING) << "Received invalid snippets: " << last_fetch_json_; | 601 LOG(WARNING) << "Received invalid snippets: " << last_fetch_json_; |
| 587 FetchFinished(OptionalSnippets(), | 602 FetchFinished(OptionalSnippets(), |
| 588 FetchResult::INVALID_SNIPPET_CONTENT_ERROR, | 603 FetchResult::INVALID_SNIPPET_CONTENT_ERROR, |
| 589 /*extra_message=*/std::string()); | 604 /*extra_message=*/std::string()); |
| 590 } | 605 } |
| 591 } | 606 } |
| 592 | 607 |
| 593 void NTPSnippetsFetcher::OnJsonError(const std::string& error) { | 608 void NTPSnippetsFetcher::OnJsonError(const std::string& error) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 614 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", | 629 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", |
| 615 static_cast<int>(result), | 630 static_cast<int>(result), |
| 616 static_cast<int>(FetchResult::RESULT_MAX)); | 631 static_cast<int>(FetchResult::RESULT_MAX)); |
| 617 | 632 |
| 618 DVLOG(1) << "Fetch finished: " << last_status_; | 633 DVLOG(1) << "Fetch finished: " << last_status_; |
| 619 if (!snippets_available_callback_.is_null()) | 634 if (!snippets_available_callback_.is_null()) |
| 620 snippets_available_callback_.Run(std::move(snippets)); | 635 snippets_available_callback_.Run(std::move(snippets)); |
| 621 } | 636 } |
| 622 | 637 |
| 623 } // namespace ntp_snippets | 638 } // namespace ntp_snippets |
| OLD | NEW |