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/self_device_provider.h" | 5 #include "chrome/browser/devtools/device/self_device_provider.h" |
6 | 6 |
7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "net/socket/tcp_client_socket.h" | 9 #include "net/socket/tcp_client_socket.h" |
10 | 10 |
11 namespace { | 11 namespace { |
12 | 12 |
13 const char kDeviceModelCommand[] = "shell:getprop ro.product.model"; | |
14 const char kOpenedUnixSocketsCommand[] = "shell:cat /proc/net/unix"; | |
15 const char kOpenedUnixSocketsResponse[] = | |
16 "Num RefCount Protocol Flags Type St Inode Path\n" | |
17 "00000000: 00000002 00000000 00010000 0001 01 20894 @%s\n"; | |
18 const char kRemoteDebuggingSocket[] = "chrome_devtools_remote"; | |
19 | |
20 const char kDeviceModel[] = "Local Chrome"; | 13 const char kDeviceModel[] = "Local Chrome"; |
| 14 const char kBrowserName[] = "Chrome"; |
21 const char kLocalhost[] = "127.0.0.1"; | 15 const char kLocalhost[] = "127.0.0.1"; |
22 | 16 |
23 class SelfAsDevice : public AndroidDeviceManager::Device { | 17 class SelfAsDevice : public AndroidDeviceManager::Device { |
24 public: | 18 public: |
25 explicit SelfAsDevice(int port); | 19 explicit SelfAsDevice(int port); |
26 | 20 |
27 virtual void RunCommand(const std::string& command, | 21 virtual void QueryDeviceInfo(const DeviceInfoCallback& callback) OVERRIDE; |
28 const CommandCallback& callback) OVERRIDE; | 22 |
29 virtual void OpenSocket(const std::string& socket_name, | 23 virtual void OpenSocket(const std::string& socket_name, |
30 const SocketCallback& callback) OVERRIDE; | 24 const SocketCallback& callback) OVERRIDE; |
31 private: | 25 private: |
32 void RunCommandCallback(const CommandCallback& callback, | |
33 const std::string& response, | |
34 int result); | |
35 | |
36 void RunSocketCallback(const SocketCallback& callback, | |
37 net::StreamSocket* socket, | |
38 int result); | |
39 virtual ~SelfAsDevice() {} | 26 virtual ~SelfAsDevice() {} |
40 | 27 |
41 int port_; | 28 int port_; |
42 }; | 29 }; |
43 | 30 |
44 SelfAsDevice::SelfAsDevice(int port) | 31 SelfAsDevice::SelfAsDevice(int port) |
45 : Device("local", true), | 32 : Device("local", true), |
46 port_(port) | 33 port_(port) |
47 {} | 34 {} |
48 | 35 |
49 void SelfAsDevice::RunCommandCallback(const CommandCallback& callback, | 36 void SelfAsDevice::QueryDeviceInfo(const DeviceInfoCallback& callback) { |
50 const std::string& response, | 37 AndroidDeviceManager::DeviceInfo device_info; |
51 int result) { | 38 device_info.model = kDeviceModel; |
52 callback.Run(result, response); | 39 |
| 40 AndroidDeviceManager::BrowserInfo browser_info; |
| 41 browser_info.socket = base::IntToString(port_); |
| 42 browser_info.display_name = kBrowserName; |
| 43 browser_info.type = AndroidDeviceManager::BrowserInfo::kTypeChrome; |
| 44 |
| 45 device_info.browser_info.push_back(browser_info); |
| 46 |
| 47 base::MessageLoop::current()->PostTask( |
| 48 FROM_HERE, base::Bind(callback, device_info)); |
53 } | 49 } |
54 | 50 |
55 void SelfAsDevice::RunSocketCallback(const SocketCallback& callback, | 51 static void RunSocketCallback( |
56 net::StreamSocket* socket, | 52 const AndroidDeviceManager::SocketCallback& callback, |
57 int result) { | 53 net::StreamSocket* socket, |
| 54 int result) { |
58 callback.Run(result, socket); | 55 callback.Run(result, socket); |
59 } | 56 } |
60 | 57 |
61 void SelfAsDevice::RunCommand(const std::string& command, | |
62 const CommandCallback& callback) { | |
63 DCHECK(CalledOnValidThread()); | |
64 std::string response; | |
65 if (command == kDeviceModelCommand) { | |
66 response = kDeviceModel; | |
67 } else if (command == kOpenedUnixSocketsCommand) { | |
68 response = base::StringPrintf(kOpenedUnixSocketsResponse, | |
69 kRemoteDebuggingSocket); | |
70 } | |
71 | |
72 base::MessageLoop::current()->PostTask(FROM_HERE, | |
73 base::Bind(&SelfAsDevice::RunCommandCallback, this, callback, | |
74 response, 0)); | |
75 } | |
76 | |
77 void SelfAsDevice::OpenSocket(const std::string& socket_name, | 58 void SelfAsDevice::OpenSocket(const std::string& socket_name, |
78 const SocketCallback& callback) { | 59 const SocketCallback& callback) { |
79 DCHECK(CalledOnValidThread()); | 60 DCHECK(CalledOnValidThread()); |
80 // Use plain socket for remote debugging and port forwarding on Desktop | 61 // Use plain socket for remote debugging and port forwarding on Desktop |
81 // (debugging purposes). | 62 // (debugging purposes). |
82 net::IPAddressNumber ip_number; | 63 net::IPAddressNumber ip_number; |
83 net::ParseIPLiteralToNumber(kLocalhost, &ip_number); | 64 net::ParseIPLiteralToNumber(kLocalhost, &ip_number); |
84 | 65 int port; |
85 int port = 0; | 66 base::StringToInt(socket_name, &port); |
86 if (socket_name == kRemoteDebuggingSocket) | |
87 port = port_; | |
88 else | |
89 base::StringToInt(socket_name, &port); | |
90 | |
91 net::AddressList address_list = | 67 net::AddressList address_list = |
92 net::AddressList::CreateFromIPAddress(ip_number, port); | 68 net::AddressList::CreateFromIPAddress(ip_number, port); |
93 net::TCPClientSocket* socket = new net::TCPClientSocket( | 69 net::TCPClientSocket* socket = new net::TCPClientSocket( |
94 address_list, NULL, net::NetLog::Source()); | 70 address_list, NULL, net::NetLog::Source()); |
95 socket->Connect(base::Bind(&SelfAsDevice::RunSocketCallback, this, callback, | 71 socket->Connect(base::Bind(&RunSocketCallback, callback, socket)); |
96 socket)); | |
97 } | 72 } |
98 | 73 |
99 } // namespace | 74 } // namespace |
100 | 75 |
101 SelfAsDeviceProvider::SelfAsDeviceProvider(int port) | 76 SelfAsDeviceProvider::SelfAsDeviceProvider(int port) |
102 : port_(port) { | 77 : port_(port) { |
103 } | 78 } |
104 | 79 |
105 void SelfAsDeviceProvider::QueryDevices(const QueryDevicesCallback& callback) { | 80 void SelfAsDeviceProvider::QueryDevices(const QueryDevicesCallback& callback) { |
106 AndroidDeviceManager::Devices result; | 81 AndroidDeviceManager::Devices result; |
107 result.push_back(new SelfAsDevice(port_)); | 82 result.push_back(new SelfAsDevice(port_)); |
108 callback.Run(result); | 83 callback.Run(result); |
109 } | 84 } |
OLD | NEW |