Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1962)

Unified Diff: chrome/browser/google_apis/test_server/http_server.cc

Issue 14365019: Break dependencies preventing move of test_server down to net. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..9d2aaea2ee21c699940a04d654d13117a8cd79e0 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,59 @@ 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));
}
void HttpListenSocket::Listen() {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ DCHECK(CalledOnValidThread());
net::TCPListenSocket::Listen();
}
HttpListenSocket::~HttpListenSocket() {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
}
-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_);
}
HttpServer::~HttpServer() {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
}
bool HttpServer::InitializeAndWaitUntilReady() {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ DCHECK(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(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();
- // 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 +111,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 +164,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 +175,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 +186,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 +199,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);

Powered by Google App Engine
This is Rietveld 408576698