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/devtools_adb_bridge.h" | 5 #include "chrome/browser/devtools/devtools_adb_bridge.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/base64.h" | 10 #include "base/base64.h" |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 | 77 |
78 // AdbDeviceImpl -------------------------------------------------------------- | 78 // AdbDeviceImpl -------------------------------------------------------------- |
79 | 79 |
80 class AdbDeviceImpl : public DevToolsAdbBridge::AndroidDevice { | 80 class AdbDeviceImpl : public DevToolsAdbBridge::AndroidDevice { |
81 public: | 81 public: |
82 explicit AdbDeviceImpl(const std::string& serial); | 82 explicit AdbDeviceImpl(const std::string& serial); |
83 virtual void RunCommand(const std::string& command, | 83 virtual void RunCommand(const std::string& command, |
84 const CommandCallback& callback) OVERRIDE; | 84 const CommandCallback& callback) OVERRIDE; |
85 virtual void OpenSocket(const std::string& name, | 85 virtual void OpenSocket(const std::string& name, |
86 const SocketCallback& callback) OVERRIDE; | 86 const SocketCallback& callback) OVERRIDE; |
| 87 virtual bool IsConnected() OVERRIDE; |
| 88 |
87 private: | 89 private: |
88 virtual ~AdbDeviceImpl() {} | 90 virtual ~AdbDeviceImpl() {} |
89 }; | 91 }; |
90 | 92 |
91 AdbDeviceImpl::AdbDeviceImpl(const std::string& serial) | 93 AdbDeviceImpl::AdbDeviceImpl(const std::string& serial) |
92 : AndroidDevice(serial) { | 94 : AndroidDevice(serial) { |
93 } | 95 } |
94 | 96 |
95 void AdbDeviceImpl::RunCommand(const std::string& command, | 97 void AdbDeviceImpl::RunCommand(const std::string& command, |
96 const CommandCallback& callback) { | 98 const CommandCallback& callback) { |
97 std::string query = base::StringPrintf(kHostTransportCommand, | 99 std::string query = base::StringPrintf(kHostTransportCommand, |
98 serial().c_str(), command.c_str()); | 100 serial().c_str(), command.c_str()); |
99 AdbClientSocket::AdbQuery(kAdbPort, query, callback); | 101 AdbClientSocket::AdbQuery(kAdbPort, query, callback); |
100 } | 102 } |
101 | 103 |
102 void AdbDeviceImpl::OpenSocket(const std::string& name, | 104 void AdbDeviceImpl::OpenSocket(const std::string& name, |
103 const SocketCallback& callback) { | 105 const SocketCallback& callback) { |
104 std::string socket_name = | 106 std::string socket_name = |
105 base::StringPrintf(kLocalAbstractCommand, name.c_str()); | 107 base::StringPrintf(kLocalAbstractCommand, name.c_str()); |
106 AdbClientSocket::TransportQuery(kAdbPort, serial(), socket_name, callback); | 108 AdbClientSocket::TransportQuery(kAdbPort, serial(), socket_name, callback); |
107 } | 109 } |
108 | 110 |
| 111 bool AdbDeviceImpl::IsConnected() { |
| 112 return true; |
| 113 } |
| 114 |
109 | 115 |
110 // UsbDeviceImpl -------------------------------------------------------------- | 116 // UsbDeviceImpl -------------------------------------------------------------- |
111 | 117 |
112 class UsbDeviceImpl : public DevToolsAdbBridge::AndroidDevice { | 118 class UsbDeviceImpl : public DevToolsAdbBridge::AndroidDevice { |
113 public: | 119 public: |
114 explicit UsbDeviceImpl(AndroidUsbDevice* device); | 120 explicit UsbDeviceImpl(AndroidUsbDevice* device); |
115 virtual void RunCommand(const std::string& command, | 121 virtual void RunCommand(const std::string& command, |
116 const CommandCallback& callback) OVERRIDE; | 122 const CommandCallback& callback) OVERRIDE; |
117 virtual void OpenSocket(const std::string& name, | 123 virtual void OpenSocket(const std::string& name, |
118 const SocketCallback& callback) OVERRIDE; | 124 const SocketCallback& callback) OVERRIDE; |
| 125 virtual bool IsConnected() OVERRIDE; |
119 | 126 |
120 private: | 127 private: |
121 void OnOpenSocket(const SocketCallback& callback, | 128 void OnOpenSocket(const SocketCallback& callback, |
122 net::StreamSocket* socket, | 129 net::StreamSocket* socket, |
123 int result); | 130 int result); |
124 void OpenedForCommand(const CommandCallback& callback, | 131 void OpenedForCommand(const CommandCallback& callback, |
125 net::StreamSocket* socket, | 132 net::StreamSocket* socket, |
126 int result); | 133 int result); |
127 void OnRead(net::StreamSocket* socket, | 134 void OnRead(net::StreamSocket* socket, |
128 scoped_refptr<net::IOBuffer> buffer, | 135 scoped_refptr<net::IOBuffer> buffer, |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 } | 201 } |
195 | 202 |
196 std::string new_data = data + std::string(buffer->data(), result); | 203 std::string new_data = data + std::string(buffer->data(), result); |
197 result = socket->Read(buffer, kBufferSize, | 204 result = socket->Read(buffer, kBufferSize, |
198 base::Bind(&UsbDeviceImpl::OnRead, this, | 205 base::Bind(&UsbDeviceImpl::OnRead, this, |
199 socket, buffer, new_data, callback)); | 206 socket, buffer, new_data, callback)); |
200 if (result != net::ERR_IO_PENDING) | 207 if (result != net::ERR_IO_PENDING) |
201 OnRead(socket, buffer, new_data, callback, result); | 208 OnRead(socket, buffer, new_data, callback, result); |
202 } | 209 } |
203 | 210 |
| 211 bool UsbDeviceImpl::IsConnected() { |
| 212 return device_->is_connected(); |
| 213 } |
| 214 |
204 | 215 |
205 // AdbCountDevicesCommand ----------------------------------------------------- | 216 // AdbCountDevicesCommand ----------------------------------------------------- |
206 | 217 |
207 class AdbCountDevicesCommand : public base::RefCountedThreadSafe< | 218 class AdbCountDevicesCommand : public base::RefCountedThreadSafe< |
208 AdbCountDevicesCommand, | 219 AdbCountDevicesCommand, |
209 content::BrowserThread::DeleteOnUIThread> { | 220 content::BrowserThread::DeleteOnUIThread> { |
210 public: | 221 public: |
211 typedef base::Callback<void(int)> Callback; | 222 typedef base::Callback<void(int)> Callback; |
212 | 223 |
213 AdbCountDevicesCommand( | 224 AdbCountDevicesCommand( |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 remote_devices_->back()->AddBrowser(remote_browser); | 402 remote_devices_->back()->AddBrowser(remote_browser); |
392 browsers_.push_back(remote_browser); | 403 browsers_.push_back(remote_browser); |
393 device->HttpQuery( | 404 device->HttpQuery( |
394 std::string(), kVersionRequest, | 405 std::string(), kVersionRequest, |
395 base::Bind(&AdbPagesCommand::ReceivedVersion, this)); | 406 base::Bind(&AdbPagesCommand::ReceivedVersion, this)); |
396 return; | 407 return; |
397 } | 408 } |
398 #endif // defined(DEBUG_DEVTOOLS) | 409 #endif // defined(DEBUG_DEVTOOLS) |
399 | 410 |
400 scoped_refptr<DevToolsAdbBridge::AndroidDevice> device = devices_.back(); | 411 scoped_refptr<DevToolsAdbBridge::AndroidDevice> device = devices_.back(); |
401 device->RunCommand(kDeviceModelCommand, | 412 if (device->IsConnected()) { |
402 base::Bind(&AdbPagesCommand::ReceivedModel, this)); | 413 device->RunCommand(kDeviceModelCommand, |
| 414 base::Bind(&AdbPagesCommand::ReceivedModel, this)); |
| 415 } else { |
| 416 remote_devices_->push_back(new DevToolsAdbBridge::RemoteDevice(device)); |
| 417 devices_.pop_back(); |
| 418 ProcessSerials(); |
| 419 } |
403 } | 420 } |
404 | 421 |
405 void AdbPagesCommand::ReceivedModel(int result, const std::string& response) { | 422 void AdbPagesCommand::ReceivedModel(int result, const std::string& response) { |
406 DCHECK_EQ(adb_thread_->message_loop(), base::MessageLoop::current()); | 423 DCHECK_EQ(adb_thread_->message_loop(), base::MessageLoop::current()); |
407 if (result < 0) { | 424 if (result < 0) { |
408 devices_.pop_back(); | 425 devices_.pop_back(); |
409 ProcessSerials(); | 426 ProcessSerials(); |
410 return; | 427 return; |
411 } | 428 } |
412 scoped_refptr<DevToolsAdbBridge::AndroidDevice> device = devices_.back(); | 429 scoped_refptr<DevToolsAdbBridge::AndroidDevice> device = devices_.back(); |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
632 | 649 |
633 int width; | 650 int width; |
634 int height; | 651 int height; |
635 std::vector<std::string> numbers; | 652 std::vector<std::string> numbers; |
636 Tokenize(pairs[1].substr(1, pairs[1].size() - 2), ",", &numbers); | 653 Tokenize(pairs[1].substr(1, pairs[1].size() - 2), ",", &numbers); |
637 if (numbers.size() != 2 || | 654 if (numbers.size() != 2 || |
638 !base::StringToInt(numbers[0], &width) || | 655 !base::StringToInt(numbers[0], &width) || |
639 !base::StringToInt(numbers[1], &height)) | 656 !base::StringToInt(numbers[1], &height)) |
640 return; | 657 return; |
641 | 658 |
642 remote_devices_->back()->SetScreenSize(gfx::Size(width, height)); | 659 remote_devices_->back()->set_screen_size(gfx::Size(width, height)); |
643 } | 660 } |
644 | 661 |
645 | 662 |
646 // AdbProtocolCommand --------------------------------------------------------- | 663 // AdbProtocolCommand --------------------------------------------------------- |
647 | 664 |
648 class AdbProtocolCommand : public AdbWebSocket::Delegate { | 665 class AdbProtocolCommand : public AdbWebSocket::Delegate { |
649 public: | 666 public: |
650 AdbProtocolCommand( | 667 AdbProtocolCommand( |
651 scoped_refptr<DevToolsAdbBridge::RefCountedAdbThread> adb_thread, | 668 scoped_refptr<DevToolsAdbBridge::RefCountedAdbThread> adb_thread, |
652 scoped_refptr<DevToolsAdbBridge::AndroidDevice> device, | 669 scoped_refptr<DevToolsAdbBridge::AndroidDevice> device, |
(...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1029 } | 1046 } |
1030 | 1047 |
1031 | 1048 |
1032 // DevToolsAdbBridge::RemoteDevice -------------------------------------------- | 1049 // DevToolsAdbBridge::RemoteDevice -------------------------------------------- |
1033 | 1050 |
1034 DevToolsAdbBridge::RemoteDevice::RemoteDevice( | 1051 DevToolsAdbBridge::RemoteDevice::RemoteDevice( |
1035 scoped_refptr<AndroidDevice> device) | 1052 scoped_refptr<AndroidDevice> device) |
1036 : device_(device) { | 1053 : device_(device) { |
1037 } | 1054 } |
1038 | 1055 |
| 1056 std::string DevToolsAdbBridge::RemoteDevice::GetSerial() { |
| 1057 return device_->serial(); |
| 1058 } |
| 1059 |
| 1060 std::string DevToolsAdbBridge::RemoteDevice::GetModel() { |
| 1061 return device_->model(); |
| 1062 } |
| 1063 |
| 1064 bool DevToolsAdbBridge::RemoteDevice::IsConnected() { |
| 1065 return device_->IsConnected(); |
| 1066 } |
| 1067 |
| 1068 void DevToolsAdbBridge::RemoteDevice::AddBrowser( |
| 1069 scoped_refptr<RemoteBrowser> browser) { |
| 1070 browsers_.push_back(browser); |
| 1071 } |
| 1072 |
1039 DevToolsAdbBridge::RemoteDevice::~RemoteDevice() { | 1073 DevToolsAdbBridge::RemoteDevice::~RemoteDevice() { |
1040 } | 1074 } |
1041 | 1075 |
1042 | 1076 |
1043 // DevToolsAdbBridge::RefCountedAdbThread ------------------------------------- | 1077 // DevToolsAdbBridge::RefCountedAdbThread ------------------------------------- |
1044 | 1078 |
1045 DevToolsAdbBridge::RefCountedAdbThread* | 1079 DevToolsAdbBridge::RefCountedAdbThread* |
1046 DevToolsAdbBridge::RefCountedAdbThread::instance_ = NULL; | 1080 DevToolsAdbBridge::RefCountedAdbThread::instance_ = NULL; |
1047 | 1081 |
1048 // static | 1082 // static |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1145 | 1179 |
1146 if (listeners_.empty()) | 1180 if (listeners_.empty()) |
1147 return; | 1181 return; |
1148 | 1182 |
1149 BrowserThread::PostDelayedTask( | 1183 BrowserThread::PostDelayedTask( |
1150 BrowserThread::UI, | 1184 BrowserThread::UI, |
1151 FROM_HERE, | 1185 FROM_HERE, |
1152 base::Bind(&DevToolsAdbBridge::RequestRemoteDevices, this), | 1186 base::Bind(&DevToolsAdbBridge::RequestRemoteDevices, this), |
1153 base::TimeDelta::FromMilliseconds(kAdbPollingIntervalMs)); | 1187 base::TimeDelta::FromMilliseconds(kAdbPollingIntervalMs)); |
1154 } | 1188 } |
OLD | NEW |