Chromium Code Reviews| Index: chrome/browser/google_apis/test_server/http_server.cc |
| diff --git a/chrome/browser/google_apis/test_server/http_server.cc b/chrome/browser/google_apis/test_server/http_server.cc |
| index b8d5b47c41e38340c1d479607d85b9ad323a1604..5cbcd05e955157d6d02e78b14440abdd61ddd365 100644 |
| --- a/chrome/browser/google_apis/test_server/http_server.cc |
| +++ b/chrome/browser/google_apis/test_server/http_server.cc |
| @@ -8,18 +8,15 @@ |
| #include "base/stl_util.h" |
| #include "base/string_util.h" |
| #include "base/stringprintf.h" |
| +#include "base/synchronization/waitable_event.h" |
| #include "chrome/browser/google_apis/test_server/http_connection.h" |
| #include "chrome/browser/google_apis/test_server/http_request.h" |
| #include "chrome/browser/google_apis/test_server/http_response.h" |
| -#include "content/public/browser/browser_thread.h" |
| -#include "content/public/test/test_utils.h" |
| #include "net/tools/fetch/http_listen_socket.h" |
| namespace google_apis { |
| namespace test_server { |
| -using content::BrowserThread; |
| - |
| namespace { |
| const int kPort = 8040; |
| @@ -42,58 +39,63 @@ scoped_ptr<HttpResponse> HandleDefaultRequest(const GURL& url, |
| HttpListenSocket::HttpListenSocket(const SocketDescriptor socket_descriptor, |
| net::StreamListenSocket::Delegate* delegate) |
| : net::TCPListenSocket(socket_descriptor, delegate) { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| } |
| void HttpListenSocket::Listen() { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| net::TCPListenSocket::Listen(); |
| } |
| HttpListenSocket::~HttpListenSocket() { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| } |
| -HttpServer::HttpServer() |
| - : port_(-1), |
| +HttpServer::HttpServer( |
| + const scoped_refptr<base::SingleThreadTaskRunner>& io_thread) |
| + : io_thread_(io_thread), |
| + port_(-1), |
| weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + DCHECK(io_thread_); |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| } |
| HttpServer::~HttpServer() { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| } |
| bool HttpServer::InitializeAndWaitUntilReady() { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| - BrowserThread::PostTask( |
| - BrowserThread::IO, |
| - FROM_HERE, |
| - base::Bind(&HttpServer::InitializeOnIOThread, |
| - base::Unretained(this))); |
| + base::WaitableEvent event(false, false); |
| + if (!io_thread_->PostTask(FROM_HERE, |
| + base::Bind(&HttpServer::InitializeOnIOThread, |
| + base::Unretained(this), &event))) { |
| + return false; |
| + } |
| - // Wait for the task completion. |
| - content::RunAllPendingInMessageLoop(BrowserThread::IO); |
| + event.Wait(); |
| return Started(); |
| } |
| -void HttpServer::ShutdownAndWaitUntilComplete() { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| +bool HttpServer::ShutdownAndWaitUntilComplete() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + |
| + base::WaitableEvent event(false, false); |
| + if (!io_thread_->PostTask(FROM_HERE, |
| + base::Bind(&HttpServer::ShutdownOnIOThread, |
| + base::Unretained(this), &event))) { |
| + return false; |
| + } |
| - BrowserThread::PostTask( |
| - BrowserThread::IO, |
| - FROM_HERE, |
| - base::Bind(&HttpServer::ShutdownOnIOThread, |
| - base::Unretained(this))); |
| + event.Wait(); |
|
satorux1
2013/04/23 07:57:05
What about doing this:
base::RunLoop run_loop;
io
Paweł Hajdan Jr.
2013/04/23 22:12:42
Done! Thank you for the great suggestion, it looks
satorux1
2013/04/23 23:39:53
Glad you liked it.
|
| - // Wait for the task completion. |
| - content::RunAllPendingInMessageLoop(BrowserThread::IO); |
| + return true; |
| } |
| -void HttpServer::InitializeOnIOThread() { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| +void HttpServer::InitializeOnIOThread(base::WaitableEvent* event) { |
| + DCHECK(io_thread_->BelongsToCurrentThread()); |
| DCHECK(!Started()); |
| int retries_left = kRetries + 1; |
| @@ -113,20 +115,24 @@ void HttpServer::InitializeOnIOThread() { |
| retries_left--; |
| try_port++; |
| } |
| + |
| + event->Signal(); |
| } |
| -void HttpServer::ShutdownOnIOThread() { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| +void HttpServer::ShutdownOnIOThread(base::WaitableEvent* event) { |
| + DCHECK(io_thread_->BelongsToCurrentThread()); |
| listen_socket_ = NULL; // Release the listen socket. |
| STLDeleteContainerPairSecondPointers(connections_.begin(), |
| connections_.end()); |
| connections_.clear(); |
| + |
| + event->Signal(); |
| } |
| void HttpServer::HandleRequest(HttpConnection* connection, |
| scoped_ptr<HttpRequest> request) { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + DCHECK(io_thread_->BelongsToCurrentThread()); |
| for (size_t i = 0; i < request_handlers_.size(); ++i) { |
| scoped_ptr<HttpResponse> response = |
| @@ -162,7 +168,7 @@ void HttpServer::RegisterRequestHandler( |
| void HttpServer::DidAccept(net::StreamListenSocket* server, |
| net::StreamListenSocket* connection) { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + DCHECK(io_thread_->BelongsToCurrentThread()); |
| HttpConnection* http_connection = new HttpConnection( |
| connection, |
| @@ -173,7 +179,7 @@ void HttpServer::DidAccept(net::StreamListenSocket* server, |
| void HttpServer::DidRead(net::StreamListenSocket* connection, |
| const char* data, |
| int length) { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + DCHECK(io_thread_->BelongsToCurrentThread()); |
| HttpConnection* http_connection = FindConnection(connection); |
| if (http_connection == NULL) { |
| @@ -184,7 +190,7 @@ void HttpServer::DidRead(net::StreamListenSocket* connection, |
| } |
| void HttpServer::DidClose(net::StreamListenSocket* connection) { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + DCHECK(io_thread_->BelongsToCurrentThread()); |
| HttpConnection* http_connection = FindConnection(connection); |
| if (http_connection == NULL) { |
| @@ -197,7 +203,7 @@ void HttpServer::DidClose(net::StreamListenSocket* connection) { |
| HttpConnection* HttpServer::FindConnection( |
| net::StreamListenSocket* socket) { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + DCHECK(io_thread_->BelongsToCurrentThread()); |
| std::map<net::StreamListenSocket*, HttpConnection*>::iterator it = |
| connections_.find(socket); |