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 <stddef.h> |
| 6 #include <stdint.h> |
| 7 |
5 #include <algorithm> | 8 #include <algorithm> |
6 | 9 |
7 #include "base/containers/scoped_ptr_hash_map.h" | 10 #include "base/containers/scoped_ptr_hash_map.h" |
8 #include "base/location.h" | 11 #include "base/location.h" |
9 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
10 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
11 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
12 #include "chrome/browser/devtools/device/adb/mock_adb_server.h" | 15 #include "chrome/browser/devtools/device/adb/mock_adb_server.h" |
13 #include "chrome/browser/devtools/device/devtools_android_bridge.h" | 16 #include "chrome/browser/devtools/device/devtools_android_bridge.h" |
14 #include "chrome/browser/devtools/device/usb/android_usb_device.h" | 17 #include "chrome/browser/devtools/device/usb/android_usb_device.h" |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 }; | 67 }; |
65 | 68 |
66 struct BreakingAndroidTraits { | 69 struct BreakingAndroidTraits { |
67 static const int kClass = 0xff; | 70 static const int kClass = 0xff; |
68 static const int kSubclass = 0x42; | 71 static const int kSubclass = 0x42; |
69 static const int kProtocol = 0x1; | 72 static const int kProtocol = 0x1; |
70 static const bool kBreaks = true; | 73 static const bool kBreaks = true; |
71 static const bool kConfigured = true; | 74 static const bool kConfigured = true; |
72 }; | 75 }; |
73 | 76 |
74 const uint32 kMaxPayload = 4096; | 77 const uint32_t kMaxPayload = 4096; |
75 const uint32 kVersion = 0x01000000; | 78 const uint32_t kVersion = 0x01000000; |
76 | 79 |
77 const char kDeviceManufacturer[] = "Test Manufacturer"; | 80 const char kDeviceManufacturer[] = "Test Manufacturer"; |
78 const char kDeviceModel[] = "Nexus 6"; | 81 const char kDeviceModel[] = "Nexus 6"; |
79 const char kDeviceSerial[] = "01498B321301A00A"; | 82 const char kDeviceSerial[] = "01498B321301A00A"; |
80 | 83 |
81 template <class T> | 84 template <class T> |
82 class MockUsbDevice; | 85 class MockUsbDevice; |
83 | 86 |
84 class MockLocalSocket : public MockAndroidConnection::Delegate { | 87 class MockLocalSocket : public MockAndroidConnection::Delegate { |
85 public: | 88 public: |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 void SetInterfaceAlternateSetting(int interface_number, | 164 void SetInterfaceAlternateSetting(int interface_number, |
162 int alternate_setting, | 165 int alternate_setting, |
163 const ResultCallback& callback) override { | 166 const ResultCallback& callback) override { |
164 NOTIMPLEMENTED(); | 167 NOTIMPLEMENTED(); |
165 } | 168 } |
166 | 169 |
167 void ResetDevice(const ResultCallback& callback) override { | 170 void ResetDevice(const ResultCallback& callback) override { |
168 NOTIMPLEMENTED(); | 171 NOTIMPLEMENTED(); |
169 } | 172 } |
170 | 173 |
171 void ClearHalt(uint8 endpoint, const ResultCallback& callback) override { | 174 void ClearHalt(uint8_t endpoint, const ResultCallback& callback) override { |
172 NOTIMPLEMENTED(); | 175 NOTIMPLEMENTED(); |
173 } | 176 } |
174 | 177 |
175 // Async IO. Can be called on any thread. | 178 // Async IO. Can be called on any thread. |
176 void ControlTransfer(UsbEndpointDirection direction, | 179 void ControlTransfer(UsbEndpointDirection direction, |
177 TransferRequestType request_type, | 180 TransferRequestType request_type, |
178 TransferRecipient recipient, | 181 TransferRecipient recipient, |
179 uint8 request, | 182 uint8_t request, |
180 uint16 value, | 183 uint16_t value, |
181 uint16 index, | 184 uint16_t index, |
182 scoped_refptr<net::IOBuffer> buffer, | 185 scoped_refptr<net::IOBuffer> buffer, |
183 size_t length, | 186 size_t length, |
184 unsigned int timeout, | 187 unsigned int timeout, |
185 const TransferCallback& callback) override {} | 188 const TransferCallback& callback) override {} |
186 | 189 |
187 void GenericTransfer(UsbEndpointDirection direction, | 190 void GenericTransfer(UsbEndpointDirection direction, |
188 uint8 endpoint, | 191 uint8_t endpoint, |
189 scoped_refptr<net::IOBuffer> buffer, | 192 scoped_refptr<net::IOBuffer> buffer, |
190 size_t length, | 193 size_t length, |
191 unsigned int timeout, | 194 unsigned int timeout, |
192 const TransferCallback& callback) override { | 195 const TransferCallback& callback) override { |
193 if (direction == device::USB_DIRECTION_OUTBOUND) { | 196 if (direction == device::USB_DIRECTION_OUTBOUND) { |
194 if (remaining_body_length_ == 0) { | 197 if (remaining_body_length_ == 0) { |
195 std::vector<uint32> header(6); | 198 std::vector<uint32_t> header(6); |
196 memcpy(&header[0], buffer->data(), length); | 199 memcpy(&header[0], buffer->data(), length); |
197 current_message_.reset( | 200 current_message_.reset( |
198 new AdbMessage(header[0], header[1], header[2], std::string())); | 201 new AdbMessage(header[0], header[1], header[2], std::string())); |
199 remaining_body_length_ = header[3]; | 202 remaining_body_length_ = header[3]; |
200 uint32 magic = header[5]; | 203 uint32_t magic = header[5]; |
201 if ((current_message_->command ^ 0xffffffff) != magic) { | 204 if ((current_message_->command ^ 0xffffffff) != magic) { |
202 DCHECK(false) << "Header checksum error"; | 205 DCHECK(false) << "Header checksum error"; |
203 return; | 206 return; |
204 } | 207 } |
205 } else { | 208 } else { |
206 DCHECK(current_message_.get()); | 209 DCHECK(current_message_.get()); |
207 current_message_->body += std::string(buffer->data(), length); | 210 current_message_->body += std::string(buffer->data(), length); |
208 remaining_body_length_ -= length; | 211 remaining_body_length_ -= length; |
209 } | 212 } |
210 | 213 |
(...skipping 19 matching lines...) Expand all Loading... |
230 } | 233 } |
231 | 234 |
232 template <class D> | 235 template <class D> |
233 void append(D data) { | 236 void append(D data) { |
234 std::copy(reinterpret_cast<char*>(&data), | 237 std::copy(reinterpret_cast<char*>(&data), |
235 (reinterpret_cast<char*>(&data)) + sizeof(D), | 238 (reinterpret_cast<char*>(&data)) + sizeof(D), |
236 std::back_inserter(output_buffer_)); | 239 std::back_inserter(output_buffer_)); |
237 } | 240 } |
238 | 241 |
239 // Copied from AndroidUsbDevice::Checksum | 242 // Copied from AndroidUsbDevice::Checksum |
240 uint32 Checksum(const std::string& data) { | 243 uint32_t Checksum(const std::string& data) { |
241 unsigned char* x = (unsigned char*)data.data(); | 244 unsigned char* x = (unsigned char*)data.data(); |
242 int count = data.length(); | 245 int count = data.length(); |
243 uint32 sum = 0; | 246 uint32_t sum = 0; |
244 while (count-- > 0) | 247 while (count-- > 0) |
245 sum += *x++; | 248 sum += *x++; |
246 return sum; | 249 return sum; |
247 } | 250 } |
248 | 251 |
249 void ProcessIncoming() { | 252 void ProcessIncoming() { |
250 DCHECK(current_message_.get()); | 253 DCHECK(current_message_.get()); |
251 switch (current_message_->command) { | 254 switch (current_message_->command) { |
252 case AdbMessage::kCommandCNXN: { | 255 case AdbMessage::kCommandCNXN: { |
253 WriteResponse(kVersion, | 256 WriteResponse(kVersion, |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 } | 310 } |
308 } | 311 } |
309 ProcessQueries(); | 312 ProcessQueries(); |
310 } | 313 } |
311 | 314 |
312 void WriteResponse(int arg0, int arg1, int command, const std::string& body) { | 315 void WriteResponse(int arg0, int arg1, int command, const std::string& body) { |
313 append(command); | 316 append(command); |
314 append(arg0); | 317 append(arg0); |
315 append(arg1); | 318 append(arg1); |
316 bool add_zero = !body.empty() && (command != AdbMessage::kCommandWRTE); | 319 bool add_zero = !body.empty() && (command != AdbMessage::kCommandWRTE); |
317 append(static_cast<uint32>(body.size() + (add_zero ? 1 : 0))); | 320 append(static_cast<uint32_t>(body.size() + (add_zero ? 1 : 0))); |
318 append(Checksum(body)); | 321 append(Checksum(body)); |
319 append(command ^ 0xffffffff); | 322 append(command ^ 0xffffffff); |
320 std::copy(body.begin(), body.end(), std::back_inserter(output_buffer_)); | 323 std::copy(body.begin(), body.end(), std::back_inserter(output_buffer_)); |
321 if (add_zero) { | 324 if (add_zero) { |
322 output_buffer_.push_back(0); | 325 output_buffer_.push_back(0); |
323 } | 326 } |
324 ProcessQueries(); | 327 ProcessQueries(); |
325 } | 328 } |
326 | 329 |
327 void ProcessQueries() { | 330 void ProcessQueries() { |
(...skipping 14 matching lines...) Expand all Loading... |
342 output_buffer_.begin() + query.size, | 345 output_buffer_.begin() + query.size, |
343 query.buffer->data()); | 346 query.buffer->data()); |
344 output_buffer_.erase(output_buffer_.begin(), | 347 output_buffer_.erase(output_buffer_.begin(), |
345 output_buffer_.begin() + query.size); | 348 output_buffer_.begin() + query.size); |
346 base::ThreadTaskRunnerHandle::Get()->PostTask( | 349 base::ThreadTaskRunnerHandle::Get()->PostTask( |
347 FROM_HERE, base::Bind(query.callback, device::USB_TRANSFER_COMPLETED, | 350 FROM_HERE, base::Bind(query.callback, device::USB_TRANSFER_COMPLETED, |
348 query.buffer, query.size)); | 351 query.buffer, query.size)); |
349 } | 352 } |
350 | 353 |
351 void IsochronousTransfer(UsbEndpointDirection direction, | 354 void IsochronousTransfer(UsbEndpointDirection direction, |
352 uint8 endpoint, | 355 uint8_t endpoint, |
353 scoped_refptr<net::IOBuffer> buffer, | 356 scoped_refptr<net::IOBuffer> buffer, |
354 size_t length, | 357 size_t length, |
355 unsigned int packets, | 358 unsigned int packets, |
356 unsigned int packet_length, | 359 unsigned int packet_length, |
357 unsigned int timeout, | 360 unsigned int timeout, |
358 const TransferCallback& callback) override {} | 361 const TransferCallback& callback) override {} |
359 | 362 |
360 protected: | 363 protected: |
361 virtual ~MockUsbDeviceHandle() {} | 364 virtual ~MockUsbDeviceHandle() {} |
362 | 365 |
363 struct Query { | 366 struct Query { |
364 TransferCallback callback; | 367 TransferCallback callback; |
365 scoped_refptr<net::IOBuffer> buffer; | 368 scoped_refptr<net::IOBuffer> buffer; |
366 size_t size; | 369 size_t size; |
367 | 370 |
368 Query(TransferCallback callback, | 371 Query(TransferCallback callback, |
369 scoped_refptr<net::IOBuffer> buffer, | 372 scoped_refptr<net::IOBuffer> buffer, |
370 int size) | 373 int size) |
371 : callback(callback), buffer(buffer), size(size) {} | 374 : callback(callback), buffer(buffer), size(size) {} |
372 }; | 375 }; |
373 | 376 |
374 scoped_refptr<MockUsbDevice<T> > device_; | 377 scoped_refptr<MockUsbDevice<T> > device_; |
375 uint32 remaining_body_length_; | 378 uint32_t remaining_body_length_; |
376 scoped_ptr<AdbMessage> current_message_; | 379 scoped_ptr<AdbMessage> current_message_; |
377 std::vector<char> output_buffer_; | 380 std::vector<char> output_buffer_; |
378 std::queue<Query> queries_; | 381 std::queue<Query> queries_; |
379 base::ScopedPtrHashMap<int, scoped_ptr<MockLocalSocket>> local_sockets_; | 382 base::ScopedPtrHashMap<int, scoped_ptr<MockLocalSocket>> local_sockets_; |
380 int last_local_socket_; | 383 int last_local_socket_; |
381 bool broken_; | 384 bool broken_; |
382 }; | 385 }; |
383 | 386 |
384 template <class T> | 387 template <class T> |
385 class MockUsbDevice : public UsbDevice { | 388 class MockUsbDevice : public UsbDevice { |
(...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
796 EXPECT_EQ(2, listener.invoked_); | 799 EXPECT_EQ(2, listener.invoked_); |
797 EXPECT_EQ(listener.invoked_ - 1, scheduler_invoked_); | 800 EXPECT_EQ(listener.invoked_ - 1, scheduler_invoked_); |
798 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting()); | 801 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting()); |
799 } | 802 } |
800 | 803 |
801 IN_PROC_BROWSER_TEST_F(AndroidUsbTraitsTest, TestDeviceCounting) { | 804 IN_PROC_BROWSER_TEST_F(AndroidUsbTraitsTest, TestDeviceCounting) { |
802 MockCountListenerForCheckingTraits listener(adb_bridge_); | 805 MockCountListenerForCheckingTraits listener(adb_bridge_); |
803 adb_bridge_->AddDeviceCountListener(&listener); | 806 adb_bridge_->AddDeviceCountListener(&listener); |
804 runner_->Run(); | 807 runner_->Run(); |
805 } | 808 } |
OLD | NEW |