| 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/remote/json_request.h" | 5 #include "components/ntp_snippets/remote/json_request.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.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/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 17 #include "base/time/tick_clock.h" | 17 #include "base/time/clock.h" |
| 18 #include "base/time/time.h" | |
| 19 #include "base/values.h" | 18 #include "base/values.h" |
| 20 #include "components/data_use_measurement/core/data_use_user_data.h" | 19 #include "components/data_use_measurement/core/data_use_user_data.h" |
| 21 #include "components/ntp_snippets/category_info.h" | 20 #include "components/ntp_snippets/category_info.h" |
| 22 #include "components/ntp_snippets/features.h" | 21 #include "components/ntp_snippets/features.h" |
| 23 #include "components/ntp_snippets/remote/request_params.h" | 22 #include "components/ntp_snippets/remote/request_params.h" |
| 24 #include "components/ntp_snippets/user_classifier.h" | 23 #include "components/ntp_snippets/user_classifier.h" |
| 25 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 24 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
| 26 #include "components/signin/core/browser/signin_manager.h" | 25 #include "components/signin/core/browser/signin_manager.h" |
| 27 #include "components/signin/core/browser/signin_manager_base.h" | 26 #include "components/signin/core/browser/signin_manager_base.h" |
| 28 #include "components/variations/net/variations_http_headers.h" | 27 #include "components/variations/net/variations_http_headers.h" |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 /*has_view_all_action=*/false, | 152 /*has_view_all_action=*/false, |
| 154 /*show_if_empty=*/false, | 153 /*show_if_empty=*/false, |
| 155 // TODO(tschumann): The message for no-articles is likely wrong | 154 // TODO(tschumann): The message for no-articles is likely wrong |
| 156 // and needs to be added to the stubby protocol if we want to | 155 // and needs to be added to the stubby protocol if we want to |
| 157 // support it. | 156 // support it. |
| 158 l10n_util::GetStringUTF16(IDS_NTP_ARTICLE_SUGGESTIONS_SECTION_EMPTY)); | 157 l10n_util::GetStringUTF16(IDS_NTP_ARTICLE_SUGGESTIONS_SECTION_EMPTY)); |
| 159 } | 158 } |
| 160 | 159 |
| 161 JsonRequest::JsonRequest( | 160 JsonRequest::JsonRequest( |
| 162 base::Optional<Category> exclusive_category, | 161 base::Optional<Category> exclusive_category, |
| 163 base::TickClock* tick_clock, // Needed until destruction of the request. | 162 base::Clock* clock, // Needed until destruction of the request. |
| 164 const ParseJSONCallback& callback) | 163 const ParseJSONCallback& callback) |
| 165 : exclusive_category_(exclusive_category), | 164 : exclusive_category_(exclusive_category), |
| 166 tick_clock_(tick_clock), | 165 clock_(clock), |
| 167 parse_json_callback_(callback), | 166 parse_json_callback_(callback), |
| 168 weak_ptr_factory_(this) { | 167 weak_ptr_factory_(this) { |
| 169 creation_time_ = tick_clock_->NowTicks(); | 168 creation_time_ = clock_->Now(); |
| 170 } | 169 } |
| 171 | 170 |
| 172 JsonRequest::~JsonRequest() { | 171 JsonRequest::~JsonRequest() { |
| 173 LOG_IF(DFATAL, !request_completed_callback_.is_null()) | 172 LOG_IF(DFATAL, !request_completed_callback_.is_null()) |
| 174 << "The CompletionCallback was never called!"; | 173 << "The CompletionCallback was never called!"; |
| 175 } | 174 } |
| 176 | 175 |
| 177 void JsonRequest::Start(CompletedCallback callback) { | 176 void JsonRequest::Start(CompletedCallback callback) { |
| 178 request_completed_callback_ = std::move(callback); | 177 request_completed_callback_ = std::move(callback); |
| 179 url_fetcher_->Start(); | 178 url_fetcher_->Start(); |
| 180 } | 179 } |
| 181 | 180 |
| 182 base::TimeDelta JsonRequest::GetFetchDuration() const { | 181 base::TimeDelta JsonRequest::GetFetchDuration() const { |
| 183 return tick_clock_->NowTicks() - creation_time_; | 182 return clock_->Now() - creation_time_; |
| 184 } | 183 } |
| 185 | 184 |
| 186 std::string JsonRequest::GetResponseString() const { | 185 std::string JsonRequest::GetResponseString() const { |
| 187 std::string response; | 186 std::string response; |
| 188 url_fetcher_->GetResponseAsString(&response); | 187 url_fetcher_->GetResponseAsString(&response); |
| 189 return response; | 188 return response; |
| 190 } | 189 } |
| 191 | 190 |
| 192 //////////////////////////////////////////////////////////////////////////////// | 191 //////////////////////////////////////////////////////////////////////////////// |
| 193 // URLFetcherDelegate overrides | 192 // URLFetcherDelegate overrides |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 | 245 |
| 247 JsonRequest::Builder::Builder() | 246 JsonRequest::Builder::Builder() |
| 248 : fetch_api_(CHROME_READER_API), | 247 : fetch_api_(CHROME_READER_API), |
| 249 language_model_(nullptr) {} | 248 language_model_(nullptr) {} |
| 250 JsonRequest::Builder::Builder(JsonRequest::Builder&&) = default; | 249 JsonRequest::Builder::Builder(JsonRequest::Builder&&) = default; |
| 251 JsonRequest::Builder::~Builder() = default; | 250 JsonRequest::Builder::~Builder() = default; |
| 252 | 251 |
| 253 std::unique_ptr<JsonRequest> JsonRequest::Builder::Build() const { | 252 std::unique_ptr<JsonRequest> JsonRequest::Builder::Build() const { |
| 254 DCHECK(!url_.is_empty()); | 253 DCHECK(!url_.is_empty()); |
| 255 DCHECK(url_request_context_getter_); | 254 DCHECK(url_request_context_getter_); |
| 256 DCHECK(tick_clock_); | 255 DCHECK(clock_); |
| 257 auto request = base::MakeUnique<JsonRequest>( | 256 auto request = base::MakeUnique<JsonRequest>(params_.exclusive_category, |
| 258 params_.exclusive_category, tick_clock_, parse_json_callback_); | 257 clock_, parse_json_callback_); |
| 259 std::string body = BuildBody(); | 258 std::string body = BuildBody(); |
| 260 std::string headers = BuildHeaders(); | 259 std::string headers = BuildHeaders(); |
| 261 request->url_fetcher_ = BuildURLFetcher(request.get(), headers, body); | 260 request->url_fetcher_ = BuildURLFetcher(request.get(), headers, body); |
| 262 | 261 |
| 263 // Log the request for debugging network issues. | 262 // Log the request for debugging network issues. |
| 264 VLOG(1) << "Sending a NTP snippets request to " << url_ << ":\n" | 263 VLOG(1) << "Sending a NTP snippets request to " << url_ << ":\n" |
| 265 << headers << "\n" | 264 << headers << "\n" |
| 266 << body; | 265 << body; |
| 267 | 266 |
| 268 return request; | 267 return request; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 292 params_ = params; | 291 params_ = params; |
| 293 return *this; | 292 return *this; |
| 294 } | 293 } |
| 295 | 294 |
| 296 JsonRequest::Builder& JsonRequest::Builder::SetParseJsonCallback( | 295 JsonRequest::Builder& JsonRequest::Builder::SetParseJsonCallback( |
| 297 ParseJSONCallback callback) { | 296 ParseJSONCallback callback) { |
| 298 parse_json_callback_ = callback; | 297 parse_json_callback_ = callback; |
| 299 return *this; | 298 return *this; |
| 300 } | 299 } |
| 301 | 300 |
| 302 JsonRequest::Builder& JsonRequest::Builder::SetTickClock( | 301 JsonRequest::Builder& JsonRequest::Builder::SetClock(base::Clock* clock) { |
| 303 base::TickClock* tick_clock) { | 302 clock_ = clock; |
| 304 tick_clock_ = tick_clock; | |
| 305 return *this; | 303 return *this; |
| 306 } | 304 } |
| 307 | 305 |
| 308 JsonRequest::Builder& JsonRequest::Builder::SetUrl(const GURL& url) { | 306 JsonRequest::Builder& JsonRequest::Builder::SetUrl(const GURL& url) { |
| 309 url_ = url; | 307 url_ = url; |
| 310 return *this; | 308 return *this; |
| 311 } | 309 } |
| 312 | 310 |
| 313 JsonRequest::Builder& JsonRequest::Builder::SetUrlRequestContextGetter( | 311 JsonRequest::Builder& JsonRequest::Builder::SetUrlRequestContextGetter( |
| 314 const scoped_refptr<net::URLRequestContextGetter>& context_getter) { | 312 const scoped_refptr<net::URLRequestContextGetter>& context_getter) { |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 484 "NewTabPage.Languages.UILanguageRatioInTwoTopLanguages", | 482 "NewTabPage.Languages.UILanguageRatioInTwoTopLanguages", |
| 485 ratio_ui_in_both_languages * 100); | 483 ratio_ui_in_both_languages * 100); |
| 486 break; | 484 break; |
| 487 } | 485 } |
| 488 } | 486 } |
| 489 } | 487 } |
| 490 | 488 |
| 491 } // namespace internal | 489 } // namespace internal |
| 492 | 490 |
| 493 } // namespace ntp_snippets | 491 } // namespace ntp_snippets |
| OLD | NEW |