| 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/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| 11 #include "base/metrics/sparse_histogram.h" | 11 #include "base/metrics/sparse_histogram.h" |
| 12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 16 #include "base/time/default_tick_clock.h" |
| 16 #include "base/values.h" | 17 #include "base/values.h" |
| 17 #include "components/data_use_measurement/core/data_use_user_data.h" | 18 #include "components/data_use_measurement/core/data_use_user_data.h" |
| 18 #include "components/ntp_snippets/switches.h" | 19 #include "components/ntp_snippets/switches.h" |
| 19 #include "google_apis/google_api_keys.h" | 20 #include "google_apis/google_api_keys.h" |
| 20 #include "net/base/load_flags.h" | 21 #include "net/base/load_flags.h" |
| 21 #include "net/http/http_request_headers.h" | 22 #include "net/http/http_request_headers.h" |
| 22 #include "net/http/http_response_headers.h" | 23 #include "net/http/http_response_headers.h" |
| 23 #include "net/http/http_status_code.h" | 24 #include "net/http/http_status_code.h" |
| 24 #include "net/url_request/url_fetcher.h" | 25 #include "net/url_request/url_fetcher.h" |
| 25 | 26 |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 | 93 |
| 93 } // namespace | 94 } // namespace |
| 94 | 95 |
| 95 NTPSnippetsFetcher::NTPSnippetsFetcher( | 96 NTPSnippetsFetcher::NTPSnippetsFetcher( |
| 96 scoped_refptr<URLRequestContextGetter> url_request_context_getter, | 97 scoped_refptr<URLRequestContextGetter> url_request_context_getter, |
| 97 const ParseJSONCallback& parse_json_callback, | 98 const ParseJSONCallback& parse_json_callback, |
| 98 bool is_stable_channel) | 99 bool is_stable_channel) |
| 99 : url_request_context_getter_(url_request_context_getter), | 100 : url_request_context_getter_(url_request_context_getter), |
| 100 parse_json_callback_(parse_json_callback), | 101 parse_json_callback_(parse_json_callback), |
| 101 is_stable_channel_(is_stable_channel), | 102 is_stable_channel_(is_stable_channel), |
| 103 tick_clock_(new base::DefaultTickClock()), |
| 102 weak_ptr_factory_(this) {} | 104 weak_ptr_factory_(this) {} |
| 103 | 105 |
| 104 NTPSnippetsFetcher::~NTPSnippetsFetcher() {} | 106 NTPSnippetsFetcher::~NTPSnippetsFetcher() {} |
| 105 | 107 |
| 106 void NTPSnippetsFetcher::SetCallback( | 108 void NTPSnippetsFetcher::SetCallback( |
| 107 const SnippetsAvailableCallback& callback) { | 109 const SnippetsAvailableCallback& callback) { |
| 108 snippets_available_callback_ = callback; | 110 snippets_available_callback_ = callback; |
| 109 } | 111 } |
| 110 | 112 |
| 111 void NTPSnippetsFetcher::FetchSnippetsFromHosts( | 113 void NTPSnippetsFetcher::FetchSnippetsFromHosts( |
| 112 const std::set<std::string>& hosts, int count) { | 114 const std::set<std::string>& hosts, int count) { |
| 113 std::string host_restricts; | 115 std::string host_restricts; |
| 116 fetch_start_time_ = tick_clock_->NowTicks(); |
| 114 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( | 117 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 115 switches::kDontRestrict)) { | 118 switches::kDontRestrict)) { |
| 116 if (hosts.empty()) { | 119 if (hosts.empty()) { |
| 117 FetchFinished(OptionalSnippets(), FetchResult::EMPTY_HOSTS, | 120 FetchFinished(OptionalSnippets(), FetchResult::EMPTY_HOSTS, |
| 118 /*extra_message=*/std::string()); | 121 /*extra_message=*/std::string()); |
| 119 return; | 122 return; |
| 120 } | 123 } |
| 121 for (const std::string& host : hosts) | 124 for (const std::string& host : hosts) |
| 122 host_restricts += base::StringPrintf(kHostRestrictFormat, host.c_str()); | 125 host_restricts += base::StringPrintf(kHostRestrictFormat, host.c_str()); |
| 123 } | 126 } |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 FetchFinished( | 205 FetchFinished( |
| 203 OptionalSnippets(), FetchResult::JSON_PARSE_ERROR, | 206 OptionalSnippets(), FetchResult::JSON_PARSE_ERROR, |
| 204 /*extra_message=*/base::StringPrintf(" (error %s)", error.c_str())); | 207 /*extra_message=*/base::StringPrintf(" (error %s)", error.c_str())); |
| 205 } | 208 } |
| 206 | 209 |
| 207 void NTPSnippetsFetcher::FetchFinished(OptionalSnippets snippets, | 210 void NTPSnippetsFetcher::FetchFinished(OptionalSnippets snippets, |
| 208 FetchResult result, | 211 FetchResult result, |
| 209 const std::string& extra_message) { | 212 const std::string& extra_message) { |
| 210 DCHECK(result == FetchResult::SUCCESS || !snippets); | 213 DCHECK(result == FetchResult::SUCCESS || !snippets); |
| 211 last_status_ = FetchResultToString(result) + extra_message; | 214 last_status_ = FetchResultToString(result) + extra_message; |
| 215 |
| 216 UMA_HISTOGRAM_TIMES("NewTabPage.Snippets.FetchTime", |
| 217 tick_clock_->NowTicks() - fetch_start_time_); |
| 212 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", | 218 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", |
| 213 static_cast<int>(result), | 219 static_cast<int>(result), |
| 214 static_cast<int>(FetchResult::RESULT_MAX)); | 220 static_cast<int>(FetchResult::RESULT_MAX)); |
| 221 |
| 215 if (!snippets_available_callback_.is_null()) | 222 if (!snippets_available_callback_.is_null()) |
| 216 snippets_available_callback_.Run(std::move(snippets)); | 223 snippets_available_callback_.Run(std::move(snippets)); |
| 217 } | 224 } |
| 218 | 225 |
| 219 } // namespace ntp_snippets | 226 } // namespace ntp_snippets |
| OLD | NEW |