Home | History | Annotate | Download | only in shell
      1 // Copyright (c) 2012 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 "content/shell/shell_devtools_delegate.h"
      6 
      7 #include <vector>
      8 
      9 #include "base/bind.h"
     10 #include "base/command_line.h"
     11 #include "base/strings/string_number_conversions.h"
     12 #include "content/public/browser/devtools_http_handler.h"
     13 #include "content/public/browser/web_contents.h"
     14 #include "content/public/common/content_switches.h"
     15 #include "content/public/common/url_constants.h"
     16 #include "content/shell/shell.h"
     17 #include "grit/shell_resources.h"
     18 #include "net/socket/tcp_listen_socket.h"
     19 #include "ui/base/resource/resource_bundle.h"
     20 
     21 #if defined(OS_ANDROID)
     22 #include "content/public/browser/android/devtools_auth.h"
     23 #include "net/socket/unix_domain_socket_posix.h"
     24 #endif
     25 
     26 namespace {
     27 
     28 net::StreamListenSocketFactory* CreateSocketFactory() {
     29   const CommandLine& command_line = *CommandLine::ForCurrentProcess();
     30 #if defined(OS_ANDROID)
     31   std::string socket_name = "content_shell_devtools_remote";
     32   if (command_line.HasSwitch(switches::kRemoteDebuggingSocketName)) {
     33     socket_name = command_line.GetSwitchValueASCII(
     34         switches::kRemoteDebuggingSocketName);
     35   }
     36   return new net::UnixDomainSocketWithAbstractNamespaceFactory(
     37       socket_name, "", base::Bind(&content::CanUserConnectToDevTools));
     38 #else
     39   // See if the user specified a port on the command line (useful for
     40   // automation). If not, use an ephemeral port by specifying 0.
     41   int port = 0;
     42   if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) {
     43     int temp_port;
     44     std::string port_str =
     45         command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort);
     46     if (base::StringToInt(port_str, &temp_port) &&
     47         temp_port > 0 && temp_port < 65535) {
     48       port = temp_port;
     49     } else {
     50       DLOG(WARNING) << "Invalid http debugger port number " << temp_port;
     51     }
     52   }
     53   return new net::TCPListenSocketFactory("127.0.0.1", port);
     54 #endif
     55 }
     56 }  // namespace
     57 
     58 namespace content {
     59 
     60 ShellDevToolsDelegate::ShellDevToolsDelegate(BrowserContext* browser_context)
     61     : browser_context_(browser_context) {
     62   // Note that Content Shell always used bundled DevTools frontend,
     63   // even on Android, because the shell is used for running layout tests.
     64   devtools_http_handler_ =
     65       DevToolsHttpHandler::Start(CreateSocketFactory(), std::string(), this);
     66 }
     67 
     68 ShellDevToolsDelegate::~ShellDevToolsDelegate() {
     69 }
     70 
     71 void ShellDevToolsDelegate::Stop() {
     72   // The call below destroys this.
     73   devtools_http_handler_->Stop();
     74 }
     75 
     76 std::string ShellDevToolsDelegate::GetDiscoveryPageHTML() {
     77   return ResourceBundle::GetSharedInstance().GetRawDataResource(
     78       IDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE).as_string();
     79 }
     80 
     81 bool ShellDevToolsDelegate::BundlesFrontendResources() {
     82   return true;
     83 }
     84 
     85 base::FilePath ShellDevToolsDelegate::GetDebugFrontendDir() {
     86   return base::FilePath();
     87 }
     88 
     89 std::string ShellDevToolsDelegate::GetPageThumbnailData(const GURL& url) {
     90   return std::string();
     91 }
     92 
     93 RenderViewHost* ShellDevToolsDelegate::CreateNewTarget() {
     94   Shell* shell = Shell::CreateNewWindow(browser_context_,
     95                                         GURL(kAboutBlankURL),
     96                                         NULL,
     97                                         MSG_ROUTING_NONE,
     98                                         gfx::Size());
     99   return shell->web_contents()->GetRenderViewHost();
    100 }
    101 
    102 DevToolsHttpHandlerDelegate::TargetType
    103 ShellDevToolsDelegate::GetTargetType(RenderViewHost*) {
    104   return kTargetTypeTab;
    105 }
    106 
    107 std::string ShellDevToolsDelegate::GetViewDescription(
    108     content::RenderViewHost*) {
    109   return std::string();
    110 }
    111 
    112 scoped_refptr<net::StreamListenSocket>
    113 ShellDevToolsDelegate::CreateSocketForTethering(
    114     net::StreamListenSocket::Delegate* delegate,
    115     std::string* name) {
    116   return NULL;
    117 }
    118 
    119 }  // namespace content
    120