| 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 "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/metrics/sparse_histogram.h" | 9 #include "base/metrics/sparse_histogram.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "components/data_use_measurement/core/data_use_user_data.h" |
| 14 #include "google_apis/google_api_keys.h" | 15 #include "google_apis/google_api_keys.h" |
| 15 #include "net/base/load_flags.h" | 16 #include "net/base/load_flags.h" |
| 16 #include "net/http/http_request_headers.h" | 17 #include "net/http/http_request_headers.h" |
| 17 #include "net/http/http_response_headers.h" | 18 #include "net/http/http_response_headers.h" |
| 18 #include "net/http/http_status_code.h" | 19 #include "net/http/http_status_code.h" |
| 19 #include "net/url_request/url_fetcher.h" | 20 #include "net/url_request/url_fetcher.h" |
| 20 | 21 |
| 21 using net::URLFetcher; | 22 using net::URLFetcher; |
| 22 using net::URLRequestContextGetter; | 23 using net::URLRequestContextGetter; |
| 23 using net::HttpRequestHeaders; | 24 using net::HttpRequestHeaders; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 int count) { | 87 int count) { |
| 87 const std::string& key = is_stable_channel_ | 88 const std::string& key = is_stable_channel_ |
| 88 ? google_apis::GetAPIKey() | 89 ? google_apis::GetAPIKey() |
| 89 : google_apis::GetNonStableAPIKey(); | 90 : google_apis::GetNonStableAPIKey(); |
| 90 std::string url = | 91 std::string url = |
| 91 base::StringPrintf(kContentSnippetsServerFormat, key.c_str()); | 92 base::StringPrintf(kContentSnippetsServerFormat, key.c_str()); |
| 92 url_fetcher_ = URLFetcher::Create(GURL(url), URLFetcher::POST, this); | 93 url_fetcher_ = URLFetcher::Create(GURL(url), URLFetcher::POST, this); |
| 93 url_fetcher_->SetRequestContext(url_request_context_getter_.get()); | 94 url_fetcher_->SetRequestContext(url_request_context_getter_.get()); |
| 94 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 95 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 95 net::LOAD_DO_NOT_SAVE_COOKIES); | 96 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 97 data_use_measurement::DataUseUserData::AttachToFetcher( |
| 98 url_fetcher_.get(), data_use_measurement::DataUseUserData::NTP_SNIPPETS); |
| 96 HttpRequestHeaders headers; | 99 HttpRequestHeaders headers; |
| 97 headers.SetHeader("Content-Type", "application/json; charset=UTF-8"); | 100 headers.SetHeader("Content-Type", "application/json; charset=UTF-8"); |
| 98 url_fetcher_->SetExtraRequestHeaders(headers.ToString()); | 101 url_fetcher_->SetExtraRequestHeaders(headers.ToString()); |
| 99 std::string host_restricts; | 102 std::string host_restricts; |
| 100 for (const std::string& host : hosts) | 103 for (const std::string& host : hosts) |
| 101 host_restricts += base::StringPrintf(kHostRestrictFormat, host.c_str()); | 104 host_restricts += base::StringPrintf(kHostRestrictFormat, host.c_str()); |
| 102 url_fetcher_->SetUploadData("application/json", | 105 url_fetcher_->SetUploadData("application/json", |
| 103 base::StringPrintf(kRequestParameterFormat, | 106 base::StringPrintf(kRequestParameterFormat, |
| 104 host_restricts.c_str(), | 107 host_restricts.c_str(), |
| 105 count)); | 108 count)); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 | 141 |
| 139 } else { | 142 } else { |
| 140 bool stores_result_to_string = source->GetResponseAsString(&response); | 143 bool stores_result_to_string = source->GetResponseAsString(&response); |
| 141 DCHECK(stores_result_to_string); | 144 DCHECK(stores_result_to_string); |
| 142 } | 145 } |
| 143 | 146 |
| 144 callback_list_.Notify(response, message); | 147 callback_list_.Notify(response, message); |
| 145 } | 148 } |
| 146 | 149 |
| 147 } // namespace ntp_snippets | 150 } // namespace ntp_snippets |
| OLD | NEW |