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/adb/adb_device_provider.h"
7 #include "base/strings/string_util.h"
8 #include "base/strings/stringprintf.h"
9 #include "chrome/browser/devtools/device/adb/adb_client_socket.h"
13 const char kHostDevicesCommand
[] = "host:devices";
14 const char kHostTransportCommand
[] = "host:transport:%s|%s";
15 const char kLocalAbstractCommand
[] = "localabstract:%s";
17 const int kAdbPort
= 5037;
19 static void RunCommand(const std::string
& serial
,
20 const std::string
& command
,
21 const AdbDeviceProvider::CommandCallback
& callback
) {
22 std::string query
= base::StringPrintf(
23 kHostTransportCommand
, serial
.c_str(), command
.c_str());
24 AdbClientSocket::AdbQuery(kAdbPort
, query
, callback
);
27 static void ReceivedAdbDevices(
28 const AdbDeviceProvider::SerialsCallback
& callback
,
30 const std::string
& response
) {
31 std::vector
<std::string
> result
;
32 if (result_code
< 0) {
36 std::vector
<std::string
> serials
;
37 Tokenize(response
, "\n", &serials
);
38 for (size_t i
= 0; i
< serials
.size(); ++i
) {
39 std::vector
<std::string
> tokens
;
40 Tokenize(serials
[i
], "\t ", &tokens
);
41 result
.push_back(tokens
[0]);
48 void AdbDeviceProvider::QueryDevices(const SerialsCallback
& callback
) {
49 AdbClientSocket::AdbQuery(
50 kAdbPort
, kHostDevicesCommand
, base::Bind(&ReceivedAdbDevices
, callback
));
53 void AdbDeviceProvider::QueryDeviceInfo(const std::string
& serial
,
54 const DeviceInfoCallback
& callback
) {
55 AndroidDeviceManager::QueryDeviceInfo(base::Bind(&RunCommand
, serial
),
59 void AdbDeviceProvider::OpenSocket(const std::string
& serial
,
60 const std::string
& socket_name
,
61 const SocketCallback
& callback
) {
63 base::StringPrintf(kLocalAbstractCommand
, socket_name
.c_str());
64 AdbClientSocket::TransportQuery(kAdbPort
, serial
, request
, callback
);
67 AdbDeviceProvider::~AdbDeviceProvider() {