Home | History | Annotate | Download | only in device
      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.
      4 
      5 #include "chrome/browser/devtools/device/self_device_provider.h"
      6 
      7 #include "base/strings/string_number_conversions.h"
      8 #include "base/strings/stringprintf.h"
      9 #include "net/socket/tcp_client_socket.h"
     10 
     11 namespace {
     12 
     13 const char kDeviceModel[] = "Local Chrome";
     14 const char kBrowserName[] = "Chrome";
     15 const char kLocalhost[] = "127.0.0.1";
     16 const char kSerial[] = "local";
     17 
     18 static void RunSocketCallback(
     19     const AndroidDeviceManager::SocketCallback& callback,
     20     scoped_ptr<net::StreamSocket> socket,
     21     int result) {
     22   callback.Run(result, socket.Pass());
     23 }
     24 
     25 }  // namespace
     26 
     27 SelfAsDeviceProvider::SelfAsDeviceProvider(int port) : port_(port) {
     28 }
     29 
     30 void SelfAsDeviceProvider::QueryDevices(const SerialsCallback& callback) {
     31   std::vector<std::string> result;
     32   result.push_back(kSerial);
     33   callback.Run(result);
     34 }
     35 
     36 void SelfAsDeviceProvider::QueryDeviceInfo(const std::string& serial,
     37                                            const DeviceInfoCallback& callback) {
     38   AndroidDeviceManager::DeviceInfo device_info;
     39   device_info.model = kDeviceModel;
     40   device_info.connected = true;
     41 
     42   AndroidDeviceManager::BrowserInfo browser_info;
     43   browser_info.socket_name = base::IntToString(port_);
     44   browser_info.display_name = kBrowserName;
     45   browser_info.type = AndroidDeviceManager::BrowserInfo::kTypeChrome;
     46 
     47   device_info.browser_info.push_back(browser_info);
     48 
     49   base::MessageLoop::current()->PostTask(
     50       FROM_HERE, base::Bind(callback, device_info));
     51 }
     52 
     53 void SelfAsDeviceProvider::OpenSocket(const std::string& serial,
     54                                       const std::string& socket_name,
     55                                       const SocketCallback& callback) {
     56   // Use plain socket for remote debugging and port forwarding on Desktop
     57   // (debugging purposes).
     58   net::IPAddressNumber ip_number;
     59   net::ParseIPLiteralToNumber(kLocalhost, &ip_number);
     60   int port;
     61   base::StringToInt(socket_name, &port);
     62   net::AddressList address_list =
     63       net::AddressList::CreateFromIPAddress(ip_number, port);
     64   scoped_ptr<net::StreamSocket> socket(new net::TCPClientSocket(
     65       address_list, NULL, net::NetLog::Source()));
     66   socket->Connect(
     67       base::Bind(&RunSocketCallback, callback, base::Passed(&socket)));
     68 }
     69