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

Side by Side Diff: chrome/browser/google_apis/test_server/http_server.h

Issue 14365019: Break dependencies preventing move of test_server down to net. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ThreadChecker 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_SERVER_H_ 5 #ifndef CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_SERVER_H_
6 #define CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_SERVER_H_ 6 #define CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_SERVER_H_
7 7
8 #include <map> 8 #include <map>
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/basictypes.h" 12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h" 13 #include "base/compiler_specific.h"
14 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
15 #include "base/memory/weak_ptr.h" 15 #include "base/memory/weak_ptr.h"
16 #include "base/threading/thread_checker.h"
16 #include "googleurl/src/gurl.h" 17 #include "googleurl/src/gurl.h"
17 #include "net/socket/tcp_listen_socket.h" 18 #include "net/socket/tcp_listen_socket.h"
18 19
20 namespace base {
21 class WaitableEvent;
22 };
23
19 namespace google_apis { 24 namespace google_apis {
20 namespace test_server { 25 namespace test_server {
21 26
22 class HttpConnection; 27 class HttpConnection;
23 class HttpResponse; 28 class HttpResponse;
24 struct HttpRequest; 29 struct HttpRequest;
25 30
26 // This class is required to be able to have composition instead of inheritance, 31 // This class is required to be able to have composition instead of inheritance,
27 class HttpListenSocket: public net::TCPListenSocket { 32 class HttpListenSocket : public net::TCPListenSocket {
28 public: 33 public:
29 HttpListenSocket(const SocketDescriptor socket_descriptor, 34 HttpListenSocket(const SocketDescriptor socket_descriptor,
30 net::StreamListenSocket::Delegate* delegate); 35 net::StreamListenSocket::Delegate* delegate);
31 virtual void Listen(); 36 virtual void Listen();
32 37
33 private: 38 private:
34 virtual ~HttpListenSocket(); 39 virtual ~HttpListenSocket();
40
41 base::ThreadChecker thread_checker_;
35 }; 42 };
36 43
37 // Class providing an HTTP server for testing purpose. This is a basic server 44 // Class providing an HTTP server for testing purpose. This is a basic server
38 // providing only an essential subset of HTTP/1.1 protocol. Especially, 45 // providing only an essential subset of HTTP/1.1 protocol. Especially,
39 // it assumes that the request syntax is correct. It *does not* support 46 // it assumes that the request syntax is correct. It *does not* support
40 // a Chunked Transfer Encoding. 47 // a Chunked Transfer Encoding.
41 // 48 //
42 // The common use case is below: 49 // The common use case is below:
43 // 50 //
44 // scoped_ptr<HttpServer> test_server_; 51 // scoped_ptr<HttpServer> test_server_;
(...skipping 15 matching lines...) Expand all
60 // http_response->set_content("hello"); 67 // http_response->set_content("hello");
61 // http_response->set_content_type("text/plain"); 68 // http_response->set_content_type("text/plain");
62 // return http_response.Pass(); 69 // return http_response.Pass();
63 // } 70 // }
64 // 71 //
65 class HttpServer : public net::StreamListenSocket::Delegate { 72 class HttpServer : public net::StreamListenSocket::Delegate {
66 public: 73 public:
67 typedef base::Callback<scoped_ptr<HttpResponse>(const HttpRequest& request)> 74 typedef base::Callback<scoped_ptr<HttpResponse>(const HttpRequest& request)>
68 HandleRequestCallback; 75 HandleRequestCallback;
69 76
70 // Creates a http test server. InitializeAndWaitUntilReady() must be called 77 // Creates a http test server. InitializeAndWaitUntilReady() must be called
satorux1 2013/04/23 07:57:05 Please add some comment about |io_thread| paramete
Paweł Hajdan Jr. 2013/04/23 22:12:42 Done.
71 // to start the server. 78 // to start the server.
72 HttpServer(); 79 explicit HttpServer(
80 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread);
73 virtual ~HttpServer(); 81 virtual ~HttpServer();
74 82
75 // Initializes and waits until the server is ready to accept requests. 83 // Initializes and waits until the server is ready to accept requests.
76 bool InitializeAndWaitUntilReady(); 84 bool InitializeAndWaitUntilReady() WARN_UNUSED_RESULT;
77 85
78 // Shuts down the http server and waits until the shutdown is complete. 86 // Shuts down the http server and waits until the shutdown is complete.
79 void ShutdownAndWaitUntilComplete(); 87 bool ShutdownAndWaitUntilComplete() WARN_UNUSED_RESULT;
80 88
81 // Checks if the server is started. 89 // Checks if the server is started.
82 bool Started() const { 90 bool Started() const {
83 return listen_socket_.get() != NULL; 91 return listen_socket_.get() != NULL;
84 } 92 }
85 93
86 // Returns the base URL to the server, which looks like 94 // Returns the base URL to the server, which looks like
87 // http://127.0.0.1:<port>/, where <port> is the actual port number used by 95 // http://127.0.0.1:<port>/, where <port> is the actual port number used by
88 // the server. 96 // the server.
89 const GURL& base_url() const { return base_url_; } 97 const GURL& base_url() const { return base_url_; }
90 98
91 // Returns a URL to the server based on the given relative URL, which 99 // Returns a URL to the server based on the given relative URL, which
92 // should start with '/'. For example: GetURL("/path?query=foo") => 100 // should start with '/'. For example: GetURL("/path?query=foo") =>
93 // http://127.0.0.1:<port>/path?query=foo. 101 // http://127.0.0.1:<port>/path?query=foo.
94 GURL GetURL(const std::string& relative_url) const; 102 GURL GetURL(const std::string& relative_url) const;
95 103
96 // Returns the port number used by the server. 104 // Returns the port number used by the server.
97 int port() const { return port_; } 105 int port() const { return port_; }
98 106
99 // The most general purpose method. Any request processing can be added using 107 // The most general purpose method. Any request processing can be added using
100 // this method. Takes ownership of the object. The |callback| is called 108 // this method. Takes ownership of the object. The |callback| is called
101 // on UI thread. 109 // on UI thread.
102 void RegisterRequestHandler(const HandleRequestCallback& callback); 110 void RegisterRequestHandler(const HandleRequestCallback& callback);
103 111
104 private: 112 private:
105 // Initializes and starts the server. If initialization succeeds, Starts() 113 // Initializes and starts the server. If initialization succeeds, Starts()
106 // will return true. 114 // will return true.
107 void InitializeOnIOThread(); 115 void InitializeOnIOThread(base::WaitableEvent* event);
108 116
109 // Shuts down the server. 117 // Shuts down the server.
110 void ShutdownOnIOThread(); 118 void ShutdownOnIOThread(base::WaitableEvent* event);
111 119
112 // Handles a request when it is parsed. It passes the request to registed 120 // Handles a request when it is parsed. It passes the request to registed
113 // request handlers and sends a http response. 121 // request handlers and sends a http response.
114 void HandleRequest(HttpConnection* connection, 122 void HandleRequest(HttpConnection* connection,
115 scoped_ptr<HttpRequest> request); 123 scoped_ptr<HttpRequest> request);
116 124
117 // net::StreamListenSocket::Delegate overrides: 125 // net::StreamListenSocket::Delegate overrides:
118 virtual void DidAccept(net::StreamListenSocket* server, 126 virtual void DidAccept(net::StreamListenSocket* server,
119 net::StreamListenSocket* connection) OVERRIDE; 127 net::StreamListenSocket* connection) OVERRIDE;
120 virtual void DidRead(net::StreamListenSocket* connection, 128 virtual void DidRead(net::StreamListenSocket* connection,
121 const char* data, 129 const char* data,
122 int length) OVERRIDE; 130 int length) OVERRIDE;
123 virtual void DidClose(net::StreamListenSocket* connection) OVERRIDE; 131 virtual void DidClose(net::StreamListenSocket* connection) OVERRIDE;
124 132
125 HttpConnection* FindConnection(net::StreamListenSocket* socket); 133 HttpConnection* FindConnection(net::StreamListenSocket* socket);
126 134
135 scoped_refptr<base::SingleThreadTaskRunner> io_thread_;
136
127 scoped_refptr<HttpListenSocket> listen_socket_; 137 scoped_refptr<HttpListenSocket> listen_socket_;
128 int port_; 138 int port_;
129 GURL base_url_; 139 GURL base_url_;
130 140
131 // Owns the HttpConnection objects. 141 // Owns the HttpConnection objects.
132 std::map<net::StreamListenSocket*, HttpConnection*> connections_; 142 std::map<net::StreamListenSocket*, HttpConnection*> connections_;
133 143
134 // Vector of registered request handlers. 144 // Vector of registered request handlers.
135 std::vector<HandleRequestCallback> request_handlers_; 145 std::vector<HandleRequestCallback> request_handlers_;
136 146
137 // Note: This should remain the last member so it'll be destroyed and 147 // Note: This should remain the last member so it'll be destroyed and
138 // invalidate its weak pointers before any other members are destroyed. 148 // invalidate its weak pointers before any other members are destroyed.
139 base::WeakPtrFactory<HttpServer> weak_factory_; 149 base::WeakPtrFactory<HttpServer> weak_factory_;
140 150
151 base::ThreadChecker thread_checker_;
152
141 DISALLOW_COPY_AND_ASSIGN(HttpServer); 153 DISALLOW_COPY_AND_ASSIGN(HttpServer);
142 }; 154 };
143 155
144 } // namespace test_servers 156 } // namespace test_servers
145 } // namespace google_apis 157 } // namespace google_apis
146 158
147 #endif // CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_SERVER_H_ 159 #endif // CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_SERVER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698