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_split.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 for (const base::StringPiece
& line
:
37 base::SplitStringPiece(response
, "\n", base::KEEP_WHITESPACE
,
38 base::SPLIT_WANT_NONEMPTY
)) {
39 std::vector
<base::StringPiece
> tokens
=
40 base::SplitStringPiece(line
, "\t ", base::KEEP_WHITESPACE
,
41 base::SPLIT_WANT_NONEMPTY
);
42 result
.push_back(tokens
[0].as_string());
49 void AdbDeviceProvider::QueryDevices(const SerialsCallback
& callback
) {
50 AdbClientSocket::AdbQuery(
51 kAdbPort
, kHostDevicesCommand
, base::Bind(&ReceivedAdbDevices
, callback
));
54 void AdbDeviceProvider::QueryDeviceInfo(const std::string
& serial
,
55 const DeviceInfoCallback
& callback
) {
56 AndroidDeviceManager::QueryDeviceInfo(base::Bind(&RunCommand
, serial
),
60 void AdbDeviceProvider::OpenSocket(const std::string
& serial
,
61 const std::string
& socket_name
,
62 const SocketCallback
& callback
) {
64 base::StringPrintf(kLocalAbstractCommand
, socket_name
.c_str());
65 AdbClientSocket::TransportQuery(kAdbPort
, serial
, request
, callback
);
68 AdbDeviceProvider::~AdbDeviceProvider() {