| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/test/chromedriver/net/adb_client_socket.h" | 5 #include "chrome/test/chromedriver/net/adb_client_socket.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 void OnSocketAvailable(int result, | 134 void OnSocketAvailable(int result, |
| 135 net::StreamSocket* socket) { | 135 net::StreamSocket* socket) { |
| 136 if (!CheckNetResultOrDie(result)) | 136 if (!CheckNetResultOrDie(result)) |
| 137 return; | 137 return; |
| 138 | 138 |
| 139 socket_.reset(socket); | 139 socket_.reset(socket); |
| 140 | 140 |
| 141 scoped_refptr<net::StringIOBuffer> request_buffer = | 141 scoped_refptr<net::StringIOBuffer> request_buffer = |
| 142 new net::StringIOBuffer(request_); | 142 new net::StringIOBuffer(request_); |
| 143 | 143 |
| 144 result = socket_->Write(request_buffer, request_buffer->size(), | 144 result = socket_->Write( |
| 145 request_buffer.get(), |
| 146 request_buffer->size(), |
| 145 base::Bind(&HttpOverAdbSocket::ReadResponse, base::Unretained(this))); | 147 base::Bind(&HttpOverAdbSocket::ReadResponse, base::Unretained(this))); |
| 146 if (result != net::ERR_IO_PENDING) | 148 if (result != net::ERR_IO_PENDING) |
| 147 ReadResponse(result); | 149 ReadResponse(result); |
| 148 } | 150 } |
| 149 | 151 |
| 150 void ReadResponse(int result) { | 152 void ReadResponse(int result) { |
| 151 if (!CheckNetResultOrDie(result)) | 153 if (!CheckNetResultOrDie(result)) |
| 152 return; | 154 return; |
| 153 | 155 |
| 154 scoped_refptr<net::IOBuffer> response_buffer = | 156 scoped_refptr<net::IOBuffer> response_buffer = |
| 155 new net::IOBuffer(kBufferSize); | 157 new net::IOBuffer(kBufferSize); |
| 156 | 158 |
| 157 result = socket_->Read(response_buffer, kBufferSize, | 159 result = socket_->Read(response_buffer.get(), |
| 158 base::Bind(&HttpOverAdbSocket::OnResponseData, base::Unretained(this), | 160 kBufferSize, |
| 159 response_buffer, -1)); | 161 base::Bind(&HttpOverAdbSocket::OnResponseData, |
| 162 base::Unretained(this), |
| 163 response_buffer, |
| 164 -1)); |
| 160 if (result != net::ERR_IO_PENDING) | 165 if (result != net::ERR_IO_PENDING) |
| 161 OnResponseData(response_buffer, -1, result); | 166 OnResponseData(response_buffer, -1, result); |
| 162 } | 167 } |
| 163 | 168 |
| 164 void OnResponseData(scoped_refptr<net::IOBuffer> response_buffer, | 169 void OnResponseData(scoped_refptr<net::IOBuffer> response_buffer, |
| 165 int bytes_total, | 170 int bytes_total, |
| 166 int result) { | 171 int result) { |
| 167 if (!CheckNetResultOrDie(result)) | 172 if (!CheckNetResultOrDie(result)) |
| 168 return; | 173 return; |
| 169 if (result == 0) { | 174 if (result == 0) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 197 | 202 |
| 198 if (bytes_total == static_cast<int>(response_.length())) { | 203 if (bytes_total == static_cast<int>(response_.length())) { |
| 199 if (!command_callback_.is_null()) | 204 if (!command_callback_.is_null()) |
| 200 command_callback_.Run(body_pos_, response_); | 205 command_callback_.Run(body_pos_, response_); |
| 201 else | 206 else |
| 202 socket_callback_.Run(net::OK, socket_.release()); | 207 socket_callback_.Run(net::OK, socket_.release()); |
| 203 delete this; | 208 delete this; |
| 204 return; | 209 return; |
| 205 } | 210 } |
| 206 | 211 |
| 207 result = socket_->Read(response_buffer, kBufferSize, | 212 result = socket_->Read(response_buffer.get(), |
| 208 base::Bind(&HttpOverAdbSocket::OnResponseData, base::Unretained(this), | 213 kBufferSize, |
| 209 response_buffer, bytes_total)); | 214 base::Bind(&HttpOverAdbSocket::OnResponseData, |
| 215 base::Unretained(this), |
| 216 response_buffer, |
| 217 bytes_total)); |
| 210 if (result != net::ERR_IO_PENDING) | 218 if (result != net::ERR_IO_PENDING) |
| 211 OnResponseData(response_buffer, bytes_total, result); | 219 OnResponseData(response_buffer, bytes_total, result); |
| 212 } | 220 } |
| 213 | 221 |
| 214 bool CheckNetResultOrDie(int result) { | 222 bool CheckNetResultOrDie(int result) { |
| 215 if (result >= 0) | 223 if (result >= 0) |
| 216 return true; | 224 return true; |
| 217 if (!command_callback_.is_null()) | 225 if (!command_callback_.is_null()) |
| 218 command_callback_.Run(result, std::string()); | 226 command_callback_.Run(result, std::string()); |
| 219 else | 227 else |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 int result = socket_->Connect(callback); | 377 int result = socket_->Connect(callback); |
| 370 if (result != net::ERR_IO_PENDING) | 378 if (result != net::ERR_IO_PENDING) |
| 371 callback.Run(result); | 379 callback.Run(result); |
| 372 } | 380 } |
| 373 | 381 |
| 374 void AdbClientSocket::SendCommand(const std::string& command, | 382 void AdbClientSocket::SendCommand(const std::string& command, |
| 375 bool is_void, | 383 bool is_void, |
| 376 const CommandCallback& callback) { | 384 const CommandCallback& callback) { |
| 377 scoped_refptr<net::StringIOBuffer> request_buffer = | 385 scoped_refptr<net::StringIOBuffer> request_buffer = |
| 378 new net::StringIOBuffer(EncodeMessage(command)); | 386 new net::StringIOBuffer(EncodeMessage(command)); |
| 379 int result = socket_->Write(request_buffer, request_buffer->size(), | 387 int result = socket_->Write(request_buffer.get(), |
| 380 base::Bind(&AdbClientSocket::ReadResponse, base::Unretained(this), | 388 request_buffer->size(), |
| 381 callback, is_void)); | 389 base::Bind(&AdbClientSocket::ReadResponse, |
| 390 base::Unretained(this), |
| 391 callback, |
| 392 is_void)); |
| 382 if (result != net::ERR_IO_PENDING) | 393 if (result != net::ERR_IO_PENDING) |
| 383 ReadResponse(callback, is_void, result); | 394 ReadResponse(callback, is_void, result); |
| 384 } | 395 } |
| 385 | 396 |
| 386 void AdbClientSocket::ReadResponse(const CommandCallback& callback, | 397 void AdbClientSocket::ReadResponse(const CommandCallback& callback, |
| 387 bool is_void, | 398 bool is_void, |
| 388 int result) { | 399 int result) { |
| 389 if (result < 0) { | 400 if (result < 0) { |
| 390 callback.Run(result, "IO error"); | 401 callback.Run(result, "IO error"); |
| 391 return; | 402 return; |
| 392 } | 403 } |
| 393 scoped_refptr<net::IOBuffer> response_buffer = | 404 scoped_refptr<net::IOBuffer> response_buffer = |
| 394 new net::IOBuffer(kBufferSize); | 405 new net::IOBuffer(kBufferSize); |
| 395 result = socket_->Read(response_buffer, kBufferSize, | 406 result = socket_->Read(response_buffer.get(), |
| 396 base::Bind(&AdbClientSocket::OnResponseHeader, base::Unretained(this), | 407 kBufferSize, |
| 397 callback, is_void, response_buffer)); | 408 base::Bind(&AdbClientSocket::OnResponseHeader, |
| 409 base::Unretained(this), |
| 410 callback, |
| 411 is_void, |
| 412 response_buffer)); |
| 398 if (result != net::ERR_IO_PENDING) | 413 if (result != net::ERR_IO_PENDING) |
| 399 OnResponseHeader(callback, is_void, response_buffer, result); | 414 OnResponseHeader(callback, is_void, response_buffer, result); |
| 400 } | 415 } |
| 401 | 416 |
| 402 void AdbClientSocket::OnResponseHeader( | 417 void AdbClientSocket::OnResponseHeader( |
| 403 const CommandCallback& callback, | 418 const CommandCallback& callback, |
| 404 bool is_void, | 419 bool is_void, |
| 405 scoped_refptr<net::IOBuffer> response_buffer, | 420 scoped_refptr<net::IOBuffer> response_buffer, |
| 406 int result) { | 421 int result) { |
| 407 if (result <= 0) { | 422 if (result <= 0) { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 453 | 468 |
| 454 bytes_left -= result; | 469 bytes_left -= result; |
| 455 std::string new_response = | 470 std::string new_response = |
| 456 response + std::string(response_buffer->data(), result); | 471 response + std::string(response_buffer->data(), result); |
| 457 if (bytes_left == 0) { | 472 if (bytes_left == 0) { |
| 458 callback.Run(net::OK, new_response); | 473 callback.Run(net::OK, new_response); |
| 459 return; | 474 return; |
| 460 } | 475 } |
| 461 | 476 |
| 462 // Read tail | 477 // Read tail |
| 463 result = socket_->Read(response_buffer, kBufferSize, | 478 result = socket_->Read(response_buffer.get(), |
| 464 base::Bind(&AdbClientSocket::OnResponseData, base::Unretained(this), | 479 kBufferSize, |
| 465 callback, new_response, response_buffer, bytes_left)); | 480 base::Bind(&AdbClientSocket::OnResponseData, |
| 481 base::Unretained(this), |
| 482 callback, |
| 483 new_response, |
| 484 response_buffer, |
| 485 bytes_left)); |
| 466 if (result > 0) | 486 if (result > 0) |
| 467 OnResponseData(callback, new_response, response_buffer, bytes_left, result); | 487 OnResponseData(callback, new_response, response_buffer, bytes_left, result); |
| 468 else if (result != net::ERR_IO_PENDING) | 488 else if (result != net::ERR_IO_PENDING) |
| 469 callback.Run(net::OK, new_response); | 489 callback.Run(net::OK, new_response); |
| 470 } | 490 } |
| OLD | NEW |