diff options
author | joaodasilva <joaodasilva@chromium.org> | 2014-12-03 02:51:31 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-12-03 10:51:42 +0000 |
commit | 89500901a470788feedfaaac6e1e66804e9f70b9 (patch) | |
tree | 3d3bf239544a366f64b6e3a140771647c322b67e /chrome/browser/policy/cloud | |
parent | 2bbabbd1da1c0b1cff0144cc86d6567ccf596365 (diff) | |
download | chromium_src-89500901a470788feedfaaac6e1e66804e9f70b9.zip chromium_src-89500901a470788feedfaaac6e1e66804e9f70b9.tar.gz chromium_src-89500901a470788feedfaaac6e1e66804e9f70b9.tar.bz2 |
Add callbacks for requests serviced to TestRequestInterceptor.
This enables test code to pass a RunLoop::QuitClosure to the interceptor
and wait until the next pending request is serviced.
BUG=None
Review URL: https://codereview.chromium.org/769293002
Cr-Commit-Position: refs/heads/master@{#306584}
Diffstat (limited to 'chrome/browser/policy/cloud')
-rw-r--r-- | chrome/browser/policy/cloud/test_request_interceptor.cc | 46 | ||||
-rw-r--r-- | chrome/browser/policy/cloud/test_request_interceptor.h | 4 |
2 files changed, 50 insertions, 0 deletions
diff --git a/chrome/browser/policy/cloud/test_request_interceptor.cc b/chrome/browser/policy/cloud/test_request_interceptor.cc index aa45435..ce185ab 100644 --- a/chrome/browser/policy/cloud/test_request_interceptor.cc +++ b/chrome/browser/policy/cloud/test_request_interceptor.cc @@ -6,10 +6,12 @@ #include <limits> #include <queue> +#include <vector> #include "base/bind.h" #include "base/bind_helpers.h" #include "base/memory/scoped_ptr.h" +#include "base/message_loop/message_loop_proxy.h" #include "base/run_loop.h" #include "base/sequenced_task_runner.h" #include "content/public/browser/browser_thread.h" @@ -168,9 +170,13 @@ class TestRequestInterceptor::Delegate : public net::URLRequestInterceptor { net::NetworkDelegate* network_delegate) const override; void GetPendingSize(size_t* pending_size) const; + void AddRequestServicedCallback(const base::Closure& callback); void PushJobCallback(const JobCallback& callback); private: + static void InvokeRequestServicedCallbacks( + scoped_ptr<std::vector<base::Closure>> callbacks); + const std::string hostname_; scoped_refptr<base::SequencedTaskRunner> io_task_runner_; @@ -178,6 +184,10 @@ class TestRequestInterceptor::Delegate : public net::URLRequestInterceptor { // const method; it can't reenter though, because it runs exclusively on // the IO thread. mutable std::queue<JobCallback> pending_job_callbacks_; + + // Queue of pending request serviced callbacks. Mutable for the same reason + // as |pending_job_callbacks_|. + mutable std::vector<base::Closure> request_serviced_callbacks_; }; TestRequestInterceptor::Delegate::Delegate( @@ -203,6 +213,17 @@ net::URLRequestJob* TestRequestInterceptor::Delegate::MaybeInterceptRequest( return BadRequestJobCallback(request, network_delegate); } + // Invoke any callbacks that are waiting for the next request to be serviced + // after this job is serviced. + if (!request_serviced_callbacks_.empty()) { + scoped_ptr<std::vector<base::Closure>> callbacks( + new std::vector<base::Closure>); + callbacks->swap(request_serviced_callbacks_); + io_task_runner_->PostTask( + FROM_HERE, base::Bind(&Delegate::InvokeRequestServicedCallbacks, + base::Passed(&callbacks))); + } + JobCallback callback = pending_job_callbacks_.front(); pending_job_callbacks_.pop(); return callback.Run(request, network_delegate); @@ -214,12 +235,25 @@ void TestRequestInterceptor::Delegate::GetPendingSize( *pending_size = pending_job_callbacks_.size(); } +void TestRequestInterceptor::Delegate::AddRequestServicedCallback( + const base::Closure& callback) { + CHECK(io_task_runner_->RunsTasksOnCurrentThread()); + request_serviced_callbacks_.push_back(callback); +} + void TestRequestInterceptor::Delegate::PushJobCallback( const JobCallback& callback) { CHECK(io_task_runner_->RunsTasksOnCurrentThread()); pending_job_callbacks_.push(callback); } +// static +void TestRequestInterceptor::Delegate::InvokeRequestServicedCallbacks( + scoped_ptr<std::vector<base::Closure>> callbacks) { + for (const auto& p : *callbacks) + p.Run(); +} + TestRequestInterceptor::TestRequestInterceptor(const std::string& hostname, scoped_refptr<base::SequencedTaskRunner> io_task_runner) : hostname_(hostname), @@ -249,6 +283,18 @@ size_t TestRequestInterceptor::GetPendingSize() { return pending_size; } +void TestRequestInterceptor::AddRequestServicedCallback( + const base::Closure& callback) { + base::Closure post_callback = + base::Bind(base::IgnoreResult(&base::MessageLoopProxy::PostTask), + base::MessageLoopProxy::current(), + FROM_HERE, + callback); + PostToIOAndWait(base::Bind(&Delegate::AddRequestServicedCallback, + base::Unretained(delegate_), + post_callback)); +} + void TestRequestInterceptor::PushJobCallback(const JobCallback& callback) { PostToIOAndWait(base::Bind(&Delegate::PushJobCallback, base::Unretained(delegate_), diff --git a/chrome/browser/policy/cloud/test_request_interceptor.h b/chrome/browser/policy/cloud/test_request_interceptor.h index e3a740a..858af64 100644 --- a/chrome/browser/policy/cloud/test_request_interceptor.h +++ b/chrome/browser/policy/cloud/test_request_interceptor.h @@ -44,6 +44,10 @@ class TestRequestInterceptor { // Returns the number of pending callback jobs that haven't been used yet. size_t GetPendingSize(); + // |callback| will be invoked on the current loop once the next request + // intercepted by this class has been dispatched. + void AddRequestServicedCallback(const base::Closure& callback); + // Queues |callback| to handle a request to |hostname_|. Each callback is // used only once, and in the order that they're pushed. void PushJobCallback(const JobCallback& callback); |