| 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 | 11 |
| 11 namespace { | 12 namespace { |
| 12 | 13 |
| 13 const char kHostDevicesCommand[] = "host:devices"; | 14 const char kHostDevicesCommand[] = "host:devices"; |
| 14 const char kHostTransportCommand[] = "host:transport:%s|%s"; | 15 const char kHostTransportCommand[] = "host:transport:%s|%s"; |
| 15 const char kLocalAbstractCommand[] = "localabstract:%s"; | 16 const char kLocalAbstractCommand[] = "localabstract:%s"; |
| 16 | 17 |
| 17 const int kAdbPort = 5037; | 18 const int kAdbPort = 5037; |
| 18 | 19 |
| 19 class AdbDeviceImpl : public AndroidDeviceManager::Device { | 20 class AdbDeviceImpl : public AndroidDeviceManager::Device { |
| 20 public: | 21 public: |
| 21 AdbDeviceImpl(const std::string& serial, bool is_connected); | 22 AdbDeviceImpl(const std::string& serial, bool is_connected); |
| 22 | 23 virtual void QueryDeviceInfo(const DeviceInfoCallback& callback) OVERRIDE; |
| 23 virtual void RunCommand(const std::string& command, | |
| 24 const CommandCallback& callback) OVERRIDE; | |
| 25 | 24 |
| 26 virtual void OpenSocket(const std::string& name, | 25 virtual void OpenSocket(const std::string& name, |
| 27 const SocketCallback& callback) OVERRIDE; | 26 const SocketCallback& callback) OVERRIDE; |
| 28 private: | 27 private: |
| 29 virtual ~AdbDeviceImpl() {} | 28 virtual ~AdbDeviceImpl() {} |
| 29 |
| 30 void RunCommand(const std::string& command, |
| 31 const CommandCallback& callback); |
| 30 }; | 32 }; |
| 31 | 33 |
| 32 AdbDeviceImpl::AdbDeviceImpl(const std::string& serial, bool is_connected) | 34 AdbDeviceImpl::AdbDeviceImpl(const std::string& serial, bool is_connected) |
| 33 : Device(serial, is_connected) { | 35 : Device(serial, is_connected) { |
| 34 } | 36 } |
| 35 | 37 |
| 36 void AdbDeviceImpl::RunCommand(const std::string& command, | 38 void AdbDeviceImpl::QueryDeviceInfo(const DeviceInfoCallback& callback) { |
| 37 const CommandCallback& callback) { | 39 AdbDeviceInfoQuery::Start( |
| 38 DCHECK(CalledOnValidThread()); | 40 base::Bind(&AdbDeviceImpl::RunCommand, this), callback); |
| 39 std::string query = base::StringPrintf(kHostTransportCommand, | |
| 40 serial().c_str(), command.c_str()); | |
| 41 AdbClientSocket::AdbQuery(kAdbPort, query, callback); | |
| 42 } | 41 } |
| 43 | 42 |
| 44 void AdbDeviceImpl::OpenSocket(const std::string& name, | 43 void AdbDeviceImpl::OpenSocket(const std::string& name, |
| 45 const SocketCallback& callback) { | 44 const SocketCallback& callback) { |
| 46 DCHECK(CalledOnValidThread()); | 45 DCHECK(CalledOnValidThread()); |
| 47 std::string socket_name = | 46 std::string socket_name = |
| 48 base::StringPrintf(kLocalAbstractCommand, name.c_str()); | 47 base::StringPrintf(kLocalAbstractCommand, name.c_str()); |
| 49 AdbClientSocket::TransportQuery(kAdbPort, serial(), socket_name, callback); | 48 AdbClientSocket::TransportQuery(kAdbPort, serial(), socket_name, callback); |
| 50 } | 49 } |
| 51 | 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); |
| 57 } |
| 58 |
| 52 // static | 59 // static |
| 53 void ReceivedAdbDevices( | 60 void ReceivedAdbDevices( |
| 54 const AdbDeviceProvider::QueryDevicesCallback& callback, | 61 const AdbDeviceProvider::QueryDevicesCallback& callback, |
| 55 int result_code, | 62 int result_code, |
| 56 const std::string& response) { | 63 const std::string& response) { |
| 57 AndroidDeviceManager::Devices result; | 64 AndroidDeviceManager::Devices result; |
| 58 std::vector<std::string> serials; | 65 std::vector<std::string> serials; |
| 59 Tokenize(response, "\n", &serials); | 66 Tokenize(response, "\n", &serials); |
| 60 for (size_t i = 0; i < serials.size(); ++i) { | 67 for (size_t i = 0; i < serials.size(); ++i) { |
| 61 std::vector<std::string> tokens; | 68 std::vector<std::string> tokens; |
| 62 Tokenize(serials[i], "\t ", &tokens); | 69 Tokenize(serials[i], "\t ", &tokens); |
| 63 bool offline = tokens.size() > 1 && tokens[1] == "offline"; | 70 bool offline = tokens.size() > 1 && tokens[1] == "offline"; |
| 64 result.push_back(new AdbDeviceImpl(tokens[0], !offline)); | 71 result.push_back(new AdbDeviceImpl(tokens[0], !offline)); |
| 65 } | 72 } |
| 66 callback.Run(result); | 73 callback.Run(result); |
| 67 } | 74 } |
| 68 | 75 |
| 69 } // namespace | 76 } // namespace |
| 70 | 77 |
| 71 AdbDeviceProvider::~AdbDeviceProvider() { | 78 AdbDeviceProvider::~AdbDeviceProvider() { |
| 72 } | 79 } |
| 73 | 80 |
| 74 void AdbDeviceProvider::QueryDevices(const QueryDevicesCallback& callback) { | 81 void AdbDeviceProvider::QueryDevices(const QueryDevicesCallback& callback) { |
| 75 AdbClientSocket::AdbQuery( | 82 AdbClientSocket::AdbQuery( |
| 76 kAdbPort, kHostDevicesCommand, base::Bind(&ReceivedAdbDevices, callback)); | 83 kAdbPort, kHostDevicesCommand, base::Bind(&ReceivedAdbDevices, callback)); |
| 77 } | 84 } |
| OLD | NEW |