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 #ifndef CHROME_BROWSER_CHROMEOS_GDATA_TEST_SERVERS_HTTP_SERVER_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_GDATA_TEST_SERVERS_HTTP_SERVER_H_ |
| 7 |
| 8 #include <string> |
| 9 #include "base/basictypes.h" |
| 10 #include "chrome/browser/chromeos/gdata/test_servers/http_connection.h" |
| 11 #include "net/base/tcp_listen_socket.h" |
| 12 |
| 13 namespace base { |
| 14 class WaitableEvent; |
| 15 } |
| 16 |
| 17 namespace gdata { |
| 18 namespace test_servers { |
| 19 |
| 20 class HttpConnectionDelegate; |
| 21 |
| 22 // General purpose, simple http server which runs on Browser::IO thread. |
| 23 class HttpServer : public net::TCPListenSocket, |
| 24 private net::StreamListenSocket::Delegate { |
| 25 public: |
| 26 // Factory method. Returns empty scoped_refptr, if creating of a server |
| 27 // failed. Can be called from any thread. Returned object runs in the |
| 28 // Browser::IO thread, though. |
| 29 // Passed |connection_delegate|'s methods are called from the IO thread. |
| 30 static scoped_refptr<HttpServer> CreateServer( |
| 31 const std::string& address, |
| 32 int port, |
| 33 HttpConnectionDelegate* connection_delegate); |
| 34 |
| 35 // Drops the http connection. It will remove reference to it from internal |
| 36 // collections and cause closing of an underlying socket. Should be called |
| 37 // from the IO thread. |
| 38 void DropHttpConnection(HttpConnection* connection); |
| 39 |
| 40 private: |
| 41 // Creates a server on IO thread. The test server pointer is returned |
| 42 // before signaling the waiter (if provided). |
| 43 static void CreateServerOnIOThread( |
| 44 const std::string& address, |
| 45 int port, |
| 46 HttpConnectionDelegate* connection_delegate, |
| 47 base::WaitableEvent* waiter, |
| 48 HttpServer** server_ptr); |
| 49 |
| 50 virtual ~HttpServer(); |
| 51 |
| 52 // Owns the HttpConnection objects. |
| 53 std::map<StreamListenSocket*, HttpConnection*> connections_; |
| 54 HttpConnectionDelegate* connection_delegate_; // Does not own. |
| 55 |
| 56 HttpServer(SocketDescriptor& socket, |
| 57 HttpConnectionDelegate* connection_delegate); |
| 58 |
| 59 // |server| is the original listening Socket, connection is the new |
| 60 // Socket that was created. Ownership of |connection| is transferred |
| 61 // to the delegate with this call. |
| 62 virtual void DidAccept(StreamListenSocket* server, |
| 63 StreamListenSocket* connection) OVERRIDE; |
| 64 virtual void DidRead(StreamListenSocket* connection, |
| 65 const char* data, |
| 66 int length) OVERRIDE; |
| 67 virtual void DidClose(StreamListenSocket* connection) OVERRIDE; |
| 68 |
| 69 virtual HttpConnection* FindConnection(StreamListenSocket* socket); |
| 70 |
| 71 DISALLOW_COPY_AND_ASSIGN(HttpServer); |
| 72 }; |
| 73 |
| 74 } // namespace test_servers |
| 75 } // namespace gdata |
| 76 |
| 77 #endif // CHROME_BROWSER_CHROMEOS_GDATA_TEST_SERVERS_HTTP_SERVER_H_ |
OLD | NEW |