OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/strings/string_number_conversions.h" | 5 #include "base/strings/string_number_conversions.h" |
6 #include "base/strings/string_util.h" | 6 #include "base/strings/string_util.h" |
7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
8 #include "base/threading/non_thread_safe.h" | 8 #include "base/threading/non_thread_safe.h" |
9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
10 #include "content/public/test/browser_test.h" | 10 #include "content/public/test/browser_test.h" |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 " \"url\": \"about:blank\",\n" | 139 " \"url\": \"about:blank\",\n" |
140 " \"webSocketDebuggerUrl\": \"ws:///devtools/page/" | 140 " \"webSocketDebuggerUrl\": \"ws:///devtools/page/" |
141 "44681551-ADFD-2411-076B-3AB14C1C60E2\"\n" | 141 "44681551-ADFD-2411-076B-3AB14C1C60E2\"\n" |
142 "}]"; | 142 "}]"; |
143 | 143 |
144 static const int kBufferSize = 16*1024; | 144 static const int kBufferSize = 16*1024; |
145 static const int kAdbPort = 5037; | 145 static const int kAdbPort = 5037; |
146 | 146 |
147 static const int kAdbMessageHeaderSize = 4; | 147 static const int kAdbMessageHeaderSize = 4; |
148 | 148 |
149 | 149 class SimpleHttpServer : base::NonThreadSafe { |
150 class SingleConnectionServer : base::NonThreadSafe { | |
151 public: | 150 public: |
152 class Parser { | 151 class Parser { |
153 public: | 152 public: |
154 virtual int Consume(const char* data, int size) = 0; | 153 virtual int Consume(const char* data, int size) = 0; |
155 virtual void Reset() = 0; | |
156 | |
157 protected: | |
158 virtual ~Parser() {} | 154 virtual ~Parser() {} |
159 }; | 155 }; |
160 | 156 |
161 SingleConnectionServer( | 157 typedef base::Callback<void(const std::string&)> SendCallback; |
162 Parser* parser, net::IPEndPoint endpoint, int buffer_size); | 158 typedef base::Callback<Parser*(const SendCallback&)> ParserFactory; |
163 | 159 |
164 virtual ~SingleConnectionServer(); | 160 SimpleHttpServer(const ParserFactory& factory, net::IPEndPoint endpoint); |
165 | 161 virtual ~SimpleHttpServer(); |
166 void Send(const std::string& message); | |
167 | 162 |
168 private: | 163 private: |
169 void SendData(const char* data, int size); | 164 class Connection : base::NonThreadSafe { |
| 165 public: |
| 166 Connection(net::StreamSocket* socket, const ParserFactory& factory); |
| 167 virtual ~Connection(); |
| 168 |
| 169 private: |
| 170 void Send(const std::string& message); |
| 171 void ReadData(); |
| 172 void OnDataRead(int count); |
| 173 void WriteData(); |
| 174 void OnDataWritten(int count); |
| 175 |
| 176 scoped_ptr<net::StreamSocket> socket_; |
| 177 scoped_ptr<Parser> parser_; |
| 178 scoped_refptr<net::GrowableIOBuffer> input_buffer_; |
| 179 scoped_refptr<net::GrowableIOBuffer> output_buffer_; |
| 180 int bytes_to_write_; |
| 181 bool read_closed_; |
| 182 |
| 183 DISALLOW_COPY_AND_ASSIGN(Connection); |
| 184 }; |
170 | 185 |
171 void AcceptConnection(); | 186 void AcceptConnection(); |
172 void OnAccepted(int result); | 187 void OnAccepted(int result); |
173 | 188 |
174 void ReadData(); | 189 ParserFactory factory_; |
175 void OnDataRead(int count); | 190 scoped_ptr<net::TCPServerSocket> socket_; |
| 191 scoped_ptr<net::StreamSocket> client_socket_; |
176 | 192 |
177 void WriteData(); | 193 DISALLOW_COPY_AND_ASSIGN(SimpleHttpServer); |
178 void OnDataWritten(int count); | |
179 | |
180 Parser* parser_; | |
181 int bytes_to_write_; | |
182 scoped_ptr<net::TCPServerSocket> server_socket_; | |
183 scoped_ptr<net::StreamSocket> client_socket_; | |
184 scoped_refptr<net::GrowableIOBuffer> input_buffer_; | |
185 scoped_refptr<net::GrowableIOBuffer> output_buffer_; | |
186 | |
187 DISALLOW_COPY_AND_ASSIGN(SingleConnectionServer); | |
188 }; | 194 }; |
189 | 195 |
190 SingleConnectionServer::SingleConnectionServer(Parser* parser, | 196 SimpleHttpServer::SimpleHttpServer(const ParserFactory& factory, |
191 net::IPEndPoint endpoint, | 197 net::IPEndPoint endpoint) |
192 int buffer_size) | 198 : factory_(factory), |
193 : parser_(parser), | 199 socket_(new net::TCPServerSocket(NULL, net::NetLog::Source())) { |
194 bytes_to_write_(0) { | 200 socket_->Listen(endpoint, 5); |
195 CHECK(CalledOnValidThread()); | |
196 | |
197 input_buffer_ = new net::GrowableIOBuffer(); | |
198 input_buffer_->SetCapacity(buffer_size); | |
199 | |
200 output_buffer_ = new net::GrowableIOBuffer(); | |
201 | |
202 server_socket_.reset(new net::TCPServerSocket(NULL, net::NetLog::Source())); | |
203 server_socket_->Listen(endpoint, 1); | |
204 | |
205 AcceptConnection(); | 201 AcceptConnection(); |
206 } | 202 } |
207 | 203 |
208 SingleConnectionServer::~SingleConnectionServer() { | 204 SimpleHttpServer::~SimpleHttpServer() { |
209 CHECK(CalledOnValidThread()); | |
210 | |
211 server_socket_.reset(); | |
212 | |
213 if (client_socket_) { | |
214 client_socket_->Disconnect(); | |
215 client_socket_.reset(); | |
216 } | |
217 } | 205 } |
218 | 206 |
219 void SingleConnectionServer::Send(const std::string& message) { | 207 SimpleHttpServer::Connection::Connection(net::StreamSocket* socket, |
220 SendData(message.c_str(), message.size()); | 208 const ParserFactory& factory) |
| 209 : socket_(socket), |
| 210 parser_(factory.Run(base::Bind(&Connection::Send, |
| 211 base::Unretained(this)))), |
| 212 input_buffer_(new net::GrowableIOBuffer()), |
| 213 output_buffer_(new net::GrowableIOBuffer()), |
| 214 bytes_to_write_(0), |
| 215 read_closed_(false) { |
| 216 input_buffer_->SetCapacity(kBufferSize); |
| 217 ReadData(); |
221 } | 218 } |
222 | 219 |
223 void SingleConnectionServer::SendData(const char* data, int size) { | 220 SimpleHttpServer::Connection::~Connection() { |
| 221 } |
| 222 |
| 223 void SimpleHttpServer::Connection::Send(const std::string& message) { |
224 CHECK(CalledOnValidThread()); | 224 CHECK(CalledOnValidThread()); |
| 225 const char* data = message.c_str(); |
| 226 int size = message.size(); |
225 | 227 |
226 if ((output_buffer_->offset() + bytes_to_write_ + size) > | 228 if ((output_buffer_->offset() + bytes_to_write_ + size) > |
227 output_buffer_->capacity()) { | 229 output_buffer_->capacity()) { |
228 // If not enough space without relocation | 230 // If not enough space without relocation |
229 if (output_buffer_->capacity() < (bytes_to_write_ + size)) { | 231 if (output_buffer_->capacity() < (bytes_to_write_ + size)) { |
230 // If even buffer is not enough | 232 // If even buffer is not enough |
231 int new_size = std::max(output_buffer_->capacity() * 2, size * 2); | 233 int new_size = std::max(output_buffer_->capacity() * 2, size * 2); |
232 output_buffer_->SetCapacity(new_size); | 234 output_buffer_->SetCapacity(new_size); |
233 } | 235 } |
234 memmove(output_buffer_->StartOfBuffer(), | 236 memmove(output_buffer_->StartOfBuffer(), |
235 output_buffer_->data(), | 237 output_buffer_->data(), |
236 bytes_to_write_); | 238 bytes_to_write_); |
237 output_buffer_->set_offset(0); | 239 output_buffer_->set_offset(0); |
238 } | 240 } |
239 | 241 |
240 memcpy(output_buffer_->data() + bytes_to_write_, data, size); | 242 memcpy(output_buffer_->data() + bytes_to_write_, data, size); |
241 bytes_to_write_ += size; | 243 bytes_to_write_ += size; |
242 | 244 |
243 if (bytes_to_write_ == size) | 245 if (bytes_to_write_ == size) |
244 // If write loop wasn't yet started, then start it | 246 // If write loop wasn't yet started, then start it |
245 WriteData(); | 247 WriteData(); |
246 } | 248 } |
247 | 249 |
248 void SingleConnectionServer::AcceptConnection() { | 250 void SimpleHttpServer::Connection::ReadData() { |
249 CHECK(CalledOnValidThread()); | |
250 | |
251 if (client_socket_) { | |
252 client_socket_->Disconnect(); | |
253 client_socket_.reset(); | |
254 } | |
255 | |
256 int accept_result = server_socket_->Accept(&client_socket_, | |
257 base::Bind(&SingleConnectionServer::OnAccepted, base::Unretained(this))); | |
258 | |
259 if (accept_result != net::ERR_IO_PENDING) | |
260 base::MessageLoop::current()->PostTask( | |
261 FROM_HERE, | |
262 base::Bind(&SingleConnectionServer::OnAccepted, | |
263 base::Unretained(this), | |
264 accept_result)); | |
265 } | |
266 | |
267 void SingleConnectionServer::OnAccepted(int result) { | |
268 CHECK(CalledOnValidThread()); | |
269 | |
270 ASSERT_EQ(result, 0); // Fails if the socket is already in use. | |
271 parser_->Reset(); | |
272 ReadData(); | |
273 } | |
274 | |
275 void SingleConnectionServer::ReadData() { | |
276 CHECK(CalledOnValidThread()); | 251 CHECK(CalledOnValidThread()); |
277 | 252 |
278 if (input_buffer_->RemainingCapacity() == 0) | 253 if (input_buffer_->RemainingCapacity() == 0) |
279 input_buffer_->SetCapacity(input_buffer_->capacity() * 2); | 254 input_buffer_->SetCapacity(input_buffer_->capacity() * 2); |
280 | 255 |
281 int read_result = client_socket_->Read( | 256 int read_result = socket_->Read( |
282 input_buffer_.get(), | 257 input_buffer_.get(), |
283 input_buffer_->RemainingCapacity(), | 258 input_buffer_->RemainingCapacity(), |
284 base::Bind(&SingleConnectionServer::OnDataRead, base::Unretained(this))); | 259 base::Bind(&Connection::OnDataRead, base::Unretained(this))); |
285 | 260 |
286 if (read_result != net::ERR_IO_PENDING) | 261 if (read_result != net::ERR_IO_PENDING) |
287 OnDataRead(read_result); | 262 OnDataRead(read_result); |
288 } | 263 } |
289 | 264 |
290 void SingleConnectionServer::OnDataRead(int count) { | 265 void SimpleHttpServer::Connection::OnDataRead(int count) { |
291 CHECK(CalledOnValidThread()); | 266 CHECK(CalledOnValidThread()); |
292 | |
293 if (count <= 0) { | 267 if (count <= 0) { |
294 AcceptConnection(); | 268 if (bytes_to_write_ == 0) |
| 269 delete this; |
| 270 else |
| 271 read_closed_ = true; |
295 return; | 272 return; |
296 } | 273 } |
297 | |
298 input_buffer_->set_offset(input_buffer_->offset() + count); | 274 input_buffer_->set_offset(input_buffer_->offset() + count); |
299 | |
300 int bytes_processed; | 275 int bytes_processed; |
301 | 276 |
302 do { | 277 do { |
303 char* data = input_buffer_->StartOfBuffer(); | 278 char* data = input_buffer_->StartOfBuffer(); |
304 int data_size = input_buffer_->offset(); | 279 int data_size = input_buffer_->offset(); |
305 | |
306 bytes_processed = parser_->Consume(data, data_size); | 280 bytes_processed = parser_->Consume(data, data_size); |
307 | 281 |
308 if (bytes_processed) { | 282 if (bytes_processed) { |
309 memmove(data, data + bytes_processed, data_size - bytes_processed); | 283 memmove(data, data + bytes_processed, data_size - bytes_processed); |
310 input_buffer_->set_offset(data_size - bytes_processed); | 284 input_buffer_->set_offset(data_size - bytes_processed); |
311 } | 285 } |
312 } while (bytes_processed); | 286 } while (bytes_processed); |
313 | 287 // Posting to avoid deep recursion in case of synchronous IO |
314 // Posting is needed not to enter deep recursion in case too synchronous IO | |
315 base::MessageLoop::current()->PostTask( | 288 base::MessageLoop::current()->PostTask( |
316 FROM_HERE, | 289 FROM_HERE, |
317 base::Bind(&SingleConnectionServer::ReadData, base::Unretained(this))); | 290 base::Bind(&Connection::ReadData, base::Unretained(this))); |
318 } | 291 } |
319 | 292 |
320 void SingleConnectionServer::WriteData() { | 293 void SimpleHttpServer::Connection::WriteData() { |
321 CHECK(CalledOnValidThread()); | 294 CHECK(CalledOnValidThread()); |
322 | |
323 CHECK_GE(output_buffer_->capacity(), | 295 CHECK_GE(output_buffer_->capacity(), |
324 output_buffer_->offset() + bytes_to_write_) << "Overflow"; | 296 output_buffer_->offset() + bytes_to_write_) << "Overflow"; |
325 | 297 |
326 int write_result = client_socket_->Write( | 298 int write_result = socket_->Write( |
327 output_buffer_, | 299 output_buffer_, |
328 bytes_to_write_, | 300 bytes_to_write_, |
329 base::Bind(&SingleConnectionServer::OnDataWritten, | 301 base::Bind(&Connection::OnDataWritten, base::Unretained(this))); |
330 base::Unretained(this))); | 302 |
331 if (write_result != net::ERR_IO_PENDING) | 303 if (write_result != net::ERR_IO_PENDING) |
332 OnDataWritten(write_result); | 304 OnDataWritten(write_result); |
333 } | 305 } |
334 | 306 |
335 void SingleConnectionServer::OnDataWritten(int count) { | 307 void SimpleHttpServer::Connection::OnDataWritten(int count) { |
336 CHECK(CalledOnValidThread()); | 308 CHECK(CalledOnValidThread()); |
337 | |
338 if (count < 0) { | 309 if (count < 0) { |
339 AcceptConnection(); | 310 delete this; |
340 return; | 311 return; |
341 } | 312 } |
342 | |
343 CHECK_GT(count, 0); | 313 CHECK_GT(count, 0); |
344 CHECK_GE(output_buffer_->capacity(), | 314 CHECK_GE(output_buffer_->capacity(), |
345 output_buffer_->offset() + bytes_to_write_) << "Overflow"; | 315 output_buffer_->offset() + bytes_to_write_) << "Overflow"; |
346 | 316 |
347 bytes_to_write_ -= count; | 317 bytes_to_write_ -= count; |
348 output_buffer_->set_offset(output_buffer_->offset() + count); | 318 output_buffer_->set_offset(output_buffer_->offset() + count); |
349 | 319 |
350 if (bytes_to_write_ != 0) | 320 if (bytes_to_write_ != 0) |
351 // Posting is needed not to enter deep recursion in case too synchronous IO | 321 // Posting to avoid deep recursion in case of synchronous IO |
352 base::MessageLoop::current()->PostTask( | 322 base::MessageLoop::current()->PostTask( |
353 FROM_HERE, | 323 FROM_HERE, |
354 base::Bind(&SingleConnectionServer::WriteData, base::Unretained(this))); | 324 base::Bind(&Connection::WriteData, base::Unretained(this))); |
| 325 else if (read_closed_) |
| 326 delete this; |
355 } | 327 } |
356 | 328 |
| 329 void SimpleHttpServer::AcceptConnection() { |
| 330 CHECK(CalledOnValidThread()); |
357 | 331 |
358 class MockAdbServer : SingleConnectionServer::Parser, | 332 int accept_result = socket_->Accept(&client_socket_, |
359 base::NonThreadSafe { | 333 base::Bind(&SimpleHttpServer::OnAccepted, base::Unretained(this))); |
| 334 |
| 335 if (accept_result != net::ERR_IO_PENDING) |
| 336 base::MessageLoop::current()->PostTask( |
| 337 FROM_HERE, |
| 338 base::Bind(&SimpleHttpServer::OnAccepted, |
| 339 base::Unretained(this), |
| 340 accept_result)); |
| 341 } |
| 342 |
| 343 void SimpleHttpServer::OnAccepted(int result) { |
| 344 CHECK(CalledOnValidThread()); |
| 345 ASSERT_EQ(result, 0); // Fails if the socket is already in use. |
| 346 new Connection(client_socket_.release(), factory_); |
| 347 AcceptConnection(); |
| 348 } |
| 349 |
| 350 class AdbParser : SimpleHttpServer::Parser, base::NonThreadSafe { |
360 public: | 351 public: |
361 MockAdbServer() { | 352 static Parser* Create(const SimpleHttpServer::SendCallback& callback) { |
362 CHECK(CalledOnValidThread()); | 353 return new AdbParser(callback); |
363 net::IPAddressNumber address; | |
364 net::ParseIPLiteralToNumber("127.0.0.1", &address); | |
365 net::IPEndPoint endpoint(address, kAdbPort); | |
366 server_.reset(new SingleConnectionServer(this, endpoint, kBufferSize)); | |
367 } | 354 } |
368 | 355 |
369 virtual ~MockAdbServer() { | 356 explicit AdbParser(const SimpleHttpServer::SendCallback& callback) |
370 CHECK(CalledOnValidThread()); | 357 : callback_(callback) { |
| 358 } |
| 359 |
| 360 virtual ~AdbParser() { |
371 } | 361 } |
372 | 362 |
373 private: | 363 private: |
374 virtual int Consume(const char* data, int size) OVERRIDE { | 364 virtual int Consume(const char* data, int size) OVERRIDE { |
375 CHECK(CalledOnValidThread()); | 365 CHECK(CalledOnValidThread()); |
376 if (!selected_socket_.empty()) { | 366 if (!selected_socket_.empty()) { |
377 std::string message(data, size); | 367 std::string message(data, size); |
378 size_t request_end_pos = message.find(kHttpRequestTerminator); | 368 size_t request_end_pos = message.find(kHttpRequestTerminator); |
379 if (request_end_pos != std::string::npos) { | 369 if (request_end_pos != std::string::npos) { |
380 ProcessHTTPRequest(message.substr(0, request_end_pos)); | 370 ProcessHTTPRequest(message.substr(0, request_end_pos)); |
381 return request_end_pos + strlen(kHttpRequestTerminator); | 371 return request_end_pos + strlen(kHttpRequestTerminator); |
382 } | 372 } |
383 return 0; | 373 return 0; |
384 } | 374 } |
385 | |
386 if (size >= kAdbMessageHeaderSize) { | 375 if (size >= kAdbMessageHeaderSize) { |
387 std::string message_header(data, kAdbMessageHeaderSize); | 376 std::string message_header(data, kAdbMessageHeaderSize); |
388 int message_size; | 377 int message_size; |
389 | 378 |
390 EXPECT_TRUE(base::HexStringToInt(message_header, &message_size)); | 379 EXPECT_TRUE(base::HexStringToInt(message_header, &message_size)); |
391 | 380 |
392 if (size >= message_size + kAdbMessageHeaderSize) { | 381 if (size >= message_size + kAdbMessageHeaderSize) { |
393 std::string message_body(data + kAdbMessageHeaderSize, message_size ); | 382 std::string message_body(data + kAdbMessageHeaderSize, message_size); |
394 | |
395 ProcessCommand(message_body); | 383 ProcessCommand(message_body); |
396 | |
397 return kAdbMessageHeaderSize + message_size; | 384 return kAdbMessageHeaderSize + message_size; |
398 } | 385 } |
399 } | 386 } |
400 | |
401 return 0; | 387 return 0; |
402 } | 388 } |
403 | 389 |
404 virtual void Reset() OVERRIDE { | |
405 CHECK(CalledOnValidThread()); | |
406 selected_device_ = std::string(); | |
407 selected_socket_ = std::string(); | |
408 } | |
409 | |
410 void ProcessHTTPRequest(const std::string& request) { | 390 void ProcessHTTPRequest(const std::string& request) { |
411 CHECK(CalledOnValidThread()); | 391 CHECK(CalledOnValidThread()); |
412 std::vector<std::string> tokens; | 392 std::vector<std::string> tokens; |
413 Tokenize(request, " ", &tokens); | 393 Tokenize(request, " ", &tokens); |
414 CHECK_EQ(3U, tokens.size()); | 394 CHECK_EQ(3U, tokens.size()); |
415 CHECK_EQ("GET", tokens[0]); | 395 CHECK_EQ("GET", tokens[0]); |
416 CHECK_EQ("HTTP/1.1", tokens[2]); | 396 CHECK_EQ("HTTP/1.1", tokens[2]); |
417 | 397 |
418 std::string path(tokens[1]); | 398 std::string path(tokens[1]); |
419 | |
420 if (path == kJsonPath) | 399 if (path == kJsonPath) |
421 path = kJsonListPath; | 400 path = kJsonListPath; |
422 | 401 |
423 if (selected_socket_ == "chrome_devtools_remote") { | 402 if (selected_socket_ == "chrome_devtools_remote") { |
424 if (path == kJsonVersionPath) | 403 if (path == kJsonVersionPath) |
425 SendHTTPResponse(kSampleChromeVersion); | 404 SendHTTPResponse(kSampleChromeVersion); |
426 else if (path == kJsonListPath) | 405 else if (path == kJsonListPath) |
427 SendHTTPResponse(kSampleChromePages); | 406 SendHTTPResponse(kSampleChromePages); |
428 else | 407 else |
429 NOTREACHED() << "Unknown command " << request; | 408 NOTREACHED() << "Unknown command " << request; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
473 } else if (command == kListProcessesCommand) { | 452 } else if (command == kListProcessesCommand) { |
474 SendResponse(kSampleListProcesses); | 453 SendResponse(kSampleListProcesses); |
475 } else if (command.find(kLocalAbstractPrefix) == 0) { | 454 } else if (command.find(kLocalAbstractPrefix) == 0) { |
476 selected_socket_ = command.substr(strlen(kLocalAbstractPrefix)); | 455 selected_socket_ = command.substr(strlen(kLocalAbstractPrefix)); |
477 SendResponse(""); | 456 SendResponse(""); |
478 } else { | 457 } else { |
479 NOTREACHED() << "Unknown command - " << command; | 458 NOTREACHED() << "Unknown command - " << command; |
480 } | 459 } |
481 } | 460 } |
482 | 461 |
483 void SendResponse(const std::string& response) { Send("OKAY", response); } | 462 void SendResponse(const std::string& response) { |
| 463 Send("OKAY", response); |
| 464 } |
484 | 465 |
485 void Send(const std::string& status, const std::string& response) { | 466 void Send(const std::string& status, const std::string& response) { |
486 CHECK(CalledOnValidThread()); | 467 CHECK(CalledOnValidThread()); |
487 CHECK_EQ(4U, status.size()); | 468 CHECK_EQ(4U, status.size()); |
488 | 469 |
489 std::stringstream response_stream; | 470 std::stringstream response_stream; |
490 response_stream << status; | 471 response_stream << status; |
491 | 472 |
492 int size = response.size(); | 473 int size = response.size(); |
493 if (size > 0) { | 474 if (size > 0) { |
494 static const char kHexChars[] = "0123456789ABCDEF"; | 475 static const char kHexChars[] = "0123456789ABCDEF"; |
495 for (int i = 3; i >= 0; i--) | 476 for (int i = 3; i >= 0; i--) |
496 response_stream << kHexChars[ (size >> 4*i) & 0x0f ]; | 477 response_stream << kHexChars[ (size >> 4*i) & 0x0f ]; |
497 response_stream << response; | 478 response_stream << response; |
498 } | 479 } |
499 | 480 callback_.Run(response_stream.str()); |
500 server_->Send(response_stream.str()); | |
501 } | 481 } |
502 | 482 |
503 void SendHTTPResponse(const std::string& body) { | 483 void SendHTTPResponse(const std::string& body) { |
504 CHECK(CalledOnValidThread()); | 484 CHECK(CalledOnValidThread()); |
505 std::string response_data(base::StringPrintf(kHttpResponse, | 485 std::string response_data(base::StringPrintf(kHttpResponse, |
506 static_cast<int>(body.size()), | 486 static_cast<int>(body.size()), |
507 body.c_str())); | 487 body.c_str())); |
508 server_->Send(response_data); | 488 callback_.Run(response_data); |
509 } | 489 } |
510 | 490 |
511 std::string selected_device_; | 491 std::string selected_device_; |
512 std::string selected_socket_; | 492 std::string selected_socket_; |
513 | 493 SimpleHttpServer::SendCallback callback_; |
514 scoped_ptr<SingleConnectionServer> server_; | |
515 }; | 494 }; |
516 | 495 |
517 static MockAdbServer* mock_adb_server_ = NULL; | 496 static SimpleHttpServer* mock_adb_server_ = NULL; |
518 | 497 |
519 void StartMockAdbServerOnIOThread() { | 498 void StartMockAdbServerOnIOThread() { |
520 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 499 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
521 CHECK(mock_adb_server_ == NULL); | 500 CHECK(mock_adb_server_ == NULL); |
522 mock_adb_server_ = new MockAdbServer(); | 501 net::IPAddressNumber address; |
| 502 net::ParseIPLiteralToNumber("127.0.0.1", &address); |
| 503 net::IPEndPoint endpoint(address, kAdbPort); |
| 504 mock_adb_server_ = |
| 505 new SimpleHttpServer(base::Bind(&AdbParser::Create), endpoint); |
523 } | 506 } |
524 | 507 |
525 void StopMockAdbServerOnIOThread() { | 508 void StopMockAdbServerOnIOThread() { |
526 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 509 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
527 CHECK(mock_adb_server_ != NULL); | 510 CHECK(mock_adb_server_ != NULL); |
528 delete mock_adb_server_; | 511 delete mock_adb_server_; |
529 mock_adb_server_ = NULL; | 512 mock_adb_server_ = NULL; |
530 } | 513 } |
531 | 514 |
532 } // namespace | 515 } // namespace |
533 | 516 |
534 void StartMockAdbServer() { | 517 void StartMockAdbServer() { |
535 BrowserThread::PostTaskAndReply( | 518 BrowserThread::PostTaskAndReply( |
536 BrowserThread::IO, | 519 BrowserThread::IO, |
537 FROM_HERE, | 520 FROM_HERE, |
538 base::Bind(&StartMockAdbServerOnIOThread), | 521 base::Bind(&StartMockAdbServerOnIOThread), |
539 base::MessageLoop::QuitClosure()); | 522 base::MessageLoop::QuitClosure()); |
540 content::RunMessageLoop(); | 523 content::RunMessageLoop(); |
541 } | 524 } |
542 | 525 |
543 void StopMockAdbServer() { | 526 void StopMockAdbServer() { |
544 BrowserThread::PostTaskAndReply( | 527 BrowserThread::PostTaskAndReply( |
545 BrowserThread::IO, | 528 BrowserThread::IO, |
546 FROM_HERE, | 529 FROM_HERE, |
547 base::Bind(&StopMockAdbServerOnIOThread), | 530 base::Bind(&StopMockAdbServerOnIOThread), |
548 base::MessageLoop::QuitClosure()); | 531 base::MessageLoop::QuitClosure()); |
549 content::RunMessageLoop(); | 532 content::RunMessageLoop(); |
550 } | 533 } |
551 | 534 |
OLD | NEW |