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

Side by Side 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: fixes 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 #include "chrome/browser/google_apis/test_server/http_server.h" 5 #include "chrome/browser/google_apis/test_server/http_server.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/run_loop.h"
8 #include "base/stl_util.h" 9 #include "base/stl_util.h"
9 #include "base/string_util.h" 10 #include "base/string_util.h"
10 #include "base/stringprintf.h" 11 #include "base/stringprintf.h"
11 #include "chrome/browser/google_apis/test_server/http_connection.h" 12 #include "chrome/browser/google_apis/test_server/http_connection.h"
12 #include "chrome/browser/google_apis/test_server/http_request.h" 13 #include "chrome/browser/google_apis/test_server/http_request.h"
13 #include "chrome/browser/google_apis/test_server/http_response.h" 14 #include "chrome/browser/google_apis/test_server/http_response.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/test/test_utils.h"
16 #include "net/tools/fetch/http_listen_socket.h" 15 #include "net/tools/fetch/http_listen_socket.h"
17 16
18 namespace google_apis { 17 namespace google_apis {
19 namespace test_server { 18 namespace test_server {
20 19
21 using content::BrowserThread;
22
23 namespace { 20 namespace {
24 21
25 const int kPort = 8040; 22 const int kPort = 8040;
26 const char kIp[] = "127.0.0.1"; 23 const char kIp[] = "127.0.0.1";
27 const int kRetries = 10; 24 const int kRetries = 10;
28 25
29 // Callback to handle requests with default predefined response for requests 26 // Callback to handle requests with default predefined response for requests
30 // matching the address |url|. 27 // matching the address |url|.
31 scoped_ptr<HttpResponse> HandleDefaultRequest(const GURL& url, 28 scoped_ptr<HttpResponse> HandleDefaultRequest(const GURL& url,
32 const HttpResponse& response, 29 const HttpResponse& response,
33 const HttpRequest& request) { 30 const HttpRequest& request) {
34 const GURL request_url = url.Resolve(request.relative_url); 31 const GURL request_url = url.Resolve(request.relative_url);
35 if (url.path() != request_url.path()) 32 if (url.path() != request_url.path())
36 return scoped_ptr<HttpResponse>(NULL); 33 return scoped_ptr<HttpResponse>(NULL);
37 return scoped_ptr<HttpResponse>(new HttpResponse(response)); 34 return scoped_ptr<HttpResponse>(new HttpResponse(response));
38 } 35 }
39 36
40 } // namespace 37 } // namespace
41 38
42 HttpListenSocket::HttpListenSocket(const SocketDescriptor socket_descriptor, 39 HttpListenSocket::HttpListenSocket(const SocketDescriptor socket_descriptor,
43 net::StreamListenSocket::Delegate* delegate) 40 net::StreamListenSocket::Delegate* delegate)
44 : net::TCPListenSocket(socket_descriptor, delegate) { 41 : net::TCPListenSocket(socket_descriptor, delegate) {
45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 42 DCHECK(thread_checker_.CalledOnValidThread());
46 } 43 }
47 44
48 void HttpListenSocket::Listen() { 45 void HttpListenSocket::Listen() {
49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 46 DCHECK(thread_checker_.CalledOnValidThread());
50 net::TCPListenSocket::Listen(); 47 net::TCPListenSocket::Listen();
51 } 48 }
52 49
53 HttpListenSocket::~HttpListenSocket() { 50 HttpListenSocket::~HttpListenSocket() {
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 51 DCHECK(thread_checker_.CalledOnValidThread());
55 } 52 }
56 53
57 HttpServer::HttpServer() 54 HttpServer::HttpServer(
58 : port_(-1), 55 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread)
56 : io_thread_(io_thread),
57 port_(-1),
59 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { 58 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 59 DCHECK(io_thread_);
60 DCHECK(thread_checker_.CalledOnValidThread());
61 } 61 }
62 62
63 HttpServer::~HttpServer() { 63 HttpServer::~HttpServer() {
64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 64 DCHECK(thread_checker_.CalledOnValidThread());
65 } 65 }
66 66
67 bool HttpServer::InitializeAndWaitUntilReady() { 67 bool HttpServer::InitializeAndWaitUntilReady() {
68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 68 DCHECK(thread_checker_.CalledOnValidThread());
69 69
70 BrowserThread::PostTask( 70 base::RunLoop run_loop;
71 BrowserThread::IO, 71 if (!io_thread_->PostTaskAndReply(
72 FROM_HERE, 72 FROM_HERE,
73 base::Bind(&HttpServer::InitializeOnIOThread, 73 base::Bind(&HttpServer::InitializeOnIOThread, base::Unretained(this)),
74 base::Unretained(this))); 74 run_loop.QuitClosure())) {
75 75 return false;
76 // Wait for the task completion. 76 }
77 content::RunAllPendingInMessageLoop(BrowserThread::IO); 77 run_loop.Run();
78 78
79 return Started(); 79 return Started();
80 } 80 }
81 81
82 void HttpServer::ShutdownAndWaitUntilComplete() { 82 bool HttpServer::ShutdownAndWaitUntilComplete() {
83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 83 DCHECK(thread_checker_.CalledOnValidThread());
84 84
85 BrowserThread::PostTask( 85 base::RunLoop run_loop;
86 BrowserThread::IO, 86 if (!io_thread_->PostTaskAndReply(
87 FROM_HERE, 87 FROM_HERE,
88 base::Bind(&HttpServer::ShutdownOnIOThread, 88 base::Bind(&HttpServer::ShutdownOnIOThread, base::Unretained(this)),
89 base::Unretained(this))); 89 run_loop.QuitClosure())) {
90 return false;
91 }
92 run_loop.Run();
90 93
91 // Wait for the task completion. 94 return true;
92 content::RunAllPendingInMessageLoop(BrowserThread::IO);
93 } 95 }
94 96
95 void HttpServer::InitializeOnIOThread() { 97 void HttpServer::InitializeOnIOThread() {
96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 98 DCHECK(io_thread_->BelongsToCurrentThread());
97 DCHECK(!Started()); 99 DCHECK(!Started());
98 100
99 int retries_left = kRetries + 1; 101 int retries_left = kRetries + 1;
100 int try_port = kPort; 102 int try_port = kPort;
101 103
102 while (retries_left > 0) { 104 while (retries_left > 0) {
103 SocketDescriptor socket_descriptor = net::TCPListenSocket::CreateAndBind( 105 SocketDescriptor socket_descriptor = net::TCPListenSocket::CreateAndBind(
104 kIp, 106 kIp,
105 try_port); 107 try_port);
106 if (socket_descriptor != net::TCPListenSocket::kInvalidSocket) { 108 if (socket_descriptor != net::TCPListenSocket::kInvalidSocket) {
107 listen_socket_ = new HttpListenSocket(socket_descriptor, this); 109 listen_socket_ = new HttpListenSocket(socket_descriptor, this);
108 listen_socket_->Listen(); 110 listen_socket_->Listen();
109 base_url_ = GURL(base::StringPrintf("http://%s:%d", kIp, try_port)); 111 base_url_ = GURL(base::StringPrintf("http://%s:%d", kIp, try_port));
110 port_ = try_port; 112 port_ = try_port;
111 break; 113 break;
112 } 114 }
113 retries_left--; 115 retries_left--;
114 try_port++; 116 try_port++;
115 } 117 }
116 } 118 }
117 119
118 void HttpServer::ShutdownOnIOThread() { 120 void HttpServer::ShutdownOnIOThread() {
119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 121 DCHECK(io_thread_->BelongsToCurrentThread());
120 122
121 listen_socket_ = NULL; // Release the listen socket. 123 listen_socket_ = NULL; // Release the listen socket.
122 STLDeleteContainerPairSecondPointers(connections_.begin(), 124 STLDeleteContainerPairSecondPointers(connections_.begin(),
123 connections_.end()); 125 connections_.end());
124 connections_.clear(); 126 connections_.clear();
125 } 127 }
126 128
127 void HttpServer::HandleRequest(HttpConnection* connection, 129 void HttpServer::HandleRequest(HttpConnection* connection,
128 scoped_ptr<HttpRequest> request) { 130 scoped_ptr<HttpRequest> request) {
129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 131 DCHECK(io_thread_->BelongsToCurrentThread());
130 132
131 for (size_t i = 0; i < request_handlers_.size(); ++i) { 133 for (size_t i = 0; i < request_handlers_.size(); ++i) {
132 scoped_ptr<HttpResponse> response = 134 scoped_ptr<HttpResponse> response =
133 request_handlers_[i].Run(*request.get()); 135 request_handlers_[i].Run(*request.get());
134 if (response.get()) { 136 if (response.get()) {
135 connection->SendResponse(response.Pass()); 137 connection->SendResponse(response.Pass());
136 return; 138 return;
137 } 139 }
138 } 140 }
139 141
(...skipping 15 matching lines...) Expand all
155 return base_url_.Resolve(relative_url); 157 return base_url_.Resolve(relative_url);
156 } 158 }
157 159
158 void HttpServer::RegisterRequestHandler( 160 void HttpServer::RegisterRequestHandler(
159 const HandleRequestCallback& callback) { 161 const HandleRequestCallback& callback) {
160 request_handlers_.push_back(callback); 162 request_handlers_.push_back(callback);
161 } 163 }
162 164
163 void HttpServer::DidAccept(net::StreamListenSocket* server, 165 void HttpServer::DidAccept(net::StreamListenSocket* server,
164 net::StreamListenSocket* connection) { 166 net::StreamListenSocket* connection) {
165 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 167 DCHECK(io_thread_->BelongsToCurrentThread());
166 168
167 HttpConnection* http_connection = new HttpConnection( 169 HttpConnection* http_connection = new HttpConnection(
168 connection, 170 connection,
169 base::Bind(&HttpServer::HandleRequest, weak_factory_.GetWeakPtr())); 171 base::Bind(&HttpServer::HandleRequest, weak_factory_.GetWeakPtr()));
170 connections_[connection] = http_connection; 172 connections_[connection] = http_connection;
171 } 173 }
172 174
173 void HttpServer::DidRead(net::StreamListenSocket* connection, 175 void HttpServer::DidRead(net::StreamListenSocket* connection,
174 const char* data, 176 const char* data,
175 int length) { 177 int length) {
176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 178 DCHECK(io_thread_->BelongsToCurrentThread());
177 179
178 HttpConnection* http_connection = FindConnection(connection); 180 HttpConnection* http_connection = FindConnection(connection);
179 if (http_connection == NULL) { 181 if (http_connection == NULL) {
180 LOG(WARNING) << "Unknown connection."; 182 LOG(WARNING) << "Unknown connection.";
181 return; 183 return;
182 } 184 }
183 http_connection->ReceiveData(std::string(data, length)); 185 http_connection->ReceiveData(std::string(data, length));
184 } 186 }
185 187
186 void HttpServer::DidClose(net::StreamListenSocket* connection) { 188 void HttpServer::DidClose(net::StreamListenSocket* connection) {
187 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 189 DCHECK(io_thread_->BelongsToCurrentThread());
188 190
189 HttpConnection* http_connection = FindConnection(connection); 191 HttpConnection* http_connection = FindConnection(connection);
190 if (http_connection == NULL) { 192 if (http_connection == NULL) {
191 LOG(WARNING) << "Unknown connection."; 193 LOG(WARNING) << "Unknown connection.";
192 return; 194 return;
193 } 195 }
194 delete http_connection; 196 delete http_connection;
195 connections_.erase(connection); 197 connections_.erase(connection);
196 } 198 }
197 199
198 HttpConnection* HttpServer::FindConnection( 200 HttpConnection* HttpServer::FindConnection(
199 net::StreamListenSocket* socket) { 201 net::StreamListenSocket* socket) {
200 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 202 DCHECK(io_thread_->BelongsToCurrentThread());
201 203
202 std::map<net::StreamListenSocket*, HttpConnection*>::iterator it = 204 std::map<net::StreamListenSocket*, HttpConnection*>::iterator it =
203 connections_.find(socket); 205 connections_.find(socket);
204 if (it == connections_.end()) { 206 if (it == connections_.end()) {
205 return NULL; 207 return NULL;
206 } 208 }
207 return it->second; 209 return it->second;
208 } 210 }
209 211
210 } // namespace test_server 212 } // namespace test_server
211 } // namespace google_apis 213 } // namespace google_apis
OLDNEW
« no previous file with comments | « chrome/browser/google_apis/test_server/http_server.h ('k') | chrome/browser/google_apis/test_server/http_server_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698