| 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 "chrome/browser/devtools/device/adb/mock_adb_server.h" | 5 #include "chrome/browser/devtools/device/adb/mock_adb_server.h" |
| 6 | 6 |
| 7 #include "base/location.h" |
| 7 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
| 9 #include "base/single_thread_task_runner.h" |
| 8 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 10 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 13 #include "base/thread_task_runner_handle.h" |
| 11 #include "base/threading/non_thread_safe.h" | 14 #include "base/threading/non_thread_safe.h" |
| 12 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/test/browser_test.h" | 16 #include "content/public/test/browser_test.h" |
| 14 #include "content/public/test/test_utils.h" | 17 #include "content/public/test/test_utils.h" |
| 15 #include "net/base/io_buffer.h" | 18 #include "net/base/io_buffer.h" |
| 16 #include "net/base/ip_endpoint.h" | 19 #include "net/base/ip_endpoint.h" |
| 17 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
| 18 #include "net/socket/stream_socket.h" | 21 #include "net/socket/stream_socket.h" |
| 19 #include "net/socket/tcp_server_socket.h" | 22 #include "net/socket/tcp_server_socket.h" |
| 20 | 23 |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 char* data = input_buffer_->StartOfBuffer(); | 310 char* data = input_buffer_->StartOfBuffer(); |
| 308 int data_size = input_buffer_->offset(); | 311 int data_size = input_buffer_->offset(); |
| 309 bytes_processed = parser_->Consume(data, data_size); | 312 bytes_processed = parser_->Consume(data, data_size); |
| 310 | 313 |
| 311 if (bytes_processed) { | 314 if (bytes_processed) { |
| 312 memmove(data, data + bytes_processed, data_size - bytes_processed); | 315 memmove(data, data + bytes_processed, data_size - bytes_processed); |
| 313 input_buffer_->set_offset(data_size - bytes_processed); | 316 input_buffer_->set_offset(data_size - bytes_processed); |
| 314 } | 317 } |
| 315 } while (bytes_processed); | 318 } while (bytes_processed); |
| 316 // Posting to avoid deep recursion in case of synchronous IO | 319 // Posting to avoid deep recursion in case of synchronous IO |
| 317 base::MessageLoop::current()->PostTask( | 320 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 318 FROM_HERE, | 321 FROM_HERE, base::Bind(&Connection::ReadData, weak_factory_.GetWeakPtr())); |
| 319 base::Bind(&Connection::ReadData, weak_factory_.GetWeakPtr())); | |
| 320 } | 322 } |
| 321 | 323 |
| 322 void SimpleHttpServer::Connection::WriteData() { | 324 void SimpleHttpServer::Connection::WriteData() { |
| 323 CHECK(CalledOnValidThread()); | 325 CHECK(CalledOnValidThread()); |
| 324 CHECK_GE(output_buffer_->capacity(), | 326 CHECK_GE(output_buffer_->capacity(), |
| 325 output_buffer_->offset() + bytes_to_write_) << "Overflow"; | 327 output_buffer_->offset() + bytes_to_write_) << "Overflow"; |
| 326 | 328 |
| 327 int write_result = socket_->Write( | 329 int write_result = socket_->Write( |
| 328 output_buffer_.get(), | 330 output_buffer_.get(), |
| 329 bytes_to_write_, | 331 bytes_to_write_, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 341 } | 343 } |
| 342 CHECK_GT(count, 0); | 344 CHECK_GT(count, 0); |
| 343 CHECK_GE(output_buffer_->capacity(), | 345 CHECK_GE(output_buffer_->capacity(), |
| 344 output_buffer_->offset() + bytes_to_write_) << "Overflow"; | 346 output_buffer_->offset() + bytes_to_write_) << "Overflow"; |
| 345 | 347 |
| 346 bytes_to_write_ -= count; | 348 bytes_to_write_ -= count; |
| 347 output_buffer_->set_offset(output_buffer_->offset() + count); | 349 output_buffer_->set_offset(output_buffer_->offset() + count); |
| 348 | 350 |
| 349 if (bytes_to_write_ != 0) | 351 if (bytes_to_write_ != 0) |
| 350 // Posting to avoid deep recursion in case of synchronous IO | 352 // Posting to avoid deep recursion in case of synchronous IO |
| 351 base::MessageLoop::current()->PostTask( | 353 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 352 FROM_HERE, | 354 FROM_HERE, |
| 353 base::Bind(&Connection::WriteData, weak_factory_.GetWeakPtr())); | 355 base::Bind(&Connection::WriteData, weak_factory_.GetWeakPtr())); |
| 354 else if (read_closed_) | 356 else if (read_closed_) |
| 355 delete this; | 357 delete this; |
| 356 } | 358 } |
| 357 | 359 |
| 358 void SimpleHttpServer::AcceptConnection() { | 360 void SimpleHttpServer::AcceptConnection() { |
| 359 CHECK(CalledOnValidThread()); | 361 CHECK(CalledOnValidThread()); |
| 360 | 362 |
| 361 int accept_result = socket_->Accept(&client_socket_, | 363 int accept_result = socket_->Accept(&client_socket_, |
| 362 base::Bind(&SimpleHttpServer::OnAccepted, base::Unretained(this))); | 364 base::Bind(&SimpleHttpServer::OnAccepted, base::Unretained(this))); |
| 363 | 365 |
| 364 if (accept_result != net::ERR_IO_PENDING) | 366 if (accept_result != net::ERR_IO_PENDING) |
| 365 base::MessageLoop::current()->PostTask( | 367 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 366 FROM_HERE, | 368 FROM_HERE, base::Bind(&SimpleHttpServer::OnAccepted, |
| 367 base::Bind(&SimpleHttpServer::OnAccepted, | 369 weak_factory_.GetWeakPtr(), accept_result)); |
| 368 weak_factory_.GetWeakPtr(), | |
| 369 accept_result)); | |
| 370 } | 370 } |
| 371 | 371 |
| 372 void SimpleHttpServer::OnAccepted(int result) { | 372 void SimpleHttpServer::OnAccepted(int result) { |
| 373 CHECK(CalledOnValidThread()); | 373 CHECK(CalledOnValidThread()); |
| 374 ASSERT_EQ(result, 0); // Fails if the socket is already in use. | 374 ASSERT_EQ(result, 0); // Fails if the socket is already in use. |
| 375 new Connection(client_socket_.release(), factory_); | 375 new Connection(client_socket_.release(), factory_); |
| 376 AcceptConnection(); | 376 AcceptConnection(); |
| 377 } | 377 } |
| 378 | 378 |
| 379 class AdbParser : public SimpleHttpServer::Parser, | 379 class AdbParser : public SimpleHttpServer::Parser, |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 595 | 595 |
| 596 void StopMockAdbServer() { | 596 void StopMockAdbServer() { |
| 597 BrowserThread::PostTaskAndReply( | 597 BrowserThread::PostTaskAndReply( |
| 598 BrowserThread::IO, | 598 BrowserThread::IO, |
| 599 FROM_HERE, | 599 FROM_HERE, |
| 600 base::Bind(&StopMockAdbServerOnIOThread), | 600 base::Bind(&StopMockAdbServerOnIOThread), |
| 601 base::MessageLoop::QuitClosure()); | 601 base::MessageLoop::QuitClosure()); |
| 602 content::RunMessageLoop(); | 602 content::RunMessageLoop(); |
| 603 } | 603 } |
| 604 | 604 |
| OLD | NEW |