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/time/default_tick_clock.h" | 20 #include "base/time/default_tick_clock.h" |
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/ntp_snippets_constants.h" | 23 #include "components/ntp_snippets/ntp_snippets_constants.h" |
24 #include "components/ntp_snippets/switches.h" | 24 #include "components/ntp_snippets/switches.h" |
25 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 25 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
26 #include "components/signin/core/browser/signin_manager.h" | 26 #include "components/signin/core/browser/signin_manager.h" |
27 #include "components/signin/core/browser/signin_manager_base.h" | 27 #include "components/signin/core/browser/signin_manager_base.h" |
| 28 #include "components/variations/net/variations_http_headers.h" |
28 #include "components/variations/variations_associated_data.h" | 29 #include "components/variations/variations_associated_data.h" |
29 #include "google_apis/google_api_keys.h" | 30 #include "google_apis/google_api_keys.h" |
30 #include "net/base/load_flags.h" | 31 #include "net/base/load_flags.h" |
31 #include "net/http/http_request_headers.h" | 32 #include "net/http/http_request_headers.h" |
32 #include "net/http/http_response_headers.h" | 33 #include "net/http/http_response_headers.h" |
33 #include "net/http/http_status_code.h" | 34 #include "net/http/http_status_code.h" |
34 #include "net/url_request/url_fetcher.h" | 35 #include "net/url_request/url_fetcher.h" |
35 #include "third_party/icu/source/common/unicode/uloc.h" | 36 #include "third_party/icu/source/common/unicode/uloc.h" |
36 #include "third_party/icu/source/common/unicode/utypes.h" | 37 #include "third_party/icu/source/common/unicode/utypes.h" |
37 | 38 |
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 329 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
329 net::LOAD_DO_NOT_SAVE_COOKIES); | 330 net::LOAD_DO_NOT_SAVE_COOKIES); |
330 | 331 |
331 data_use_measurement::DataUseUserData::AttachToFetcher( | 332 data_use_measurement::DataUseUserData::AttachToFetcher( |
332 url_fetcher_.get(), data_use_measurement::DataUseUserData::NTP_SNIPPETS); | 333 url_fetcher_.get(), data_use_measurement::DataUseUserData::NTP_SNIPPETS); |
333 | 334 |
334 HttpRequestHeaders headers; | 335 HttpRequestHeaders headers; |
335 if (!auth_header.empty()) | 336 if (!auth_header.empty()) |
336 headers.SetHeader("Authorization", auth_header); | 337 headers.SetHeader("Authorization", auth_header); |
337 headers.SetHeader("Content-Type", "application/json; charset=UTF-8"); | 338 headers.SetHeader("Content-Type", "application/json; charset=UTF-8"); |
| 339 // Add X-Client-Data header with experiment IDs from field trials. |
| 340 variations::AppendVariationHeaders(url, |
| 341 false, // incognito |
| 342 false, // uma_enabled |
| 343 &headers); |
338 url_fetcher_->SetExtraRequestHeaders(headers.ToString()); | 344 url_fetcher_->SetExtraRequestHeaders(headers.ToString()); |
339 url_fetcher_->SetUploadData("application/json", request); | 345 url_fetcher_->SetUploadData("application/json", request); |
340 // Log the request for debugging network issues. | 346 // Log the request for debugging network issues. |
341 VLOG(1) << "Sending a NTP snippets request to " << url << ":" << std::endl | 347 VLOG(1) << "Sending a NTP snippets request to " << url << ":" << std::endl |
342 << headers.ToString() << std::endl << request; | 348 << headers.ToString() << std::endl << request; |
343 // Fetchers are sometimes cancelled because a network change was detected. | 349 // Fetchers are sometimes cancelled because a network change was detected. |
344 url_fetcher_->SetAutomaticallyRetryOnNetworkChanges(3); | 350 url_fetcher_->SetAutomaticallyRetryOnNetworkChanges(3); |
345 // Try to make fetching the files bit more robust even with poor connection. | 351 // Try to make fetching the files bit more robust even with poor connection. |
346 url_fetcher_->SetMaxRetriesOn5xx(3); | 352 url_fetcher_->SetMaxRetriesOn5xx(3); |
347 url_fetcher_->Start(); | 353 url_fetcher_->Start(); |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
513 tick_clock_->NowTicks() - fetch_start_time_); | 519 tick_clock_->NowTicks() - fetch_start_time_); |
514 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", | 520 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", |
515 static_cast<int>(result), | 521 static_cast<int>(result), |
516 static_cast<int>(FetchResult::RESULT_MAX)); | 522 static_cast<int>(FetchResult::RESULT_MAX)); |
517 | 523 |
518 if (!snippets_available_callback_.is_null()) | 524 if (!snippets_available_callback_.is_null()) |
519 snippets_available_callback_.Run(std::move(snippets)); | 525 snippets_available_callback_.Run(std::move(snippets)); |
520 } | 526 } |
521 | 527 |
522 } // namespace ntp_snippets | 528 } // namespace ntp_snippets |
OLD | NEW |