Chromium Code Reviews| Index: chrome/browser/policy/cloud/test_request_interceptor.cc |
| diff --git a/chrome/browser/policy/cloud/test_request_interceptor.cc b/chrome/browser/policy/cloud/test_request_interceptor.cc |
| index ce185ab94dbfbae2705f837033b4ff328d34fbe0..34fe7fc324f03f2d4c96171faab17e73384307c8 100644 |
| --- a/chrome/browser/policy/cloud/test_request_interceptor.cc |
| +++ b/chrome/browser/policy/cloud/test_request_interceptor.cc |
| @@ -14,6 +14,7 @@ |
| #include "base/message_loop/message_loop_proxy.h" |
| #include "base/run_loop.h" |
| #include "base/sequenced_task_runner.h" |
| +#include "chrome/browser/policy/remote_commands/testing_remote_commands_server.h" |
| #include "content/public/browser/browser_thread.h" |
| #include "net/base/net_errors.h" |
| #include "net/base/upload_bytes_element_reader.h" |
| @@ -32,6 +33,15 @@ namespace policy { |
| namespace { |
| +static const char kGoodHeaders[] = |
| + "HTTP/1.1 200 OK\0" |
| + "Content-type: application/protobuf\0" |
| + "\0"; |
| +static const char kBadHeaders[] = |
| + "HTTP/1.1 400 Bad request\0" |
| + "Content-type: application/protobuf\0" |
| + "\0"; |
| + |
| // Helper callback for jobs that should fail with a network |error|. |
| net::URLRequestJob* ErrorJobCallback(int error, |
| net::URLRequest* request, |
| @@ -43,10 +53,6 @@ net::URLRequestJob* ErrorJobCallback(int error, |
| net::URLRequestJob* BadRequestJobCallback( |
| net::URLRequest* request, |
| net::NetworkDelegate* network_delegate) { |
| - static const char kBadHeaders[] = |
| - "HTTP/1.1 400 Bad request\0" |
| - "Content-type: application/protobuf\0" |
| - "\0"; |
| std::string headers(kBadHeaders, arraysize(kBadHeaders)); |
| return new net::URLRequestTestJob( |
| request, network_delegate, headers, std::string(), true); |
| @@ -115,7 +121,8 @@ net::URLRequestJob* RegisterJobCallback( |
| request_msg.has_policy_request() || |
| request_msg.has_device_status_report_request() || |
| request_msg.has_session_status_report_request() || |
| - request_msg.has_auto_enrollment_request()) { |
| + request_msg.has_auto_enrollment_request() || |
| + request_msg.has_remote_command_request()) { |
| return BadRequestJobCallback(request, network_delegate); |
| } |
| @@ -140,10 +147,46 @@ net::URLRequestJob* RegisterJobCallback( |
| std::string data; |
| response.SerializeToString(&data); |
| - static const char kGoodHeaders[] = |
| - "HTTP/1.1 200 OK\0" |
| - "Content-type: application/protobuf\0" |
| - "\0"; |
| + std::string headers(kGoodHeaders, arraysize(kGoodHeaders)); |
|
bartfab (slow)
2015/02/12 14:29:17
Nit: const.
binjin
2015/02/16 22:46:22
Done.
|
| + return new net::URLRequestTestJob(request, network_delegate, headers, data, |
| + true); |
| +} |
| + |
| +net::URLRequestJob* FetchRemoteCommandsJobCallback( |
| + TestingRemoteCommandsServer* server, |
| + bool ignore_other_requests, |
| + net::URLRequest* request, |
| + net::NetworkDelegate* network_delegate) { |
| + em::DeviceManagementRequest request_msg; |
| + if (!ValidRequest(request, "remote_commands", &request_msg)) { |
| + return ignore_other_requests ? nullptr : BadRequestJobCallback( |
| + request, network_delegate); |
| + } |
| + |
| + if (!request_msg.has_remote_command_request()) |
| + return BadRequestJobCallback(request, network_delegate); |
| + |
| + const em::DeviceRemoteCommandRequest& remote_command_request = |
| + request_msg.remote_command_request(); |
| + if (!remote_command_request.has_last_command_unique_id()) |
| + return BadRequestJobCallback(request, network_delegate); |
| + |
| + std::vector<em::RemoteCommandResult> previous_results( |
| + remote_command_request.command_results().begin(), |
| + remote_command_request.command_results().end()); |
| + std::vector<em::RemoteCommand> fetched_commands; |
| + server->FetchCommandsFromIO(remote_command_request.last_command_unique_id(), |
| + previous_results, &fetched_commands); |
| + |
| + em::DeviceManagementResponse response; |
| + em::DeviceRemoteCommandResponse* remote_command_response = |
| + response.mutable_remote_command_response(); |
| + |
| + for (const auto& fetched_command : fetched_commands) |
| + *remote_command_response->add_commands() = fetched_command; |
| + std::string data; |
| + response.SerializeToString(&data); |
| + |
| std::string headers(kGoodHeaders, arraysize(kGoodHeaders)); |
| return new net::URLRequestTestJob( |
| request, network_delegate, headers, data, true); |
| @@ -213,6 +256,17 @@ net::URLRequestJob* TestRequestInterceptor::Delegate::MaybeInterceptRequest( |
| return BadRequestJobCallback(request, network_delegate); |
| } |
| + net::URLRequestJob* new_request = |
| + pending_job_callbacks_.front().Run(request, network_delegate); |
| + |
| + if (!new_request) { |
| + // The current request job is legal and explicitly ignored by the next |
| + // pending job callback. |
| + return BadRequestJobCallback(request, network_delegate); |
|
bartfab (slow)
2015/02/12 14:29:17
This early return will cause the callbacks below n
binjin
2015/02/16 22:46:22
Yes, it's designed to not be consumed from callbac
|
| + } |
| + |
| + pending_job_callbacks_.pop(); |
| + |
| // Invoke any callbacks that are waiting for the next request to be serviced |
| // after this job is serviced. |
| if (!request_serviced_callbacks_.empty()) { |
|
bartfab (slow)
2015/02/12 14:29:17
These callbacks used to run *before* the ob, now t
binjin
2015/02/16 22:46:22
I think the callback is for intercepting, parsing
|
| @@ -224,9 +278,7 @@ net::URLRequestJob* TestRequestInterceptor::Delegate::MaybeInterceptRequest( |
| base::Passed(&callbacks))); |
| } |
| - JobCallback callback = pending_job_callbacks_.front(); |
| - pending_job_callbacks_.pop(); |
| - return callback.Run(request, network_delegate); |
| + return new_request; |
| } |
| void TestRequestInterceptor::Delegate::GetPendingSize( |
| @@ -325,6 +377,15 @@ TestRequestInterceptor::JobCallback TestRequestInterceptor::FileJob( |
| return base::Bind(&FileJobCallback, file_path); |
| } |
| +// static |
| +TestRequestInterceptor::JobCallback |
| +TestRequestInterceptor::FetchRemoteCommandsJob( |
| + TestingRemoteCommandsServer* server, |
| + bool ignore_other_requests) { |
| + return base::Bind(&FetchRemoteCommandsJobCallback, server, |
| + ignore_other_requests); |
| +} |
| + |
| void TestRequestInterceptor::PostToIOAndWait(const base::Closure& task) { |
| io_task_runner_->PostTask(FROM_HERE, task); |
| base::RunLoop run_loop; |