| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/sequence_checker.h" |
| 15 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
| 16 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/strings/string_split.h" | 18 #include "base/strings/string_split.h" |
| 18 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
| 19 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
| 20 #include "base/threading/non_thread_safe.h" | |
| 21 #include "base/threading/thread_task_runner_handle.h" | 21 #include "base/threading/thread_task_runner_handle.h" |
| 22 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
| 23 #include "content/public/test/browser_test.h" | 23 #include "content/public/test/browser_test.h" |
| 24 #include "content/public/test/test_utils.h" | 24 #include "content/public/test/test_utils.h" |
| 25 #include "net/base/io_buffer.h" | 25 #include "net/base/io_buffer.h" |
| 26 #include "net/base/ip_address.h" | 26 #include "net/base/ip_address.h" |
| 27 #include "net/base/ip_endpoint.h" | 27 #include "net/base/ip_endpoint.h" |
| 28 #include "net/base/net_errors.h" | 28 #include "net/base/net_errors.h" |
| 29 #include "net/log/net_log_source.h" | 29 #include "net/log/net_log_source.h" |
| 30 #include "net/socket/stream_socket.h" | 30 #include "net/socket/stream_socket.h" |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 " \"url\": \"about:blank\",\n" | 174 " \"url\": \"about:blank\",\n" |
| 175 " \"webSocketDebuggerUrl\": \"ws:///devtools/page/" | 175 " \"webSocketDebuggerUrl\": \"ws:///devtools/page/" |
| 176 "44681551-ADFD-2411-076B-3AB14C1C60E2\"\n" | 176 "44681551-ADFD-2411-076B-3AB14C1C60E2\"\n" |
| 177 "}]"; | 177 "}]"; |
| 178 | 178 |
| 179 static const int kBufferSize = 16*1024; | 179 static const int kBufferSize = 16*1024; |
| 180 static const uint16_t kAdbPort = 5037; | 180 static const uint16_t kAdbPort = 5037; |
| 181 | 181 |
| 182 static const int kAdbMessageHeaderSize = 4; | 182 static const int kAdbMessageHeaderSize = 4; |
| 183 | 183 |
| 184 class SimpleHttpServer : base::NonThreadSafe { | 184 class SimpleHttpServer { |
| 185 public: | 185 public: |
| 186 class Parser { | 186 class Parser { |
| 187 public: | 187 public: |
| 188 virtual int Consume(const char* data, int size) = 0; | 188 virtual int Consume(const char* data, int size) = 0; |
| 189 virtual ~Parser() {} | 189 virtual ~Parser() {} |
| 190 }; | 190 }; |
| 191 | 191 |
| 192 using SendCallback = base::Callback<void(const std::string&)>; | 192 using SendCallback = base::Callback<void(const std::string&)>; |
| 193 using ParserFactory = base::Callback<Parser*(const SendCallback&)>; | 193 using ParserFactory = base::Callback<Parser*(const SendCallback&)>; |
| 194 | 194 |
| 195 SimpleHttpServer(const ParserFactory& factory, net::IPEndPoint endpoint); | 195 SimpleHttpServer(const ParserFactory& factory, net::IPEndPoint endpoint); |
| 196 virtual ~SimpleHttpServer(); | 196 virtual ~SimpleHttpServer(); |
| 197 | 197 |
| 198 private: | 198 private: |
| 199 class Connection : base::NonThreadSafe { | 199 class Connection { |
| 200 public: | 200 public: |
| 201 Connection(net::StreamSocket* socket, const ParserFactory& factory); | 201 Connection(net::StreamSocket* socket, const ParserFactory& factory); |
| 202 virtual ~Connection(); | 202 virtual ~Connection(); |
| 203 | 203 |
| 204 private: | 204 private: |
| 205 void Send(const std::string& message); | 205 void Send(const std::string& message); |
| 206 void ReadData(); | 206 void ReadData(); |
| 207 void OnDataRead(int count); | 207 void OnDataRead(int count); |
| 208 void WriteData(); | 208 void WriteData(); |
| 209 void OnDataWritten(int count); | 209 void OnDataWritten(int count); |
| 210 | 210 |
| 211 std::unique_ptr<net::StreamSocket> socket_; | 211 std::unique_ptr<net::StreamSocket> socket_; |
| 212 std::unique_ptr<Parser> parser_; | 212 std::unique_ptr<Parser> parser_; |
| 213 scoped_refptr<net::GrowableIOBuffer> input_buffer_; | 213 scoped_refptr<net::GrowableIOBuffer> input_buffer_; |
| 214 scoped_refptr<net::GrowableIOBuffer> output_buffer_; | 214 scoped_refptr<net::GrowableIOBuffer> output_buffer_; |
| 215 int bytes_to_write_; | 215 int bytes_to_write_; |
| 216 bool read_closed_; | 216 bool read_closed_; |
| 217 |
| 218 SEQUENCE_CHECKER(sequence_checker_); |
| 219 |
| 217 base::WeakPtrFactory<Connection> weak_factory_; | 220 base::WeakPtrFactory<Connection> weak_factory_; |
| 218 | 221 |
| 219 DISALLOW_COPY_AND_ASSIGN(Connection); | 222 DISALLOW_COPY_AND_ASSIGN(Connection); |
| 220 }; | 223 }; |
| 221 | 224 |
| 222 void OnConnect(); | 225 void OnConnect(); |
| 223 void OnAccepted(int result); | 226 void OnAccepted(int result); |
| 224 | 227 |
| 225 ParserFactory factory_; | 228 ParserFactory factory_; |
| 226 std::unique_ptr<net::TCPServerSocket> socket_; | 229 std::unique_ptr<net::TCPServerSocket> socket_; |
| 227 std::unique_ptr<net::StreamSocket> client_socket_; | 230 std::unique_ptr<net::StreamSocket> client_socket_; |
| 231 |
| 232 SEQUENCE_CHECKER(sequence_checker_); |
| 233 |
| 228 base::WeakPtrFactory<SimpleHttpServer> weak_factory_; | 234 base::WeakPtrFactory<SimpleHttpServer> weak_factory_; |
| 229 | 235 |
| 230 DISALLOW_COPY_AND_ASSIGN(SimpleHttpServer); | 236 DISALLOW_COPY_AND_ASSIGN(SimpleHttpServer); |
| 231 }; | 237 }; |
| 232 | 238 |
| 233 SimpleHttpServer::SimpleHttpServer(const ParserFactory& factory, | 239 SimpleHttpServer::SimpleHttpServer(const ParserFactory& factory, |
| 234 net::IPEndPoint endpoint) | 240 net::IPEndPoint endpoint) |
| 235 : factory_(factory), | 241 : factory_(factory), |
| 236 socket_(new net::TCPServerSocket(nullptr, net::NetLogSource())), | 242 socket_(new net::TCPServerSocket(nullptr, net::NetLogSource())), |
| 237 weak_factory_(this) { | 243 weak_factory_(this) { |
| 238 socket_->Listen(endpoint, 5); | 244 socket_->Listen(endpoint, 5); |
| 239 OnConnect(); | 245 OnConnect(); |
| 240 } | 246 } |
| 241 | 247 |
| 242 SimpleHttpServer::~SimpleHttpServer() { | 248 SimpleHttpServer::~SimpleHttpServer() { |
| 249 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 243 } | 250 } |
| 244 | 251 |
| 245 SimpleHttpServer::Connection::Connection(net::StreamSocket* socket, | 252 SimpleHttpServer::Connection::Connection(net::StreamSocket* socket, |
| 246 const ParserFactory& factory) | 253 const ParserFactory& factory) |
| 247 : socket_(socket), | 254 : socket_(socket), |
| 248 parser_(factory.Run(base::Bind(&Connection::Send, | 255 parser_(factory.Run(base::Bind(&Connection::Send, |
| 249 base::Unretained(this)))), | 256 base::Unretained(this)))), |
| 250 input_buffer_(new net::GrowableIOBuffer()), | 257 input_buffer_(new net::GrowableIOBuffer()), |
| 251 output_buffer_(new net::GrowableIOBuffer()), | 258 output_buffer_(new net::GrowableIOBuffer()), |
| 252 bytes_to_write_(0), | 259 bytes_to_write_(0), |
| 253 read_closed_(false), | 260 read_closed_(false), |
| 254 weak_factory_(this) { | 261 weak_factory_(this) { |
| 255 input_buffer_->SetCapacity(kBufferSize); | 262 input_buffer_->SetCapacity(kBufferSize); |
| 256 ReadData(); | 263 ReadData(); |
| 257 } | 264 } |
| 258 | 265 |
| 259 SimpleHttpServer::Connection::~Connection() { | 266 SimpleHttpServer::Connection::~Connection() { |
| 267 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 260 } | 268 } |
| 261 | 269 |
| 262 void SimpleHttpServer::Connection::Send(const std::string& message) { | 270 void SimpleHttpServer::Connection::Send(const std::string& message) { |
| 263 CHECK(CalledOnValidThread()); | 271 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 264 const char* data = message.c_str(); | 272 const char* data = message.c_str(); |
| 265 int size = message.size(); | 273 int size = message.size(); |
| 266 | 274 |
| 267 if ((output_buffer_->offset() + bytes_to_write_ + size) > | 275 if ((output_buffer_->offset() + bytes_to_write_ + size) > |
| 268 output_buffer_->capacity()) { | 276 output_buffer_->capacity()) { |
| 269 // If not enough space without relocation | 277 // If not enough space without relocation |
| 270 if (output_buffer_->capacity() < (bytes_to_write_ + size)) { | 278 if (output_buffer_->capacity() < (bytes_to_write_ + size)) { |
| 271 // If even buffer is not enough | 279 // If even buffer is not enough |
| 272 int new_size = std::max(output_buffer_->capacity() * 2, size * 2); | 280 int new_size = std::max(output_buffer_->capacity() * 2, size * 2); |
| 273 output_buffer_->SetCapacity(new_size); | 281 output_buffer_->SetCapacity(new_size); |
| 274 } | 282 } |
| 275 memmove(output_buffer_->StartOfBuffer(), | 283 memmove(output_buffer_->StartOfBuffer(), |
| 276 output_buffer_->data(), | 284 output_buffer_->data(), |
| 277 bytes_to_write_); | 285 bytes_to_write_); |
| 278 output_buffer_->set_offset(0); | 286 output_buffer_->set_offset(0); |
| 279 } | 287 } |
| 280 | 288 |
| 281 memcpy(output_buffer_->data() + bytes_to_write_, data, size); | 289 memcpy(output_buffer_->data() + bytes_to_write_, data, size); |
| 282 bytes_to_write_ += size; | 290 bytes_to_write_ += size; |
| 283 | 291 |
| 284 if (bytes_to_write_ == size) | 292 if (bytes_to_write_ == size) |
| 285 // If write loop wasn't yet started, then start it | 293 // If write loop wasn't yet started, then start it |
| 286 WriteData(); | 294 WriteData(); |
| 287 } | 295 } |
| 288 | 296 |
| 289 void SimpleHttpServer::Connection::ReadData() { | 297 void SimpleHttpServer::Connection::ReadData() { |
| 290 CHECK(CalledOnValidThread()); | 298 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 291 | 299 |
| 292 if (input_buffer_->RemainingCapacity() == 0) | 300 if (input_buffer_->RemainingCapacity() == 0) |
| 293 input_buffer_->SetCapacity(input_buffer_->capacity() * 2); | 301 input_buffer_->SetCapacity(input_buffer_->capacity() * 2); |
| 294 | 302 |
| 295 int read_result = socket_->Read( | 303 int read_result = socket_->Read( |
| 296 input_buffer_.get(), | 304 input_buffer_.get(), |
| 297 input_buffer_->RemainingCapacity(), | 305 input_buffer_->RemainingCapacity(), |
| 298 base::Bind(&Connection::OnDataRead, base::Unretained(this))); | 306 base::Bind(&Connection::OnDataRead, base::Unretained(this))); |
| 299 | 307 |
| 300 if (read_result != net::ERR_IO_PENDING) | 308 if (read_result != net::ERR_IO_PENDING) |
| 301 OnDataRead(read_result); | 309 OnDataRead(read_result); |
| 302 } | 310 } |
| 303 | 311 |
| 304 void SimpleHttpServer::Connection::OnDataRead(int count) { | 312 void SimpleHttpServer::Connection::OnDataRead(int count) { |
| 305 CHECK(CalledOnValidThread()); | 313 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 306 if (count <= 0) { | 314 if (count <= 0) { |
| 307 if (bytes_to_write_ == 0) | 315 if (bytes_to_write_ == 0) |
| 308 delete this; | 316 delete this; |
| 309 else | 317 else |
| 310 read_closed_ = true; | 318 read_closed_ = true; |
| 311 return; | 319 return; |
| 312 } | 320 } |
| 313 input_buffer_->set_offset(input_buffer_->offset() + count); | 321 input_buffer_->set_offset(input_buffer_->offset() + count); |
| 314 int bytes_processed; | 322 int bytes_processed; |
| 315 | 323 |
| 316 do { | 324 do { |
| 317 char* data = input_buffer_->StartOfBuffer(); | 325 char* data = input_buffer_->StartOfBuffer(); |
| 318 int data_size = input_buffer_->offset(); | 326 int data_size = input_buffer_->offset(); |
| 319 bytes_processed = parser_->Consume(data, data_size); | 327 bytes_processed = parser_->Consume(data, data_size); |
| 320 | 328 |
| 321 if (bytes_processed) { | 329 if (bytes_processed) { |
| 322 memmove(data, data + bytes_processed, data_size - bytes_processed); | 330 memmove(data, data + bytes_processed, data_size - bytes_processed); |
| 323 input_buffer_->set_offset(data_size - bytes_processed); | 331 input_buffer_->set_offset(data_size - bytes_processed); |
| 324 } | 332 } |
| 325 } while (bytes_processed); | 333 } while (bytes_processed); |
| 326 // Posting to avoid deep recursion in case of synchronous IO | 334 // Posting to avoid deep recursion in case of synchronous IO |
| 327 base::ThreadTaskRunnerHandle::Get()->PostTask( | 335 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 328 FROM_HERE, | 336 FROM_HERE, |
| 329 base::BindOnce(&Connection::ReadData, weak_factory_.GetWeakPtr())); | 337 base::BindOnce(&Connection::ReadData, weak_factory_.GetWeakPtr())); |
| 330 } | 338 } |
| 331 | 339 |
| 332 void SimpleHttpServer::Connection::WriteData() { | 340 void SimpleHttpServer::Connection::WriteData() { |
| 333 CHECK(CalledOnValidThread()); | 341 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 334 CHECK_GE(output_buffer_->capacity(), | 342 CHECK_GE(output_buffer_->capacity(), |
| 335 output_buffer_->offset() + bytes_to_write_) << "Overflow"; | 343 output_buffer_->offset() + bytes_to_write_) << "Overflow"; |
| 336 | 344 |
| 337 int write_result = socket_->Write( | 345 int write_result = socket_->Write( |
| 338 output_buffer_.get(), | 346 output_buffer_.get(), |
| 339 bytes_to_write_, | 347 bytes_to_write_, |
| 340 base::Bind(&Connection::OnDataWritten, base::Unretained(this))); | 348 base::Bind(&Connection::OnDataWritten, base::Unretained(this))); |
| 341 | 349 |
| 342 if (write_result != net::ERR_IO_PENDING) | 350 if (write_result != net::ERR_IO_PENDING) |
| 343 OnDataWritten(write_result); | 351 OnDataWritten(write_result); |
| 344 } | 352 } |
| 345 | 353 |
| 346 void SimpleHttpServer::Connection::OnDataWritten(int count) { | 354 void SimpleHttpServer::Connection::OnDataWritten(int count) { |
| 347 CHECK(CalledOnValidThread()); | 355 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 348 if (count < 0) { | 356 if (count < 0) { |
| 349 delete this; | 357 delete this; |
| 350 return; | 358 return; |
| 351 } | 359 } |
| 352 CHECK_GT(count, 0); | 360 CHECK_GT(count, 0); |
| 353 CHECK_GE(output_buffer_->capacity(), | 361 CHECK_GE(output_buffer_->capacity(), |
| 354 output_buffer_->offset() + bytes_to_write_) << "Overflow"; | 362 output_buffer_->offset() + bytes_to_write_) << "Overflow"; |
| 355 | 363 |
| 356 bytes_to_write_ -= count; | 364 bytes_to_write_ -= count; |
| 357 output_buffer_->set_offset(output_buffer_->offset() + count); | 365 output_buffer_->set_offset(output_buffer_->offset() + count); |
| 358 | 366 |
| 359 if (bytes_to_write_ != 0) | 367 if (bytes_to_write_ != 0) |
| 360 // Posting to avoid deep recursion in case of synchronous IO | 368 // Posting to avoid deep recursion in case of synchronous IO |
| 361 base::ThreadTaskRunnerHandle::Get()->PostTask( | 369 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 362 FROM_HERE, | 370 FROM_HERE, |
| 363 base::BindOnce(&Connection::WriteData, weak_factory_.GetWeakPtr())); | 371 base::BindOnce(&Connection::WriteData, weak_factory_.GetWeakPtr())); |
| 364 else if (read_closed_) | 372 else if (read_closed_) |
| 365 delete this; | 373 delete this; |
| 366 } | 374 } |
| 367 | 375 |
| 368 void SimpleHttpServer::OnConnect() { | 376 void SimpleHttpServer::OnConnect() { |
| 369 CHECK(CalledOnValidThread()); | 377 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 370 | 378 |
| 371 int accept_result = socket_->Accept(&client_socket_, | 379 int accept_result = socket_->Accept(&client_socket_, |
| 372 base::Bind(&SimpleHttpServer::OnAccepted, base::Unretained(this))); | 380 base::Bind(&SimpleHttpServer::OnAccepted, base::Unretained(this))); |
| 373 | 381 |
| 374 if (accept_result != net::ERR_IO_PENDING) | 382 if (accept_result != net::ERR_IO_PENDING) |
| 375 base::ThreadTaskRunnerHandle::Get()->PostTask( | 383 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 376 FROM_HERE, base::BindOnce(&SimpleHttpServer::OnAccepted, | 384 FROM_HERE, base::BindOnce(&SimpleHttpServer::OnAccepted, |
| 377 weak_factory_.GetWeakPtr(), accept_result)); | 385 weak_factory_.GetWeakPtr(), accept_result)); |
| 378 } | 386 } |
| 379 | 387 |
| 380 void SimpleHttpServer::OnAccepted(int result) { | 388 void SimpleHttpServer::OnAccepted(int result) { |
| 381 CHECK(CalledOnValidThread()); | 389 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 382 ASSERT_EQ(result, 0); // Fails if the socket is already in use. | 390 ASSERT_EQ(result, 0); // Fails if the socket is already in use. |
| 383 new Connection(client_socket_.release(), factory_); | 391 new Connection(client_socket_.release(), factory_); |
| 384 OnConnect(); | 392 OnConnect(); |
| 385 } | 393 } |
| 386 | 394 |
| 387 class AdbParser : public SimpleHttpServer::Parser, | 395 class AdbParser : public SimpleHttpServer::Parser, |
| 388 public base::NonThreadSafe, | |
| 389 public MockAndroidConnection::Delegate { | 396 public MockAndroidConnection::Delegate { |
| 390 public: | 397 public: |
| 391 static Parser* Create(FlushMode flush_mode, | 398 static Parser* Create(FlushMode flush_mode, |
| 392 const SimpleHttpServer::SendCallback& callback) { | 399 const SimpleHttpServer::SendCallback& callback) { |
| 393 return new AdbParser(flush_mode, callback); | 400 return new AdbParser(flush_mode, callback); |
| 394 } | 401 } |
| 395 | 402 |
| 396 ~AdbParser() override {} | 403 ~AdbParser() override { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); } |
| 404 |
| 397 private: | 405 private: |
| 398 explicit AdbParser(FlushMode flush_mode, | 406 explicit AdbParser(FlushMode flush_mode, |
| 399 const SimpleHttpServer::SendCallback& callback) | 407 const SimpleHttpServer::SendCallback& callback) |
| 400 : flush_mode_(flush_mode), | 408 : flush_mode_(flush_mode), |
| 401 callback_(callback) { | 409 callback_(callback) { |
| 402 } | 410 } |
| 403 | 411 |
| 404 int Consume(const char* data, int size) override { | 412 int Consume(const char* data, int size) override { |
| 405 CHECK(CalledOnValidThread()); | 413 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 406 if (mock_connection_) { | 414 if (mock_connection_) { |
| 407 mock_connection_->Receive(std::string(data, size)); | 415 mock_connection_->Receive(std::string(data, size)); |
| 408 return size; | 416 return size; |
| 409 } | 417 } |
| 410 if (size >= kAdbMessageHeaderSize) { | 418 if (size >= kAdbMessageHeaderSize) { |
| 411 std::string message_header(data, kAdbMessageHeaderSize); | 419 std::string message_header(data, kAdbMessageHeaderSize); |
| 412 int message_size; | 420 int message_size; |
| 413 | 421 |
| 414 EXPECT_TRUE(base::HexStringToInt(message_header, &message_size)); | 422 EXPECT_TRUE(base::HexStringToInt(message_header, &message_size)); |
| 415 | 423 |
| 416 if (size >= message_size + kAdbMessageHeaderSize) { | 424 if (size >= message_size + kAdbMessageHeaderSize) { |
| 417 std::string message_body(data + kAdbMessageHeaderSize, message_size); | 425 std::string message_body(data + kAdbMessageHeaderSize, message_size); |
| 418 ProcessCommand(message_body); | 426 ProcessCommand(message_body); |
| 419 return kAdbMessageHeaderSize + message_size; | 427 return kAdbMessageHeaderSize + message_size; |
| 420 } | 428 } |
| 421 } | 429 } |
| 422 return 0; | 430 return 0; |
| 423 } | 431 } |
| 424 | 432 |
| 425 void ProcessCommand(const std::string& command) { | 433 void ProcessCommand(const std::string& command) { |
| 426 CHECK(CalledOnValidThread()); | 434 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 427 if (command == "host:devices") { | 435 if (command == "host:devices") { |
| 428 SendSuccess(base::StringPrintf("%s\tdevice\n%s\toffline", | 436 SendSuccess(base::StringPrintf("%s\tdevice\n%s\toffline", |
| 429 kSerialOnline, | 437 kSerialOnline, |
| 430 kSerialOffline)); | 438 kSerialOffline)); |
| 431 } else if (base::StartsWith(command, kHostTransportPrefix, | 439 } else if (base::StartsWith(command, kHostTransportPrefix, |
| 432 base::CompareCase::SENSITIVE)) { | 440 base::CompareCase::SENSITIVE)) { |
| 433 serial_ = command.substr(sizeof(kHostTransportPrefix) - 1); | 441 serial_ = command.substr(sizeof(kHostTransportPrefix) - 1); |
| 434 SendSuccess(std::string()); | 442 SendSuccess(std::string()); |
| 435 } else if (serial_ != kSerialOnline) { | 443 } else if (serial_ != kSerialOnline) { |
| 436 Send("FAIL", "device offline (x)"); | 444 Send("FAIL", "device offline (x)"); |
| 437 } else { | 445 } else { |
| 438 mock_connection_ = | 446 mock_connection_ = |
| 439 base::MakeUnique<MockAndroidConnection>(this, serial_, command); | 447 base::MakeUnique<MockAndroidConnection>(this, serial_, command); |
| 440 } | 448 } |
| 441 } | 449 } |
| 442 | 450 |
| 443 void SendSuccess(const std::string& response) override { | 451 void SendSuccess(const std::string& response) override { |
| 444 Send("OKAY", response); | 452 Send("OKAY", response); |
| 445 } | 453 } |
| 446 | 454 |
| 447 void SendRaw(const std::string& data) override { | 455 void SendRaw(const std::string& data) override { |
| 448 callback_.Run(data); | 456 callback_.Run(data); |
| 449 } | 457 } |
| 450 | 458 |
| 451 void Send(const std::string& status, const std::string& response) { | 459 void Send(const std::string& status, const std::string& response) { |
| 452 CHECK(CalledOnValidThread()); | 460 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 453 CHECK_EQ(4U, status.size()); | 461 CHECK_EQ(4U, status.size()); |
| 454 std::string buffer = status; | 462 std::string buffer = status; |
| 455 if (flush_mode_ == FlushWithoutSize) { | 463 if (flush_mode_ == FlushWithoutSize) { |
| 456 callback_.Run(buffer); | 464 callback_.Run(buffer); |
| 457 buffer = std::string(); | 465 buffer = std::string(); |
| 458 } | 466 } |
| 459 | 467 |
| 460 int size = response.size(); | 468 int size = response.size(); |
| 461 if (size > 0) { | 469 if (size > 0) { |
| 462 static const char kHexChars[] = "0123456789ABCDEF"; | 470 static const char kHexChars[] = "0123456789ABCDEF"; |
| 463 for (int i = 3; i >= 0; i--) | 471 for (int i = 3; i >= 0; i--) |
| 464 buffer += kHexChars[ (size >> 4*i) & 0x0f ]; | 472 buffer += kHexChars[ (size >> 4*i) & 0x0f ]; |
| 465 if (flush_mode_ == FlushWithSize) { | 473 if (flush_mode_ == FlushWithSize) { |
| 466 callback_.Run(buffer); | 474 callback_.Run(buffer); |
| 467 buffer = std::string(); | 475 buffer = std::string(); |
| 468 } | 476 } |
| 469 buffer += response; | 477 buffer += response; |
| 470 callback_.Run(buffer); | 478 callback_.Run(buffer); |
| 471 } else if (flush_mode_ != FlushWithoutSize) { | 479 } else if (flush_mode_ != FlushWithoutSize) { |
| 472 callback_.Run(buffer); | 480 callback_.Run(buffer); |
| 473 } | 481 } |
| 474 } | 482 } |
| 475 | 483 |
| 476 FlushMode flush_mode_; | 484 FlushMode flush_mode_; |
| 477 SimpleHttpServer::SendCallback callback_; | 485 SimpleHttpServer::SendCallback callback_; |
| 478 std::string serial_; | 486 std::string serial_; |
| 479 std::unique_ptr<MockAndroidConnection> mock_connection_; | 487 std::unique_ptr<MockAndroidConnection> mock_connection_; |
| 488 |
| 489 SEQUENCE_CHECKER(sequence_checker_); |
| 480 }; | 490 }; |
| 481 | 491 |
| 482 static SimpleHttpServer* mock_adb_server_ = NULL; | 492 static SimpleHttpServer* mock_adb_server_ = NULL; |
| 483 | 493 |
| 484 void StartMockAdbServerOnIOThread(FlushMode flush_mode) { | 494 void StartMockAdbServerOnIOThread(FlushMode flush_mode) { |
| 485 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 495 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 486 CHECK(mock_adb_server_ == NULL); | 496 CHECK(mock_adb_server_ == NULL); |
| 487 net::IPEndPoint endpoint(net::IPAddress(127, 0, 0, 1), kAdbPort); | 497 net::IPEndPoint endpoint(net::IPAddress(127, 0, 0, 1), kAdbPort); |
| 488 mock_adb_server_ = new SimpleHttpServer( | 498 mock_adb_server_ = new SimpleHttpServer( |
| 489 base::Bind(&AdbParser::Create, flush_mode), endpoint); | 499 base::Bind(&AdbParser::Create, flush_mode), endpoint); |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 615 base::MessageLoop::QuitWhenIdleClosure()); | 625 base::MessageLoop::QuitWhenIdleClosure()); |
| 616 content::RunMessageLoop(); | 626 content::RunMessageLoop(); |
| 617 } | 627 } |
| 618 | 628 |
| 619 void StopMockAdbServer() { | 629 void StopMockAdbServer() { |
| 620 BrowserThread::PostTaskAndReply(BrowserThread::IO, FROM_HERE, | 630 BrowserThread::PostTaskAndReply(BrowserThread::IO, FROM_HERE, |
| 621 base::BindOnce(&StopMockAdbServerOnIOThread), | 631 base::BindOnce(&StopMockAdbServerOnIOThread), |
| 622 base::MessageLoop::QuitWhenIdleClosure()); | 632 base::MessageLoop::QuitWhenIdleClosure()); |
| 623 content::RunMessageLoop(); | 633 content::RunMessageLoop(); |
| 624 } | 634 } |
| 625 | |
| OLD | NEW |