Commit abbf07fa authored by Kinuko Yasuda's avatar Kinuko Yasuda Committed by Chromium LUCI CQ

Remove URLFetcher usage in UsbTestGadgetImpl

Bug: 1010491
Change-Id: I122de2811a029c17afd184077d7693350917179f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2586296Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Commit-Queue: Kinuko Yasuda <kinuko@chromium.org>
Cr-Commit-Position: refs/heads/master@{#836873}
parent fdd76fc6
......@@ -28,7 +28,9 @@
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "net/base/elements_upload_data_stream.h"
#include "net/base/escape.h"
#include "net/base/upload_bytes_element_reader.h"
#include "net/proxy_resolution/configured_proxy_resolution_service.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "net/url_request/url_fetcher.h"
......@@ -36,6 +38,7 @@
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_builder.h"
#include "net/url_request/url_request_context_getter.h"
#include "net/url_request/url_request_test_util.h"
#include "services/device/test/usb_test_gadget.h"
#include "services/device/usb/usb_device.h"
#include "services/device/usb/usb_device_handle.h"
......@@ -46,10 +49,7 @@ namespace device {
class UsbTestGadgetImpl : public UsbTestGadget {
public:
UsbTestGadgetImpl(
scoped_refptr<net::URLRequestContextGetter> request_context_getter,
UsbService* usb_service,
scoped_refptr<UsbDevice> device);
UsbTestGadgetImpl(UsbService* usb_service, scoped_refptr<UsbDevice> device);
~UsbTestGadgetImpl() override;
bool Unclaim() override;
......@@ -58,25 +58,9 @@ class UsbTestGadgetImpl : public UsbTestGadget {
bool SetType(Type type) override;
UsbDevice* GetDevice() const override;
// Member of UsbTestGadgetImpl so that the deprecated net::URLFetcher test
// class can friend it.
static std::unique_ptr<net::URLFetcher> CreateURLFetcher(
scoped_refptr<net::URLRequestContextGetter> request_context_getter,
const GURL& url,
net::URLFetcher::RequestType request_type,
net::URLFetcherDelegate* delegate) {
std::unique_ptr<net::URLFetcher> url_fetcher = net::URLFetcher::Create(
url, request_type, delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
url_fetcher->SetRequestContext(request_context_getter.get());
return url_fetcher;
}
private:
std::string device_address_;
scoped_refptr<UsbDevice> device_;
scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
UsbService* usb_service_;
DISALLOW_COPY_AND_ASSIGN(UsbTestGadgetImpl);
......@@ -170,50 +154,55 @@ class URLRequestContextGetter : public net::URLRequestContextGetter {
scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
};
class URLFetcherDelegate : public net::URLFetcherDelegate {
public:
URLFetcherDelegate() = default;
~URLFetcherDelegate() override = default;
void WaitForCompletion() { run_loop_.Run(); }
void OnURLFetchComplete(const net::URLFetcher* source) override {
run_loop_.Quit();
std::unique_ptr<net::URLRequest> CreateSimpleRequest(
net::URLRequestContext& request_context,
net::URLRequest::Delegate* delegate,
const GURL& url,
const std::string& form_data_type,
const std::string& form_data) {
std::unique_ptr<net::URLRequest> request(request_context.CreateRequest(
url, net::DEFAULT_PRIORITY, delegate, TRAFFIC_ANNOTATION_FOR_TESTS));
if (!form_data_type.empty()) {
net::HttpRequestHeaders extra_headers;
extra_headers.SetHeader(net::HttpRequestHeaders::kContentType,
"application/x-www-form-urlencoded");
request->SetExtraRequestHeaders(extra_headers);
}
private:
base::RunLoop run_loop_;
DISALLOW_COPY_AND_ASSIGN(URLFetcherDelegate);
};
if (!form_data.empty()) {
std::unique_ptr<net::UploadElementReader> reader(
new net::UploadBytesElementReader(form_data.data(), form_data.size()));
request->set_upload(
net::ElementsUploadDataStream::CreateWithReader(std::move(reader), 0));
}
return request;
}
int SimplePOSTRequest(
scoped_refptr<net::URLRequestContextGetter> request_context_getter,
const GURL& url,
const std::string& form_data) {
URLFetcherDelegate delegate;
std::unique_ptr<net::URLFetcher> url_fetcher =
UsbTestGadgetImpl::CreateURLFetcher(request_context_getter, url,
net::URLFetcher::POST, &delegate);
net::TestDelegate delegate;
net::TestURLRequestContext request_context;
std::unique_ptr<net::URLRequest> request =
CreateSimpleRequest(request_context, &delegate, url,
"application/x-www-form-urlencoded", form_data);
request->set_method("POST");
url_fetcher->SetUploadData("application/x-www-form-urlencoded", form_data);
url_fetcher->Start();
delegate.WaitForCompletion();
request->Start();
delegate.RunUntilComplete();
return url_fetcher->GetResponseCode();
return request->response_headers()->response_code();
}
class UsbGadgetFactory : public UsbService::Observer,
public net::URLFetcherDelegate {
class UsbGadgetFactory : public UsbService::Observer {
public:
// TODO(crbug.com/1010491): Remove `io_task_runner` parameter.
UsbGadgetFactory(UsbService* usb_service,
scoped_refptr<base::SingleThreadTaskRunner> io_task_runner)
: usb_service_(usb_service), observer_(this) {
// Gadget tests shouldn't be enabled without available |usb_service|.
DCHECK(usb_service_);
request_context_getter_ = new URLRequestContextGetter(io_task_runner);
static uint32_t next_session_id;
base::ProcessId process_id = base::GetCurrentProcId();
session_id_ =
......@@ -227,8 +216,7 @@ class UsbGadgetFactory : public UsbService::Observer,
std::unique_ptr<UsbTestGadget> WaitForDevice() {
EnumerateDevices();
run_loop_.Run();
return std::make_unique<UsbTestGadgetImpl>(request_context_getter_,
usb_service_, device_);
return std::make_unique<UsbTestGadgetImpl>(usb_service_, device_);
}
private:
......@@ -289,25 +277,34 @@ class UsbGadgetFactory : public UsbService::Observer,
GURL url("http://" + serial_number_ + "/claim");
std::string form_data = base::StringPrintf(
"session_id=%s", net::EscapeUrlEncodedData(session_id_, true).c_str());
url_fetcher_ = UsbTestGadgetImpl::CreateURLFetcher(
request_context_getter_, url, net::URLFetcher::POST, this);
url_fetcher_->SetUploadData("application/x-www-form-urlencoded", form_data);
url_fetcher_->Start();
std::unique_ptr<net::URLRequest> request =
CreateSimpleRequest(request_context_, &delegate_, url,
"application/x-www-form-urlencoded", form_data);
request->set_method("POST");
request->Start();
delegate_.set_on_complete(
base::BindOnce(&UsbGadgetFactory::OnURLRequestComplete,
weak_factory_.GetWeakPtr(), std::move(request)));
}
void GetVersion() {
GURL url("http://" + serial_number_ + "/version");
url_fetcher_ = UsbTestGadgetImpl::CreateURLFetcher(
request_context_getter_, url, net::URLFetcher::GET, this);
url_fetcher_->Start();
std::unique_ptr<net::URLRequest> request = CreateSimpleRequest(
request_context_, &delegate_, url, std::string(), std::string());
request->set_method("GET");
request->Start();
delegate_.set_on_complete(
base::BindOnce(&UsbGadgetFactory::OnURLRequestComplete,
weak_factory_.GetWeakPtr(), std::move(request)));
}
bool Update(const std::string& version) {
LOG(INFO) << "Updating " << serial_number_ << " to " << version << "...";
GURL url("http://" + serial_number_ + "/update");
url_fetcher_ = UsbTestGadgetImpl::CreateURLFetcher(
request_context_getter_, url, net::URLFetcher::POST, this);
std::string mime_header = base::StringPrintf(
"--foo\r\n"
"Content-Disposition: form-data; name=\"file\"; "
......@@ -322,17 +319,23 @@ class UsbGadgetFactory : public UsbService::Observer,
return false;
}
url_fetcher_->SetUploadData("multipart/form-data; boundary=foo",
mime_header + package + mime_footer);
url_fetcher_->Start();
std::unique_ptr<net::URLRequest> request = CreateSimpleRequest(
request_context_, &delegate_, url, "multipart/form-data; boundary=foo",
mime_header + package + mime_footer);
request->set_method("POST");
request->Start();
delegate_.set_on_complete(
base::BindOnce(&UsbGadgetFactory::OnURLRequestComplete,
weak_factory_.GetWeakPtr(), std::move(request)));
device_ = nullptr;
return true;
}
void OnURLFetchComplete(const net::URLFetcher* source) override {
void OnURLRequestComplete(std::unique_ptr<net::URLRequest> request) {
DCHECK(!serial_number_.empty());
int response_code = source->GetResponseCode();
int response_code = request->response_headers()->response_code();
if (!claimed_) {
// Just completed a /claim request.
if (response_code == 200) {
......@@ -354,7 +357,8 @@ class UsbGadgetFactory : public UsbService::Observer,
return;
}
if (!source->GetResponseAsString(&version_)) {
version_ = delegate_.data_received();
if (version_.empty()) {
LOG(WARNING) << "Failed to read body from /version.";
Reset();
return;
......@@ -400,9 +404,9 @@ class UsbGadgetFactory : public UsbService::Observer,
}
UsbService* usb_service_ = nullptr;
scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
net::TestDelegate delegate_;
net::TestURLRequestContext request_context_;
std::string session_id_;
std::unique_ptr<net::URLFetcher> url_fetcher_;
scoped_refptr<UsbDevice> device_;
std::string serial_number_;
bool claimed_ = false;
......@@ -544,12 +548,10 @@ std::unique_ptr<UsbTestGadget> UsbTestGadget::Claim(
}
UsbTestGadgetImpl::UsbTestGadgetImpl(
scoped_refptr<net::URLRequestContextGetter> request_context_getter,
UsbService* usb_service,
scoped_refptr<UsbDevice> device)
: device_address_(base::UTF16ToUTF8(device->serial_number())),
device_(device),
request_context_getter_(request_context_getter),
usb_service_(usb_service) {}
UsbTestGadgetImpl::~UsbTestGadgetImpl() {
......@@ -566,7 +568,7 @@ bool UsbTestGadgetImpl::Unclaim() {
VLOG(1) << "Releasing the device at " << device_address_ << ".";
GURL url("http://" + device_address_ + "/unclaim");
int response_code = SimplePOSTRequest(request_context_getter_, url, "");
int response_code = SimplePOSTRequest(url, "");
if (response_code != 200) {
LOG(ERROR) << "Unexpected HTTP " << response_code << " from /unclaim.";
......@@ -587,7 +589,7 @@ bool UsbTestGadgetImpl::SetType(Type type) {
CHECK(config);
GURL url("http://" + device_address_ + config->http_resource);
int response_code = SimplePOSTRequest(request_context_getter_, url, "");
int response_code = SimplePOSTRequest(url, "");
if (response_code != 200) {
LOG(ERROR) << "Unexpected HTTP " << response_code << " from "
......@@ -605,7 +607,7 @@ bool UsbTestGadgetImpl::SetType(Type type) {
bool UsbTestGadgetImpl::Disconnect() {
GURL url("http://" + device_address_ + "/disconnect");
int response_code = SimplePOSTRequest(request_context_getter_, url, "");
int response_code = SimplePOSTRequest(url, "");
if (response_code != 200) {
LOG(ERROR) << "Unexpected HTTP " << response_code << " from " << url << ".";
......@@ -621,7 +623,7 @@ bool UsbTestGadgetImpl::Disconnect() {
bool UsbTestGadgetImpl::Reconnect() {
GURL url("http://" + device_address_ + "/reconnect");
int response_code = SimplePOSTRequest(request_context_getter_, url, "");
int response_code = SimplePOSTRequest(url, "");
if (response_code != 200) {
LOG(ERROR) << "Unexpected HTTP " << response_code << " from " << url << ".";
......
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