| 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 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 snippets_available_callback_ = callback; | 194 snippets_available_callback_ = callback; |
| 195 } | 195 } |
| 196 | 196 |
| 197 void NTPSnippetsFetcher::FetchSnippetsFromHosts( | 197 void NTPSnippetsFetcher::FetchSnippetsFromHosts( |
| 198 const std::set<std::string>& hosts, | 198 const std::set<std::string>& hosts, |
| 199 const std::string& language_code, | 199 const std::string& language_code, |
| 200 int count) { | 200 int count) { |
| 201 hosts_ = hosts; | 201 hosts_ = hosts; |
| 202 fetch_start_time_ = tick_clock_->NowTicks(); | 202 fetch_start_time_ = tick_clock_->NowTicks(); |
| 203 | 203 |
| 204 if (UseHostRestriction() && hosts_.empty()) { | 204 if (UsesHostRestrictions() && hosts_.empty()) { |
| 205 FetchFinished(OptionalSnippets(), FetchResult::EMPTY_HOSTS, | 205 FetchFinished(OptionalSnippets(), FetchResult::EMPTY_HOSTS, |
| 206 /*extra_message=*/std::string()); | 206 /*extra_message=*/std::string()); |
| 207 return; | 207 return; |
| 208 } | 208 } |
| 209 | 209 |
| 210 // Translate the BCP 47 |language_code| into a posix locale string. | 210 // Translate the BCP 47 |language_code| into a posix locale string. |
| 211 char locale[ULOC_FULLNAME_CAPACITY]; | 211 char locale[ULOC_FULLNAME_CAPACITY]; |
| 212 UErrorCode error = U_ZERO_ERROR; | 212 UErrorCode error = U_ZERO_ERROR; |
| 213 uloc_forLanguageTag(language_code.c_str(), locale, ULOC_FULLNAME_CAPACITY, | 213 uloc_forLanguageTag(language_code.c_str(), locale, ULOC_FULLNAME_CAPACITY, |
| 214 nullptr, &error); | 214 nullptr, &error); |
| 215 DLOG_IF(WARNING, U_ZERO_ERROR != error) | 215 DLOG_IF(WARNING, U_ZERO_ERROR != error) |
| 216 << "Error in translating language code to a locale string: " << error; | 216 << "Error in translating language code to a locale string: " << error; |
| 217 locale_ = locale; | 217 locale_ = locale; |
| 218 | 218 |
| 219 count_to_fetch_ = count; | 219 count_to_fetch_ = count; |
| 220 | 220 |
| 221 bool use_authentication = UseAuthentication(); | 221 bool use_authentication = UsesAuthentication(); |
| 222 | 222 |
| 223 if (use_authentication && signin_manager_->IsAuthenticated()) { | 223 if (use_authentication && signin_manager_->IsAuthenticated()) { |
| 224 // Signed-in: get OAuth token --> fetch snippets. | 224 // Signed-in: get OAuth token --> fetch snippets. |
| 225 StartTokenRequest(); | 225 StartTokenRequest(); |
| 226 } else if (use_authentication && signin_manager_->AuthInProgress()) { | 226 } else if (use_authentication && signin_manager_->AuthInProgress()) { |
| 227 // Currently signing in: wait for auth to finish (the refresh token) --> | 227 // Currently signing in: wait for auth to finish (the refresh token) --> |
| 228 // get OAuth token --> fetch snippets. | 228 // get OAuth token --> fetch snippets. |
| 229 if (!waiting_for_refresh_token_) { | 229 if (!waiting_for_refresh_token_) { |
| 230 // Wait until we get a refresh token. | 230 // Wait until we get a refresh token. |
| 231 waiting_for_refresh_token_ = true; | 231 waiting_for_refresh_token_ = true; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 257 url_fetcher_->SetUploadData("application/json", request); | 257 url_fetcher_->SetUploadData("application/json", request); |
| 258 // Fetchers are sometimes cancelled because a network change was detected. | 258 // Fetchers are sometimes cancelled because a network change was detected. |
| 259 url_fetcher_->SetAutomaticallyRetryOnNetworkChanges(3); | 259 url_fetcher_->SetAutomaticallyRetryOnNetworkChanges(3); |
| 260 // Try to make fetching the files bit more robust even with poor connection. | 260 // Try to make fetching the files bit more robust even with poor connection. |
| 261 url_fetcher_->SetMaxRetriesOn5xx(3); | 261 url_fetcher_->SetMaxRetriesOn5xx(3); |
| 262 url_fetcher_->Start(); | 262 url_fetcher_->Start(); |
| 263 } | 263 } |
| 264 | 264 |
| 265 std::string NTPSnippetsFetcher::GetHostRestricts() const { | 265 std::string NTPSnippetsFetcher::GetHostRestricts() const { |
| 266 std::string host_restricts; | 266 std::string host_restricts; |
| 267 if (UseHostRestriction()) { | 267 if (UsesHostRestrictions()) { |
| 268 for (const std::string& host : hosts_) { | 268 for (const std::string& host : hosts_) { |
| 269 if (!host_restricts.empty()) | 269 if (!host_restricts.empty()) |
| 270 host_restricts.push_back(','); | 270 host_restricts.push_back(','); |
| 271 host_restricts += base::StringPrintf(kHostRestrictFormat, host.c_str()); | 271 host_restricts += base::StringPrintf(kHostRestrictFormat, host.c_str()); |
| 272 } | 272 } |
| 273 } | 273 } |
| 274 return host_restricts; | 274 return host_restricts; |
| 275 } | 275 } |
| 276 | 276 |
| 277 bool NTPSnippetsFetcher::UseHostRestriction() const { | 277 bool NTPSnippetsFetcher::UsesHostRestrictions() const { |
| 278 return use_host_restriction_ && | 278 return use_host_restriction_ && |
| 279 !base::CommandLine::ForCurrentProcess()->HasSwitch( | 279 !base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 280 switches::kDontRestrict); | 280 switches::kDontRestrict); |
| 281 } | 281 } |
| 282 | 282 |
| 283 bool NTPSnippetsFetcher::UseAuthentication() const { | 283 bool NTPSnippetsFetcher::UsesAuthentication() const { |
| 284 return (personalization_ == Personalization::kPersonal || | 284 return (personalization_ == Personalization::kPersonal || |
| 285 personalization_ == Personalization::kBoth); | 285 personalization_ == Personalization::kBoth); |
| 286 } | 286 } |
| 287 | 287 |
| 288 void NTPSnippetsFetcher::FetchSnippetsNonAuthenticated() { | 288 void NTPSnippetsFetcher::FetchSnippetsNonAuthenticated() { |
| 289 // When not providing OAuth token, we need to pass the Google API key. | 289 // When not providing OAuth token, we need to pass the Google API key. |
| 290 const std::string& key = is_stable_channel_ | 290 const std::string& key = is_stable_channel_ |
| 291 ? google_apis::GetAPIKey() | 291 ? google_apis::GetAPIKey() |
| 292 : google_apis::GetNonStableAPIKey(); | 292 : google_apis::GetNonStableAPIKey(); |
| 293 GURL url(base::StringPrintf(kSnippetsServerNonAuthorizedFormat, | 293 GURL url(base::StringPrintf(kSnippetsServerNonAuthorizedFormat, |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 tick_clock_->NowTicks() - fetch_start_time_); | 433 tick_clock_->NowTicks() - fetch_start_time_); |
| 434 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", | 434 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", |
| 435 static_cast<int>(result), | 435 static_cast<int>(result), |
| 436 static_cast<int>(FetchResult::RESULT_MAX)); | 436 static_cast<int>(FetchResult::RESULT_MAX)); |
| 437 | 437 |
| 438 if (!snippets_available_callback_.is_null()) | 438 if (!snippets_available_callback_.is_null()) |
| 439 snippets_available_callback_.Run(std::move(snippets)); | 439 snippets_available_callback_.Run(std::move(snippets)); |
| 440 } | 440 } |
| 441 | 441 |
| 442 } // namespace ntp_snippets | 442 } // namespace ntp_snippets |
| OLD | NEW |