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" |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "base/values.h" | 21 #include "base/values.h" |
22 #include "components/data_use_measurement/core/data_use_user_data.h" | 22 #include "components/data_use_measurement/core/data_use_user_data.h" |
23 #include "components/ntp_snippets/category_factory.h" | 23 #include "components/ntp_snippets/category_factory.h" |
24 #include "components/ntp_snippets/ntp_snippets_constants.h" | 24 #include "components/ntp_snippets/ntp_snippets_constants.h" |
25 #include "components/ntp_snippets/switches.h" | 25 #include "components/ntp_snippets/switches.h" |
26 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 26 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
27 #include "components/signin/core/browser/signin_manager.h" | 27 #include "components/signin/core/browser/signin_manager.h" |
28 #include "components/signin/core/browser/signin_manager_base.h" | 28 #include "components/signin/core/browser/signin_manager_base.h" |
29 #include "components/variations/net/variations_http_headers.h" | 29 #include "components/variations/net/variations_http_headers.h" |
30 #include "components/variations/variations_associated_data.h" | 30 #include "components/variations/variations_associated_data.h" |
31 #include "google_apis/google_api_keys.h" | |
32 #include "net/base/load_flags.h" | 31 #include "net/base/load_flags.h" |
33 #include "net/http/http_request_headers.h" | 32 #include "net/http/http_request_headers.h" |
34 #include "net/http/http_response_headers.h" | 33 #include "net/http/http_response_headers.h" |
35 #include "net/http/http_status_code.h" | 34 #include "net/http/http_status_code.h" |
36 #include "net/url_request/url_fetcher.h" | 35 #include "net/url_request/url_fetcher.h" |
37 #include "third_party/icu/source/common/unicode/uloc.h" | 36 #include "third_party/icu/source/common/unicode/uloc.h" |
38 #include "third_party/icu/source/common/unicode/utypes.h" | 37 #include "third_party/icu/source/common/unicode/utypes.h" |
39 | 38 |
40 using net::URLFetcher; | 39 using net::URLFetcher; |
41 using net::URLRequestContextGetter; | 40 using net::URLRequestContextGetter; |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 | 173 |
175 } // namespace | 174 } // namespace |
176 | 175 |
177 NTPSnippetsFetcher::NTPSnippetsFetcher( | 176 NTPSnippetsFetcher::NTPSnippetsFetcher( |
178 SigninManagerBase* signin_manager, | 177 SigninManagerBase* signin_manager, |
179 OAuth2TokenService* token_service, | 178 OAuth2TokenService* token_service, |
180 scoped_refptr<URLRequestContextGetter> url_request_context_getter, | 179 scoped_refptr<URLRequestContextGetter> url_request_context_getter, |
181 PrefService* pref_service, | 180 PrefService* pref_service, |
182 CategoryFactory* category_factory, | 181 CategoryFactory* category_factory, |
183 const ParseJSONCallback& parse_json_callback, | 182 const ParseJSONCallback& parse_json_callback, |
184 bool is_stable_channel) | 183 const std::string& api_key) |
185 : OAuth2TokenService::Consumer("ntp_snippets"), | 184 : OAuth2TokenService::Consumer("ntp_snippets"), |
186 signin_manager_(signin_manager), | 185 signin_manager_(signin_manager), |
187 token_service_(token_service), | 186 token_service_(token_service), |
188 waiting_for_refresh_token_(false), | 187 waiting_for_refresh_token_(false), |
189 url_request_context_getter_(url_request_context_getter), | 188 url_request_context_getter_(url_request_context_getter), |
190 category_factory_(category_factory), | 189 category_factory_(category_factory), |
191 parse_json_callback_(parse_json_callback), | 190 parse_json_callback_(parse_json_callback), |
192 fetch_url_(GetFetchEndpoint()), | 191 fetch_url_(GetFetchEndpoint()), |
193 fetch_api_(UsesChromeContentSuggestionsAPI(fetch_url_) | 192 fetch_api_(UsesChromeContentSuggestionsAPI(fetch_url_) |
194 ? CHROME_CONTENT_SUGGESTIONS_API | 193 ? CHROME_CONTENT_SUGGESTIONS_API |
195 : CHROME_READER_API), | 194 : CHROME_READER_API), |
196 is_stable_channel_(is_stable_channel), | 195 api_key_(api_key), |
197 interactive_request_(false), | 196 interactive_request_(false), |
198 tick_clock_(new base::DefaultTickClock()), | 197 tick_clock_(new base::DefaultTickClock()), |
199 request_throttler_( | 198 request_throttler_( |
200 pref_service, | 199 pref_service, |
201 RequestThrottler::RequestType::CONTENT_SUGGESTION_FETCHER), | 200 RequestThrottler::RequestType::CONTENT_SUGGESTION_FETCHER), |
202 oauth_token_retried_(false), | 201 oauth_token_retried_(false), |
203 weak_ptr_factory_(this) { | 202 weak_ptr_factory_(this) { |
204 // Parse the variation parameters and set the defaults if missing. | 203 // Parse the variation parameters and set the defaults if missing. |
205 std::string personalization = variations::GetVariationParamValue( | 204 std::string personalization = variations::GetVariationParamValue( |
206 ntp_snippets::kStudyName, kPersonalizationName); | 205 ntp_snippets::kStudyName, kPersonalizationName); |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 switches::kDontRestrict); | 423 switches::kDontRestrict); |
425 } | 424 } |
426 | 425 |
427 bool NTPSnippetsFetcher::UsesAuthentication() const { | 426 bool NTPSnippetsFetcher::UsesAuthentication() const { |
428 return (personalization_ == Personalization::kPersonal || | 427 return (personalization_ == Personalization::kPersonal || |
429 personalization_ == Personalization::kBoth); | 428 personalization_ == Personalization::kBoth); |
430 } | 429 } |
431 | 430 |
432 void NTPSnippetsFetcher::FetchSnippetsNonAuthenticated() { | 431 void NTPSnippetsFetcher::FetchSnippetsNonAuthenticated() { |
433 // When not providing OAuth token, we need to pass the Google API key. | 432 // When not providing OAuth token, we need to pass the Google API key. |
434 const std::string& key = is_stable_channel_ | |
435 ? google_apis::GetAPIKey() | |
436 : google_apis::GetNonStableAPIKey(); | |
437 GURL url(base::StringPrintf(kSnippetsServerNonAuthorizedFormat, | 433 GURL url(base::StringPrintf(kSnippetsServerNonAuthorizedFormat, |
438 fetch_url_.spec().c_str(), key.c_str())); | 434 fetch_url_.spec().c_str(), api_key_.c_str())); |
439 | 435 |
440 RequestParams params; | 436 RequestParams params; |
441 params.fetch_api = fetch_api_; | 437 params.fetch_api = fetch_api_; |
442 params.host_restricts = | 438 params.host_restricts = |
443 UsesHostRestrictions() ? hosts_ : std::set<std::string>(); | 439 UsesHostRestrictions() ? hosts_ : std::set<std::string>(); |
444 params.excluded_ids = excluded_ids_; | 440 params.excluded_ids = excluded_ids_; |
445 params.count_to_fetch = count_to_fetch_; | 441 params.count_to_fetch = count_to_fetch_; |
446 params.interactive_request = interactive_request_; | 442 params.interactive_request = interactive_request_; |
447 FetchSnippetsImpl(url, std::string(), params.BuildRequest()); | 443 FetchSnippetsImpl(url, std::string(), params.BuildRequest()); |
448 } | 444 } |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
650 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", | 646 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", |
651 static_cast<int>(result), | 647 static_cast<int>(result), |
652 static_cast<int>(FetchResult::RESULT_MAX)); | 648 static_cast<int>(FetchResult::RESULT_MAX)); |
653 | 649 |
654 DVLOG(1) << "Fetch finished: " << last_status_; | 650 DVLOG(1) << "Fetch finished: " << last_status_; |
655 if (!snippets_available_callback_.is_null()) | 651 if (!snippets_available_callback_.is_null()) |
656 snippets_available_callback_.Run(std::move(snippets)); | 652 snippets_available_callback_.Run(std::move(snippets)); |
657 } | 653 } |
658 | 654 |
659 } // namespace ntp_snippets | 655 } // namespace ntp_snippets |
OLD | NEW |