| 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 "chrome/browser/search/one_google_bar/one_google_bar_fetcher_impl.h" | 5 #include "chrome/browser/search/one_google_bar/one_google_bar_fetcher_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/command_line.h" | |
| 12 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
| 13 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/metrics/field_trial_params.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/values.h" | 16 #include "base/values.h" |
| 17 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
| 18 #include "chrome/browser/search/one_google_bar/one_google_bar_data.h" | 18 #include "chrome/browser/search/one_google_bar/one_google_bar_data.h" |
| 19 #include "chrome/common/chrome_content_client.h" | 19 #include "chrome/common/chrome_content_client.h" |
| 20 #include "chrome/common/chrome_features.h" |
| 20 #include "components/google/core/browser/google_url_tracker.h" | 21 #include "components/google/core/browser/google_url_tracker.h" |
| 21 #include "components/safe_json/safe_json_parser.h" | 22 #include "components/safe_json/safe_json_parser.h" |
| 22 #include "components/signin/core/browser/access_token_fetcher.h" | 23 #include "components/signin/core/browser/access_token_fetcher.h" |
| 23 #include "components/signin/core/browser/signin_manager.h" | 24 #include "components/signin/core/browser/signin_manager.h" |
| 24 #include "components/variations/net/variations_http_headers.h" | 25 #include "components/variations/net/variations_http_headers.h" |
| 25 #include "google_apis/gaia/oauth2_token_service.h" | 26 #include "google_apis/gaia/oauth2_token_service.h" |
| 26 #include "google_apis/google_api_keys.h" | 27 #include "google_apis/google_api_keys.h" |
| 27 #include "net/base/load_flags.h" | 28 #include "net/base/load_flags.h" |
| 28 #include "net/http/http_status_code.h" | 29 #include "net/http/http_status_code.h" |
| 29 #include "net/url_request/url_fetcher.h" | 30 #include "net/url_request/url_fetcher.h" |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 OAuth2TokenService::ScopeSet scopes; | 185 OAuth2TokenService::ScopeSet scopes; |
| 185 scopes.insert(kApiScope); | 186 scopes.insert(kApiScope); |
| 186 token_fetcher_ = base::MakeUnique<AccessTokenFetcher>( | 187 token_fetcher_ = base::MakeUnique<AccessTokenFetcher>( |
| 187 "one_google", signin_manager_, token_service_, scopes, | 188 "one_google", signin_manager_, token_service_, scopes, |
| 188 base::BindOnce(&AuthenticatedURLFetcher::GotAccessToken, | 189 base::BindOnce(&AuthenticatedURLFetcher::GotAccessToken, |
| 189 base::Unretained(this))); | 190 base::Unretained(this))); |
| 190 } | 191 } |
| 191 | 192 |
| 192 GURL OneGoogleBarFetcherImpl::AuthenticatedURLFetcher::GetApiUrl( | 193 GURL OneGoogleBarFetcherImpl::AuthenticatedURLFetcher::GetApiUrl( |
| 193 bool use_oauth) const { | 194 bool use_oauth) const { |
| 194 std::string api_url(kApiUrl); | 195 std::string api_url = base::GetFieldTrialParamValueByFeature( |
| 195 // TODO(treib): Attach to feature instead of cmdline. | 196 features::kOneGoogleBarOnLocalNtp, "one-google-api-url"); |
| 196 base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess(); | 197 if (api_url.empty()) { |
| 197 if (cmdline->HasSwitch("one-google-api-url")) | 198 api_url = kApiUrl; |
| 198 api_url = cmdline->GetSwitchValueASCII("one-google-api-url"); | 199 } |
| 200 |
| 199 // Append the API key only for unauthenticated requests. | 201 // Append the API key only for unauthenticated requests. |
| 200 if (!use_oauth) { | 202 if (!use_oauth) { |
| 201 api_url += | 203 api_url += |
| 202 base::StringPrintf(kApiKeyFormat, google_apis::GetAPIKey().c_str()); | 204 base::StringPrintf(kApiKeyFormat, google_apis::GetAPIKey().c_str()); |
| 203 } | 205 } |
| 204 | 206 |
| 205 return GURL(api_url); | 207 return GURL(api_url); |
| 206 } | 208 } |
| 207 | 209 |
| 208 std::string OneGoogleBarFetcherImpl::AuthenticatedURLFetcher::GetRequestBody() | 210 std::string OneGoogleBarFetcherImpl::AuthenticatedURLFetcher::GetRequestBody() |
| 209 const { | 211 const { |
| 210 // TODO(treib): Attach to feature instead of cmdline. | 212 std::string override_options = base::GetFieldTrialParamValueByFeature( |
| 211 base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess(); | 213 features::kOneGoogleBarOnLocalNtp, "one-google-bar-options"); |
| 212 if (cmdline->HasSwitch("one-google-bar-options")) | 214 if (!override_options.empty()) { |
| 213 return cmdline->GetSwitchValueASCII("one-google-bar-options"); | 215 return override_options; |
| 216 } |
| 214 | 217 |
| 215 base::DictionaryValue dict; | 218 base::DictionaryValue dict; |
| 216 dict.SetInteger("subproduct", 243); | 219 dict.SetInteger("subproduct", 243); |
| 217 dict.SetBoolean("enable_multilogin", true); | 220 dict.SetBoolean("enable_multilogin", true); |
| 218 dict.SetString("user_agent", GetUserAgent()); | 221 dict.SetString("user_agent", GetUserAgent()); |
| 219 dict.SetString("accept_language", g_browser_process->GetApplicationLocale()); | 222 dict.SetString("accept_language", g_browser_process->GetApplicationLocale()); |
| 220 dict.SetString("original_request_url", google_base_url_.spec()); | 223 dict.SetString("original_request_url", google_base_url_.spec()); |
| 221 auto material_options_dict = base::MakeUnique<base::DictionaryValue>(); | 224 auto material_options_dict = base::MakeUnique<base::DictionaryValue>(); |
| 222 material_options_dict->SetString("page_title", " "); | 225 material_options_dict->SetString("page_title", " "); |
| 223 material_options_dict->SetBoolean("position_fixed", true); | 226 material_options_dict->SetBoolean("position_fixed", true); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 | 299 |
| 297 OneGoogleBarFetcherImpl::~OneGoogleBarFetcherImpl() = default; | 300 OneGoogleBarFetcherImpl::~OneGoogleBarFetcherImpl() = default; |
| 298 | 301 |
| 299 void OneGoogleBarFetcherImpl::Fetch(OneGoogleCallback callback) { | 302 void OneGoogleBarFetcherImpl::Fetch(OneGoogleCallback callback) { |
| 300 callbacks_.push_back(std::move(callback)); | 303 callbacks_.push_back(std::move(callback)); |
| 301 IssueRequestIfNoneOngoing(); | 304 IssueRequestIfNoneOngoing(); |
| 302 } | 305 } |
| 303 | 306 |
| 304 void OneGoogleBarFetcherImpl::IssueRequestIfNoneOngoing() { | 307 void OneGoogleBarFetcherImpl::IssueRequestIfNoneOngoing() { |
| 305 // If there is an ongoing request, let it complete. | 308 // If there is an ongoing request, let it complete. |
| 306 if (pending_request_.get()) | 309 if (pending_request_.get()) { |
| 307 return; | 310 return; |
| 311 } |
| 308 | 312 |
| 309 pending_request_ = base::MakeUnique<AuthenticatedURLFetcher>( | 313 pending_request_ = base::MakeUnique<AuthenticatedURLFetcher>( |
| 310 signin_manager_, token_service_, request_context_, | 314 signin_manager_, token_service_, request_context_, |
| 311 google_url_tracker_->google_url(), | 315 google_url_tracker_->google_url(), |
| 312 base::BindOnce(&OneGoogleBarFetcherImpl::FetchDone, | 316 base::BindOnce(&OneGoogleBarFetcherImpl::FetchDone, |
| 313 base::Unretained(this))); | 317 base::Unretained(this))); |
| 314 pending_request_->Start(); | 318 pending_request_->Start(); |
| 315 } | 319 } |
| 316 | 320 |
| 317 void OneGoogleBarFetcherImpl::FetchDone(const net::URLFetcher* source) { | 321 void OneGoogleBarFetcherImpl::FetchDone(const net::URLFetcher* source) { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 Respond(base::nullopt); | 369 Respond(base::nullopt); |
| 366 } | 370 } |
| 367 | 371 |
| 368 void OneGoogleBarFetcherImpl::Respond( | 372 void OneGoogleBarFetcherImpl::Respond( |
| 369 const base::Optional<OneGoogleBarData>& data) { | 373 const base::Optional<OneGoogleBarData>& data) { |
| 370 for (auto& callback : callbacks_) { | 374 for (auto& callback : callbacks_) { |
| 371 std::move(callback).Run(data); | 375 std::move(callback).Run(data); |
| 372 } | 376 } |
| 373 callbacks_.clear(); | 377 callbacks_.clear(); |
| 374 } | 378 } |
| OLD | NEW |