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

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: 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/non_thread_safe.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,
33 public base::NonThreadSafe {
satorux1 2013/04/22 01:12:01 In favor of less inheritance, please use base::Thr
Paweł Hajdan Jr. 2013/04/22 18:53:44 Done.
28 public: 34 public:
29 HttpListenSocket(const SocketDescriptor socket_descriptor, 35 HttpListenSocket(const SocketDescriptor socket_descriptor,
30 net::StreamListenSocket::Delegate* delegate); 36 net::StreamListenSocket::Delegate* delegate);
31 virtual void Listen(); 37 virtual void Listen();
32 38
33 private: 39 private:
34 virtual ~HttpListenSocket(); 40 virtual ~HttpListenSocket();
35 }; 41 };
36 42
37 // Class providing an HTTP server for testing purpose. This is a basic server 43 // Class providing an HTTP server for testing purpose. This is a basic server
(...skipping 17 matching lines...) Expand all
55 // if (absolute_url.path() != "/test") 61 // if (absolute_url.path() != "/test")
56 // return scoped_ptr<HttpResponse>(); 62 // return scoped_ptr<HttpResponse>();
57 // 63 //
58 // scoped_ptr<HttpResponse> http_response(new HttpResponse()); 64 // scoped_ptr<HttpResponse> http_response(new HttpResponse());
59 // http_response->set_code(test_server::SUCCESS); 65 // http_response->set_code(test_server::SUCCESS);
60 // http_response->set_content("hello"); 66 // http_response->set_content("hello");
61 // http_response->set_content_type("text/plain"); 67 // http_response->set_content_type("text/plain");
62 // return http_response.Pass(); 68 // return http_response.Pass();
63 // } 69 // }
64 // 70 //
65 class HttpServer : public net::StreamListenSocket::Delegate { 71 class HttpServer : public net::StreamListenSocket::Delegate,
72 public base::NonThreadSafe {
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
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);
satorux1 2013/04/22 01:12:01 Could you avoid using WaitableEvent? My experience
Paweł Hajdan Jr. 2013/04/22 18:53:44 It'd be great to have a constructive alternative.
satorux1 2013/04/23 01:31:04 Sorry I wasn't clear. I'm not disliking WaitableEv
satorux1 2013/04/23 08:08:50 FWIW, just to make it clearer: issues introduced
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
141 DISALLOW_COPY_AND_ASSIGN(HttpServer); 151 DISALLOW_COPY_AND_ASSIGN(HttpServer);
142 }; 152 };
143 153
144 } // namespace test_servers 154 } // namespace test_servers
145 } // namespace google_apis 155 } // namespace google_apis
146 156
147 #endif // CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_SERVER_H_ 157 #endif // CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_SERVER_H_
OLDNEW
« no previous file with comments | « chrome/browser/google_apis/gdata_wapi_operations_unittest.cc ('k') | chrome/browser/google_apis/test_server/http_server.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698