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/usb/android_usb_socket.h" | 5 #include "chrome/browser/devtools/device/usb/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 27 matching lines...) Expand all Loading... |
38 | 38 |
39 AndroidUsbSocket::~AndroidUsbSocket() { | 39 AndroidUsbSocket::~AndroidUsbSocket() { |
40 DCHECK(CalledOnValidThread()); | 40 DCHECK(CalledOnValidThread()); |
41 if (is_connected_) | 41 if (is_connected_) |
42 Disconnect(); | 42 Disconnect(); |
43 if (!delete_callback_.is_null()) | 43 if (!delete_callback_.is_null()) |
44 delete_callback_.Run(local_id_); | 44 delete_callback_.Run(local_id_); |
45 } | 45 } |
46 | 46 |
47 void AndroidUsbSocket::HandleIncoming(scoped_refptr<AdbMessage> message) { | 47 void AndroidUsbSocket::HandleIncoming(scoped_refptr<AdbMessage> message) { |
48 if (!device_) | 48 if (!device_.get()) |
49 return; | 49 return; |
50 | 50 |
51 CHECK_EQ(message->arg1, local_id_); | 51 CHECK_EQ(message->arg1, local_id_); |
52 switch (message->command) { | 52 switch (message->command) { |
53 case AdbMessage::kCommandOKAY: | 53 case AdbMessage::kCommandOKAY: |
54 if (!is_connected_) { | 54 if (!is_connected_) { |
55 remote_id_ = message->arg0; | 55 remote_id_ = message->arg0; |
56 is_connected_ = true; | 56 is_connected_ = true; |
57 net::CompletionCallback callback = connect_callback_; | 57 net::CompletionCallback callback = connect_callback_; |
58 connect_callback_.Reset(); | 58 connect_callback_.Reset(); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 // "this" can be NULL. | 109 // "this" can be NULL. |
110 return; | 110 return; |
111 } | 111 } |
112 RespondToReaders(true); | 112 RespondToReaders(true); |
113 } | 113 } |
114 | 114 |
115 int AndroidUsbSocket::Read(net::IOBuffer* buffer, | 115 int AndroidUsbSocket::Read(net::IOBuffer* buffer, |
116 int length, | 116 int length, |
117 const net::CompletionCallback& callback) { | 117 const net::CompletionCallback& callback) { |
118 if (!is_connected_) | 118 if (!is_connected_) |
119 return device_ ? net::ERR_SOCKET_NOT_CONNECTED : 0; | 119 return device_.get() ? net::ERR_SOCKET_NOT_CONNECTED : 0; |
120 | 120 |
121 if (read_buffer_.empty()) { | 121 if (read_buffer_.empty()) { |
122 read_requests_.push_back(IORequest(buffer, length, callback)); | 122 read_requests_.push_back(IORequest(buffer, length, callback)); |
123 return net::ERR_IO_PENDING; | 123 return net::ERR_IO_PENDING; |
124 } | 124 } |
125 | 125 |
126 size_t bytes_to_copy = static_cast<size_t>(length) > read_buffer_.length() ? | 126 size_t bytes_to_copy = static_cast<size_t>(length) > read_buffer_.length() ? |
127 read_buffer_.length() : static_cast<size_t>(length); | 127 read_buffer_.length() : static_cast<size_t>(length); |
128 memcpy(buffer->data(), read_buffer_.data(), bytes_to_copy); | 128 memcpy(buffer->data(), read_buffer_.data(), bytes_to_copy); |
129 if (read_buffer_.length() > bytes_to_copy) | 129 if (read_buffer_.length() > bytes_to_copy) |
(...skipping 22 matching lines...) Expand all Loading... |
152 return net::ERR_NOT_IMPLEMENTED; | 152 return net::ERR_NOT_IMPLEMENTED; |
153 } | 153 } |
154 | 154 |
155 int AndroidUsbSocket::SetSendBufferSize(int32 size) { | 155 int AndroidUsbSocket::SetSendBufferSize(int32 size) { |
156 NOTIMPLEMENTED(); | 156 NOTIMPLEMENTED(); |
157 return net::ERR_NOT_IMPLEMENTED; | 157 return net::ERR_NOT_IMPLEMENTED; |
158 } | 158 } |
159 | 159 |
160 int AndroidUsbSocket::Connect(const net::CompletionCallback& callback) { | 160 int AndroidUsbSocket::Connect(const net::CompletionCallback& callback) { |
161 DCHECK(CalledOnValidThread()); | 161 DCHECK(CalledOnValidThread()); |
162 if (!device_) | 162 if (!device_.get()) |
163 return net::ERR_FAILED; | 163 return net::ERR_FAILED; |
164 connect_callback_ = callback; | 164 connect_callback_ = callback; |
165 device_->Send(AdbMessage::kCommandOPEN, local_id_, 0, command_); | 165 device_->Send(AdbMessage::kCommandOPEN, local_id_, 0, command_); |
166 return net::ERR_IO_PENDING; | 166 return net::ERR_IO_PENDING; |
167 } | 167 } |
168 | 168 |
169 void AndroidUsbSocket::Disconnect() { | 169 void AndroidUsbSocket::Disconnect() { |
170 if (!device_) | 170 if (!device_.get()) |
171 return; | 171 return; |
172 device_->Send(AdbMessage::kCommandCLSE, local_id_, remote_id_, ""); | 172 device_->Send(AdbMessage::kCommandCLSE, local_id_, remote_id_, ""); |
173 Terminated(false); | 173 Terminated(false); |
174 } | 174 } |
175 | 175 |
176 bool AndroidUsbSocket::IsConnected() const { | 176 bool AndroidUsbSocket::IsConnected() const { |
177 DCHECK(CalledOnValidThread()); | 177 DCHECK(CalledOnValidThread()); |
178 return is_connected_; | 178 return is_connected_; |
179 } | 179 } |
180 | 180 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 } | 248 } |
249 } | 249 } |
250 | 250 |
251 void AndroidUsbSocket::RespondToWriters() { | 251 void AndroidUsbSocket::RespondToWriters() { |
252 if (!write_requests_.empty()) { | 252 if (!write_requests_.empty()) { |
253 IORequest write_request = write_requests_.front(); | 253 IORequest write_request = write_requests_.front(); |
254 write_requests_.pop_front(); | 254 write_requests_.pop_front(); |
255 write_request.callback.Run(write_request.length); | 255 write_request.callback.Run(write_request.length); |
256 } | 256 } |
257 } | 257 } |
OLD | NEW |