| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/doodle/doodle_fetcher_impl.h" | 5 #include "components/doodle/doodle_fetcher_impl.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/stringprintf.h" |
| 10 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 11 #include "base/values.h" | 12 #include "base/values.h" |
| 12 #include "components/data_use_measurement/core/data_use_user_data.h" | 13 #include "components/data_use_measurement/core/data_use_user_data.h" |
| 13 #include "components/google/core/browser/google_url_tracker.h" | 14 #include "components/google/core/browser/google_url_tracker.h" |
| 14 #include "components/google/core/browser/google_util.h" | 15 #include "components/google/core/browser/google_util.h" |
| 15 #include "net/base/load_flags.h" | 16 #include "net/base/load_flags.h" |
| 16 #include "net/http/http_status_code.h" | 17 #include "net/http/http_status_code.h" |
| 17 #include "net/url_request/url_fetcher.h" | 18 #include "net/url_request/url_fetcher.h" |
| 18 | 19 |
| 19 using net::URLFetcher; | 20 using net::URLFetcher; |
| 20 | 21 |
| 21 namespace doodle { | 22 namespace doodle { |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 const double kMaxTimeToLiveMS = 30.0 * 24 * 60 * 60 * 1000; // 30 days | 26 const double kMaxTimeToLiveMS = 30.0 * 24 * 60 * 60 * 1000; // 30 days |
| 26 | 27 |
| 27 const char kDoodleConfigPath[] = "/async/ddljson"; | 28 // "/async/ddljson" is the base API path. "ntp:1" identifies this request as |
| 29 // being for a New Tab page. The "graybg:" param specifies whether the doodle |
| 30 // will be displayed on a gray background. |
| 31 const char kDoodleConfigPathFormat[] = "/async/ddljson?async=ntp:1,graybg:%d"; |
| 28 | 32 |
| 29 std::string StripSafetyPreamble(const std::string& json) { | 33 std::string StripSafetyPreamble(const std::string& json) { |
| 30 // The response may start with )]}'. Ignore this. | 34 // The response may start with )]}'. Ignore this. |
| 31 const char kResponsePreamble[] = ")]}'"; | 35 const char kResponsePreamble[] = ")]}'"; |
| 32 | 36 |
| 33 base::StringPiece json_sp(json); | 37 base::StringPiece json_sp(json); |
| 34 if (json_sp.starts_with(kResponsePreamble)) { | 38 if (json_sp.starts_with(kResponsePreamble)) { |
| 35 json_sp.remove_prefix(strlen(kResponsePreamble)); | 39 json_sp.remove_prefix(strlen(kResponsePreamble)); |
| 36 } | 40 } |
| 37 | 41 |
| 38 return json_sp.as_string(); | 42 return json_sp.as_string(); |
| 39 } | 43 } |
| 40 | 44 |
| 45 GURL BuildDoodleURL(const GURL& base_url, bool gray_background) { |
| 46 return base_url.Resolve( |
| 47 base::StringPrintf(kDoodleConfigPathFormat, gray_background ? 1 : 0)); |
| 48 } |
| 49 |
| 41 } // namespace | 50 } // namespace |
| 42 | 51 |
| 43 DoodleFetcherImpl::DoodleFetcherImpl( | 52 DoodleFetcherImpl::DoodleFetcherImpl( |
| 44 scoped_refptr<net::URLRequestContextGetter> download_context, | 53 scoped_refptr<net::URLRequestContextGetter> download_context, |
| 45 GoogleURLTracker* google_url_tracker, | 54 GoogleURLTracker* google_url_tracker, |
| 46 const ParseJSONCallback& json_parsing_callback) | 55 const ParseJSONCallback& json_parsing_callback, |
| 56 bool gray_background) |
| 47 : download_context_(download_context), | 57 : download_context_(download_context), |
| 58 google_url_tracker_(google_url_tracker), |
| 48 json_parsing_callback_(json_parsing_callback), | 59 json_parsing_callback_(json_parsing_callback), |
| 49 google_url_tracker_(google_url_tracker), | 60 gray_background_(gray_background), |
| 50 weak_ptr_factory_(this) { | 61 weak_ptr_factory_(this) { |
| 51 DCHECK(google_url_tracker_); | 62 DCHECK(google_url_tracker_); |
| 52 } | 63 } |
| 53 | 64 |
| 54 DoodleFetcherImpl::~DoodleFetcherImpl() = default; | 65 DoodleFetcherImpl::~DoodleFetcherImpl() = default; |
| 55 | 66 |
| 56 void DoodleFetcherImpl::FetchDoodle(FinishedCallback callback) { | 67 void DoodleFetcherImpl::FetchDoodle(FinishedCallback callback) { |
| 57 if (IsFetchInProgress()) { | 68 if (IsFetchInProgress()) { |
| 58 callbacks_.push_back(std::move(callback)); | 69 callbacks_.push_back(std::move(callback)); |
| 59 return; // The callback will be called for the existing request's results. | 70 return; // The callback will be called for the existing request's results. |
| 60 } | 71 } |
| 61 DCHECK(!fetcher_.get()); | 72 DCHECK(!fetcher_.get()); |
| 62 callbacks_.push_back(std::move(callback)); | 73 callbacks_.push_back(std::move(callback)); |
| 63 fetcher_ = URLFetcher::Create(GetGoogleBaseUrl().Resolve(kDoodleConfigPath), | 74 fetcher_ = |
| 64 URLFetcher::GET, this); | 75 URLFetcher::Create(BuildDoodleURL(GetGoogleBaseUrl(), gray_background_), |
| 76 URLFetcher::GET, this); |
| 65 fetcher_->SetRequestContext(download_context_.get()); | 77 fetcher_->SetRequestContext(download_context_.get()); |
| 66 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 78 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 67 net::LOAD_DO_NOT_SAVE_COOKIES | | 79 net::LOAD_DO_NOT_SAVE_COOKIES | |
| 68 net::LOAD_DO_NOT_SEND_AUTH_DATA); | 80 net::LOAD_DO_NOT_SEND_AUTH_DATA); |
| 69 fetcher_->SetAutomaticallyRetryOnNetworkChanges(1); | 81 fetcher_->SetAutomaticallyRetryOnNetworkChanges(1); |
| 70 data_use_measurement::DataUseUserData::AttachToFetcher( | 82 data_use_measurement::DataUseUserData::AttachToFetcher( |
| 71 fetcher_.get(), data_use_measurement::DataUseUserData::DOODLE); | 83 fetcher_.get(), data_use_measurement::DataUseUserData::DOODLE); |
| 72 fetcher_->Start(); | 84 fetcher_->Start(); |
| 73 } | 85 } |
| 74 | 86 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 | 175 |
| 164 GURL DoodleFetcherImpl::GetGoogleBaseUrl() const { | 176 GURL DoodleFetcherImpl::GetGoogleBaseUrl() const { |
| 165 GURL cmd_line_url = google_util::CommandLineGoogleBaseURL(); | 177 GURL cmd_line_url = google_util::CommandLineGoogleBaseURL(); |
| 166 if (cmd_line_url.is_valid()) { | 178 if (cmd_line_url.is_valid()) { |
| 167 return cmd_line_url; | 179 return cmd_line_url; |
| 168 } | 180 } |
| 169 return google_url_tracker_->google_url(); | 181 return google_url_tracker_->google_url(); |
| 170 } | 182 } |
| 171 | 183 |
| 172 } // namespace doodle | 184 } // namespace doodle |
| OLD | NEW |