Commit b624551f authored by Reilly Grant's avatar Reilly Grant Committed by Commit Bot

Revert "Port policy::UploadJobImpl to network::SimpleURLLoader"

This reverts commit 581330a9.

Reason for revert: SimpleURLLoaderTest.OnUploadProgressCallback/3 is flaky

Original change's description:
> Port policy::UploadJobImpl to network::SimpleURLLoader
> 
> This change ports UploadJobImpl from net::URLFetcher to SimpleURLLoader.
> 
> Since this request does not make use of the response a new method has
> been added to SimpleURLLoader, DownloadToNull. This download mode still
> reads the response body from the network but does not save it anywhere.
> This is useful for requests which upload data and would otherwise have
> to specify an arbitrary |max_body_size|. A maximum may still be set if
> the caller wants to limit the amount of data transferred over the
> network.
> 
> Bug: 773295
> Cq-Include-Trybots: luci.chromium.try:linux_mojo
> Change-Id: Iee1fdc6f7406066ced8c91122e22cd51ddcb1c5f
> Reviewed-on: https://chromium-review.googlesource.com/1161551
> Commit-Queue: Reilly Grant <reillyg@chromium.org>
> Reviewed-by: Matt Menke <mmenke@chromium.org>
> Reviewed-by: Julian Pastarmov <pastarmovj@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#581060}

TBR=pastarmovj@chromium.org,reillyg@chromium.org,mmenke@chromium.org

Change-Id: Ica09c85a12861d35efbdfd9a882d137bac6b9e56
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: 773295, 872023
Cq-Include-Trybots: luci.chromium.try:linux_mojo
Reviewed-on: https://chromium-review.googlesource.com/1166288Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Commit-Queue: Reilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#581389}
parent e00760c6
...@@ -53,6 +53,8 @@ std::unique_ptr<UploadJob> ScreenshotDelegate::CreateUploadJob( ...@@ -53,6 +53,8 @@ std::unique_ptr<UploadJob> ScreenshotDelegate::CreateUploadJob(
chromeos::DeviceOAuth2TokenService* device_oauth2_token_service = chromeos::DeviceOAuth2TokenService* device_oauth2_token_service =
chromeos::DeviceOAuth2TokenServiceFactory::Get(); chromeos::DeviceOAuth2TokenServiceFactory::Get();
scoped_refptr<net::URLRequestContextGetter> system_request_context =
g_browser_process->system_request_context();
std::string robot_account_id = std::string robot_account_id =
device_oauth2_token_service->GetRobotAccountId(); device_oauth2_token_service->GetRobotAccountId();
...@@ -78,7 +80,7 @@ std::unique_ptr<UploadJob> ScreenshotDelegate::CreateUploadJob( ...@@ -78,7 +80,7 @@ std::unique_ptr<UploadJob> ScreenshotDelegate::CreateUploadJob(
)"); )");
return std::unique_ptr<UploadJob>(new UploadJobImpl( return std::unique_ptr<UploadJob>(new UploadJobImpl(
upload_url, robot_account_id, device_oauth2_token_service, upload_url, robot_account_id, device_oauth2_token_service,
g_browser_process->shared_url_loader_factory(), delegate, system_request_context, delegate,
base::WrapUnique(new UploadJobImpl::RandomMimeBoundaryGenerator), base::WrapUnique(new UploadJobImpl::RandomMimeBoundaryGenerator),
traffic_annotation, base::ThreadTaskRunnerHandle::Get())); traffic_annotation, base::ThreadTaskRunnerHandle::Get()));
} }
......
...@@ -27,7 +27,6 @@ ...@@ -27,7 +27,6 @@
#include "components/policy/core/browser/browser_policy_connector.h" #include "components/policy/core/browser/browser_policy_connector.h"
#include "components/user_manager/user_manager.h" #include "components/user_manager/user_manager.h"
#include "net/http/http_request_headers.h" #include "net/http/http_request_headers.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
namespace policy { namespace policy {
...@@ -145,6 +144,8 @@ std::unique_ptr<UploadJob> SystemLogDelegate::CreateUploadJob( ...@@ -145,6 +144,8 @@ std::unique_ptr<UploadJob> SystemLogDelegate::CreateUploadJob(
chromeos::DeviceOAuth2TokenService* device_oauth2_token_service = chromeos::DeviceOAuth2TokenService* device_oauth2_token_service =
chromeos::DeviceOAuth2TokenServiceFactory::Get(); chromeos::DeviceOAuth2TokenServiceFactory::Get();
scoped_refptr<net::URLRequestContextGetter> system_request_context =
g_browser_process->system_request_context();
std::string robot_account_id = std::string robot_account_id =
device_oauth2_token_service->GetRobotAccountId(); device_oauth2_token_service->GetRobotAccountId();
...@@ -172,7 +173,7 @@ std::unique_ptr<UploadJob> SystemLogDelegate::CreateUploadJob( ...@@ -172,7 +173,7 @@ std::unique_ptr<UploadJob> SystemLogDelegate::CreateUploadJob(
)"); )");
return std::make_unique<UploadJobImpl>( return std::make_unique<UploadJobImpl>(
upload_url, robot_account_id, device_oauth2_token_service, upload_url, robot_account_id, device_oauth2_token_service,
g_browser_process->shared_url_loader_factory(), delegate, system_request_context, delegate,
std::make_unique<UploadJobImpl::RandomMimeBoundaryGenerator>(), std::make_unique<UploadJobImpl::RandomMimeBoundaryGenerator>(),
traffic_annotation, task_runner_); traffic_annotation, task_runner_);
} }
......
...@@ -19,8 +19,7 @@ ...@@ -19,8 +19,7 @@
#include "net/base/mime_util.h" #include "net/base/mime_util.h"
#include "net/http/http_status_code.h" #include "net/http/http_status_code.h"
#include "net/traffic_annotation/network_traffic_annotation.h" #include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/cpp/shared_url_loader_factory.h" #include "net/url_request/url_request_status.h"
#include "services/network/public/cpp/simple_url_loader.h"
namespace policy { namespace policy {
...@@ -28,7 +27,7 @@ namespace { ...@@ -28,7 +27,7 @@ namespace {
// Format for bearer tokens in HTTP requests to access OAuth 2.0 protected // Format for bearer tokens in HTTP requests to access OAuth 2.0 protected
// resources. // resources.
const char kAuthorizationHeaderFormat[] = "Bearer %s"; const char kAuthorizationHeaderFormat[] = "Authorization: Bearer %s";
// Value the "Content-Type" field will be set to in the POST request. // Value the "Content-Type" field will be set to in the POST request.
const char kUploadContentType[] = "multipart/form-data"; const char kUploadContentType[] = "multipart/form-data";
...@@ -152,7 +151,7 @@ UploadJobImpl::UploadJobImpl( ...@@ -152,7 +151,7 @@ UploadJobImpl::UploadJobImpl(
const GURL& upload_url, const GURL& upload_url,
const std::string& account_id, const std::string& account_id,
OAuth2TokenService* token_service, OAuth2TokenService* token_service,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, scoped_refptr<net::URLRequestContextGetter> url_context_getter,
Delegate* delegate, Delegate* delegate,
std::unique_ptr<MimeBoundaryGenerator> boundary_generator, std::unique_ptr<MimeBoundaryGenerator> boundary_generator,
net::NetworkTrafficAnnotationTag traffic_annotation, net::NetworkTrafficAnnotationTag traffic_annotation,
...@@ -161,7 +160,7 @@ UploadJobImpl::UploadJobImpl( ...@@ -161,7 +160,7 @@ UploadJobImpl::UploadJobImpl(
upload_url_(upload_url), upload_url_(upload_url),
account_id_(account_id), account_id_(account_id),
token_service_(token_service), token_service_(token_service),
url_loader_factory_(std::move(url_loader_factory)), url_context_getter_(url_context_getter),
delegate_(delegate), delegate_(delegate),
boundary_generator_(std::move(boundary_generator)), boundary_generator_(std::move(boundary_generator)),
traffic_annotation_(traffic_annotation), traffic_annotation_(traffic_annotation),
...@@ -170,7 +169,7 @@ UploadJobImpl::UploadJobImpl( ...@@ -170,7 +169,7 @@ UploadJobImpl::UploadJobImpl(
task_runner_(task_runner), task_runner_(task_runner),
weak_factory_(this) { weak_factory_(this) {
DCHECK(token_service_); DCHECK(token_service_);
DCHECK(url_loader_factory_); DCHECK(url_context_getter_);
DCHECK(delegate_); DCHECK(delegate_);
SYSLOG(INFO) << "Upload job created."; SYSLOG(INFO) << "Upload job created.";
if (!upload_url_.is_valid()) { if (!upload_url_.is_valid()) {
...@@ -300,7 +299,7 @@ bool UploadJobImpl::SetUpMultipart() { ...@@ -300,7 +299,7 @@ bool UploadJobImpl::SetUpMultipart() {
return true; return true;
} }
void UploadJobImpl::CreateAndStartURLLoader(const std::string& access_token) { void UploadJobImpl::CreateAndStartURLFetcher(const std::string& access_token) {
// Ensure that the content has been prepared and the upload url is valid. // Ensure that the content has been prepared and the upload url is valid.
DCHECK_EQ(PREPARING_CONTENT, state_); DCHECK_EQ(PREPARING_CONTENT, state_);
SYSLOG(INFO) << "Starting URL fetcher."; SYSLOG(INFO) << "Starting URL fetcher.";
...@@ -309,20 +308,13 @@ void UploadJobImpl::CreateAndStartURLLoader(const std::string& access_token) { ...@@ -309,20 +308,13 @@ void UploadJobImpl::CreateAndStartURLLoader(const std::string& access_token) {
content_type.append("; boundary="); content_type.append("; boundary=");
content_type.append(*mime_boundary_.get()); content_type.append(*mime_boundary_.get());
auto resource_request = std::make_unique<network::ResourceRequest>(); upload_fetcher_ = net::URLFetcher::Create(upload_url_, net::URLFetcher::POST,
resource_request->method = "POST"; this, traffic_annotation_);
resource_request->url = upload_url_; upload_fetcher_->SetRequestContext(url_context_getter_.get());
resource_request->headers.SetHeader( upload_fetcher_->SetUploadData(content_type, *post_data_);
net::HttpRequestHeaders::kAuthorization, upload_fetcher_->AddExtraRequestHeader(
base::StringPrintf(kAuthorizationHeaderFormat, access_token.c_str())); base::StringPrintf(kAuthorizationHeaderFormat, access_token.c_str()));
upload_fetcher_->Start();
url_loader_ = network::SimpleURLLoader::Create(std::move(resource_request),
traffic_annotation_);
url_loader_->AttachStringForUpload(*post_data_, content_type);
url_loader_->DownloadHeadersOnly(
url_loader_factory_.get(),
base::BindOnce(&UploadJobImpl::OnURLLoadComplete,
base::Unretained(this)));
} }
void UploadJobImpl::StartUpload() { void UploadJobImpl::StartUpload() {
...@@ -334,7 +326,7 @@ void UploadJobImpl::StartUpload() { ...@@ -334,7 +326,7 @@ void UploadJobImpl::StartUpload() {
state_ = ERROR; state_ = ERROR;
return; return;
} }
CreateAndStartURLLoader(access_token_); CreateAndStartURLFetcher(access_token_);
state_ = UPLOADING; state_ = UPLOADING;
} }
...@@ -364,7 +356,7 @@ void UploadJobImpl::OnGetTokenFailure( ...@@ -364,7 +356,7 @@ void UploadJobImpl::OnGetTokenFailure(
void UploadJobImpl::HandleError(ErrorCode error_code) { void UploadJobImpl::HandleError(ErrorCode error_code) {
retry_++; retry_++;
url_loader_.reset(); upload_fetcher_.reset();
SYSLOG(ERROR) << "Upload failed, error code: " << error_code; SYSLOG(ERROR) << "Upload failed, error code: " << error_code;
...@@ -405,31 +397,34 @@ void UploadJobImpl::HandleError(ErrorCode error_code) { ...@@ -405,31 +397,34 @@ void UploadJobImpl::HandleError(ErrorCode error_code) {
} }
} }
void UploadJobImpl::OnURLLoadComplete( void UploadJobImpl::OnURLFetchComplete(const net::URLFetcher* source) {
scoped_refptr<net::HttpResponseHeaders> headers) { DCHECK_EQ(upload_fetcher_.get(), source);
DCHECK_EQ(UPLOADING, state_); DCHECK_EQ(UPLOADING, state_);
SYSLOG(INFO) << "URL fetch completed."; SYSLOG(INFO) << "URL fetch completed.";
std::unique_ptr<network::SimpleURLLoader> url_loader = std::move(url_loader_); const net::URLRequestStatus& status = source->GetStatus();
if (!status.is_success()) {
if (!headers) { SYSLOG(ERROR) << "URLRequestStatus error " << status.error();
SYSLOG(ERROR) << "SimpleURLLoader error " << url_loader->NetError();
HandleError(NETWORK_ERROR); HandleError(NETWORK_ERROR);
} else if (headers->response_code() == net::HTTP_OK) {
// Successful upload
access_token_.clear();
post_data_.reset();
state_ = SUCCESS;
UMA_HISTOGRAM_EXACT_LINEAR(kUploadJobSuccessHistogram, retry_,
static_cast<int>(UploadJobSuccess::REQUEST_MAX));
delegate_->OnSuccess();
} else if (headers->response_code() == net::HTTP_UNAUTHORIZED) {
SYSLOG(ERROR) << "Unauthorized request.";
HandleError(AUTHENTICATION_ERROR);
} else { } else {
SYSLOG(ERROR) << "POST request failed with HTTP status code " const int response_code = source->GetResponseCode();
<< headers->response_code() << "."; if (response_code == net::HTTP_OK) {
HandleError(SERVER_ERROR); // Successful upload
upload_fetcher_.reset();
access_token_.clear();
post_data_.reset();
state_ = SUCCESS;
UMA_HISTOGRAM_EXACT_LINEAR(
kUploadJobSuccessHistogram, retry_,
static_cast<int>(UploadJobSuccess::REQUEST_MAX));
delegate_->OnSuccess();
} else if (response_code == net::HTTP_UNAUTHORIZED) {
SYSLOG(ERROR) << "Unauthorized request.";
HandleError(AUTHENTICATION_ERROR);
} else {
SYSLOG(ERROR) << "POST request failed with HTTP status code "
<< response_code << ".";
HandleError(SERVER_ERROR);
}
} }
} }
......
...@@ -15,28 +15,23 @@ ...@@ -15,28 +15,23 @@
#include "base/threading/thread_checker.h" #include "base/threading/thread_checker.h"
#include "chrome/browser/chromeos/policy/upload_job.h" #include "chrome/browser/chromeos/policy/upload_job.h"
#include "google_apis/gaia/oauth2_token_service.h" #include "google_apis/gaia/oauth2_token_service.h"
#include "net/traffic_annotation/network_traffic_annotation.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 "url/gurl.h" #include "url/gurl.h"
namespace base { namespace base {
class SequencedTaskRunner; class SequencedTaskRunner;
} }
namespace net {
class HttpResponseHeaders;
}
namespace network {
class SharedURLLoaderFactory;
class SimpleURLLoader;
} // namespace network
namespace policy { namespace policy {
// This implementation of UploadJob uses the OAuth2TokenService to acquire // This implementation of UploadJob uses the OAuth2TokenService to acquire
// access tokens for the device management (cloud-based policy) server scope and // access tokens for the device management (cloud-based policy) server scope and
// uses a SimpleURLLoader to upload data to the specified upload url. // uses a URLFetcher to upload data to the specified upload url.
class UploadJobImpl : public UploadJob, public OAuth2TokenService::Consumer { class UploadJobImpl : public UploadJob,
public OAuth2TokenService::Consumer,
public net::URLFetcherDelegate {
public: public:
// UploadJobImpl uses a MimeBoundaryGenerator to generate strings which // UploadJobImpl uses a MimeBoundaryGenerator to generate strings which
// mark the boundaries between data segments. // mark the boundaries between data segments.
...@@ -61,15 +56,14 @@ class UploadJobImpl : public UploadJob, public OAuth2TokenService::Consumer { ...@@ -61,15 +56,14 @@ class UploadJobImpl : public UploadJob, public OAuth2TokenService::Consumer {
// |task_runner| must belong to the same thread from which the constructor and // |task_runner| must belong to the same thread from which the constructor and
// all the public methods are called. // all the public methods are called.
UploadJobImpl( UploadJobImpl(const GURL& upload_url,
const GURL& upload_url, const std::string& account_id,
const std::string& account_id, OAuth2TokenService* token_service,
OAuth2TokenService* token_service, scoped_refptr<net::URLRequestContextGetter> url_context_getter,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, Delegate* delegate,
Delegate* delegate, std::unique_ptr<MimeBoundaryGenerator> boundary_generator,
std::unique_ptr<MimeBoundaryGenerator> boundary_generator, net::NetworkTrafficAnnotationTag traffic_annotation,
net::NetworkTrafficAnnotationTag traffic_annotation, scoped_refptr<base::SequencedTaskRunner> task_runner);
scoped_refptr<base::SequencedTaskRunner> task_runner);
~UploadJobImpl() override; ~UploadJobImpl() override;
// UploadJob: // UploadJob:
...@@ -109,15 +103,15 @@ class UploadJobImpl : public UploadJob, public OAuth2TokenService::Consumer { ...@@ -109,15 +103,15 @@ class UploadJobImpl : public UploadJob, public OAuth2TokenService::Consumer {
void OnGetTokenFailure(const OAuth2TokenService::Request* request, void OnGetTokenFailure(const OAuth2TokenService::Request* request,
const GoogleServiceAuthError& error) override; const GoogleServiceAuthError& error) override;
// Called when the SimpleURLLoader is finished. // net::URLFetcherDelegate:
void OnURLLoadComplete(scoped_refptr<net::HttpResponseHeaders> headers); void OnURLFetchComplete(const net::URLFetcher* source) override;
void HandleError(ErrorCode errorCode); void HandleError(ErrorCode errorCode);
// Requests an access token for the upload scope. // Requests an access token for the upload scope.
void RequestAccessToken(); void RequestAccessToken();
// Dispatches POST request. // Dispatches POST request to URLFetcher.
void StartUpload(); void StartUpload();
// Constructs the body of the POST request by concatenating the // Constructs the body of the POST request by concatenating the
...@@ -128,9 +122,9 @@ class UploadJobImpl : public UploadJob, public OAuth2TokenService::Consumer { ...@@ -128,9 +122,9 @@ class UploadJobImpl : public UploadJob, public OAuth2TokenService::Consumer {
// an error, clears |post_data_| and |mime_boundary_| and returns false. // an error, clears |post_data_| and |mime_boundary_| and returns false.
bool SetUpMultipart(); bool SetUpMultipart();
// Assembles the request and starts the SimpleURLLoader. Fails if another // Assembles the request and starts the URLFetcher. Fails if another upload
// upload is still in progress or the content was not successfully encoded. // is still in progress or the content was not successfully encoded.
void CreateAndStartURLLoader(const std::string& access_token); void CreateAndStartURLFetcher(const std::string& access_token);
// The URL to which the POST request should be directed. // The URL to which the POST request should be directed.
const GURL upload_url_; const GURL upload_url_;
...@@ -141,8 +135,8 @@ class UploadJobImpl : public UploadJob, public OAuth2TokenService::Consumer { ...@@ -141,8 +135,8 @@ class UploadJobImpl : public UploadJob, public OAuth2TokenService::Consumer {
// The token service used to retrieve the access token. // The token service used to retrieve the access token.
OAuth2TokenService* const token_service_; OAuth2TokenService* const token_service_;
// This is used to initialize the network::SimpleURLLoader object. // This is used to initialize the net::URLFetcher object.
const scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_; const scoped_refptr<net::URLRequestContextGetter> url_context_getter_;
// The delegate to be notified of events. // The delegate to be notified of events.
Delegate* const delegate_; Delegate* const delegate_;
...@@ -175,7 +169,7 @@ class UploadJobImpl : public UploadJob, public OAuth2TokenService::Consumer { ...@@ -175,7 +169,7 @@ class UploadJobImpl : public UploadJob, public OAuth2TokenService::Consumer {
std::string access_token_; std::string access_token_;
// Helper to upload the data. // Helper to upload the data.
std::unique_ptr<network::SimpleURLLoader> url_loader_; std::unique_ptr<net::URLFetcher> upload_fetcher_;
// The data chunks to be uploaded. // The data chunks to be uploaded.
std::vector<std::unique_ptr<DataSegment>> data_segments_; std::vector<std::unique_ptr<DataSegment>> data_segments_;
......
...@@ -27,7 +27,8 @@ ...@@ -27,7 +27,8 @@
#include "net/test/embedded_test_server/http_request.h" #include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h" #include "net/test/embedded_test_server/http_response.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h" #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "services/network/test/test_shared_url_loader_factory.h" #include "net/url_request/url_request_test_util.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
namespace policy { namespace policy {
...@@ -182,8 +183,8 @@ class UploadJobTestBase : public testing::Test, public UploadJob::Delegate { ...@@ -182,8 +183,8 @@ class UploadJobTestBase : public testing::Test, public UploadJob::Delegate {
// testing::Test: // testing::Test:
void SetUp() override { void SetUp() override {
url_loader_factory_ = request_context_getter_ = new net::TestURLRequestContextGetter(
base::MakeRefCounted<network::TestSharedURLLoaderFactory>(); base::ThreadTaskRunnerHandle::Get());
oauth2_service_.AddAccount("robot@gmail.com"); oauth2_service_.AddAccount("robot@gmail.com");
ASSERT_TRUE(test_server_.Start()); ASSERT_TRUE(test_server_.Start());
// Set retry delay to prevent timeouts // Set retry delay to prevent timeouts
...@@ -200,9 +201,9 @@ class UploadJobTestBase : public testing::Test, public UploadJob::Delegate { ...@@ -200,9 +201,9 @@ class UploadJobTestBase : public testing::Test, public UploadJob::Delegate {
std::unique_ptr<UploadJobImpl::MimeBoundaryGenerator> std::unique_ptr<UploadJobImpl::MimeBoundaryGenerator>
mime_boundary_generator) { mime_boundary_generator) {
std::unique_ptr<UploadJob> upload_job(new UploadJobImpl( std::unique_ptr<UploadJob> upload_job(new UploadJobImpl(
GetServerURL(), kRobotAccountId, &oauth2_service_, url_loader_factory_, GetServerURL(), kRobotAccountId, &oauth2_service_,
this, std::move(mime_boundary_generator), TRAFFIC_ANNOTATION_FOR_TESTS, request_context_getter_.get(), this, std::move(mime_boundary_generator),
base::ThreadTaskRunnerHandle::Get())); TRAFFIC_ANNOTATION_FOR_TESTS, base::ThreadTaskRunnerHandle::Get()));
std::map<std::string, std::string> header_entries; std::map<std::string, std::string> header_entries;
header_entries.insert(std::make_pair(kCustomField1, "CUSTOM1")); header_entries.insert(std::make_pair(kCustomField1, "CUSTOM1"));
...@@ -219,7 +220,7 @@ class UploadJobTestBase : public testing::Test, public UploadJob::Delegate { ...@@ -219,7 +220,7 @@ class UploadJobTestBase : public testing::Test, public UploadJob::Delegate {
content::TestBrowserThreadBundle test_browser_thread_bundle_; content::TestBrowserThreadBundle test_browser_thread_bundle_;
base::RunLoop run_loop_; base::RunLoop run_loop_;
net::EmbeddedTestServer test_server_; net::EmbeddedTestServer test_server_;
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_; scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
MockOAuth2TokenService oauth2_service_; MockOAuth2TokenService oauth2_service_;
std::unique_ptr<UploadJob::ErrorCode> expected_error_; std::unique_ptr<UploadJob::ErrorCode> expected_error_;
......
...@@ -188,8 +188,6 @@ class SimpleURLLoaderImpl : public SimpleURLLoader, ...@@ -188,8 +188,6 @@ class SimpleURLLoaderImpl : public SimpleURLLoader,
void DownloadToStringOfUnboundedSizeUntilCrashAndDie( void DownloadToStringOfUnboundedSizeUntilCrashAndDie(
mojom::URLLoaderFactory* url_loader_factory, mojom::URLLoaderFactory* url_loader_factory,
BodyAsStringCallback body_as_string_callback) override; BodyAsStringCallback body_as_string_callback) override;
void DownloadHeadersOnly(mojom::URLLoaderFactory* url_loader_factory,
HeadersOnlyCallback headers_only_callback) override;
void DownloadToFile( void DownloadToFile(
mojom::URLLoaderFactory* url_loader_factory, mojom::URLLoaderFactory* url_loader_factory,
DownloadToFileCompleteCallback download_to_file_complete_callback, DownloadToFileCompleteCallback download_to_file_complete_callback,
...@@ -627,58 +625,6 @@ class SaveToStringBodyHandler : public BodyHandler, ...@@ -627,58 +625,6 @@ class SaveToStringBodyHandler : public BodyHandler,
DISALLOW_COPY_AND_ASSIGN(SaveToStringBodyHandler); DISALLOW_COPY_AND_ASSIGN(SaveToStringBodyHandler);
}; };
// BodyHandler that discards the response body.
class HeadersOnlyBodyHandler : public BodyHandler, public BodyReader::Delegate {
public:
HeadersOnlyBodyHandler(
SimpleURLLoaderImpl* simple_url_loader,
SimpleURLLoader::HeadersOnlyCallback headers_only_callback)
: BodyHandler(simple_url_loader),
headers_only_callback_(std::move(headers_only_callback)) {}
~HeadersOnlyBodyHandler() override {}
// BodyHandler implementation
void OnStartLoadingResponseBody(
mojo::ScopedDataPipeConsumerHandle body_data_pipe) override {
// TODO(crbug.com/871420): The request can be completed at this point
// however that requires more changes to SimpleURLLoader as OnComplete()
// will not have been called yet.
DCHECK(!body_reader_);
body_reader_ =
std::make_unique<BodyReader>(this, std::numeric_limits<int64_t>::max());
body_reader_->Start(std::move(body_data_pipe));
}
void NotifyConsumerOfCompletion(bool destroy_results) override {
body_reader_.reset();
std::move(headers_only_callback_)
.Run(simple_url_loader()->ResponseInfo()
? simple_url_loader()->ResponseInfo()->headers
: nullptr);
}
void PrepareToRetry(base::OnceClosure retry_callback) override {
body_reader_.reset();
std::move(retry_callback).Run();
}
private:
// BodyReader::Delegate implementation
net::Error OnDataRead(uint32_t length, const char* data) override {
return net::OK;
}
void OnDone(net::Error error, int64_t total_bytes) override {
simple_url_loader()->OnBodyHandlerDone(error, total_bytes);
}
SimpleURLLoader::HeadersOnlyCallback headers_only_callback_;
std::unique_ptr<BodyReader> body_reader_;
DISALLOW_COPY_AND_ASSIGN(HeadersOnlyBodyHandler);
};
// BodyHandler implementation for saving the response to a file // BodyHandler implementation for saving the response to a file
class SaveToFileBodyHandler : public BodyHandler { class SaveToFileBodyHandler : public BodyHandler {
public: public:
...@@ -1115,14 +1061,6 @@ void SimpleURLLoaderImpl::DownloadToStringOfUnboundedSizeUntilCrashAndDie( ...@@ -1115,14 +1061,6 @@ void SimpleURLLoaderImpl::DownloadToStringOfUnboundedSizeUntilCrashAndDie(
Start(url_loader_factory); Start(url_loader_factory);
} }
void SimpleURLLoaderImpl::DownloadHeadersOnly(
mojom::URLLoaderFactory* url_loader_factory,
HeadersOnlyCallback headers_only_callback) {
body_handler_ = std::make_unique<HeadersOnlyBodyHandler>(
this, std::move(headers_only_callback));
Start(url_loader_factory);
}
void SimpleURLLoaderImpl::DownloadToFile( void SimpleURLLoaderImpl::DownloadToFile(
mojom::URLLoaderFactory* url_loader_factory, mojom::URLLoaderFactory* url_loader_factory,
DownloadToFileCompleteCallback download_to_file_complete_callback, DownloadToFileCompleteCallback download_to_file_complete_callback,
...@@ -1308,7 +1246,6 @@ void SimpleURLLoaderImpl::FinishWithResult(int net_error) { ...@@ -1308,7 +1246,6 @@ void SimpleURLLoaderImpl::FinishWithResult(int net_error) {
request_state_->finished = true; request_state_->finished = true;
request_state_->net_error = net_error; request_state_->net_error = net_error;
// If it's a partial download or an error was received, erase the body. // If it's a partial download or an error was received, erase the body.
bool destroy_results = bool destroy_results =
request_state_->net_error != net::OK && !allow_partial_results_; request_state_->net_error != net::OK && !allow_partial_results_;
...@@ -1374,6 +1311,7 @@ void SimpleURLLoaderImpl::Retry() { ...@@ -1374,6 +1311,7 @@ void SimpleURLLoaderImpl::Retry() {
url_loader_.reset(); url_loader_.reset();
request_state_ = std::make_unique<RequestState>(); request_state_ = std::make_unique<RequestState>();
body_handler_->PrepareToRetry(base::BindOnce( body_handler_->PrepareToRetry(base::BindOnce(
&SimpleURLLoaderImpl::StartRequest, weak_ptr_factory_.GetWeakPtr(), &SimpleURLLoaderImpl::StartRequest, weak_ptr_factory_.GetWeakPtr(),
url_loader_factory_ptr_.get())); url_loader_factory_ptr_.get()));
......
...@@ -18,15 +18,11 @@ ...@@ -18,15 +18,11 @@
class GURL; class GURL;
template <class T>
class scoped_refptr;
namespace base { namespace base {
class FilePath; class FilePath;
} }
namespace net { namespace net {
class HttpResponseHeaders;
struct NetworkTrafficAnnotationTag; struct NetworkTrafficAnnotationTag;
struct RedirectInfo; struct RedirectInfo;
} // namespace net } // namespace net
...@@ -89,14 +85,8 @@ class COMPONENT_EXPORT(NETWORK_CPP) SimpleURLLoader { ...@@ -89,14 +85,8 @@ class COMPONENT_EXPORT(NETWORK_CPP) SimpleURLLoader {
using BodyAsStringCallback = using BodyAsStringCallback =
base::OnceCallback<void(std::unique_ptr<std::string> response_body)>; base::OnceCallback<void(std::unique_ptr<std::string> response_body)>;
// Callback used when ignoring the response body. |headers| are the received // Callback used when download the response body to a file. On failure, |path|
// HTTP headers, or nullptr if none were received. It is safe to delete the // will be empty. It is safe to delete the SimpleURLLoader during the
// SimpleURLLoader during the callback.
using HeadersOnlyCallback =
base::OnceCallback<void(scoped_refptr<net::HttpResponseHeaders> headers)>;
// Callback used when downloading the response body to a file. On failure,
// |path| will be empty. It is safe to delete the SimpleURLLoader during the
// callback. // callback.
using DownloadToFileCompleteCallback = using DownloadToFileCompleteCallback =
base::OnceCallback<void(base::FilePath path)>; base::OnceCallback<void(base::FilePath path)>;
...@@ -130,7 +120,7 @@ class COMPONENT_EXPORT(NETWORK_CPP) SimpleURLLoader { ...@@ -130,7 +120,7 @@ class COMPONENT_EXPORT(NETWORK_CPP) SimpleURLLoader {
virtual ~SimpleURLLoader(); virtual ~SimpleURLLoader();
// Starts the request using |url_loader_factory|. The SimpleURLLoader will // Starts the request using |network_context|. The SimpleURLLoader will
// accumulate all downloaded data in an in-memory string of bounded size. If // accumulate all downloaded data in an in-memory string of bounded size. If
// |max_body_size| is exceeded, the request will fail with // |max_body_size| is exceeded, the request will fail with
// net::ERR_INSUFFICIENT_RESOURCES. |max_body_size| must be no greater than 1 // net::ERR_INSUFFICIENT_RESOURCES. |max_body_size| must be no greater than 1
...@@ -155,14 +145,6 @@ class COMPONENT_EXPORT(NETWORK_CPP) SimpleURLLoader { ...@@ -155,14 +145,6 @@ class COMPONENT_EXPORT(NETWORK_CPP) SimpleURLLoader {
mojom::URLLoaderFactory* url_loader_factory, mojom::URLLoaderFactory* url_loader_factory,
BodyAsStringCallback body_as_string_callback) = 0; BodyAsStringCallback body_as_string_callback) = 0;
// Starts the request using |url_loader_factory|. The SimpleURLLoader will
// discard the response body as it is received and |headers_only_callback|
// will be invoked on completion. It is safe to delete the SimpleURLLoader in
// this callback.
virtual void DownloadHeadersOnly(
mojom::URLLoaderFactory* url_loader_factory,
HeadersOnlyCallback headers_only_callback) = 0;
// SimpleURLLoader will download the entire response to a file at the // SimpleURLLoader will download the entire response to a file at the
// specified path. File I/O will happen on another sequence, so it's safe to // specified path. File I/O will happen on another sequence, so it's safe to
// use this on any sequence. // use this on any sequence.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment