Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / devtools / device / android_device_manager.cc
blob32a4f5b6413d7fc26c9ddbf677870219d165d562
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/android_device_manager.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/threading/thread.h"
11 #include "chrome/browser/devtools/device/adb/adb_client_socket.h"
12 #include "chrome/browser/devtools/device/usb/android_rsa.h"
13 #include "chrome/browser/devtools/device/usb/android_usb_device.h"
14 #include "net/base/net_errors.h"
15 #include "net/base/net_util.h"
16 #include "net/socket/tcp_client_socket.h"
18 using content::BrowserThread;
20 namespace {
22 const char kHostTransportCommand[] = "host:transport:%s|%s";
23 const char kHostDevicesCommand[] = "host:devices";
24 const char kLocalAbstractCommand[] = "localabstract:%s";
26 const int kAdbPort = 5037;
27 const int kBufferSize = 16 * 1024;
29 const char kDeviceModelCommand[] = "shell:getprop ro.product.model";
30 const char kOpenedUnixSocketsCommand[] = "shell:cat /proc/net/unix";
31 const char kOpenedUnixSocketsResponse[] =
32 "Num RefCount Protocol Flags Type St Inode Path\n"
33 "00000000: 00000002 00000000 00010000 0001 01 20894 @%s\n";
34 const char kRemoteDebuggingSocket[] = "chrome_devtools_remote";
35 const char kLocalChrome[] = "Local Chrome";
36 const char kLocalhost[] = "127.0.0.1";
38 // AdbDeviceImpl --------------------------------------------------------------
40 class AdbDeviceImpl : public AndroidDeviceManager::Device {
41 public:
42 AdbDeviceImpl(const std::string& serial, bool is_connected);
43 virtual void RunCommand(const std::string& command,
44 const CommandCallback& callback) OVERRIDE;
45 virtual void OpenSocket(const std::string& name,
46 const SocketCallback& callback) OVERRIDE;
47 private:
48 virtual ~AdbDeviceImpl() {}
51 AdbDeviceImpl::AdbDeviceImpl(const std::string& serial, bool is_connected)
52 : Device(serial, is_connected) {
55 void AdbDeviceImpl::RunCommand(const std::string& command,
56 const CommandCallback& callback) {
57 DCHECK(CalledOnValidThread());
58 std::string query = base::StringPrintf(kHostTransportCommand,
59 serial().c_str(), command.c_str());
60 AdbClientSocket::AdbQuery(kAdbPort, query, callback);
63 void AdbDeviceImpl::OpenSocket(const std::string& name,
64 const SocketCallback& callback) {
65 DCHECK(CalledOnValidThread());
66 std::string socket_name =
67 base::StringPrintf(kLocalAbstractCommand, name.c_str());
68 AdbClientSocket::TransportQuery(kAdbPort, serial(), socket_name, callback);
71 // UsbDeviceImpl --------------------------------------------------------------
73 class UsbDeviceImpl : public AndroidDeviceManager::Device {
74 public:
75 explicit UsbDeviceImpl(AndroidUsbDevice* device);
76 virtual void RunCommand(const std::string& command,
77 const CommandCallback& callback) OVERRIDE;
78 virtual void OpenSocket(const std::string& name,
79 const SocketCallback& callback) OVERRIDE;
80 private:
81 void OnOpenSocket(const SocketCallback& callback,
82 net::StreamSocket* socket,
83 int result);
84 void OpenedForCommand(const CommandCallback& callback,
85 net::StreamSocket* socket,
86 int result);
87 void OnRead(net::StreamSocket* socket,
88 scoped_refptr<net::IOBuffer> buffer,
89 const std::string& data,
90 const CommandCallback& callback,
91 int result);
93 virtual ~UsbDeviceImpl() {}
94 scoped_refptr<AndroidUsbDevice> device_;
98 UsbDeviceImpl::UsbDeviceImpl(AndroidUsbDevice* device)
99 : Device(device->serial(), device->is_connected()),
100 device_(device) {
101 device_->InitOnCallerThread();
104 void UsbDeviceImpl::RunCommand(const std::string& command,
105 const CommandCallback& callback) {
106 DCHECK(CalledOnValidThread());
107 net::StreamSocket* socket = device_->CreateSocket(command);
108 if (!socket) {
109 callback.Run(net::ERR_CONNECTION_FAILED, std::string());
110 return;
112 int result = socket->Connect(base::Bind(&UsbDeviceImpl::OpenedForCommand,
113 this, callback, socket));
114 if (result != net::ERR_IO_PENDING)
115 callback.Run(result, std::string());
118 void UsbDeviceImpl::OpenSocket(const std::string& name,
119 const SocketCallback& callback) {
120 DCHECK(CalledOnValidThread());
121 std::string socket_name =
122 base::StringPrintf(kLocalAbstractCommand, name.c_str());
123 net::StreamSocket* socket = device_->CreateSocket(socket_name);
124 if (!socket) {
125 callback.Run(net::ERR_CONNECTION_FAILED, NULL);
126 return;
128 int result = socket->Connect(base::Bind(&UsbDeviceImpl::OnOpenSocket, this,
129 callback, socket));
130 if (result != net::ERR_IO_PENDING)
131 callback.Run(result, NULL);
134 void UsbDeviceImpl::OnOpenSocket(const SocketCallback& callback,
135 net::StreamSocket* socket,
136 int result) {
137 callback.Run(result, result == net::OK ? socket : NULL);
140 void UsbDeviceImpl::OpenedForCommand(const CommandCallback& callback,
141 net::StreamSocket* socket,
142 int result) {
143 if (result != net::OK) {
144 callback.Run(result, std::string());
145 return;
147 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kBufferSize);
148 result = socket->Read(buffer, kBufferSize,
149 base::Bind(&UsbDeviceImpl::OnRead, this,
150 socket, buffer, std::string(), callback));
151 if (result != net::ERR_IO_PENDING)
152 OnRead(socket, buffer, std::string(), callback, result);
155 void UsbDeviceImpl::OnRead(net::StreamSocket* socket,
156 scoped_refptr<net::IOBuffer> buffer,
157 const std::string& data,
158 const CommandCallback& callback,
159 int result) {
160 if (result <= 0) {
161 callback.Run(result, result == 0 ? data : std::string());
162 delete socket;
163 return;
166 std::string new_data = data + std::string(buffer->data(), result);
167 result = socket->Read(buffer, kBufferSize,
168 base::Bind(&UsbDeviceImpl::OnRead, this,
169 socket, buffer, new_data, callback));
170 if (result != net::ERR_IO_PENDING)
171 OnRead(socket, buffer, new_data, callback, result);
174 // AdbDeviceProvider -------------------------------------------
176 class AdbDeviceProvider : public AndroidDeviceManager::DeviceProvider {
177 public:
178 virtual void QueryDevices(const QueryDevicesCallback& callback) OVERRIDE;
179 private:
180 void ReceivedAdbDevices(const QueryDevicesCallback& callback, int result,
181 const std::string& response);
183 virtual ~AdbDeviceProvider();
186 AdbDeviceProvider::~AdbDeviceProvider() {
189 void AdbDeviceProvider::QueryDevices(const QueryDevicesCallback& callback) {
190 AdbClientSocket::AdbQuery(
191 kAdbPort, kHostDevicesCommand,
192 base::Bind(&AdbDeviceProvider::ReceivedAdbDevices, this, callback));
195 void AdbDeviceProvider::ReceivedAdbDevices(const QueryDevicesCallback& callback,
196 int result_code,
197 const std::string& response) {
198 AndroidDeviceManager::Devices result;
199 std::vector<std::string> serials;
200 Tokenize(response, "\n", &serials);
201 for (size_t i = 0; i < serials.size(); ++i) {
202 std::vector<std::string> tokens;
203 Tokenize(serials[i], "\t ", &tokens);
204 bool offline = tokens.size() > 1 && tokens[1] == "offline";
205 result.push_back(new AdbDeviceImpl(tokens[0], !offline));
207 callback.Run(result);
210 // UsbDeviceProvider -------------------------------------------
212 class UsbDeviceProvider : public AndroidDeviceManager::DeviceProvider {
213 public:
214 explicit UsbDeviceProvider(Profile* profile);
216 virtual void QueryDevices(const QueryDevicesCallback& callback) OVERRIDE;
217 private:
218 virtual ~UsbDeviceProvider();
219 void EnumeratedDevices(const QueryDevicesCallback& callback,
220 const AndroidUsbDevices& devices);
222 scoped_ptr<crypto::RSAPrivateKey> rsa_key_;
225 UsbDeviceProvider::UsbDeviceProvider(Profile* profile){
226 rsa_key_.reset(AndroidRSAPrivateKey(profile));
229 UsbDeviceProvider::~UsbDeviceProvider() {
232 void UsbDeviceProvider::QueryDevices(const QueryDevicesCallback& callback) {
233 AndroidUsbDevice::Enumerate(rsa_key_.get(),
234 base::Bind(&UsbDeviceProvider::EnumeratedDevices, this, callback));
237 void UsbDeviceProvider::EnumeratedDevices(const QueryDevicesCallback& callback,
238 const AndroidUsbDevices& devices) {
239 AndroidDeviceManager::Devices result;
240 for (AndroidUsbDevices::const_iterator it = devices.begin();
241 it != devices.end(); ++it)
242 result.push_back(new UsbDeviceImpl(*it));
243 callback.Run(result);
246 } // namespace
248 AndroidDeviceManager::Device::Device(const std::string& serial,
249 bool is_connected)
250 : serial_(serial),
251 is_connected_(is_connected) {
254 void AndroidDeviceManager::Device::HttpQuery(
255 const std::string& la_name,
256 const std::string& request,
257 const CommandCallback& callback) {
258 DCHECK(CalledOnValidThread());
259 OpenSocket(la_name, base::Bind(
260 &Device::OnHttpSocketOpened, this, request, callback));
263 void AndroidDeviceManager::Device::HttpUpgrade(
264 const std::string& la_name,
265 const std::string& request,
266 const SocketCallback& callback) {
267 DCHECK(CalledOnValidThread());
268 OpenSocket(la_name, base::Bind(
269 &Device::OnHttpSocketOpened2, this, request, callback));
272 AndroidDeviceManager::Device::~Device() {
275 void AndroidDeviceManager::Device::OnHttpSocketOpened(
276 const std::string& request,
277 const CommandCallback& callback,
278 int result,
279 net::StreamSocket* socket) {
280 if (result != net::OK) {
281 callback.Run(result, std::string());
282 return;
284 AdbClientSocket::HttpQuery(socket, request, callback);
287 void AndroidDeviceManager::Device::OnHttpSocketOpened2(
288 const std::string& request,
289 const SocketCallback& callback,
290 int result,
291 net::StreamSocket* socket) {
292 if (result != net::OK) {
293 callback.Run(result, NULL);
294 return;
296 AdbClientSocket::HttpQuery(socket, request, callback);
299 AndroidDeviceManager::DeviceProvider::DeviceProvider() {
302 AndroidDeviceManager::DeviceProvider::~DeviceProvider() {
305 // static
306 scoped_refptr<AndroidDeviceManager::DeviceProvider>
307 AndroidDeviceManager::GetUsbDeviceProvider(Profile* profile) {
308 return new UsbDeviceProvider(profile);
311 // static
312 scoped_refptr<AndroidDeviceManager::DeviceProvider>
313 AndroidDeviceManager::GetAdbDeviceProvider() {
314 return new AdbDeviceProvider();
318 class SelfAsDevice : public AndroidDeviceManager::Device {
319 public:
320 explicit SelfAsDevice(int port);
322 virtual void RunCommand(const std::string& command,
323 const CommandCallback& callback) OVERRIDE;
324 virtual void OpenSocket(const std::string& socket_name,
325 const SocketCallback& callback) OVERRIDE;
326 private:
327 void RunCommandCallback(const CommandCallback& callback,
328 const std::string& response,
329 int result);
331 void RunSocketCallback(const SocketCallback& callback,
332 net::StreamSocket* socket,
333 int result);
334 virtual ~SelfAsDevice() {}
336 int port_;
339 SelfAsDevice::SelfAsDevice(int port)
340 : Device("local", true),
341 port_(port)
344 void SelfAsDevice::RunCommandCallback(const CommandCallback& callback,
345 const std::string& response,
346 int result) {
347 callback.Run(result, response);
350 void SelfAsDevice::RunSocketCallback(const SocketCallback& callback,
351 net::StreamSocket* socket,
352 int result) {
353 callback.Run(result, socket);
356 void SelfAsDevice::RunCommand(const std::string& command,
357 const CommandCallback& callback) {
358 DCHECK(CalledOnValidThread());
359 std::string response;
360 if (command == kDeviceModelCommand) {
361 response = kLocalChrome;
362 } else if (command == kOpenedUnixSocketsCommand) {
363 response = base::StringPrintf(kOpenedUnixSocketsResponse,
364 kRemoteDebuggingSocket);
367 base::MessageLoop::current()->PostTask(FROM_HERE,
368 base::Bind(&SelfAsDevice::RunCommandCallback, this, callback,
369 response, 0));
372 void SelfAsDevice::OpenSocket(const std::string& socket_name,
373 const SocketCallback& callback) {
374 DCHECK(CalledOnValidThread());
375 // Use plain socket for remote debugging and port forwarding on Desktop
376 // (debugging purposes).
377 net::IPAddressNumber ip_number;
378 net::ParseIPLiteralToNumber(kLocalhost, &ip_number);
380 int port = 0;
381 if (socket_name == kRemoteDebuggingSocket)
382 port = port_;
383 else
384 base::StringToInt(socket_name, &port);
386 net::AddressList address_list =
387 net::AddressList::CreateFromIPAddress(ip_number, port);
388 net::TCPClientSocket* socket = new net::TCPClientSocket(
389 address_list, NULL, net::NetLog::Source());
390 socket->Connect(base::Bind(&SelfAsDevice::RunSocketCallback, this, callback,
391 socket));
394 class SelfAsDeviceProvider : public AndroidDeviceManager::DeviceProvider {
395 public:
396 explicit SelfAsDeviceProvider(int port);
398 virtual void QueryDevices(const QueryDevicesCallback& callback) OVERRIDE;
399 private:
400 virtual ~SelfAsDeviceProvider(){}
402 int port_;
405 SelfAsDeviceProvider::SelfAsDeviceProvider(int port)
406 : port_(port) {
409 void SelfAsDeviceProvider::QueryDevices(const QueryDevicesCallback& callback) {
410 AndroidDeviceManager::Devices result;
411 result.push_back(new SelfAsDevice(port_));
412 callback.Run(result);
415 // static
416 scoped_refptr<AndroidDeviceManager::DeviceProvider>
417 AndroidDeviceManager::GetSelfAsDeviceProvider(int port) {
418 return new SelfAsDeviceProvider(port);
421 // static
422 scoped_refptr<AndroidDeviceManager> AndroidDeviceManager::Create() {
423 return new AndroidDeviceManager();
426 void AndroidDeviceManager::QueryDevices(
427 const DeviceProviders& providers,
428 const QueryDevicesCallback& callback) {
429 DCHECK(CalledOnValidThread());
430 stopped_ = false;
431 Devices empty;
432 QueryNextProvider(callback, providers, empty, empty);
435 void AndroidDeviceManager::Stop() {
436 DCHECK(CalledOnValidThread());
437 stopped_ = true;
438 devices_.clear();
441 bool AndroidDeviceManager::IsConnected(const std::string& serial) {
442 DCHECK(CalledOnValidThread());
443 Device* device = FindDevice(serial);
444 return device && device->is_connected();
447 void AndroidDeviceManager::RunCommand(
448 const std::string& serial,
449 const std::string& command,
450 const CommandCallback& callback) {
451 DCHECK(CalledOnValidThread());
452 Device* device = FindDevice(serial);
453 if (device)
454 device->RunCommand(command, callback);
455 else
456 callback.Run(net::ERR_CONNECTION_FAILED, std::string());
459 void AndroidDeviceManager::OpenSocket(
460 const std::string& serial,
461 const std::string& socket_name,
462 const SocketCallback& callback) {
463 DCHECK(CalledOnValidThread());
464 Device* device = FindDevice(serial);
465 if (device)
466 device->OpenSocket(socket_name, callback);
467 else
468 callback.Run(net::ERR_CONNECTION_FAILED, NULL);
471 void AndroidDeviceManager::HttpQuery(
472 const std::string& serial,
473 const std::string& la_name,
474 const std::string& request,
475 const CommandCallback& callback) {
476 DCHECK(CalledOnValidThread());
477 Device* device = FindDevice(serial);
478 if (device)
479 device->HttpQuery(la_name, request, callback);
480 else
481 callback.Run(net::ERR_CONNECTION_FAILED, std::string());
484 void AndroidDeviceManager::HttpUpgrade(
485 const std::string& serial,
486 const std::string& la_name,
487 const std::string& request,
488 const SocketCallback& callback) {
489 DCHECK(CalledOnValidThread());
490 Device* device = FindDevice(serial);
491 if (device)
492 device->HttpUpgrade(la_name, request, callback);
493 else
494 callback.Run(net::ERR_CONNECTION_FAILED, NULL);
497 AndroidDeviceManager::AndroidDeviceManager()
498 : stopped_(false) {
501 AndroidDeviceManager::~AndroidDeviceManager() {
504 void AndroidDeviceManager::QueryNextProvider(
505 const QueryDevicesCallback& callback,
506 const DeviceProviders& providers,
507 const Devices& total_devices,
508 const Devices& new_devices) {
509 DCHECK(CalledOnValidThread());
511 if (stopped_)
512 return;
514 Devices more_devices(total_devices);
515 more_devices.insert(
516 more_devices.end(), new_devices.begin(), new_devices.end());
518 if (providers.empty()) {
519 std::vector<std::string> serials;
520 devices_.clear();
521 for (Devices::const_iterator it = more_devices.begin();
522 it != more_devices.end(); ++it) {
523 devices_[(*it)->serial()] = *it;
524 serials.push_back((*it)->serial());
526 callback.Run(serials);
527 return;
530 scoped_refptr<DeviceProvider> current_provider = providers.back();
531 DeviceProviders less_providers = providers;
532 less_providers.pop_back();
533 current_provider->QueryDevices(
534 base::Bind(&AndroidDeviceManager::QueryNextProvider,
535 this, callback, less_providers, more_devices));
538 AndroidDeviceManager::Device*
539 AndroidDeviceManager::FindDevice(const std::string& serial) {
540 DCHECK(CalledOnValidThread());
541 DeviceMap::const_iterator it = devices_.find(serial);
542 if (it == devices_.end())
543 return NULL;
544 return (*it).second.get();