summaryrefslogtreecommitdiffstats
path: root/chrome/browser/devtools/device/adb/adb_device_provider.cc
blob: 93ba0d0320d516269fae63afbb8cc6d163698a32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/devtools/device/adb/adb_device_provider.h"

#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/devtools/device/adb/adb_client_socket.h"
#include "chrome/browser/devtools/device/adb/adb_device_info_query.h"

namespace {

const char kHostDevicesCommand[] = "host:devices";
const char kHostTransportCommand[] = "host:transport:%s|%s";
const char kLocalAbstractCommand[] = "localabstract:%s";

const int kAdbPort = 5037;

static void RunCommand(const std::string& serial,
                       const std::string& command,
                       const AdbDeviceProvider::CommandCallback& callback) {
  std::string query = base::StringPrintf(
      kHostTransportCommand, serial.c_str(), command.c_str());
  AdbClientSocket::AdbQuery(kAdbPort, query, callback);
}

static void ReceivedAdbDevices(
    const AdbDeviceProvider::SerialsCallback& callback,
    int result_code,
    const std::string& response) {
  std::vector<std::string> result;
  if (result_code < 0) {
    callback.Run(result);
    return;
  }
  std::vector<std::string> serials;
  Tokenize(response, "\n", &serials);
  for (size_t i = 0; i < serials.size(); ++i) {
    std::vector<std::string> tokens;
    Tokenize(serials[i], "\t ", &tokens);
    result.push_back(tokens[0]);
  }
  callback.Run(result);
}

} // namespace

void AdbDeviceProvider::QueryDevices(const SerialsCallback& callback) {
  AdbClientSocket::AdbQuery(
      kAdbPort, kHostDevicesCommand, base::Bind(&ReceivedAdbDevices, callback));
}

void AdbDeviceProvider::QueryDeviceInfo(const std::string& serial,
                                        const DeviceInfoCallback& callback) {
  AdbDeviceInfoQuery::Start(base::Bind(&RunCommand, serial), callback);
}

void AdbDeviceProvider::OpenSocket(const std::string& serial,
                                   const std::string& socket_name,
                                   const SocketCallback& callback) {
  std::string request =
      base::StringPrintf(kLocalAbstractCommand, socket_name.c_str());
  AdbClientSocket::TransportQuery(kAdbPort, serial, request, callback);
}

AdbDeviceProvider::~AdbDeviceProvider() {
}