OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/device/adb/adb_device_provider.h" | 5 #include "chrome/browser/devtools/device/adb/adb_device_provider.h" |
6 | 6 |
7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "chrome/browser/devtools/device/adb/adb_client_socket.h" | 9 #include "chrome/browser/devtools/device/adb/adb_client_socket.h" |
10 #include "chrome/browser/devtools/device/adb/adb_device_info_query.h" | 10 #include "chrome/browser/devtools/device/adb/adb_device_info_query.h" |
11 | 11 |
12 namespace { | 12 namespace { |
13 | 13 |
14 const char kHostDevicesCommand[] = "host:devices"; | 14 const char kHostDevicesCommand[] = "host:devices"; |
15 const char kHostTransportCommand[] = "host:transport:%s|%s"; | 15 const char kHostTransportCommand[] = "host:transport:%s|%s"; |
16 const char kLocalAbstractCommand[] = "localabstract:%s"; | 16 const char kLocalAbstractCommand[] = "localabstract:%s"; |
17 | 17 |
18 const int kAdbPort = 5037; | 18 const int kAdbPort = 5037; |
19 | 19 |
20 class AdbDeviceImpl : public AndroidDeviceManager::Device { | 20 static void RunCommand(const std::string& serial, |
21 public: | 21 const std::string& command, |
22 AdbDeviceImpl(const std::string& serial, bool is_connected); | 22 const AdbDeviceProvider::CommandCallback& callback) { |
23 virtual void QueryDeviceInfo(const DeviceInfoCallback& callback) OVERRIDE; | 23 std::string query = base::StringPrintf( |
24 | 24 kHostTransportCommand, serial.c_str(), command.c_str()); |
25 virtual void OpenSocket(const std::string& name, | |
26 const SocketCallback& callback) OVERRIDE; | |
27 private: | |
28 virtual ~AdbDeviceImpl() {} | |
29 | |
30 void RunCommand(const std::string& command, | |
31 const CommandCallback& callback); | |
32 }; | |
33 | |
34 AdbDeviceImpl::AdbDeviceImpl(const std::string& serial, bool is_connected) | |
35 : Device(serial, is_connected) { | |
36 } | |
37 | |
38 void AdbDeviceImpl::QueryDeviceInfo(const DeviceInfoCallback& callback) { | |
39 AdbDeviceInfoQuery::Start( | |
40 base::Bind(&AdbDeviceImpl::RunCommand, this), callback); | |
41 } | |
42 | |
43 void AdbDeviceImpl::OpenSocket(const std::string& name, | |
44 const SocketCallback& callback) { | |
45 DCHECK(CalledOnValidThread()); | |
46 std::string socket_name = | |
47 base::StringPrintf(kLocalAbstractCommand, name.c_str()); | |
48 AdbClientSocket::TransportQuery(kAdbPort, serial(), socket_name, callback); | |
49 } | |
50 | |
51 void AdbDeviceImpl::RunCommand(const std::string& command, | |
52 const CommandCallback& callback) { | |
53 DCHECK(CalledOnValidThread()); | |
54 std::string query = base::StringPrintf(kHostTransportCommand, | |
55 serial().c_str(), command.c_str()); | |
56 AdbClientSocket::AdbQuery(kAdbPort, query, callback); | 25 AdbClientSocket::AdbQuery(kAdbPort, query, callback); |
57 } | 26 } |
58 | 27 |
59 // static | 28 static void ReceivedAdbDevices( |
60 void ReceivedAdbDevices( | 29 const AdbDeviceProvider::SerialsCallback& callback, |
61 const AdbDeviceProvider::QueryDevicesCallback& callback, | |
62 int result_code, | 30 int result_code, |
63 const std::string& response) { | 31 const std::string& response) { |
64 AndroidDeviceManager::Devices result; | 32 std::vector<std::string> result; |
65 std::vector<std::string> serials; | 33 std::vector<std::string> serials; |
66 Tokenize(response, "\n", &serials); | 34 Tokenize(response, "\n", &serials); |
67 for (size_t i = 0; i < serials.size(); ++i) { | 35 for (size_t i = 0; i < serials.size(); ++i) { |
68 std::vector<std::string> tokens; | 36 std::vector<std::string> tokens; |
69 Tokenize(serials[i], "\t ", &tokens); | 37 Tokenize(serials[i], "\t ", &tokens); |
70 bool offline = tokens.size() > 1 && tokens[1] == "offline"; | 38 // bool offline = tokens.size() > 1 && tokens[1] == "offline"; |
pfeldman
2014/05/26 09:57:14
Remove these comments.
vkuzkokov
2014/05/26 10:54:03
Done.
| |
71 result.push_back(new AdbDeviceImpl(tokens[0], !offline)); | 39 // result.push_back(new AdbDeviceImpl(tokens[0], !offline)); |
40 result.push_back(tokens[0]); | |
pfeldman
2014/05/26 09:57:14
You would not need to do this if you were returnin
| |
72 } | 41 } |
73 callback.Run(result); | 42 callback.Run(result); |
74 } | 43 } |
75 | 44 |
76 } // namespace | 45 } // namespace |
77 | 46 |
78 AdbDeviceProvider::~AdbDeviceProvider() { | 47 void AdbDeviceProvider::QueryDevices(const SerialsCallback& callback) { |
79 } | |
80 | |
81 void AdbDeviceProvider::QueryDevices(const QueryDevicesCallback& callback) { | |
82 AdbClientSocket::AdbQuery( | 48 AdbClientSocket::AdbQuery( |
83 kAdbPort, kHostDevicesCommand, base::Bind(&ReceivedAdbDevices, callback)); | 49 kAdbPort, kHostDevicesCommand, base::Bind(&ReceivedAdbDevices, callback)); |
84 } | 50 } |
51 | |
52 void AdbDeviceProvider::QueryDeviceInfo(const std::string& serial, | |
53 const DeviceInfoCallback& callback) { | |
54 AdbDeviceInfoQuery::Start(base::Bind(&RunCommand, serial), callback); | |
55 } | |
56 | |
57 void AdbDeviceProvider::OpenSocket(const std::string& serial, | |
58 const std::string& socket_name, | |
59 const SocketCallback& callback) { | |
60 std::string request = | |
61 base::StringPrintf(kLocalAbstractCommand, socket_name.c_str()); | |
62 AdbClientSocket::TransportQuery(kAdbPort, serial, request, callback); | |
63 } | |
64 | |
65 AdbDeviceProvider::~AdbDeviceProvider() { | |
66 } | |
OLD | NEW |