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/browser/devtools/adb/android_usb_socket.h" | 5 #include "chrome/browser/devtools/adb/android_usb_socket.h" |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 | 8 |
9 namespace { | 9 namespace { |
10 | 10 |
(...skipping 16 matching lines...) Expand all Loading... | |
27 AndroidUsbSocket::AndroidUsbSocket(scoped_refptr<AndroidUsbDevice> device, | 27 AndroidUsbSocket::AndroidUsbSocket(scoped_refptr<AndroidUsbDevice> device, |
28 uint32 socket_id, | 28 uint32 socket_id, |
29 const std::string& command, | 29 const std::string& command, |
30 base::Callback<void(uint32)> delete_callback) | 30 base::Callback<void(uint32)> delete_callback) |
31 : device_(device), | 31 : device_(device), |
32 command_(command), | 32 command_(command), |
33 delete_callback_(delete_callback), | 33 delete_callback_(delete_callback), |
34 local_id_(socket_id), | 34 local_id_(socket_id), |
35 remote_id_(0), | 35 remote_id_(0), |
36 is_connected_(false), | 36 is_connected_(false), |
37 is_closed_(false) { | 37 is_closed_(false) { |
Vladislav Kaznacheev
2014/04/10 14:38:11
is_closed_ seems to be redundant now and can be re
pfeldman
2014/04/10 15:37:46
Done.
| |
38 } | 38 } |
39 | 39 |
40 AndroidUsbSocket::~AndroidUsbSocket() { | 40 AndroidUsbSocket::~AndroidUsbSocket() { |
41 DCHECK(CalledOnValidThread()); | 41 DCHECK(CalledOnValidThread()); |
42 if (is_connected_) | 42 if (is_connected_) |
43 Disconnect(); | 43 Disconnect(); |
44 delete_callback_.Run(local_id_); | 44 if (!delete_callback_.is_null()) |
45 delete_callback_.Run(local_id_); | |
45 } | 46 } |
46 | 47 |
47 void AndroidUsbSocket::HandleIncoming(scoped_refptr<AdbMessage> message) { | 48 void AndroidUsbSocket::HandleIncoming(scoped_refptr<AdbMessage> message) { |
49 if (!device_) | |
50 return; | |
51 | |
48 CHECK_EQ(message->arg1, local_id_); | 52 CHECK_EQ(message->arg1, local_id_); |
49 switch (message->command) { | 53 switch (message->command) { |
50 case AdbMessage::kCommandOKAY: | 54 case AdbMessage::kCommandOKAY: |
51 if (!is_connected_) { | 55 if (!is_connected_) { |
52 remote_id_ = message->arg0; | 56 remote_id_ = message->arg0; |
53 is_connected_ = true; | 57 is_connected_ = true; |
54 net::CompletionCallback callback = connect_callback_; | 58 net::CompletionCallback callback = connect_callback_; |
55 connect_callback_.Reset(); | 59 connect_callback_.Reset(); |
56 callback.Run(net::OK); | 60 callback.Run(net::OK); |
57 // "this" can be NULL. | 61 // "this" can be NULL. |
(...skipping 14 matching lines...) Expand all Loading... | |
72 callback.Run(net::OK); | 76 callback.Run(net::OK); |
73 // "this" can be NULL. | 77 // "this" can be NULL. |
74 } else { | 78 } else { |
75 RespondToReaders(false); | 79 RespondToReaders(false); |
76 // "this" can be NULL. | 80 // "this" can be NULL. |
77 } | 81 } |
78 break; | 82 break; |
79 case AdbMessage::kCommandCLSE: | 83 case AdbMessage::kCommandCLSE: |
80 if (is_connected_) | 84 if (is_connected_) |
81 device_->Send(AdbMessage::kCommandCLSE, local_id_, 0, ""); | 85 device_->Send(AdbMessage::kCommandCLSE, local_id_, 0, ""); |
82 is_connected_ = false; | 86 Terminated(); |
83 is_closed_ = true; | |
84 RespondToReaders(true); | |
85 // "this" can be NULL. | 87 // "this" can be NULL. |
86 break; | 88 break; |
87 default: | 89 default: |
88 break; | 90 break; |
89 } | 91 } |
90 } | 92 } |
91 | 93 |
92 void AndroidUsbSocket::Terminated() { | 94 void AndroidUsbSocket::Terminated() { |
93 is_connected_ = false; | 95 is_connected_ = false; |
94 is_closed_ = true; | 96 is_closed_ = true; |
97 | |
98 // Break the socket -> device connection, release the device. | |
99 delete_callback_.Run(local_id_); | |
100 delete_callback_.Reset(); | |
101 device_ = NULL; | |
102 | |
103 // Respond to pending callbacks. | |
95 if (!connect_callback_.is_null()) { | 104 if (!connect_callback_.is_null()) { |
96 net::CompletionCallback callback = connect_callback_; | 105 net::CompletionCallback callback = connect_callback_; |
97 connect_callback_.Reset(); | 106 connect_callback_.Reset(); |
98 callback.Run(net::ERR_FAILED); | 107 callback.Run(net::ERR_FAILED); |
99 // "this" can be NULL. | 108 // "this" can be NULL. |
100 return; | 109 return; |
101 } | 110 } |
102 RespondToReaders(true); | 111 RespondToReaders(true); |
103 } | 112 } |
104 | 113 |
105 int AndroidUsbSocket::Read(net::IOBuffer* buffer, | 114 int AndroidUsbSocket::Read(net::IOBuffer* buffer, |
106 int length, | 115 int length, |
107 const net::CompletionCallback& callback) { | 116 const net::CompletionCallback& callback) { |
108 if (!is_connected_) | 117 if (!is_connected_) |
109 return is_closed_ ? 0 : net::ERR_SOCKET_NOT_CONNECTED; | 118 return is_closed_ ? 0 : net::ERR_SOCKET_NOT_CONNECTED; |
Vladislav Kaznacheev
2014/04/10 14:38:11
Just out of curiosity, why not return net::ERR_SOC
pfeldman
2014/04/10 15:37:46
Done.
| |
110 | 119 |
111 if (read_buffer_.empty()) { | 120 if (read_buffer_.empty()) { |
112 read_requests_.push_back(IORequest(buffer, length, callback)); | 121 read_requests_.push_back(IORequest(buffer, length, callback)); |
113 return net::ERR_IO_PENDING; | 122 return net::ERR_IO_PENDING; |
114 } | 123 } |
115 | 124 |
116 size_t bytes_to_copy = static_cast<size_t>(length) > read_buffer_.length() ? | 125 size_t bytes_to_copy = static_cast<size_t>(length) > read_buffer_.length() ? |
117 read_buffer_.length() : static_cast<size_t>(length); | 126 read_buffer_.length() : static_cast<size_t>(length); |
118 memcpy(buffer->data(), read_buffer_.data(), bytes_to_copy); | 127 memcpy(buffer->data(), read_buffer_.data(), bytes_to_copy); |
119 if (read_buffer_.length() > bytes_to_copy) | 128 if (read_buffer_.length() > bytes_to_copy) |
(...skipping 22 matching lines...) Expand all Loading... | |
142 return false; | 151 return false; |
143 } | 152 } |
144 | 153 |
145 bool AndroidUsbSocket::SetSendBufferSize(int32 size) { | 154 bool AndroidUsbSocket::SetSendBufferSize(int32 size) { |
146 NOTIMPLEMENTED(); | 155 NOTIMPLEMENTED(); |
147 return false; | 156 return false; |
148 } | 157 } |
149 | 158 |
150 int AndroidUsbSocket::Connect(const net::CompletionCallback& callback) { | 159 int AndroidUsbSocket::Connect(const net::CompletionCallback& callback) { |
151 DCHECK(CalledOnValidThread()); | 160 DCHECK(CalledOnValidThread()); |
152 if (device_->terminated()) | 161 if (!device_) |
153 return net::ERR_FAILED; | 162 return net::ERR_FAILED; |
154 connect_callback_ = callback; | 163 connect_callback_ = callback; |
155 device_->Send(AdbMessage::kCommandOPEN, local_id_, 0, command_); | 164 device_->Send(AdbMessage::kCommandOPEN, local_id_, 0, command_); |
156 return net::ERR_IO_PENDING; | 165 return net::ERR_IO_PENDING; |
157 } | 166 } |
158 | 167 |
159 void AndroidUsbSocket::Disconnect() { | 168 void AndroidUsbSocket::Disconnect() { |
160 is_connected_ = false; | 169 if (!device_) |
170 return; | |
161 device_->Send(AdbMessage::kCommandCLSE, local_id_, remote_id_, ""); | 171 device_->Send(AdbMessage::kCommandCLSE, local_id_, remote_id_, ""); |
162 RespondToReaders(true); | 172 Terminated(); |
163 } | 173 } |
164 | 174 |
165 bool AndroidUsbSocket::IsConnected() const { | 175 bool AndroidUsbSocket::IsConnected() const { |
166 DCHECK(CalledOnValidThread()); | 176 DCHECK(CalledOnValidThread()); |
167 return is_connected_; | 177 return is_connected_; |
168 } | 178 } |
169 | 179 |
170 bool AndroidUsbSocket::IsConnectedAndIdle() const { | 180 bool AndroidUsbSocket::IsConnectedAndIdle() const { |
171 NOTIMPLEMENTED(); | 181 NOTIMPLEMENTED(); |
172 return false; | 182 return false; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
215 return net::kProtoUnknown; | 225 return net::kProtoUnknown; |
216 } | 226 } |
217 | 227 |
218 bool AndroidUsbSocket::GetSSLInfo(net::SSLInfo* ssl_info) { | 228 bool AndroidUsbSocket::GetSSLInfo(net::SSLInfo* ssl_info) { |
219 return false; | 229 return false; |
220 } | 230 } |
221 | 231 |
222 void AndroidUsbSocket::RespondToReaders(bool disconnect) { | 232 void AndroidUsbSocket::RespondToReaders(bool disconnect) { |
223 std::deque<IORequest> read_requests; | 233 std::deque<IORequest> read_requests; |
224 read_requests.swap(read_requests_); | 234 read_requests.swap(read_requests_); |
225 while (!read_requests.empty() && (!read_buffer_.empty() || disconnect)) { | 235 while (!read_requests.empty() && (!read_buffer_.empty() || disconnect)) { |
Vladislav Kaznacheev
2014/04/10 14:38:11
Theoretically you can replace |disconnect| with |i
pfeldman
2014/04/10 15:37:46
Lets keep it.
| |
226 IORequest read_request = read_requests.front(); | 236 IORequest read_request = read_requests.front(); |
227 read_requests.pop_front(); | 237 read_requests.pop_front(); |
228 size_t bytes_to_copy = | 238 size_t bytes_to_copy = |
229 static_cast<size_t>(read_request.length) > read_buffer_.length() ? | 239 static_cast<size_t>(read_request.length) > read_buffer_.length() ? |
230 read_buffer_.length() : static_cast<size_t>(read_request.length); | 240 read_buffer_.length() : static_cast<size_t>(read_request.length); |
231 memcpy(read_request.buffer->data(), read_buffer_.data(), bytes_to_copy); | 241 memcpy(read_request.buffer->data(), read_buffer_.data(), bytes_to_copy); |
232 if (read_buffer_.length() > bytes_to_copy) | 242 if (read_buffer_.length() > bytes_to_copy) |
233 read_buffer_ = read_buffer_.substr(bytes_to_copy); | 243 read_buffer_ = read_buffer_.substr(bytes_to_copy); |
234 else | 244 else |
235 read_buffer_ = ""; | 245 read_buffer_ = ""; |
236 read_request.callback.Run(bytes_to_copy); | 246 read_request.callback.Run(bytes_to_copy); |
237 } | 247 } |
238 } | 248 } |
239 | 249 |
240 void AndroidUsbSocket::RespondToWriters() { | 250 void AndroidUsbSocket::RespondToWriters() { |
241 if (!write_requests_.empty()) { | 251 if (!write_requests_.empty()) { |
242 IORequest write_request = write_requests_.front(); | 252 IORequest write_request = write_requests_.front(); |
243 write_requests_.pop_front(); | 253 write_requests_.pop_front(); |
244 write_request.callback.Run(write_request.length); | 254 write_request.callback.Run(write_request.length); |
245 } | 255 } |
246 } | 256 } |
OLD | NEW |