Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(220)

Side by Side Diff: chrome/browser/devtools/device/usb/android_usb_device.cc

Issue 980023002: Move device/usb classes from the FILE thread to UI thread. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Allow USB transfer calls from any thread again. Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_device.h" 5 #include "chrome/browser/devtools/device/usb/android_usb_device.h"
6 6
7 #include <set> 7 #include <set>
8 8
9 #include "base/barrier_closure.h" 9 #include "base/barrier_closure.h"
10 #include "base/base64.h" 10 #include "base/base64.h"
11 #include "base/lazy_instance.h" 11 #include "base/lazy_instance.h"
12 #include "base/message_loop/message_loop.h" 12 #include "base/single_thread_task_runner.h"
13 #include "base/stl_util.h" 13 #include "base/stl_util.h"
14 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h" 15 #include "base/strings/stringprintf.h"
16 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
17 #include "base/thread_task_runner_handle.h"
17 #include "chrome/browser/devtools/device/usb/android_rsa.h" 18 #include "chrome/browser/devtools/device/usb/android_rsa.h"
18 #include "chrome/browser/devtools/device/usb/android_usb_socket.h" 19 #include "chrome/browser/devtools/device/usb/android_usb_socket.h"
19 #include "content/public/browser/browser_thread.h" 20 #include "content/public/browser/browser_thread.h"
20 #include "crypto/rsa_private_key.h" 21 #include "crypto/rsa_private_key.h"
21 #include "device/core/device_client.h" 22 #include "device/core/device_client.h"
22 #include "device/usb/usb_descriptors.h" 23 #include "device/usb/usb_descriptors.h"
23 #include "device/usb/usb_device.h" 24 #include "device/usb/usb_device.h"
24 #include "device/usb/usb_service.h" 25 #include "device/usb/usb_service.h"
25 #include "net/base/ip_endpoint.h" 26 #include "net/base/ip_endpoint.h"
26 #include "net/base/net_errors.h" 27 #include "net/base/net_errors.h"
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 if (interface.alternate_setting != 0 || 63 if (interface.alternate_setting != 0 ||
63 interface.interface_class != kAdbClass || 64 interface.interface_class != kAdbClass ||
64 interface.interface_subclass != kAdbSubclass || 65 interface.interface_subclass != kAdbSubclass ||
65 interface.interface_protocol != kAdbProtocol || 66 interface.interface_protocol != kAdbProtocol ||
66 interface.endpoints.size() != 2) { 67 interface.endpoints.size() != 2) {
67 return false; 68 return false;
68 } 69 }
69 return true; 70 return true;
70 } 71 }
71 72
72 scoped_refptr<AndroidUsbDevice> ClaimInterface( 73 void CountAndroidDevices(const base::Callback<void(int)>& callback,
73 crypto::RSAPrivateKey* rsa_key, 74 const UsbDevices& devices) {
74 scoped_refptr<UsbDeviceHandle> usb_handle, 75 int device_count = 0;
75 const base::string16& serial, 76 for (const scoped_refptr<UsbDevice>& device : devices) {
76 const UsbInterfaceDescriptor& interface) { 77 const UsbConfigDescriptor* config = device->GetConfiguration();
77 int inbound_address = 0; 78 if (config) {
78 int outbound_address = 0; 79 for (const UsbInterfaceDescriptor& iface : config->interfaces) {
79 int zero_mask = 0; 80 if (IsAndroidInterface(iface)) {
80 81 ++device_count;
81 for (const UsbEndpointDescriptor& endpoint : interface.endpoints) { 82 }
82 if (endpoint.transfer_type != device::USB_TRANSFER_BULK) 83 }
83 continue; 84 }
84 if (endpoint.direction == device::USB_DIRECTION_INBOUND)
85 inbound_address = endpoint.address;
86 else
87 outbound_address = endpoint.address;
88 zero_mask = endpoint.maximum_packet_size - 1;
89 } 85 }
90 86
91 if (inbound_address == 0 || outbound_address == 0) 87 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
92 return NULL; 88 base::Bind(callback, device_count));
93
94 if (!usb_handle->ClaimInterface(interface.interface_number))
95 return NULL;
96
97 return new AndroidUsbDevice(rsa_key,
98 usb_handle,
99 base::UTF16ToASCII(serial),
100 inbound_address,
101 outbound_address,
102 zero_mask,
103 interface.interface_number);
104 } 89 }
105 90
106 uint32 Checksum(const std::string& data) { 91 uint32 Checksum(const std::string& data) {
107 unsigned char* x = (unsigned char*)data.data(); 92 unsigned char* x = (unsigned char*)data.data();
108 int count = data.length(); 93 int count = data.length();
109 uint32 sum = 0; 94 uint32 sum = 0;
110 while (count-- > 0) 95 while (count-- > 0)
111 sum += *x++; 96 sum += *x++;
112 return sum; 97 return sum;
113 } 98 }
(...skipping 26 matching lines...) Expand all
140 LOG(ERROR) << (outgoing ? "[out] " : "[ in] ") << result; 125 LOG(ERROR) << (outgoing ? "[out] " : "[ in] ") << result;
141 #endif // 0 126 #endif // 0
142 } 127 }
143 128
144 void ReleaseInterface(scoped_refptr<UsbDeviceHandle> usb_device, 129 void ReleaseInterface(scoped_refptr<UsbDeviceHandle> usb_device,
145 int interface_id) { 130 int interface_id) {
146 usb_device->ReleaseInterface(interface_id); 131 usb_device->ReleaseInterface(interface_id);
147 usb_device->Close(); 132 usb_device->Close();
148 } 133 }
149 134
150 } // namespace 135 void RespondOnCallerThread(const AndroidUsbDevicesCallback& callback,
151 136 AndroidUsbDevices* new_devices) {
152 AdbMessage::AdbMessage(uint32 command,
153 uint32 arg0,
154 uint32 arg1,
155 const std::string& body)
156 : command(command),
157 arg0(arg0),
158 arg1(arg1),
159 body(body) {
160 }
161
162 AdbMessage::~AdbMessage() {
163 }
164
165 static void RespondOnCallerThread(const AndroidUsbDevicesCallback& callback,
166 AndroidUsbDevices* new_devices) {
167 scoped_ptr<AndroidUsbDevices> devices(new_devices); 137 scoped_ptr<AndroidUsbDevices> devices(new_devices);
168 138
169 // Add raw pointers to the newly claimed devices. 139 // Add raw pointers to the newly claimed devices.
170 for (const scoped_refptr<AndroidUsbDevice>& device : *devices) { 140 for (const scoped_refptr<AndroidUsbDevice>& device : *devices) {
171 g_devices.Get().push_back(device.get()); 141 g_devices.Get().push_back(device.get());
172 } 142 }
173 143
174 // Return all claimed devices. 144 // Return all claimed devices.
175 AndroidUsbDevices result(g_devices.Get().begin(), g_devices.Get().end()); 145 AndroidUsbDevices result(g_devices.Get().begin(), g_devices.Get().end());
176 callback.Run(result); 146 callback.Run(result);
177 } 147 }
178 148
179 static void RespondOnFileThread( 149 void RespondOnUIThread(
180 const AndroidUsbDevicesCallback& callback, 150 const AndroidUsbDevicesCallback& callback,
181 AndroidUsbDevices* devices, 151 AndroidUsbDevices* devices,
182 scoped_refptr<base::MessageLoopProxy> caller_message_loop_proxy) { 152 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner) {
183 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 153 DCHECK_CURRENTLY_ON(BrowserThread::UI);
184 caller_message_loop_proxy->PostTask( 154 caller_task_runner->PostTask(
185 FROM_HERE, 155 FROM_HERE, base::Bind(&RespondOnCallerThread, callback, devices));
186 base::Bind(&RespondOnCallerThread, callback, devices));
187 } 156 }
188 157
189 static void OpenAndroidDeviceOnFileThread( 158 void CreateDeviceOnInterfaceClaimed(AndroidUsbDevices* devices,
190 AndroidUsbDevices* devices, 159 crypto::RSAPrivateKey* rsa_key,
191 crypto::RSAPrivateKey* rsa_key, 160 scoped_refptr<UsbDeviceHandle> usb_handle,
192 const base::Closure& barrier, 161 int inbound_address,
193 scoped_refptr<UsbDevice> device, 162 int outbound_address,
194 int interface_id, 163 int zero_mask,
195 bool success) { 164 int interface_number,
196 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 165 const base::Closure& barrier,
166 bool success) {
197 if (success) { 167 if (success) {
198 base::string16 serial; 168 devices->push_back(new AndroidUsbDevice(
199 if (device->GetSerialNumber(&serial) && !serial.empty()) { 169 rsa_key, usb_handle,
200 const UsbConfigDescriptor* config = device->GetConfiguration(); 170 base::UTF16ToASCII(usb_handle->GetDevice()->serial_number()),
201 if (config) { 171 inbound_address, outbound_address, zero_mask, interface_number));
202 scoped_refptr<UsbDeviceHandle> usb_handle = device->Open(); 172 } else {
203 if (usb_handle.get()) { 173 usb_handle->Close();
204 scoped_refptr<AndroidUsbDevice> android_device = ClaimInterface(
205 rsa_key, usb_handle, serial, config->interfaces[interface_id]);
206 if (android_device.get())
207 devices->push_back(android_device);
208 else
209 usb_handle->Close();
210 }
211 }
212 }
213 } 174 }
214 barrier.Run(); 175 barrier.Run();
215 } 176 }
216 177
217 static int CountOnFileThread() { 178 void OnDeviceOpened(AndroidUsbDevices* devices,
218 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 179 crypto::RSAPrivateKey* rsa_key,
219 UsbService* service = device::DeviceClient::Get()->GetUsbService(); 180 int inbound_address,
220 UsbDevices usb_devices; 181 int outbound_address,
221 if (service != NULL) 182 int zero_mask,
222 service->GetDevices(&usb_devices); 183 int interface_number,
223 int device_count = 0; 184 const base::Closure& barrier,
224 for (const scoped_refptr<UsbDevice>& device : usb_devices) { 185 scoped_refptr<UsbDeviceHandle> usb_handle) {
225 const UsbConfigDescriptor* config = device->GetConfiguration(); 186 if (usb_handle.get()) {
226 if (config) { 187 usb_handle->ClaimInterface(
227 for (const UsbInterfaceDescriptor& iface : config->interfaces) { 188 interface_number,
228 if (IsAndroidInterface(iface)) { 189 base::Bind(&CreateDeviceOnInterfaceClaimed, devices, rsa_key,
229 ++device_count; 190 usb_handle, inbound_address, outbound_address, zero_mask,
230 } 191 interface_number, barrier));
231 } 192 } else {
232 } 193 barrier.Run();
233 } 194 }
234 return device_count;
235 } 195 }
236 196
237 static void EnumerateOnFileThread( 197 void OpenAndroidDevice(AndroidUsbDevices* devices,
198 crypto::RSAPrivateKey* rsa_key,
199 const base::Closure& barrier,
200 scoped_refptr<UsbDevice> device,
201 int interface_id,
202 bool success) {
203 if (!success) {
204 barrier.Run();
205 return;
206 }
207
208 if (device->serial_number().empty()) {
209 barrier.Run();
210 return;
211 }
212
213 const UsbConfigDescriptor* config = device->GetConfiguration();
214 if (!config) {
215 barrier.Run();
216 return;
217 }
218
219 const UsbInterfaceDescriptor& interface = config->interfaces[interface_id];
220 int inbound_address = 0;
221 int outbound_address = 0;
222 int zero_mask = 0;
223
224 for (const UsbEndpointDescriptor& endpoint : interface.endpoints) {
225 if (endpoint.transfer_type != device::USB_TRANSFER_BULK)
226 continue;
227 if (endpoint.direction == device::USB_DIRECTION_INBOUND)
228 inbound_address = endpoint.address;
229 else
230 outbound_address = endpoint.address;
231 zero_mask = endpoint.maximum_packet_size - 1;
232 }
233
234 if (inbound_address == 0 || outbound_address == 0) {
235 barrier.Run();
236 return;
237 }
238
239 device->Open(base::Bind(&OnDeviceOpened, devices, rsa_key, inbound_address,
240 outbound_address, zero_mask,
241 interface.interface_number, barrier));
242 }
243
244 void OpenAndroidDevices(
238 crypto::RSAPrivateKey* rsa_key, 245 crypto::RSAPrivateKey* rsa_key,
239 const AndroidUsbDevicesCallback& callback, 246 const AndroidUsbDevicesCallback& callback,
240 scoped_refptr<base::MessageLoopProxy> caller_message_loop_proxy) { 247 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner,
241 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 248 const UsbDevices& usb_devices) {
242
243 UsbService* service = device::DeviceClient::Get()->GetUsbService();
244 UsbDevices usb_devices;
245 if (service != NULL)
246 service->GetDevices(&usb_devices);
247
248 // Add new devices. 249 // Add new devices.
249 AndroidUsbDevices* devices = new AndroidUsbDevices(); 250 AndroidUsbDevices* devices = new AndroidUsbDevices();
250 base::Closure barrier = base::BarrierClosure( 251 base::Closure barrier = base::BarrierClosure(
251 usb_devices.size(), base::Bind(&RespondOnFileThread, 252 usb_devices.size(),
252 callback, 253 base::Bind(&RespondOnUIThread, callback, devices, caller_task_runner));
253 devices,
254 caller_message_loop_proxy));
255 254
256 for (const scoped_refptr<UsbDevice>& device : usb_devices) { 255 for (const scoped_refptr<UsbDevice>& device : usb_devices) {
257 const UsbConfigDescriptor* config = device->GetConfiguration(); 256 const UsbConfigDescriptor* config = device->GetConfiguration();
258 if (!config) { 257 if (!config) {
259 barrier.Run(); 258 barrier.Run();
260 continue; 259 continue;
261 } 260 }
262 bool has_android_interface = false; 261 bool has_android_interface = false;
263 for (size_t j = 0; j < config->interfaces.size(); ++j) { 262 for (size_t j = 0; j < config->interfaces.size(); ++j) {
264 if (!IsAndroidInterface(config->interfaces[j])) { 263 if (!IsAndroidInterface(config->interfaces[j])) {
265 continue; 264 continue;
266 } 265 }
267 266
268 device->RequestUsbAccess( 267 device->RequestUsbAccess(j, base::Bind(&OpenAndroidDevice, devices,
269 j, base::Bind(&OpenAndroidDeviceOnFileThread, devices, rsa_key, 268 rsa_key, barrier, device, j));
270 barrier, device, j));
271 269
272 has_android_interface = true; 270 has_android_interface = true;
273 break; 271 break;
274 } 272 }
275 if (!has_android_interface) 273 if (!has_android_interface) {
276 barrier.Run(); 274 barrier.Run();
275 }
277 } 276 }
278 } 277 }
279 278
279 void EnumerateOnUIThread(
280 crypto::RSAPrivateKey* rsa_key,
281 const AndroidUsbDevicesCallback& callback,
282 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner) {
283 DCHECK_CURRENTLY_ON(BrowserThread::UI);
284
285 UsbService* service = device::DeviceClient::Get()->GetUsbService();
286 if (service == NULL) {
287 caller_task_runner->PostTask(FROM_HERE,
288 base::Bind(callback, AndroidUsbDevices()));
289 } else {
290 service->GetDevices(
291 base::Bind(&OpenAndroidDevices, rsa_key, callback, caller_task_runner));
292 }
293 }
294
295 } // namespace
296
297 AdbMessage::AdbMessage(uint32 command,
298 uint32 arg0,
299 uint32 arg1,
300 const std::string& body)
301 : command(command), arg0(arg0), arg1(arg1), body(body) {
302 }
303
304 AdbMessage::~AdbMessage() {
305 }
306
280 // static 307 // static
281 void AndroidUsbDevice::CountDevices( 308 void AndroidUsbDevice::CountDevices(const base::Callback<void(int)>& callback) {
282 const base::Callback<void(int)>& callback) { 309 UsbService* service = device::DeviceClient::Get()->GetUsbService();
283 BrowserThread::PostTaskAndReplyWithResult( 310 if (service != NULL) {
284 BrowserThread::FILE, 311 service->GetDevices(base::Bind(&CountAndroidDevices, callback));
285 FROM_HERE, 312 } else {
286 base::Bind(&CountOnFileThread), 313 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
287 callback); 314 base::Bind(callback, 0));
315 }
288 } 316 }
289 317
290 // static 318 // static
291 void AndroidUsbDevice::Enumerate(crypto::RSAPrivateKey* rsa_key, 319 void AndroidUsbDevice::Enumerate(crypto::RSAPrivateKey* rsa_key,
292 const AndroidUsbDevicesCallback& callback) { 320 const AndroidUsbDevicesCallback& callback) {
293
294 // Collect devices with closed handles. 321 // Collect devices with closed handles.
295 for (AndroidUsbDevice* device : g_devices.Get()) { 322 for (AndroidUsbDevice* device : g_devices.Get()) {
296 if (device->usb_handle_.get()) { 323 if (device->usb_handle_.get()) {
297 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 324 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
298 base::Bind(&AndroidUsbDevice::TerminateIfReleased, device, 325 base::Bind(&AndroidUsbDevice::TerminateIfReleased,
299 device->usb_handle_)); 326 device, device->usb_handle_));
300 } 327 }
301 } 328 }
302 329
303 // Then look for the new devices. 330 // Then look for the new devices.
304 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 331 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
305 base::Bind(&EnumerateOnFileThread, rsa_key, callback, 332 base::Bind(&EnumerateOnUIThread, rsa_key, callback,
306 base::MessageLoopProxy::current())); 333 base::ThreadTaskRunnerHandle::Get()));
307 } 334 }
308 335
309 AndroidUsbDevice::AndroidUsbDevice(crypto::RSAPrivateKey* rsa_key, 336 AndroidUsbDevice::AndroidUsbDevice(crypto::RSAPrivateKey* rsa_key,
310 scoped_refptr<UsbDeviceHandle> usb_device, 337 scoped_refptr<UsbDeviceHandle> usb_device,
311 const std::string& serial, 338 const std::string& serial,
312 int inbound_address, 339 int inbound_address,
313 int outbound_address, 340 int outbound_address,
314 int zero_mask, 341 int zero_mask,
315 int interface_id) 342 int interface_id)
316 : message_loop_(NULL), 343 : rsa_key_(rsa_key->Copy()),
317 rsa_key_(rsa_key->Copy()),
318 usb_handle_(usb_device), 344 usb_handle_(usb_device),
319 serial_(serial), 345 serial_(serial),
320 inbound_address_(inbound_address), 346 inbound_address_(inbound_address),
321 outbound_address_(outbound_address), 347 outbound_address_(outbound_address),
322 zero_mask_(zero_mask), 348 zero_mask_(zero_mask),
323 interface_id_(interface_id), 349 interface_id_(interface_id),
324 is_connected_(false), 350 is_connected_(false),
325 signature_sent_(false), 351 signature_sent_(false),
326 last_socket_id_(256), 352 last_socket_id_(256),
327 weak_factory_(this) { 353 weak_factory_(this) {
328 } 354 }
329 355
330 void AndroidUsbDevice::InitOnCallerThread() { 356 void AndroidUsbDevice::InitOnCallerThread() {
331 if (message_loop_) 357 if (task_runner_)
332 return; 358 return;
333 message_loop_ = base::MessageLoop::current(); 359 task_runner_ = base::ThreadTaskRunnerHandle::Get();
334 Queue(make_scoped_ptr(new AdbMessage(AdbMessage::kCommandCNXN, kVersion, 360 Queue(make_scoped_ptr(new AdbMessage(AdbMessage::kCommandCNXN, kVersion,
335 kMaxPayload, kHostConnectMessage))); 361 kMaxPayload, kHostConnectMessage)));
336 ReadHeader(); 362 ReadHeader();
337 } 363 }
338 364
339 net::StreamSocket* AndroidUsbDevice::CreateSocket(const std::string& command) { 365 net::StreamSocket* AndroidUsbDevice::CreateSocket(const std::string& command) {
340 if (!usb_handle_.get()) 366 if (!usb_handle_.get())
341 return NULL; 367 return NULL;
342 368
343 uint32 socket_id = ++last_socket_id_; 369 uint32 socket_id = ++last_socket_id_;
344 sockets_[socket_id] = new AndroidUsbSocket(this, socket_id, command, 370 sockets_[socket_id] = new AndroidUsbSocket(this, socket_id, command,
345 base::Bind(&AndroidUsbDevice::SocketDeleted, this, socket_id)); 371 base::Bind(&AndroidUsbDevice::SocketDeleted, this, socket_id));
346 return sockets_[socket_id]; 372 return sockets_[socket_id];
347 } 373 }
348 374
349 void AndroidUsbDevice::Send(uint32 command, 375 void AndroidUsbDevice::Send(uint32 command,
350 uint32 arg0, 376 uint32 arg0,
351 uint32 arg1, 377 uint32 arg1,
352 const std::string& body) { 378 const std::string& body) {
353 scoped_ptr<AdbMessage> message(new AdbMessage(command, arg0, arg1, body)); 379 scoped_ptr<AdbMessage> message(new AdbMessage(command, arg0, arg1, body));
354 // Delay open request if not yet connected. 380 // Delay open request if not yet connected.
355 if (!is_connected_) { 381 if (!is_connected_) {
356 pending_messages_.push_back(message.release()); 382 pending_messages_.push_back(message.release());
357 return; 383 return;
358 } 384 }
359 Queue(message.Pass()); 385 Queue(message.Pass());
360 } 386 }
361 387
362 AndroidUsbDevice::~AndroidUsbDevice() { 388 AndroidUsbDevice::~AndroidUsbDevice() {
363 DCHECK(message_loop_ == base::MessageLoop::current()); 389 DCHECK(task_runner_->BelongsToCurrentThread());
364 Terminate(); 390 Terminate();
365 } 391 }
366 392
367 void AndroidUsbDevice::Queue(scoped_ptr<AdbMessage> message) { 393 void AndroidUsbDevice::Queue(scoped_ptr<AdbMessage> message) {
368 DCHECK(message_loop_ == base::MessageLoop::current()); 394 DCHECK(task_runner_->BelongsToCurrentThread());
369 395
370 // Queue header. 396 // Queue header.
371 std::vector<uint32> header; 397 std::vector<uint32> header;
372 header.push_back(message->command); 398 header.push_back(message->command);
373 header.push_back(message->arg0); 399 header.push_back(message->arg0);
374 header.push_back(message->arg1); 400 header.push_back(message->arg1);
375 bool append_zero = true; 401 bool append_zero = true;
376 if (message->body.empty()) 402 if (message->body.empty())
377 append_zero = false; 403 append_zero = false;
378 if (message->command == AdbMessage::kCommandAUTH && 404 if (message->command == AdbMessage::kCommandAUTH &&
(...skipping 21 matching lines...) Expand all
400 outgoing_queue_.push(body_buffer); 426 outgoing_queue_.push(body_buffer);
401 if (zero_mask_ && (body_length & zero_mask_) == 0) { 427 if (zero_mask_ && (body_length & zero_mask_) == 0) {
402 // Send a zero length packet. 428 // Send a zero length packet.
403 outgoing_queue_.push(new net::IOBufferWithSize(0)); 429 outgoing_queue_.push(new net::IOBufferWithSize(0));
404 } 430 }
405 } 431 }
406 ProcessOutgoing(); 432 ProcessOutgoing();
407 } 433 }
408 434
409 void AndroidUsbDevice::ProcessOutgoing() { 435 void AndroidUsbDevice::ProcessOutgoing() {
410 DCHECK(message_loop_ == base::MessageLoop::current()); 436 DCHECK(task_runner_->BelongsToCurrentThread());
411 437
412 if (outgoing_queue_.empty() || !usb_handle_.get()) 438 if (outgoing_queue_.empty() || !usb_handle_.get())
413 return; 439 return;
414 440
415 BulkMessage message = outgoing_queue_.front(); 441 BulkMessage message = outgoing_queue_.front();
416 outgoing_queue_.pop(); 442 outgoing_queue_.pop();
417 DumpMessage(true, message->data(), message->size()); 443 DumpMessage(true, message->data(), message->size());
418 usb_handle_->BulkTransfer(device::USB_DIRECTION_OUTBOUND, 444
419 outbound_address_, 445 usb_handle_->BulkTransfer(device::USB_DIRECTION_OUTBOUND, outbound_address_,
420 message.get(), 446 message, message->size(), kUsbTimeout,
421 message->size(),
422 kUsbTimeout,
423 base::Bind(&AndroidUsbDevice::OutgoingMessageSent, 447 base::Bind(&AndroidUsbDevice::OutgoingMessageSent,
424 weak_factory_.GetWeakPtr())); 448 weak_factory_.GetWeakPtr()));
425 } 449 }
426 450
427 void AndroidUsbDevice::OutgoingMessageSent(UsbTransferStatus status, 451 void AndroidUsbDevice::OutgoingMessageSent(UsbTransferStatus status,
428 scoped_refptr<net::IOBuffer> buffer, 452 scoped_refptr<net::IOBuffer> buffer,
429 size_t result) { 453 size_t result) {
430 DCHECK(message_loop_ == base::MessageLoop::current()); 454 if (status != device::USB_TRANSFER_COMPLETED) {
455 return;
456 }
431 457
432 if (status != device::USB_TRANSFER_COMPLETED) 458 ProcessOutgoing();
vkuzkokov 2015/04/15 14:15:46 This and other |PostTask|'s are ok to remove as lo
Reilly Grant (use Gerrit) 2015/04/15 20:03:47 I'll replace the PostTask calls in BulkTransfer ca
433 return;
434 message_loop_->PostTask(FROM_HERE,
435 base::Bind(&AndroidUsbDevice::ProcessOutgoing, this));
436 } 459 }
437 460
438 void AndroidUsbDevice::ReadHeader() { 461 void AndroidUsbDevice::ReadHeader() {
439 DCHECK(message_loop_ == base::MessageLoop::current()); 462 DCHECK(task_runner_->BelongsToCurrentThread());
440 463
441 if (!usb_handle_.get()) 464 if (!usb_handle_.get()) {
442 return; 465 return;
466 }
467
443 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kHeaderSize); 468 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kHeaderSize);
444 usb_handle_->BulkTransfer( 469 usb_handle_->BulkTransfer(
445 device::USB_DIRECTION_INBOUND, 470 device::USB_DIRECTION_INBOUND, inbound_address_, buffer, kHeaderSize,
446 inbound_address_,
447 buffer.get(),
448 kHeaderSize,
449 kUsbTimeout, 471 kUsbTimeout,
450 base::Bind(&AndroidUsbDevice::ParseHeader, weak_factory_.GetWeakPtr())); 472 base::Bind(&AndroidUsbDevice::ParseHeader, weak_factory_.GetWeakPtr()));
451 } 473 }
452 474
453 void AndroidUsbDevice::ParseHeader(UsbTransferStatus status, 475 void AndroidUsbDevice::ParseHeader(UsbTransferStatus status,
454 scoped_refptr<net::IOBuffer> buffer, 476 scoped_refptr<net::IOBuffer> buffer,
455 size_t result) { 477 size_t result) {
456 DCHECK(message_loop_ == base::MessageLoop::current()); 478 DCHECK(task_runner_->BelongsToCurrentThread());
457 479
458 if (status == device::USB_TRANSFER_TIMEOUT) { 480 if (status == device::USB_TRANSFER_TIMEOUT) {
459 message_loop_->PostTask(FROM_HERE, 481 ReadHeader();
460 base::Bind(&AndroidUsbDevice::ReadHeader, this));
461 return; 482 return;
462 } 483 }
463 484
464 if (status != device::USB_TRANSFER_COMPLETED || result != kHeaderSize) { 485 if (status != device::USB_TRANSFER_COMPLETED || result != kHeaderSize) {
465 TransferError(status); 486 TransferError(status);
466 return; 487 return;
467 } 488 }
468 489
469 DumpMessage(false, buffer->data(), result); 490 DumpMessage(false, buffer->data(), result);
470 std::vector<uint32> header(6); 491 std::vector<uint32> header(6);
471 memcpy(&header[0], buffer->data(), result); 492 memcpy(&header[0], buffer->data(), result);
472 scoped_ptr<AdbMessage> message( 493 scoped_ptr<AdbMessage> message(
473 new AdbMessage(header[0], header[1], header[2], "")); 494 new AdbMessage(header[0], header[1], header[2], ""));
474 uint32 data_length = header[3]; 495 uint32 data_length = header[3];
475 uint32 data_check = header[4]; 496 uint32 data_check = header[4];
476 uint32 magic = header[5]; 497 uint32 magic = header[5];
477 if ((message->command ^ 0xffffffff) != magic) { 498 if ((message->command ^ 0xffffffff) != magic) {
478 TransferError(device::USB_TRANSFER_ERROR); 499 TransferError(device::USB_TRANSFER_ERROR);
479 return; 500 return;
480 } 501 }
481 502
482 if (data_length == 0) { 503 if (data_length == 0) {
483 message_loop_->PostTask(FROM_HERE, 504 HandleIncoming(message.Pass());
484 base::Bind(&AndroidUsbDevice::HandleIncoming, this, 505 } else {
485 base::Passed(&message))); 506 ReadBody(message.Pass(), data_length, data_check);
486 return;
487 } 507 }
488
489 message_loop_->PostTask(FROM_HERE,
490 base::Bind(&AndroidUsbDevice::ReadBody, this,
491 base::Passed(&message), data_length, data_check));
492 } 508 }
493 509
494 void AndroidUsbDevice::ReadBody(scoped_ptr<AdbMessage> message, 510 void AndroidUsbDevice::ReadBody(scoped_ptr<AdbMessage> message,
495 uint32 data_length, 511 uint32 data_length,
496 uint32 data_check) { 512 uint32 data_check) {
497 DCHECK(message_loop_ == base::MessageLoop::current()); 513 DCHECK(task_runner_->BelongsToCurrentThread());
498 514
499 if (!usb_handle_.get()) 515 if (!usb_handle_.get()) {
500 return; 516 return;
517 }
518
501 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(data_length); 519 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(data_length);
502 usb_handle_->BulkTransfer(device::USB_DIRECTION_INBOUND, 520 usb_handle_->BulkTransfer(
503 inbound_address_, 521 device::USB_DIRECTION_INBOUND, inbound_address_, buffer, data_length,
504 buffer.get(), 522 kUsbTimeout,
505 data_length, 523 base::Bind(&AndroidUsbDevice::ParseBody, weak_factory_.GetWeakPtr(),
506 kUsbTimeout, 524 base::Passed(&message), data_length, data_check));
507 base::Bind(&AndroidUsbDevice::ParseBody,
508 weak_factory_.GetWeakPtr(),
509 base::Passed(&message),
510 data_length,
511 data_check));
512 } 525 }
513 526
514 void AndroidUsbDevice::ParseBody(scoped_ptr<AdbMessage> message, 527 void AndroidUsbDevice::ParseBody(scoped_ptr<AdbMessage> message,
515 uint32 data_length, 528 uint32 data_length,
516 uint32 data_check, 529 uint32 data_check,
517 UsbTransferStatus status, 530 UsbTransferStatus status,
518 scoped_refptr<net::IOBuffer> buffer, 531 scoped_refptr<net::IOBuffer> buffer,
519 size_t result) { 532 size_t result) {
520 DCHECK(message_loop_ == base::MessageLoop::current()); 533 DCHECK(task_runner_->BelongsToCurrentThread());
521 534
522 if (status == device::USB_TRANSFER_TIMEOUT) { 535 if (status == device::USB_TRANSFER_TIMEOUT) {
523 message_loop_->PostTask(FROM_HERE, 536 ReadBody(message.Pass(), data_length, data_check);
524 base::Bind(&AndroidUsbDevice::ReadBody, this,
525 base::Passed(&message), data_length, data_check));
526 return; 537 return;
527 } 538 }
528 539
529 if (status != device::USB_TRANSFER_COMPLETED || 540 if (status != device::USB_TRANSFER_COMPLETED ||
530 static_cast<uint32>(result) != data_length) { 541 static_cast<uint32>(result) != data_length) {
531 TransferError(status); 542 TransferError(status);
532 return; 543 return;
533 } 544 }
534 545
535 DumpMessage(false, buffer->data(), data_length); 546 DumpMessage(false, buffer->data(), data_length);
536 message->body = std::string(buffer->data(), result); 547 message->body = std::string(buffer->data(), result);
537 if (Checksum(message->body) != data_check) { 548 if (Checksum(message->body) != data_check) {
538 TransferError(device::USB_TRANSFER_ERROR); 549 TransferError(device::USB_TRANSFER_ERROR);
539 return; 550 return;
540 } 551 }
541 552
542 message_loop_->PostTask(FROM_HERE, 553 HandleIncoming(message.Pass());
543 base::Bind(&AndroidUsbDevice::HandleIncoming, this,
544 base::Passed(&message)));
545 } 554 }
546 555
547 void AndroidUsbDevice::HandleIncoming(scoped_ptr<AdbMessage> message) { 556 void AndroidUsbDevice::HandleIncoming(scoped_ptr<AdbMessage> message) {
548 DCHECK(message_loop_ == base::MessageLoop::current()); 557 DCHECK(task_runner_->BelongsToCurrentThread());
549 558
550 switch (message->command) { 559 switch (message->command) {
551 case AdbMessage::kCommandAUTH: 560 case AdbMessage::kCommandAUTH:
552 { 561 {
553 DCHECK_EQ(message->arg0, static_cast<uint32>(AdbMessage::kAuthToken)); 562 DCHECK_EQ(message->arg0, static_cast<uint32>(AdbMessage::kAuthToken));
554 if (signature_sent_) { 563 if (signature_sent_) {
555 Queue(make_scoped_ptr(new AdbMessage( 564 Queue(make_scoped_ptr(new AdbMessage(
556 AdbMessage::kCommandAUTH, 565 AdbMessage::kCommandAUTH,
557 AdbMessage::kAuthRSAPublicKey, 0, 566 AdbMessage::kAuthRSAPublicKey, 0,
558 AndroidRSAPublicKey(rsa_key_.get())))); 567 AndroidRSAPublicKey(rsa_key_.get()))));
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
592 it->second->HandleIncoming(message.Pass()); 601 it->second->HandleIncoming(message.Pass());
593 } 602 }
594 break; 603 break;
595 default: 604 default:
596 break; 605 break;
597 } 606 }
598 ReadHeader(); 607 ReadHeader();
599 } 608 }
600 609
601 void AndroidUsbDevice::TransferError(UsbTransferStatus status) { 610 void AndroidUsbDevice::TransferError(UsbTransferStatus status) {
602 DCHECK(message_loop_ == base::MessageLoop::current()); 611 DCHECK(task_runner_->BelongsToCurrentThread());
603 612
604 message_loop_->PostTask(FROM_HERE, 613 Terminate();
605 base::Bind(&AndroidUsbDevice::Terminate, this));
606 } 614 }
607 615
608 void AndroidUsbDevice::TerminateIfReleased( 616 void AndroidUsbDevice::TerminateIfReleased(
609 scoped_refptr<UsbDeviceHandle> usb_handle) { 617 scoped_refptr<UsbDeviceHandle> usb_handle) {
610 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 618 DCHECK_CURRENTLY_ON(BrowserThread::UI);
611 if (usb_handle->GetDevice().get()) 619 if (usb_handle->GetDevice().get()) {
612 return; 620 return;
613 message_loop_->PostTask(FROM_HERE, 621 }
614 base::Bind(&AndroidUsbDevice::Terminate, this)); 622
623 task_runner_->PostTask(FROM_HERE,
624 base::Bind(&AndroidUsbDevice::Terminate, this));
615 } 625 }
616 626
617 void AndroidUsbDevice::Terminate() { 627 void AndroidUsbDevice::Terminate() {
618 DCHECK(message_loop_ == base::MessageLoop::current()); 628 DCHECK(task_runner_->BelongsToCurrentThread());
619 629
620 std::vector<AndroidUsbDevice*>::iterator it = 630 std::vector<AndroidUsbDevice*>::iterator it =
621 std::find(g_devices.Get().begin(), g_devices.Get().end(), this); 631 std::find(g_devices.Get().begin(), g_devices.Get().end(), this);
622 if (it != g_devices.Get().end()) 632 if (it != g_devices.Get().end())
623 g_devices.Get().erase(it); 633 g_devices.Get().erase(it);
624 634
625 if (!usb_handle_.get()) 635 if (!usb_handle_.get())
626 return; 636 return;
627 637
628 // Make sure we zero-out handle so that closing connections did not open 638 // Make sure we zero-out handle so that closing connections did not open
629 // new connections. 639 // new connections.
630 scoped_refptr<UsbDeviceHandle> usb_handle = usb_handle_; 640 scoped_refptr<UsbDeviceHandle> usb_handle = usb_handle_;
631 usb_handle_ = NULL; 641 usb_handle_ = NULL;
632 642
633 // Iterate over copy. 643 // Iterate over copy.
634 AndroidUsbSockets sockets(sockets_); 644 AndroidUsbSockets sockets(sockets_);
635 for (AndroidUsbSockets::iterator it = sockets.begin(); 645 for (AndroidUsbSockets::iterator it = sockets.begin();
636 it != sockets.end(); ++it) { 646 it != sockets.end(); ++it) {
637 it->second->Terminated(true); 647 it->second->Terminated(true);
638 } 648 }
639 DCHECK(sockets_.empty()); 649 DCHECK(sockets_.empty());
640 650
641 BrowserThread::PostTask( 651 BrowserThread::PostTask(
642 BrowserThread::FILE, FROM_HERE, 652 BrowserThread::UI, FROM_HERE,
643 base::Bind(&ReleaseInterface, usb_handle, interface_id_)); 653 base::Bind(&ReleaseInterface, usb_handle, interface_id_));
644 } 654 }
645 655
646 void AndroidUsbDevice::SocketDeleted(uint32 socket_id) { 656 void AndroidUsbDevice::SocketDeleted(uint32 socket_id) {
647 DCHECK(message_loop_ == base::MessageLoop::current()); 657 DCHECK(task_runner_->BelongsToCurrentThread());
648 658
649 sockets_.erase(socket_id); 659 sockets_.erase(socket_id);
650 } 660 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698