Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/gdata/test_servers/http_server.h" | |
| 6 | |
| 7 #include "base/synchronization/waitable_event.h" | |
| 8 #include "content/public/browser/browser_thread.h" | |
| 9 | |
| 10 #include <string> | |
| 11 #include <sstream> | |
| 12 #include "base/stl_util.h" | |
| 13 | |
| 14 namespace gdata { | |
| 15 namespace test_servers { | |
| 16 | |
| 17 using content::BrowserThread; | |
| 18 | |
| 19 // static | |
| 20 scoped_refptr<HttpServer> HttpServer::CreateServer( | |
| 21 const std::string& address, | |
| 22 int port, | |
| 23 HttpConnectionDelegate* connection_delegate) { | |
| 24 | |
| 25 // Launching the server class in the IO thread and waiting for the result. | |
| 26 // TODO(mtomasz): Change it to a completely new thread. | |
| 27 base::WaitableEvent waiter(false, false); | |
|
satorux1
2012/10/12 08:29:18
WaitableEvent should almost always be avoided.
I
mtomasz
2012/10/12 11:09:46
Correct me if I am wrong, but I think I really nee
satorux1
2012/10/16 03:12:18
I think you can make the object creation asynchron
| |
| 28 HttpServer* server = NULL; | |
| 29 BrowserThread::PostTask( | |
| 30 BrowserThread::IO, | |
| 31 FROM_HERE, | |
| 32 base::Bind(CreateServerOnIOThread, | |
| 33 address, | |
| 34 port, | |
| 35 connection_delegate, | |
| 36 &waiter, | |
| 37 &server)); | |
| 38 waiter.Wait(); | |
| 39 | |
| 40 return server; | |
| 41 } | |
| 42 | |
| 43 void HttpServer::DropHttpConnection(HttpConnection* connection) { | |
| 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 45 | |
| 46 connections_.erase(connection->socket_.get()); | |
| 47 delete connection; | |
| 48 } | |
| 49 | |
| 50 // static | |
| 51 void HttpServer::CreateServerOnIOThread( | |
|
satorux1
2012/10/12 08:29:18
I guess you can make it a free function in anonymo
mtomasz
2012/10/12 11:09:46
I wanted to keep the constructor private. Also Lis
satorux1
2012/10/16 03:12:18
Then that's fine. Thank you for the explanation.
| |
| 52 const std::string& address, | |
| 53 int port, | |
| 54 HttpConnectionDelegate* connection_delegate, | |
| 55 base::WaitableEvent* waiter, | |
| 56 HttpServer** server_ptr) { | |
| 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 58 | |
| 59 SocketDescriptor socket_descriptor = CreateAndBind(address, port); | |
| 60 HttpServer* server = NULL; | |
| 61 if (socket_descriptor != kInvalidSocket) { | |
| 62 server = new HttpServer(socket_descriptor, connection_delegate); | |
| 63 server->Listen(); | |
| 64 } | |
| 65 | |
| 66 // Return the value and notify the caller (if provided) that the value | |
| 67 // is available. | |
| 68 *server_ptr = server; | |
| 69 if (waiter) | |
| 70 waiter->Signal(); | |
| 71 } | |
| 72 | |
| 73 HttpServer::HttpServer(SocketDescriptor& socket, | |
| 74 HttpConnectionDelegate* connection_delegate) : | |
| 75 net::TCPListenSocket(socket, ALLOW_THIS_IN_INITIALIZER_LIST(this)), | |
| 76 connection_delegate_(connection_delegate) { | |
| 77 } | |
| 78 | |
| 79 HttpServer::~HttpServer() { | |
| 80 STLDeleteContainerPairSecondPointers(connections_.begin(), | |
| 81 connections_.end()); | |
| 82 } | |
| 83 | |
| 84 void HttpServer::DidAccept(StreamListenSocket* server, | |
| 85 StreamListenSocket* connection) { | |
| 86 HttpConnection* http_connection = new HttpConnection(connection, | |
| 87 connection_delegate_); | |
| 88 connections_[connection] = http_connection; | |
| 89 } | |
| 90 | |
| 91 void HttpServer::DidRead(StreamListenSocket* connection, | |
| 92 const char* data, | |
| 93 int length) { | |
| 94 HttpConnection* http_connection = FindConnection(connection); | |
| 95 if (http_connection == NULL) { | |
| 96 LOG(ERROR) << "Unknown connection."; | |
| 97 return; | |
| 98 } | |
| 99 if (!http_connection->ReceiveData(data, length)) { | |
| 100 // Error occured during receiving data, or the request has not been | |
| 101 // handled by any of request handlers, so we are dropping the connection. | |
| 102 DropHttpConnection(http_connection); | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 void HttpServer::DidClose(StreamListenSocket* connection) { | |
| 107 HttpConnection* http_connection = FindConnection(connection); | |
| 108 if (http_connection == NULL) { | |
| 109 LOG(ERROR) << "Unknown connection."; | |
| 110 return; | |
| 111 } | |
| 112 delete http_connection; | |
| 113 connections_.erase(connection); | |
| 114 } | |
| 115 | |
| 116 HttpConnection* HttpServer::FindConnection(StreamListenSocket* socket) { | |
| 117 std::map<StreamListenSocket*, HttpConnection*>::iterator it = | |
| 118 connections_.find(socket); | |
| 119 if (it == connections_.end()) { | |
| 120 return NULL; | |
| 121 } | |
| 122 return it->second; | |
| 123 } | |
| 124 | |
| 125 } // namespace test_servers | |
| 126 } // namespace gdata | |
| OLD | NEW |