| 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" | |
| 10 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 11 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 12 #include "base/values.h" | 11 #include "base/values.h" |
| 13 #include "components/data_use_measurement/core/data_use_user_data.h" | 12 #include "components/data_use_measurement/core/data_use_user_data.h" |
| 14 #include "components/google/core/browser/google_url_tracker.h" | 13 #include "components/google/core/browser/google_url_tracker.h" |
| 15 #include "components/google/core/browser/google_util.h" | 14 #include "components/google/core/browser/google_util.h" |
| 16 #include "net/base/load_flags.h" | 15 #include "net/base/load_flags.h" |
| 17 #include "net/http/http_status_code.h" | 16 #include "net/http/http_status_code.h" |
| 18 #include "net/traffic_annotation/network_traffic_annotation.h" | 17 #include "net/traffic_annotation/network_traffic_annotation.h" |
| 19 #include "net/url_request/url_fetcher.h" | 18 #include "net/url_request/url_fetcher.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 34 const char kResponsePreamble[] = ")]}'"; | 33 const char kResponsePreamble[] = ")]}'"; |
| 35 | 34 |
| 36 base::StringPiece json_sp(json); | 35 base::StringPiece json_sp(json); |
| 37 if (json_sp.starts_with(kResponsePreamble)) { | 36 if (json_sp.starts_with(kResponsePreamble)) { |
| 38 json_sp.remove_prefix(strlen(kResponsePreamble)); | 37 json_sp.remove_prefix(strlen(kResponsePreamble)); |
| 39 } | 38 } |
| 40 | 39 |
| 41 return json_sp.as_string(); | 40 return json_sp.as_string(); |
| 42 } | 41 } |
| 43 | 42 |
| 44 GURL BuildDoodleURL(const GURL& base_url, bool gray_background) { | 43 GURL BuildDoodleURL(const GURL& base_url, |
| 45 return base_url.Resolve( | 44 bool gray_background, |
| 46 base::StringPrintf(kDoodleConfigPathFormat, gray_background ? 1 : 0)); | 45 const base::Optional<std::string>& override_url) { |
| 46 std::string path = |
| 47 base::StringPrintf(kDoodleConfigPathFormat, gray_background ? 1 : 0); |
| 48 if (override_url.has_value()) { |
| 49 // The override URL may or may not be relative to the base URL. |
| 50 path = *override_url; |
| 51 } |
| 52 return base_url.Resolve(path); |
| 47 } | 53 } |
| 48 | 54 |
| 49 } // namespace | 55 } // namespace |
| 50 | 56 |
| 51 DoodleFetcherImpl::DoodleFetcherImpl( | 57 DoodleFetcherImpl::DoodleFetcherImpl( |
| 52 scoped_refptr<net::URLRequestContextGetter> download_context, | 58 scoped_refptr<net::URLRequestContextGetter> download_context, |
| 53 GoogleURLTracker* google_url_tracker, | 59 GoogleURLTracker* google_url_tracker, |
| 54 const ParseJSONCallback& json_parsing_callback, | 60 const ParseJSONCallback& json_parsing_callback, |
| 55 bool gray_background) | 61 bool gray_background, |
| 62 const base::Optional<std::string>& override_url) |
| 56 : download_context_(download_context), | 63 : download_context_(download_context), |
| 57 google_url_tracker_(google_url_tracker), | 64 google_url_tracker_(google_url_tracker), |
| 58 json_parsing_callback_(json_parsing_callback), | 65 json_parsing_callback_(json_parsing_callback), |
| 59 gray_background_(gray_background), | 66 gray_background_(gray_background), |
| 67 override_url_(override_url), |
| 60 weak_ptr_factory_(this) { | 68 weak_ptr_factory_(this) { |
| 61 DCHECK(google_url_tracker_); | 69 DCHECK(google_url_tracker_); |
| 62 } | 70 } |
| 63 | 71 |
| 64 DoodleFetcherImpl::~DoodleFetcherImpl() = default; | 72 DoodleFetcherImpl::~DoodleFetcherImpl() = default; |
| 65 | 73 |
| 66 void DoodleFetcherImpl::FetchDoodle(FinishedCallback callback) { | 74 void DoodleFetcherImpl::FetchDoodle(FinishedCallback callback) { |
| 67 if (IsFetchInProgress()) { | 75 if (IsFetchInProgress()) { |
| 68 callbacks_.push_back(std::move(callback)); | 76 callbacks_.push_back(std::move(callback)); |
| 69 return; // The callback will be called for the existing request's results. | 77 return; // The callback will be called for the existing request's results. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 84 } | 92 } |
| 85 policy { | 93 policy { |
| 86 cookies_allowed: false | 94 cookies_allowed: false |
| 87 setting: | 95 setting: |
| 88 "Choosing a non-Google search engine in Chromium settings under " | 96 "Choosing a non-Google search engine in Chromium settings under " |
| 89 "'Search Engine' will disable this feature." | 97 "'Search Engine' will disable this feature." |
| 90 policy_exception_justification: | 98 policy_exception_justification: |
| 91 "Not implemented, considered not useful as it does not upload any " | 99 "Not implemented, considered not useful as it does not upload any " |
| 92 "data." | 100 "data." |
| 93 })"); | 101 })"); |
| 94 fetcher_ = | 102 fetcher_ = URLFetcher::Create( |
| 95 URLFetcher::Create(BuildDoodleURL(GetGoogleBaseUrl(), gray_background_), | 103 BuildDoodleURL(GetGoogleBaseUrl(), gray_background_, override_url_), |
| 96 URLFetcher::GET, this, traffic_annotation); | 104 URLFetcher::GET, this, traffic_annotation); |
| 97 fetcher_->SetRequestContext(download_context_.get()); | 105 fetcher_->SetRequestContext(download_context_.get()); |
| 98 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 106 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 99 net::LOAD_DO_NOT_SAVE_COOKIES | | 107 net::LOAD_DO_NOT_SAVE_COOKIES | |
| 100 net::LOAD_DO_NOT_SEND_AUTH_DATA); | 108 net::LOAD_DO_NOT_SEND_AUTH_DATA); |
| 101 fetcher_->SetAutomaticallyRetryOnNetworkChanges(1); | 109 fetcher_->SetAutomaticallyRetryOnNetworkChanges(1); |
| 102 data_use_measurement::DataUseUserData::AttachToFetcher( | 110 data_use_measurement::DataUseUserData::AttachToFetcher( |
| 103 fetcher_.get(), data_use_measurement::DataUseUserData::DOODLE); | 111 fetcher_.get(), data_use_measurement::DataUseUserData::DOODLE); |
| 104 fetcher_->Start(); | 112 fetcher_->Start(); |
| 105 } | 113 } |
| 106 | 114 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 | 198 |
| 191 GURL DoodleFetcherImpl::GetGoogleBaseUrl() const { | 199 GURL DoodleFetcherImpl::GetGoogleBaseUrl() const { |
| 192 GURL cmd_line_url = google_util::CommandLineGoogleBaseURL(); | 200 GURL cmd_line_url = google_util::CommandLineGoogleBaseURL(); |
| 193 if (cmd_line_url.is_valid()) { | 201 if (cmd_line_url.is_valid()) { |
| 194 return cmd_line_url; | 202 return cmd_line_url; |
| 195 } | 203 } |
| 196 return google_url_tracker_->google_url(); | 204 return google_url_tracker_->google_url(); |
| 197 } | 205 } |
| 198 | 206 |
| 199 } // namespace doodle | 207 } // namespace doodle |
| OLD | NEW |