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/adb_client_socket.h" | 5 #include "chrome/browser/devtools/device/adb/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/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
13 #include "net/base/net_util.h" | 13 #include "net/base/net_util.h" |
14 #include "net/socket/tcp_client_socket.h" | 14 #include "net/socket/tcp_client_socket.h" |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 const int kBufferSize = 16 * 1024; | 18 const int kBufferSize = 16 * 1024; |
19 const char kOkayResponse[] = "OKAY"; | 19 const char kOkayResponse[] = "OKAY"; |
20 const char kHostTransportCommand[] = "host:transport:%s"; | 20 const char kHostTransportCommand[] = "host:transport:%s"; |
21 const char kLocalhost[] = "127.0.0.1"; | 21 const char kLocalhost[] = "127.0.0.1"; |
22 | 22 |
23 typedef base::Callback<void(int, const std::string&)> CommandCallback; | |
24 typedef base::Callback<void(int, net::StreamSocket*)> SocketCallback; | |
25 | |
26 std::string EncodeMessage(const std::string& message) { | 23 std::string EncodeMessage(const std::string& message) { |
27 static const char kHexChars[] = "0123456789ABCDEF"; | 24 static const char kHexChars[] = "0123456789ABCDEF"; |
28 | 25 |
29 size_t length = message.length(); | 26 size_t length = message.length(); |
30 std::string result(4, '\0'); | 27 std::string result(4, '\0'); |
31 char b = reinterpret_cast<const char*>(&length)[1]; | 28 char b = reinterpret_cast<const char*>(&length)[1]; |
32 result[0] = kHexChars[(b >> 4) & 0xf]; | 29 result[0] = kHexChars[(b >> 4) & 0xf]; |
33 result[1] = kHexChars[b & 0xf]; | 30 result[1] = kHexChars[b & 0xf]; |
34 b = reinterpret_cast<const char*>(&length)[0]; | 31 b = reinterpret_cast<const char*>(&length)[0]; |
35 result[2] = kHexChars[(b >> 4) & 0xf]; | 32 result[2] = kHexChars[(b >> 4) & 0xf]; |
(...skipping 30 matching lines...) Expand all Loading... |
66 if (!CheckNetResultOrDie(result)) | 63 if (!CheckNetResultOrDie(result)) |
67 return; | 64 return; |
68 SendCommand(socket_name_, true, | 65 SendCommand(socket_name_, true, |
69 base::Bind(&AdbTransportSocket::OnSocketAvailable, | 66 base::Bind(&AdbTransportSocket::OnSocketAvailable, |
70 base::Unretained(this))); | 67 base::Unretained(this))); |
71 } | 68 } |
72 | 69 |
73 void OnSocketAvailable(int result, const std::string& response) { | 70 void OnSocketAvailable(int result, const std::string& response) { |
74 if (!CheckNetResultOrDie(result)) | 71 if (!CheckNetResultOrDie(result)) |
75 return; | 72 return; |
76 callback_.Run(net::OK, socket_.release()); | 73 callback_.Run(net::OK, socket_.Pass()); |
77 delete this; | 74 delete this; |
78 } | 75 } |
79 | 76 |
80 bool CheckNetResultOrDie(int result) { | 77 bool CheckNetResultOrDie(int result) { |
81 if (result >= 0) | 78 if (result >= 0) |
82 return true; | 79 return true; |
83 callback_.Run(result, NULL); | 80 callback_.Run(result, make_scoped_ptr<net::StreamSocket>(NULL)); |
84 delete this; | 81 delete this; |
85 return false; | 82 return false; |
86 } | 83 } |
87 | 84 |
88 std::string serial_; | 85 std::string serial_; |
89 std::string socket_name_; | 86 std::string socket_name_; |
90 SocketCallback callback_; | 87 SocketCallback callback_; |
91 }; | 88 }; |
92 | 89 |
93 class AdbQuerySocket : AdbClientSocket { | 90 class AdbQuerySocket : AdbClientSocket { |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 base::Unretained(this), | 284 base::Unretained(this), |
288 callback, | 285 callback, |
289 new_response, | 286 new_response, |
290 response_buffer, | 287 response_buffer, |
291 bytes_left)); | 288 bytes_left)); |
292 if (result > 0) | 289 if (result > 0) |
293 OnResponseData(callback, new_response, response_buffer, bytes_left, result); | 290 OnResponseData(callback, new_response, response_buffer, bytes_left, result); |
294 else if (result != net::ERR_IO_PENDING) | 291 else if (result != net::ERR_IO_PENDING) |
295 callback.Run(net::OK, new_response); | 292 callback.Run(net::OK, new_response); |
296 } | 293 } |
OLD | NEW |