Chromium Code Reviews| Index: chrome/browser/search/one_google_bar/one_google_bar_fetcher_impl.cc |
| diff --git a/chrome/browser/search/one_google_bar/one_google_bar_fetcher_impl.cc b/chrome/browser/search/one_google_bar/one_google_bar_fetcher_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1a569a82b401ff7e141ebb81758163765abde791 |
| --- /dev/null |
| +++ b/chrome/browser/search/one_google_bar/one_google_bar_fetcher_impl.cc |
| @@ -0,0 +1,357 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/search/one_google_bar/one_google_bar_fetcher_impl.h" |
| + |
| +#include <string> |
| +#include <utility> |
| + |
| +#include "base/callback.h" |
| +#include "base/command_line.h" |
| +#include "base/json/json_reader.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/search/one_google_bar/one_google_bar_data.h" |
| +#include "chrome/common/chrome_content_client.h" |
| +#include "components/google/core/browser/google_url_tracker.h" |
| +#include "components/signin/core/browser/access_token_fetcher.h" |
| +#include "components/signin/core/browser/signin_manager.h" |
| +#include "google_apis/gaia/oauth2_token_service.h" |
| +#include "google_apis/google_api_keys.h" |
| +#include "net/base/load_flags.h" |
| +#include "net/http/http_status_code.h" |
| +#include "net/url_request/url_fetcher.h" |
| +#include "net/url_request/url_fetcher_delegate.h" |
| + |
| +namespace { |
| + |
| +const char kApiUrl[] = "https://onegoogle-pa.googleapis.com/v1/getbar"; |
| + |
| +const char kApiKeyFormat[] = "?key=%s"; |
|
sfiera
2017/04/12 17:25:47
This is at least the second time that we've writte
Marc Treib
2017/04/13 09:03:34
Agreed - that's essentially crbug.com/609084, whic
|
| + |
| +const char kApiScope[] = "https://www.googleapis.com/auth/onegoogle.readonly"; |
| +const char kAuthorizationRequestHeaderFormat[] = "Bearer %s"; |
| + |
| +const char kRequestBodyFormat[] = R"json({ |
| + "subproduct": 243, |
| + "enable_multilogin": true, |
| + "user_agent": "%s", |
| + "accept_language": "%s", |
| + "original_request_url": "%s", |
| + "material_options": { |
| + "page_title": " ", |
| + "position_fixed": true, |
| + "styling_options": { |
| + "background_color": { "alpha": { "value": 0 } } |
| + }, |
| + "disable_moving_userpanel_to_menu": true |
| + } |
| +})json"; |
| + |
| +const char kResponsePreamble[] = ")]}'"; |
| + |
| +// This namespace contains helpers to extract SafeHtml-wrapped strings (see |
| +// https://github.com/google/safe-html-types) from the response json. If there |
| +// is ever a C++ version of the SafeHtml types, we should consider using that |
| +// instead of these custom functions. |
| +namespace safe_html { |
| + |
| +bool GetImpl(const base::DictionaryValue& dict, |
| + const std::string& name, |
| + const std::string& wrapped_field_name, |
| + std::string* out) { |
| + const base::DictionaryValue* value = nullptr; |
| + if (!dict.GetDictionary(name, &value)) { |
| + out->clear(); |
| + return false; |
| + } |
| + |
| + if (!value->GetString(wrapped_field_name, out)) { |
| + out->clear(); |
| + return false; |
| + } |
| + |
| + return true; |
| +} |
| + |
| +bool GetHtml(const base::DictionaryValue& dict, |
| + const std::string& name, |
| + std::string* out) { |
| + return GetImpl(dict, name, "privateDoNotAccessOrElseSafeHtmlWrappedValue", |
| + out); |
| +} |
| + |
| +bool GetScript(const base::DictionaryValue& dict, |
| + const std::string& name, |
| + std::string* out) { |
| + return GetImpl(dict, name, "privateDoNotAccessOrElseSafeScriptWrappedValue", |
| + out); |
| +} |
| + |
| +bool GetStyleSheet(const base::DictionaryValue& dict, |
| + const std::string& name, |
| + std::string* out) { |
| + return GetImpl(dict, name, |
| + "privateDoNotAccessOrElseSafeStyleSheetWrappedValue", out); |
| +} |
| + |
| +} // namespace safe_html |
| + |
| +} // namespace |
| + |
| +class OneGoogleBarFetcherImpl::AuthenticatedURLFetcher |
| + : public net::URLFetcherDelegate { |
| + public: |
| + using FetchDoneCallback = base::OnceCallback<void(const net::URLFetcher*)>; |
| + |
| + AuthenticatedURLFetcher(SigninManagerBase* signin_manager, |
| + OAuth2TokenService* token_service, |
| + net::URLRequestContextGetter* request_context, |
| + const GURL& google_base_url, |
| + FetchDoneCallback callback); |
| + ~AuthenticatedURLFetcher() override; |
| + |
| + void Start(); |
| + |
| + private: |
| + GURL GetApiUrl(bool use_oauth) const; |
| + std::string GetRequestBody() const; |
| + std::string GetExtraRequestHeaders(const std::string& access_token) const; |
| + |
| + void GotAccessToken(const GoogleServiceAuthError& error, |
| + const std::string& access_token); |
| + |
| + // URLFetcherDelegate implementation. |
| + void OnURLFetchComplete(const net::URLFetcher* source) override; |
| + |
| + SigninManagerBase* signin_manager_; |
|
sfiera
2017/04/12 17:25:47
I'm in favor of "(*) const" whenever possible beca
Marc Treib
2017/04/13 09:03:34
Done.
|
| + OAuth2TokenService* token_service_; |
| + net::URLRequestContextGetter* request_context_; |
| + GURL google_base_url_; |
| + |
| + FetchDoneCallback callback_; |
| + |
| + std::unique_ptr<AccessTokenFetcher> token_fetcher_; |
| + |
| + // The underlying URLFetcher which does the actual fetch. |
| + std::unique_ptr<net::URLFetcher> url_fetcher_; |
| +}; |
| + |
| +OneGoogleBarFetcherImpl::AuthenticatedURLFetcher::AuthenticatedURLFetcher( |
| + SigninManagerBase* signin_manager, |
| + OAuth2TokenService* token_service, |
| + net::URLRequestContextGetter* request_context, |
| + const GURL& google_base_url, |
| + FetchDoneCallback callback) |
| + : signin_manager_(signin_manager), |
| + token_service_(token_service), |
| + request_context_(request_context), |
| + google_base_url_(google_base_url), |
| + callback_(std::move(callback)) {} |
| + |
| +OneGoogleBarFetcherImpl::AuthenticatedURLFetcher::~AuthenticatedURLFetcher() = |
| + default; |
|
sfiera
2017/04/12 17:25:47
Inline with the class?
Marc Treib
2017/04/13 09:03:34
Sure, done.
|
| + |
| +void OneGoogleBarFetcherImpl::AuthenticatedURLFetcher::Start() { |
| + if (!signin_manager_->IsAuthenticated()) { |
| + GotAccessToken(GoogleServiceAuthError::AuthErrorNone(), std::string()); |
| + return; |
| + } |
| + OAuth2TokenService::ScopeSet scopes; |
| + scopes.insert(kApiScope); |
| + token_fetcher_ = base::MakeUnique<AccessTokenFetcher>( |
| + "one_google", signin_manager_, token_service_, scopes, |
| + base::BindOnce(&AuthenticatedURLFetcher::GotAccessToken, |
| + base::Unretained(this))); |
| +} |
| + |
| +GURL OneGoogleBarFetcherImpl::AuthenticatedURLFetcher::GetApiUrl( |
| + bool use_oauth) const { |
| + std::string api_url(kApiUrl); |
| + // TODO(treib): Attach to feature instead of cmdline. |
| + base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess(); |
| + if (cmdline->HasSwitch("one-google-api-url")) |
| + api_url = cmdline->GetSwitchValueASCII("one-google-api-url"); |
| + // Append the API key only for unauthenticated requests. |
| + if (!use_oauth) { |
| + api_url += |
| + base::StringPrintf(kApiKeyFormat, google_apis::GetAPIKey().c_str()); |
| + } |
| + |
| + return GURL(api_url); |
| +} |
| + |
| +std::string OneGoogleBarFetcherImpl::AuthenticatedURLFetcher::GetRequestBody() |
| + const { |
| + // TODO(treib): Attach to feature instead of cmdline. |
| + base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess(); |
| + if (cmdline->HasSwitch("one-google-bar-options")) |
| + return cmdline->GetSwitchValueASCII("one-google-bar-options"); |
| + |
| + return base::StringPrintf(kRequestBodyFormat, GetUserAgent().c_str(), |
| + g_browser_process->GetApplicationLocale().c_str(), |
| + google_base_url_.spec().c_str()); |
| +} |
| + |
| +std::string |
| +OneGoogleBarFetcherImpl::AuthenticatedURLFetcher::GetExtraRequestHeaders( |
| + const std::string& access_token) const { |
| + net::HttpRequestHeaders headers; |
| + headers.SetHeader("Content-Type", "application/json; charset=UTF-8"); |
| + if (!access_token.empty()) { |
| + headers.SetHeader("Authorization", |
| + base::StringPrintf(kAuthorizationRequestHeaderFormat, |
| + access_token.c_str())); |
| + } |
| + return headers.ToString(); |
| +} |
| + |
| +void OneGoogleBarFetcherImpl::AuthenticatedURLFetcher::GotAccessToken( |
| + const GoogleServiceAuthError& error, |
| + const std::string& access_token) { |
| + // Delete the token fetcher after we leave this method. |
| + std::unique_ptr<AccessTokenFetcher> deleter(std::move(token_fetcher_)); |
| + |
| + bool use_oauth = !access_token.empty(); |
| + url_fetcher_ = net::URLFetcher::Create(0, GetApiUrl(use_oauth), |
| + net::URLFetcher::POST, this); |
| + url_fetcher_->SetRequestContext(request_context_); |
| + |
| + url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_AUTH_DATA | |
| + net::LOAD_DO_NOT_SEND_COOKIES | |
| + net::LOAD_DO_NOT_SAVE_COOKIES); |
| + url_fetcher_->SetUploadData("application/json", GetRequestBody()); |
| + url_fetcher_->SetExtraRequestHeaders(GetExtraRequestHeaders(access_token)); |
|
sfiera
2017/04/12 17:25:47
Should we send variations headers too?
Marc Treib
2017/04/13 09:03:34
Good idea! There are no concrete plans to use them
|
| + |
| + url_fetcher_->Start(); |
| +} |
| + |
| +void OneGoogleBarFetcherImpl::AuthenticatedURLFetcher::OnURLFetchComplete( |
| + const net::URLFetcher* source) { |
| + std::move(callback_).Run(source); |
| +} |
| + |
| +OneGoogleBarFetcherImpl::OneGoogleBarFetcherImpl( |
| + SigninManagerBase* signin_manager, |
| + OAuth2TokenService* token_service, |
| + net::URLRequestContextGetter* request_context, |
| + GoogleURLTracker* google_url_tracker) |
| + : signin_manager_(signin_manager), |
| + token_service_(token_service), |
| + request_context_(request_context), |
| + google_url_tracker_(google_url_tracker) {} |
| + |
| +OneGoogleBarFetcherImpl::~OneGoogleBarFetcherImpl() = default; |
| + |
| +void OneGoogleBarFetcherImpl::Fetch(OneGoogleCallback callback) { |
| + callbacks_.push_back(std::move(callback)); |
| + IssueRequestIfNoneOngoing(); |
| +} |
| + |
| +void OneGoogleBarFetcherImpl::IssueRequestIfNoneOngoing() { |
| + // If there is an ongoing request, let it complete. |
| + if (pending_request_.get()) |
| + return; |
| + |
| + pending_request_ = base::MakeUnique<AuthenticatedURLFetcher>( |
| + signin_manager_, token_service_, request_context_, |
| + google_url_tracker_->google_url(), |
| + base::BindOnce(&OneGoogleBarFetcherImpl::FetchDone, |
| + base::Unretained(this))); |
| + pending_request_->Start(); |
| +} |
| + |
| +void OneGoogleBarFetcherImpl::FetchDone(const net::URLFetcher* source) { |
| + // The fetcher will be deleted when the request is handled. |
| + std::unique_ptr<AuthenticatedURLFetcher> deleter(std::move(pending_request_)); |
| + |
| + const net::URLRequestStatus& request_status = source->GetStatus(); |
| + if (request_status.status() != net::URLRequestStatus::SUCCESS) { |
| + // This represents network errors (i.e. the server did not provide a |
| + // response). |
| + DLOG(WARNING) << "Request failed with error: " << request_status.error() |
| + << ": " << net::ErrorToString(request_status.error()); |
| + Respond(base::nullopt); |
| + return; |
| + } |
| + |
| + const int response_code = source->GetResponseCode(); |
| + if (response_code != net::HTTP_OK) { |
| + DLOG(WARNING) << "Response code: " << response_code; |
| + std::string response; |
| + source->GetResponseAsString(&response); |
| + DLOG(WARNING) << "Response: " << response; |
| + Respond(base::nullopt); |
| + return; |
| + } |
| + |
| + std::string response; |
| + bool success = source->GetResponseAsString(&response); |
| + DCHECK(success); |
| + |
| + // The response may start with )]}'. Ignore this. |
| + base::StringPiece response_sp(response); |
| + if (response_sp.starts_with(kResponsePreamble)) |
| + response_sp.remove_prefix(strlen(kResponsePreamble)); |
| + |
| + int error_code = 0; |
| + std::string error_msg; |
| + std::unique_ptr<const base::Value> value = |
| + base::JSONReader::ReadAndReturnError(response_sp, |
| + base::JSON_ALLOW_TRAILING_COMMAS, |
| + &error_code, &error_msg); |
| + if (!value) { |
| + DLOG(WARNING) << error_code << ": " << error_msg; |
| + Respond(base::nullopt); |
| + return; |
| + } |
| + const base::DictionaryValue* dict = nullptr; |
| + if (!value->GetAsDictionary(&dict)) { |
| + DLOG(WARNING) << "Parse error: top-level dictionary not found"; |
| + Respond(base::nullopt); |
| + return; |
| + } |
| + |
| + const base::DictionaryValue* one_google_bar = nullptr; |
| + if (!dict->GetDictionary("oneGoogleBar", &one_google_bar)) { |
| + DLOG(WARNING) << "Parse error: no oneGoogleBar"; |
| + Respond(base::nullopt); |
| + return; |
| + } |
| + |
| + OneGoogleBarData result; |
| + |
| + if (!safe_html::GetHtml(*one_google_bar, "html", &result.bar_html)) { |
| + DLOG(WARNING) << "Parse error: no html"; |
| + Respond(base::nullopt); |
| + return; |
| + } |
| + |
| + const base::DictionaryValue* page_hooks = nullptr; |
| + if (!one_google_bar->GetDictionary("pageHooks", &page_hooks)) { |
| + DLOG(WARNING) << "Parse error: no pageHooks"; |
| + Respond(base::nullopt); |
| + return; |
| + } |
| + |
| + safe_html::GetScript(*page_hooks, "inHeadScript", &result.in_head_script); |
| + safe_html::GetHtml(*page_hooks, "inHeadNoscript", &result.in_head_noscript); |
| + safe_html::GetStyleSheet(*page_hooks, "inHeadStyle", &result.in_head_style); |
| + safe_html::GetScript(*page_hooks, "afterBarScript", &result.after_bar_script); |
| + safe_html::GetHtml(*page_hooks, "endOfBodyHtml", &result.end_of_body_html); |
| + safe_html::GetScript(*page_hooks, "endOfBodyScript", |
| + &result.end_of_body_script); |
| + |
| + Respond(result); |
| +} |
| + |
| +void OneGoogleBarFetcherImpl::Respond( |
| + const base::Optional<OneGoogleBarData>& data) { |
| + for (auto& callback : callbacks_) { |
| + std::move(callback).Run(data); |
| + } |
| + callbacks_.clear(); |
| +} |