Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / devtools / device / usb / android_usb_socket.cc
blob1310f6ee951dc7685a0e05964966394ea763198f
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
3 // found in the LICENSE file.
5 #include "chrome/browser/devtools/device/usb/android_usb_socket.h"
7 #include "base/callback_helpers.h"
8 #include "base/message_loop/message_loop.h"
9 #include "net/base/net_errors.h"
11 namespace {
13 const int kMaxPayload = 4096;
15 } // namespace
17 AndroidUsbSocket::AndroidUsbSocket(scoped_refptr<AndroidUsbDevice> device,
18 uint32 socket_id,
19 const std::string& command,
20 base::Closure delete_callback)
21 : device_(device),
22 command_(command),
23 local_id_(socket_id),
24 remote_id_(0),
25 is_connected_(false),
26 delete_callback_(delete_callback),
27 weak_factory_(this) {
30 AndroidUsbSocket::~AndroidUsbSocket() {
31 DCHECK(CalledOnValidThread());
32 if (is_connected_)
33 Disconnect();
34 if (!delete_callback_.is_null())
35 delete_callback_.Run();
38 void AndroidUsbSocket::HandleIncoming(scoped_ptr<AdbMessage> message) {
39 if (!device_.get())
40 return;
42 CHECK_EQ(message->arg1, local_id_);
43 switch (message->command) {
44 case AdbMessage::kCommandOKAY:
45 if (!is_connected_) {
46 remote_id_ = message->arg0;
47 is_connected_ = true;
48 if (!connect_callback_.is_null())
49 base::ResetAndReturn(&connect_callback_).Run(net::OK);
50 // "this" can be deleted.
51 } else {
52 RespondToWriter(write_length_);
53 // "this" can be deleted.
55 break;
56 case AdbMessage::kCommandWRTE:
57 device_->Send(AdbMessage::kCommandOKAY, local_id_, message->arg0, "");
58 read_buffer_ += message->body;
59 // Allow WRTE over new connection even though OKAY ack was not received.
60 if (!is_connected_) {
61 remote_id_ = message->arg0;
62 is_connected_ = true;
63 if (!connect_callback_.is_null())
64 base::ResetAndReturn(&connect_callback_).Run(net::OK);
65 // "this" can be deleted.
66 } else {
67 RespondToReader(false);
68 // "this" can be deleted.
70 break;
71 case AdbMessage::kCommandCLSE:
72 if (is_connected_)
73 device_->Send(AdbMessage::kCommandCLSE, local_id_, 0, "");
74 Terminated(true);
75 // "this" can be deleted.
76 break;
77 default:
78 break;
82 void AndroidUsbSocket::Terminated(bool closed_by_device) {
83 is_connected_ = false;
85 // Break the socket -> device connection, release the device.
86 device_ = nullptr;
87 base::ResetAndReturn(&delete_callback_).Run();
89 if (!closed_by_device)
90 return;
92 // Respond to pending callbacks.
93 if (!connect_callback_.is_null()) {
94 base::ResetAndReturn(&connect_callback_).Run(net::ERR_FAILED);
95 // "this" can be deleted.
96 return;
98 base::WeakPtr<AndroidUsbSocket> weak_this = weak_factory_.GetWeakPtr();
99 RespondToReader(true);
100 // "this" can be deleted.
101 if (weak_this) {
102 RespondToWriter(net::ERR_FAILED);
103 // "this" can be deleted.
107 int AndroidUsbSocket::Read(net::IOBuffer* buffer,
108 int length,
109 const net::CompletionCallback& callback) {
110 DCHECK(!callback.is_null());
111 if (!is_connected_)
112 return device_.get() ? net::ERR_SOCKET_NOT_CONNECTED : 0;
114 DCHECK(read_callback_.is_null());
115 if (read_buffer_.empty()) {
116 read_callback_ = callback;
117 read_io_buffer_ = buffer;
118 read_length_ = length;
119 return net::ERR_IO_PENDING;
122 size_t bytes_to_copy = static_cast<size_t>(length) > read_buffer_.length() ?
123 read_buffer_.length() : static_cast<size_t>(length);
124 memcpy(buffer->data(), read_buffer_.data(), bytes_to_copy);
125 if (read_buffer_.length() > bytes_to_copy)
126 read_buffer_ = read_buffer_.substr(bytes_to_copy);
127 else
128 read_buffer_ = std::string();
129 return bytes_to_copy;
132 int AndroidUsbSocket::Write(net::IOBuffer* buffer,
133 int length,
134 const net::CompletionCallback& callback) {
135 DCHECK(!callback.is_null());
136 if (!is_connected_)
137 return net::ERR_SOCKET_NOT_CONNECTED;
139 if (length > kMaxPayload)
140 length = kMaxPayload;
142 DCHECK(write_callback_.is_null());
143 write_callback_ = callback;
144 write_length_ = length;
145 device_->Send(AdbMessage::kCommandWRTE, local_id_, remote_id_,
146 std::string(buffer->data(), length));
147 return net::ERR_IO_PENDING;
150 int AndroidUsbSocket::SetReceiveBufferSize(int32 size) {
151 NOTIMPLEMENTED();
152 return net::ERR_NOT_IMPLEMENTED;
155 int AndroidUsbSocket::SetSendBufferSize(int32 size) {
156 NOTIMPLEMENTED();
157 return net::ERR_NOT_IMPLEMENTED;
160 int AndroidUsbSocket::Connect(const net::CompletionCallback& callback) {
161 DCHECK(CalledOnValidThread());
162 DCHECK(!callback.is_null());
163 if (!device_.get())
164 return net::ERR_FAILED;
166 DCHECK(!is_connected_);
167 DCHECK(connect_callback_.is_null());
168 connect_callback_ = callback;
169 device_->Send(AdbMessage::kCommandOPEN, local_id_, 0, command_);
170 return net::ERR_IO_PENDING;
173 void AndroidUsbSocket::Disconnect() {
174 if (!device_.get())
175 return;
176 device_->Send(AdbMessage::kCommandCLSE, local_id_, remote_id_, "");
177 Terminated(false);
180 bool AndroidUsbSocket::IsConnected() const {
181 DCHECK(CalledOnValidThread());
182 return is_connected_;
185 bool AndroidUsbSocket::IsConnectedAndIdle() const {
186 NOTIMPLEMENTED();
187 return false;
190 int AndroidUsbSocket::GetPeerAddress(net::IPEndPoint* address) const {
191 net::IPAddressNumber ip(net::kIPv4AddressSize);
192 *address = net::IPEndPoint(ip, 0);
193 return net::OK;
196 int AndroidUsbSocket::GetLocalAddress(net::IPEndPoint* address) const {
197 NOTIMPLEMENTED();
198 return net::ERR_NOT_IMPLEMENTED;
201 const net::BoundNetLog& AndroidUsbSocket::NetLog() const {
202 return net_log_;
205 void AndroidUsbSocket::SetSubresourceSpeculation() {
206 NOTIMPLEMENTED();
209 void AndroidUsbSocket::SetOmniboxSpeculation() {
210 NOTIMPLEMENTED();
213 bool AndroidUsbSocket::WasEverUsed() const {
214 NOTIMPLEMENTED();
215 return true;
218 bool AndroidUsbSocket::UsingTCPFastOpen() const {
219 NOTIMPLEMENTED();
220 return true;
223 bool AndroidUsbSocket::WasNpnNegotiated() const {
224 NOTIMPLEMENTED();
225 return true;
228 net::NextProto AndroidUsbSocket::GetNegotiatedProtocol() const {
229 NOTIMPLEMENTED();
230 return net::kProtoUnknown;
233 bool AndroidUsbSocket::GetSSLInfo(net::SSLInfo* ssl_info) {
234 return false;
237 void AndroidUsbSocket::GetConnectionAttempts(
238 net::ConnectionAttempts* out) const {
239 out->clear();
242 void AndroidUsbSocket::RespondToReader(bool disconnect) {
243 if (read_callback_.is_null() || (read_buffer_.empty() && !disconnect))
244 return;
245 size_t bytes_to_copy =
246 static_cast<size_t>(read_length_) > read_buffer_.length() ?
247 read_buffer_.length() : static_cast<size_t>(read_length_);
248 memcpy(read_io_buffer_->data(), read_buffer_.data(), bytes_to_copy);
249 if (read_buffer_.length() > bytes_to_copy)
250 read_buffer_ = read_buffer_.substr(bytes_to_copy);
251 else
252 read_buffer_ = std::string();
253 base::ResetAndReturn(&read_callback_).Run(bytes_to_copy);
256 void AndroidUsbSocket::RespondToWriter(int result) {
257 if (!write_callback_.is_null())
258 base::ResetAndReturn(&write_callback_).Run(result);