Commit b540ab34 authored by danakj's avatar danakj Committed by Commit Bot

Convert Callbacks to OnceCallbacks for Download browser tests.

Use OnceCallback where possible, and BindRepeating where it is
meant to be called more than once.

TBR=avi@chromium.org

Bug: 953861, 1007763
Change-Id: I58af3a799b7b376f1a7a902823cda3ca0c5ffb03
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1939965Reviewed-by: default avatardanakj <danakj@chromium.org>
Commit-Queue: danakj <danakj@chromium.org>
Cr-Commit-Position: refs/heads/master@{#719794}
parent a982af15
...@@ -766,8 +766,8 @@ class TestRequestPauseHandler { ...@@ -766,8 +766,8 @@ class TestRequestPauseHandler {
EXPECT_FALSE(used_) << "GetOnPauseHandler() should only be called once for " EXPECT_FALSE(used_) << "GetOnPauseHandler() should only be called once for "
"an instance of TestRequestPauseHandler."; "an instance of TestRequestPauseHandler.";
used_ = true; used_ = true;
return base::Bind(&TestRequestPauseHandler::OnPauseHandler, return base::BindRepeating(&TestRequestPauseHandler::OnPauseHandler,
base::Unretained(this)); base::Unretained(this));
} }
// Wait until the OnPauseHandler returned in a prior call to // Wait until the OnPauseHandler returned in a prior call to
......
...@@ -59,10 +59,10 @@ void OnResume(scoped_refptr<base::SingleThreadTaskRunner> task_runner, ...@@ -59,10 +59,10 @@ void OnResume(scoped_refptr<base::SingleThreadTaskRunner> task_runner,
} }
void OnResponseSentOnServerIOThread( void OnResponseSentOnServerIOThread(
const TestDownloadHttpResponse::OnResponseSentCallback& callback, TestDownloadHttpResponse::OnResponseSentCallback callback,
std::unique_ptr<TestDownloadHttpResponse::CompletedRequest> request) { std::unique_ptr<TestDownloadHttpResponse::CompletedRequest> request) {
base::PostTask(FROM_HERE, {BrowserThread::UI}, base::PostTask(FROM_HERE, {BrowserThread::UI},
base::BindOnce(callback, std::move(request))); base::BindOnce(std::move(callback), std::move(request)));
} }
GURL GetURLFromRequest(const net::test_server::HttpRequest& request) { GURL GetURLFromRequest(const net::test_server::HttpRequest& request) {
...@@ -135,42 +135,10 @@ TestDownloadHttpResponse::Parameters::Parameters() ...@@ -135,42 +135,10 @@ TestDownloadHttpResponse::Parameters::Parameters()
connection_type( connection_type(
net::HttpResponseInfo::ConnectionInfo::CONNECTION_INFO_UNKNOWN) {} net::HttpResponseInfo::ConnectionInfo::CONNECTION_INFO_UNKNOWN) {}
// Copy and move constructors / assignment operators are all defaults. TestDownloadHttpResponse::Parameters::Parameters(const Parameters& that) =
TestDownloadHttpResponse::Parameters::Parameters(const Parameters&) = default; default;
TestDownloadHttpResponse::Parameters& TestDownloadHttpResponse::Parameters:: TestDownloadHttpResponse::Parameters& TestDownloadHttpResponse::Parameters::
operator=(const Parameters&) = default; operator=(const Parameters& that) = default;
TestDownloadHttpResponse::Parameters::Parameters(Parameters&& that)
: etag(std::move(that.etag)),
last_modified(std::move(that.last_modified)),
content_type(std::move(that.content_type)),
size(that.size),
pattern_generator_seed(that.pattern_generator_seed),
support_byte_ranges(that.support_byte_ranges),
support_partial_response(that.support_partial_response),
connection_type(that.connection_type),
static_response(std::move(that.static_response)),
injected_errors(std::move(that.injected_errors)),
inject_error_cb(that.inject_error_cb),
on_pause_handler(that.on_pause_handler),
pause_offset(that.pause_offset) {}
TestDownloadHttpResponse::Parameters& TestDownloadHttpResponse::Parameters::
operator=(Parameters&& that) {
etag = std::move(that.etag);
last_modified = std::move(that.last_modified);
content_type = std::move(that.content_type);
size = that.size;
pattern_generator_seed = that.pattern_generator_seed;
support_byte_ranges = that.support_byte_ranges;
support_partial_response = that.support_partial_response;
static_response = std::move(that.static_response);
injected_errors = std::move(that.injected_errors);
inject_error_cb = that.inject_error_cb;
on_pause_handler = that.on_pause_handler;
pause_offset = that.pause_offset;
return *this;
}
TestDownloadHttpResponse::Parameters::~Parameters() = default; TestDownloadHttpResponse::Parameters::~Parameters() = default;
...@@ -203,7 +171,7 @@ void TestDownloadHttpResponse::StartServing( ...@@ -203,7 +171,7 @@ void TestDownloadHttpResponse::StartServing(
auto iter = g_parameters_map.Get().find(url); auto iter = g_parameters_map.Get().find(url);
if (iter != g_parameters_map.Get().end()) if (iter != g_parameters_map.Get().end())
g_parameters_map.Get().erase(iter); g_parameters_map.Get().erase(iter);
g_parameters_map.Get().emplace(url, std::move(parameters)); g_parameters_map.Get().emplace(url, parameters);
} }
// static // static
...@@ -244,13 +212,13 @@ std::string TestDownloadHttpResponse::GetPatternBytes(int seed, ...@@ -244,13 +212,13 @@ std::string TestDownloadHttpResponse::GetPatternBytes(int seed,
TestDownloadHttpResponse::TestDownloadHttpResponse( TestDownloadHttpResponse::TestDownloadHttpResponse(
const net::test_server::HttpRequest& request, const net::test_server::HttpRequest& request,
const Parameters& parameters, const Parameters& parameters,
const OnResponseSentCallback& on_response_sent_callback) OnResponseSentCallback on_response_sent_callback)
: range_(net::HttpByteRange::Bounded(0, parameters.size - 1)), : range_(net::HttpByteRange::Bounded(0, parameters.size - 1)),
response_sent_offset_(0u), response_sent_offset_(0u),
parameters_(std::move(parameters)), parameters_(parameters),
request_(request), request_(request),
transferred_bytes_(0u), transferred_bytes_(0u),
on_response_sent_callback_(on_response_sent_callback) { on_response_sent_callback_(std::move(on_response_sent_callback)) {
DCHECK_GT(parameters.size, 0) << "File size need to be greater than 0."; DCHECK_GT(parameters.size, 0) << "File size need to be greater than 0.";
ParseRequestHeader(); ParseRequestHeader();
} }
...@@ -554,8 +522,8 @@ bool TestDownloadHttpResponse::ShouldPause( ...@@ -554,8 +522,8 @@ bool TestDownloadHttpResponse::ShouldPause(
void TestDownloadHttpResponse::PauseResponsesAndWaitForResumption() { void TestDownloadHttpResponse::PauseResponsesAndWaitForResumption() {
// Clean up the on_pause_handler so response will not be paused again. // Clean up the on_pause_handler so response will not be paused again.
auto pause_callback = parameters_.on_pause_handler; base::OnceCallback<OnPauseHandler::RunType> pause_callback =
parameters_.on_pause_handler.Reset(); std::move(parameters_.on_pause_handler);
base::OnceClosure continue_closure = SendNextBodyChunkClosure(); base::OnceClosure continue_closure = SendNextBodyChunkClosure();
...@@ -570,8 +538,8 @@ void TestDownloadHttpResponse::PauseResponsesAndWaitForResumption() { ...@@ -570,8 +538,8 @@ void TestDownloadHttpResponse::PauseResponsesAndWaitForResumption() {
base::PostTask( base::PostTask(
FROM_HERE, {BrowserThread::UI}, FROM_HERE, {BrowserThread::UI},
base::BindOnce( base::BindOnce(
pause_callback, std::move(pause_callback),
base::BindOnce(&OnResume, base::ThreadTaskRunnerHandle::Get(), base::BindOnce(OnResume, base::ThreadTaskRunnerHandle::Get(),
std::move(continue_closure)))); std::move(continue_closure))));
} }
...@@ -620,7 +588,7 @@ void TestDownloadHttpResponse::GenerateResult() { ...@@ -620,7 +588,7 @@ void TestDownloadHttpResponse::GenerateResult() {
auto completed_request = std::make_unique<CompletedRequest>(request_); auto completed_request = std::make_unique<CompletedRequest>(request_);
// Transferred bytes in [range_.first_byte_position(), response_sent_offset_). // Transferred bytes in [range_.first_byte_position(), response_sent_offset_).
completed_request->transferred_byte_count = transferred_bytes_; completed_request->transferred_byte_count = transferred_bytes_;
OnResponseSentOnServerIOThread(on_response_sent_callback_, OnResponseSentOnServerIOThread(std::move(on_response_sent_callback_),
std::move(completed_request)); std::move(completed_request));
// Close the HTTP connection. // Close the HTTP connection.
...@@ -635,7 +603,7 @@ TestDownloadHttpResponse::GenerateResultClosure() { ...@@ -635,7 +603,7 @@ TestDownloadHttpResponse::GenerateResultClosure() {
std::unique_ptr<net::test_server::HttpResponse> std::unique_ptr<net::test_server::HttpResponse>
TestDownloadResponseHandler::HandleTestDownloadRequest( TestDownloadResponseHandler::HandleTestDownloadRequest(
const TestDownloadHttpResponse::OnResponseSentCallback& callback, TestDownloadHttpResponse::OnResponseSentCallback callback,
const net::test_server::HttpRequest& request) { const net::test_server::HttpRequest& request) {
server_task_runner_ = base::ThreadTaskRunnerHandle::Get(); server_task_runner_ = base::ThreadTaskRunnerHandle::Get();
...@@ -649,7 +617,7 @@ TestDownloadResponseHandler::HandleTestDownloadRequest( ...@@ -649,7 +617,7 @@ TestDownloadResponseHandler::HandleTestDownloadRequest(
auto iter = g_parameters_map.Get().find(url); auto iter = g_parameters_map.Get().find(url);
if (iter != g_parameters_map.Get().end()) { if (iter != g_parameters_map.Get().end()) {
auto test_response = std::make_unique<TestDownloadHttpResponse>( auto test_response = std::make_unique<TestDownloadHttpResponse>(
request, std::move(iter->second), callback); request, std::move(iter->second), std::move(callback));
auto response = test_response->CreateResponseForTestServer(); auto response = test_response->CreateResponseForTestServer();
responses_.emplace_back(std::move(test_response)); responses_.emplace_back(std::move(test_response));
return response; return response;
...@@ -670,11 +638,12 @@ void TestDownloadResponseHandler::RegisterToTestServer( ...@@ -670,11 +638,12 @@ void TestDownloadResponseHandler::RegisterToTestServer(
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!server->Started()) DCHECK(!server->Started())
<< "Register request handler before starting the server"; << "Register request handler before starting the server";
server->RegisterRequestHandler(base::Bind( server->RegisterRequestHandler(base::BindRepeating(
&content::TestDownloadResponseHandler::HandleTestDownloadRequest, &content::TestDownloadResponseHandler::HandleTestDownloadRequest,
base::Unretained(this), base::Unretained(this),
base::Bind(&content::TestDownloadResponseHandler::OnRequestCompleted, base::BindRepeating(
base::Unretained(this)))); &content::TestDownloadResponseHandler::OnRequestCompleted,
base::Unretained(this))));
} }
void TestDownloadResponseHandler::OnRequestCompleted( void TestDownloadResponseHandler::OnRequestCompleted(
......
...@@ -32,7 +32,9 @@ class TestDownloadHttpResponse { ...@@ -32,7 +32,9 @@ class TestDownloadHttpResponse {
static GURL GetNextURLForDownload(); static GURL GetNextURLForDownload();
// OnPauseHandler can be used to pause the response until the enclosed // OnPauseHandler can be used to pause the response until the enclosed
// callback is called. // callback is called. The OnPauseHandler is copyable for Parameters
// objects to be used repeatedly, though it will only be called once when
// given to a TestDownloadHttpResponse.
using OnPauseHandler = base::RepeatingCallback<void(base::OnceClosure)>; using OnPauseHandler = base::RepeatingCallback<void(base::OnceClosure)>;
// Called when an injected error triggers. // Called when an injected error triggers.
...@@ -70,11 +72,9 @@ class TestDownloadHttpResponse { ...@@ -70,11 +72,9 @@ class TestDownloadHttpResponse {
// Last-Modified header and the server supports byte range requests. // Last-Modified header and the server supports byte range requests.
Parameters(); Parameters();
// Parameters is expected to be copyable and moveable.
Parameters(Parameters&&);
Parameters(const Parameters&); Parameters(const Parameters&);
Parameters& operator=(Parameters&&);
Parameters& operator=(const Parameters&); Parameters& operator=(const Parameters&);
~Parameters(); ~Parameters();
// Clears the errors in injected_errors. // Clears the errors in injected_errors.
...@@ -176,8 +176,9 @@ class TestDownloadHttpResponse { ...@@ -176,8 +176,9 @@ class TestDownloadHttpResponse {
// Callback to run when an injected error triggers. // Callback to run when an injected error triggers.
InjectErrorCallback inject_error_cb; InjectErrorCallback inject_error_cb;
// If on_pause_handler is valid, it will be invoked when the // If on_pause_handler is valid, it will be invoked once when the
// |pause_condition| is reached. // |pause_condition| is reached. It is not a OnceCallback to allow
// Parameters to be reused.
OnPauseHandler on_pause_handler; OnPauseHandler on_pause_handler;
// Offset of body to pause the response sending. A -1 offset will pause // Offset of body to pause the response sending. A -1 offset will pause
...@@ -199,7 +200,7 @@ class TestDownloadHttpResponse { ...@@ -199,7 +200,7 @@ class TestDownloadHttpResponse {
// Called when response are sent to the client. // Called when response are sent to the client.
using OnResponseSentCallback = using OnResponseSentCallback =
base::Callback<void(std::unique_ptr<CompletedRequest>)>; base::OnceCallback<void(std::unique_ptr<CompletedRequest>)>;
// Generate a pseudo random pattern. // Generate a pseudo random pattern.
// //
...@@ -240,10 +241,9 @@ class TestDownloadHttpResponse { ...@@ -240,10 +241,9 @@ class TestDownloadHttpResponse {
static void StartServingStaticResponse(const std::string& headers, static void StartServingStaticResponse(const std::string& headers,
const GURL& url); const GURL& url);
TestDownloadHttpResponse( TestDownloadHttpResponse(const net::test_server::HttpRequest& request,
const net::test_server::HttpRequest& request, const Parameters& parameters,
const Parameters& parameters, OnResponseSentCallback on_response_sent_callback);
const OnResponseSentCallback& on_response_sent_callback);
~TestDownloadHttpResponse(); ~TestDownloadHttpResponse();
// Creates a shim HttpResponse object for embedded test server. This life time // Creates a shim HttpResponse object for embedded test server. This life time
...@@ -355,7 +355,7 @@ class TestDownloadHttpResponse { ...@@ -355,7 +355,7 @@ class TestDownloadHttpResponse {
class TestDownloadResponseHandler { class TestDownloadResponseHandler {
public: public:
std::unique_ptr<net::test_server::HttpResponse> HandleTestDownloadRequest( std::unique_ptr<net::test_server::HttpResponse> HandleTestDownloadRequest(
const TestDownloadHttpResponse::OnResponseSentCallback& callback, TestDownloadHttpResponse::OnResponseSentCallback callback,
const net::test_server::HttpRequest& request); const net::test_server::HttpRequest& request);
TestDownloadResponseHandler(); TestDownloadResponseHandler();
......
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