diff options
Diffstat (limited to 'chromium/components/history/core/browser/web_history_service.cc')
-rw-r--r-- | chromium/components/history/core/browser/web_history_service.cc | 152 |
1 files changed, 78 insertions, 74 deletions
diff --git a/chromium/components/history/core/browser/web_history_service.cc b/chromium/components/history/core/browser/web_history_service.cc index 325566e56eb..9df346bd454 100644 --- a/chromium/components/history/core/browser/web_history_service.cc +++ b/chromium/components/history/core/browser/web_history_service.cc @@ -28,10 +28,11 @@ #include "net/base/url_util.h" #include "net/http/http_status_code.h" #include "net/http/http_util.h" -#include "net/url_request/url_fetcher.h" -#include "net/url_request/url_fetcher_delegate.h" -#include "net/url_request/url_request_context_getter.h" #include "services/identity/public/cpp/identity_manager.h" +#include "services/identity/public/cpp/primary_account_access_token_fetcher.h" +#include "services/network/public/cpp/resource_request.h" +#include "services/network/public/cpp/shared_url_loader_factory.h" +#include "services/network/public/cpp/simple_url_loader.h" #include "ui/base/device_form_factor.h" #include "url/gurl.h" @@ -63,11 +64,10 @@ const char kPostDataMimeType[] = "text/plain"; const char kSyncProtoMimeType[] = "application/octet-stream"; -// The maximum number of retries for the URLFetcher requests. +// The maximum number of retries for the SimpleURLLoader requests. const size_t kMaxRetries = 1; -class RequestImpl : public WebHistoryService::Request, - private net::URLFetcherDelegate { +class RequestImpl : public WebHistoryService::Request { public: ~RequestImpl() override {} @@ -85,12 +85,12 @@ class RequestImpl : public WebHistoryService::Request, RequestImpl( identity::IdentityManager* identity_manager, - const scoped_refptr<net::URLRequestContextGetter>& request_context, + scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, const GURL& url, const WebHistoryService::CompletionCallback& callback, const net::PartialNetworkTrafficAnnotationTag& partial_traffic_annotation) : identity_manager_(identity_manager), - request_context_(request_context), + url_loader_factory_(std::move(url_loader_factory)), url_(url), post_data_mime_type_(kPostDataMimeType), response_code_(0), @@ -99,11 +99,11 @@ class RequestImpl : public WebHistoryService::Request, is_pending_(false), partial_traffic_annotation_(partial_traffic_annotation) { DCHECK(identity_manager_); - DCHECK(request_context_); + DCHECK(url_loader_factory_); } - void OnAccessTokenFetchComplete(const GoogleServiceAuthError& error, - const std::string& access_token) { + void OnAccessTokenFetchComplete(GoogleServiceAuthError error, + identity::AccessTokenInfo access_token_info) { access_token_fetcher_.reset(); if (error.state() != GoogleServiceAuthError::NONE) { @@ -116,14 +116,55 @@ class RequestImpl : public WebHistoryService::Request, return; } - DCHECK(!access_token.empty()); - access_token_ = access_token; + DCHECK(!access_token_info.token.empty()); + access_token_ = access_token_info.token; UMA_HISTOGRAM_BOOLEAN("WebHistory.OAuthTokenCompletion", true); // Got an access token -- start the actual API request. - url_fetcher_ = CreateUrlFetcher(access_token); - url_fetcher_->Start(); + net::NetworkTrafficAnnotationTag traffic_annotation = + net::CompleteNetworkTrafficAnnotation("web_history_service", + partial_traffic_annotation_, + R"( + semantics { + sender: "Web History" + destination: GOOGLE_OWNED_SERVICE + } + policy { + cookies_allowed: NO + setting: + "To disable this feature, users can either sign out or disable " + "history sync via unchecking 'History' setting under 'Advanced " + "sync settings." + })"); + auto resource_request = std::make_unique<network::ResourceRequest>(); + resource_request->url = url_; + resource_request->load_flags = + net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES; + resource_request->method = post_data_ ? "POST" : "GET"; + resource_request->headers.SetHeader(net::HttpRequestHeaders::kAuthorization, + "Bearer " + access_token_info.token); + resource_request->headers.SetHeader( + "X-Developer-Key", GaiaUrls::GetInstance()->oauth2_chrome_client_id()); + if (!user_agent_.empty()) { + resource_request->headers.SetHeader(net::HttpRequestHeaders::kUserAgent, + user_agent_); + } + // TODO(https://crbug.com/808498): Re-add data use measurement once + // SimpleURLLoader supports it. + // ID=data_use_measurement::DataUseUserData::WEB_HISTORY_SERVICE + simple_url_loader_ = network::SimpleURLLoader::Create( + std::move(resource_request), traffic_annotation); + if (post_data_) { + simple_url_loader_->AttachStringForUpload(post_data_.value(), + post_data_mime_type_); + } + simple_url_loader_->SetRetryOptions(kMaxRetries, + network::SimpleURLLoader::RETRY_ON_5XX); + simple_url_loader_->DownloadToStringOfUnboundedSizeUntilCrashAndDie( + url_loader_factory_.get(), + base::BindOnce(&RequestImpl::OnSimpleLoaderComplete, + base::Unretained(this))); } // Tells the request to do its thang. @@ -132,18 +173,22 @@ class RequestImpl : public WebHistoryService::Request, oauth_scopes.insert(kHistoryOAuthScope); access_token_fetcher_ = - identity_manager_->CreateAccessTokenFetcherForPrimaryAccount( - "web_history", oauth_scopes, + std::make_unique<identity::PrimaryAccountAccessTokenFetcher>( + "web_history", identity_manager_, oauth_scopes, base::BindOnce(&RequestImpl::OnAccessTokenFetchComplete, base::Unretained(this)), identity::PrimaryAccountAccessTokenFetcher::Mode::kImmediate); is_pending_ = true; } - // content::URLFetcherDelegate interface. - void OnURLFetchComplete(const net::URLFetcher* source) override { - DCHECK_EQ(source, url_fetcher_.get()); - response_code_ = url_fetcher_->GetResponseCode(); + void OnSimpleLoaderComplete(std::unique_ptr<std::string> response_body) { + response_code_ = -1; + if (simple_url_loader_->ResponseInfo() && + simple_url_loader_->ResponseInfo()->headers) { + response_code_ = + simple_url_loader_->ResponseInfo()->headers->response_code(); + } + simple_url_loader_.reset(); UMA_HISTOGRAM_CUSTOM_ENUMERATION("WebHistory.OAuthTokenResponseCode", net::HttpUtil::MapStatusCodeForHistogram(response_code_), @@ -155,65 +200,24 @@ class RequestImpl : public WebHistoryService::Request, OAuth2TokenService::ScopeSet oauth_scopes; oauth_scopes.insert(kHistoryOAuthScope); identity_manager_->RemoveAccessTokenFromCache( - identity_manager_->GetPrimaryAccountInfo(), oauth_scopes, + identity_manager_->GetPrimaryAccountInfo().account_id, oauth_scopes, access_token_); access_token_.clear(); Start(); return; } - url_fetcher_->GetResponseAsString(&response_body_); - url_fetcher_.reset(); + if (response_body) { + response_body_ = std::move(*response_body); + } else { + response_body_.clear(); + } is_pending_ = false; callback_.Run(this, true); // It is valid for the callback to delete |this|, so do not access any // members below here. } - // Helper for creating a new URLFetcher for the API request. - std::unique_ptr<net::URLFetcher> CreateUrlFetcher( - const std::string& access_token) { - net::URLFetcher::RequestType request_type = post_data_ ? - net::URLFetcher::POST : net::URLFetcher::GET; - net::NetworkTrafficAnnotationTag traffic_annotation = - net::CompleteNetworkTrafficAnnotation("web_history_service", - partial_traffic_annotation_, - R"( - semantics { - sender: "Web History" - destination: GOOGLE_OWNED_SERVICE - } - policy { - cookies_allowed: NO - setting: - "To disable this feature, users can either sign out or disable " - "history sync via unchecking 'History' setting under 'Advanced " - "sync settings." - })"); - std::unique_ptr<net::URLFetcher> fetcher = - net::URLFetcher::Create(url_, request_type, this, traffic_annotation); - data_use_measurement::DataUseUserData::AttachToFetcher( - fetcher.get(), - data_use_measurement::DataUseUserData::WEB_HISTORY_SERVICE); - fetcher->SetRequestContext(request_context_.get()); - fetcher->SetMaxRetriesOn5xx(kMaxRetries); - fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | - net::LOAD_DO_NOT_SAVE_COOKIES); - fetcher->AddExtraRequestHeader("Authorization: Bearer " + access_token); - fetcher->AddExtraRequestHeader("X-Developer-Key: " + - GaiaUrls::GetInstance()->oauth2_chrome_client_id()); - - if (!user_agent_.empty()) { - fetcher->AddExtraRequestHeader( - std::string(net::HttpRequestHeaders::kUserAgent) + - ": " + user_agent_); - } - - if (post_data_) - fetcher->SetUploadData(post_data_mime_type_, post_data_.value()); - return fetcher; - } - void SetPostData(const std::string& post_data) override { SetPostDataAndType(post_data, kPostDataMimeType); } @@ -229,7 +233,7 @@ class RequestImpl : public WebHistoryService::Request, } identity::IdentityManager* identity_manager_; - scoped_refptr<net::URLRequestContextGetter> request_context_; + scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_; // The URL of the API endpoint. GURL url_; @@ -250,7 +254,7 @@ class RequestImpl : public WebHistoryService::Request, std::string access_token_; // Handles the actual API requests after the OAuth token is acquired. - std::unique_ptr<net::URLFetcher> url_fetcher_; + std::unique_ptr<network::SimpleURLLoader> simple_url_loader_; // Holds the response code received from the server. int response_code_; @@ -268,7 +272,7 @@ class RequestImpl : public WebHistoryService::Request, // True if the request was started and has not yet completed, otherwise false. bool is_pending_; - // Partial Network traffic annotation used to create URLFetcher for this + // Partial Network traffic annotation used to create SimpleURLLoader for this // request. const net::PartialNetworkTrafficAnnotationTag partial_traffic_annotation_; }; @@ -348,9 +352,9 @@ WebHistoryService::Request::~Request() { WebHistoryService::WebHistoryService( identity::IdentityManager* identity_manager, - const scoped_refptr<net::URLRequestContextGetter>& request_context) + scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) : identity_manager_(identity_manager), - request_context_(request_context), + url_loader_factory_(std::move(url_loader_factory)), weak_ptr_factory_(this) {} WebHistoryService::~WebHistoryService() { @@ -368,7 +372,7 @@ WebHistoryService::Request* WebHistoryService::CreateRequest( const GURL& url, const CompletionCallback& callback, const net::PartialNetworkTrafficAnnotationTag& partial_traffic_annotation) { - return new RequestImpl(identity_manager_, request_context_, url, callback, + return new RequestImpl(identity_manager_, url_loader_factory_, url, callback, partial_traffic_annotation); } |