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/tcp_device_provider.h"
9 #include "base/location.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "chrome/browser/devtools/device/adb/adb_client_socket.h"
15 #include "net/base/net_errors.h"
16 #include "net/dns/host_resolver.h"
17 #include "net/socket/tcp_client_socket.h"
21 const char kDeviceModel
[] = "Remote Target";
22 const char kBrowserName
[] = "Target";
24 static void RunSocketCallback(
25 const AndroidDeviceManager::SocketCallback
& callback
,
26 scoped_ptr
<net::StreamSocket
> socket
,
28 callback
.Run(result
, socket
.Pass());
31 class ResolveHostAndOpenSocket final
{
33 ResolveHostAndOpenSocket(const net::HostPortPair
& address
,
34 const AdbClientSocket::SocketCallback
& callback
)
35 : callback_(callback
) {
36 host_resolver_
= net::HostResolver::CreateDefaultResolver(nullptr);
37 net::HostResolver::RequestInfo
request_info(address
);
38 int result
= host_resolver_
->Resolve(
39 request_info
, net::DEFAULT_PRIORITY
, &address_list_
,
40 base::Bind(&ResolveHostAndOpenSocket::OnResolved
,
41 base::Unretained(this)),
42 nullptr, net::BoundNetLog());
43 if (result
!= net::ERR_IO_PENDING
)
48 void OnResolved(int result
) {
50 RunSocketCallback(callback_
, nullptr, result
);
54 scoped_ptr
<net::StreamSocket
> socket(
55 new net::TCPClientSocket(address_list_
, NULL
, net::NetLog::Source()));
57 base::Bind(&RunSocketCallback
, callback_
, base::Passed(&socket
)));
61 scoped_ptr
<net::HostResolver
> host_resolver_
;
62 net::AddressList address_list_
;
63 AdbClientSocket::SocketCallback callback_
;
68 scoped_refptr
<TCPDeviceProvider
> TCPDeviceProvider::CreateForLocalhost(
70 TCPDeviceProvider::HostPortSet targets
;
71 targets
.insert(net::HostPortPair("127.0.0.1", port
));
72 return new TCPDeviceProvider(targets
);
75 TCPDeviceProvider::TCPDeviceProvider(const HostPortSet
& targets
)
79 void TCPDeviceProvider::QueryDevices(const SerialsCallback
& callback
) {
80 std::vector
<std::string
> result
;
81 for (const net::HostPortPair
& target
: targets_
) {
82 const std::string
& host
= target
.host();
83 if (std::find(result
.begin(), result
.end(), host
) != result
.end())
85 result
.push_back(host
);
90 void TCPDeviceProvider::QueryDeviceInfo(const std::string
& serial
,
91 const DeviceInfoCallback
& callback
) {
92 AndroidDeviceManager::DeviceInfo device_info
;
93 device_info
.model
= kDeviceModel
;
94 device_info
.connected
= true;
96 for (const net::HostPortPair
& target
: targets_
) {
97 if (serial
!= target
.host())
99 AndroidDeviceManager::BrowserInfo browser_info
;
100 browser_info
.socket_name
= base::IntToString(target
.port());
101 browser_info
.display_name
= kBrowserName
;
102 browser_info
.type
= AndroidDeviceManager::BrowserInfo::kTypeChrome
;
104 device_info
.browser_info
.push_back(browser_info
);
107 base::ThreadTaskRunnerHandle::Get()->PostTask(
108 FROM_HERE
, base::Bind(callback
, device_info
));
111 void TCPDeviceProvider::OpenSocket(const std::string
& serial
,
112 const std::string
& socket_name
,
113 const SocketCallback
& callback
) {
114 // Use plain socket for remote debugging and port forwarding on Desktop
115 // (debugging purposes).
117 base::StringToInt(socket_name
, &port
);
118 net::HostPortPair
host_port(serial
, port
);
119 new ResolveHostAndOpenSocket(host_port
, callback
);
122 void TCPDeviceProvider::ReleaseDevice(const std::string
& serial
) {
123 if (!release_callback_
.is_null())
124 release_callback_
.Run();
127 void TCPDeviceProvider::set_release_callback_for_test(
128 const base::Closure
& callback
) {
129 release_callback_
= callback
;
132 TCPDeviceProvider::~TCPDeviceProvider() {