Home | History | Annotate | Download | only in shell
      1 // Copyright (c) 2013 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_frontend.h"
      6 
      7 #include "base/command_line.h"
      8 #include "base/path_service.h"
      9 #include "content/public/browser/devtools_http_handler.h"
     10 #include "content/public/browser/devtools_manager.h"
     11 #include "content/public/browser/web_contents.h"
     12 #include "content/public/browser/web_contents_view.h"
     13 #include "content/public/common/content_client.h"
     14 #include "content/shell/common/shell_switches.h"
     15 #include "content/shell/shell.h"
     16 #include "content/shell/shell_browser_context.h"
     17 #include "content/shell/shell_browser_main_parts.h"
     18 #include "content/shell/shell_content_browser_client.h"
     19 #include "content/shell/shell_devtools_delegate.h"
     20 #include "net/base/net_util.h"
     21 
     22 namespace content {
     23 
     24 namespace {
     25 
     26 // DevTools frontend path for inspector LayoutTests.
     27 GURL GetDevToolsPathAsURL() {
     28   base::FilePath dir_exe;
     29   if (!PathService::Get(base::DIR_EXE, &dir_exe)) {
     30     NOTREACHED();
     31     return GURL();
     32   }
     33 #if defined(OS_MACOSX)
     34   // On Mac, the executable is in
     35   // out/Release/Content Shell.app/Contents/MacOS/Content Shell.
     36   // We need to go up 3 directories to get to out/Release.
     37   dir_exe = dir_exe.AppendASCII("../../..");
     38 #endif
     39   base::FilePath dev_tools_path = dir_exe.AppendASCII(
     40       "resources/inspector/devtools.html");
     41   return net::FilePathToFileURL(dev_tools_path);
     42 }
     43 
     44 }  // namespace
     45 
     46 // static
     47 ShellDevToolsFrontend* ShellDevToolsFrontend::Show(
     48     WebContents* inspected_contents) {
     49   Shell* shell = Shell::CreateNewWindow(inspected_contents->GetBrowserContext(),
     50                                         GURL(),
     51                                         NULL,
     52                                         MSG_ROUTING_NONE,
     53                                         gfx::Size());
     54   ShellDevToolsFrontend* devtools_frontend = new ShellDevToolsFrontend(
     55       shell,
     56       DevToolsAgentHost::GetOrCreateFor(inspected_contents->GetRenderViewHost())
     57           .get());
     58 
     59   ShellDevToolsDelegate* delegate = ShellContentBrowserClient::Get()->
     60       shell_browser_main_parts()->devtools_delegate();
     61   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree))
     62     shell->LoadURL(GetDevToolsPathAsURL());
     63   else
     64     shell->LoadURL(delegate->devtools_http_handler()->GetFrontendURL(NULL));
     65 
     66   return devtools_frontend;
     67 }
     68 
     69 void ShellDevToolsFrontend::Focus() {
     70   web_contents()->GetView()->Focus();
     71 }
     72 
     73 void ShellDevToolsFrontend::Close() {
     74   frontend_shell_->Close();
     75 }
     76 
     77 ShellDevToolsFrontend::ShellDevToolsFrontend(Shell* frontend_shell,
     78                                              DevToolsAgentHost* agent_host)
     79     : WebContentsObserver(frontend_shell->web_contents()),
     80       frontend_shell_(frontend_shell),
     81       agent_host_(agent_host) {
     82   frontend_host_.reset(
     83       DevToolsClientHost::CreateDevToolsFrontendHost(web_contents(), this));
     84 }
     85 
     86 ShellDevToolsFrontend::~ShellDevToolsFrontend() {
     87 }
     88 
     89 void ShellDevToolsFrontend::RenderViewCreated(
     90     RenderViewHost* render_view_host) {
     91   DevToolsClientHost::SetupDevToolsFrontendClient(
     92       web_contents()->GetRenderViewHost());
     93   DevToolsManager* manager = DevToolsManager::GetInstance();
     94   manager->RegisterDevToolsClientHostFor(agent_host_.get(),
     95                                          frontend_host_.get());
     96 }
     97 
     98 void ShellDevToolsFrontend::WebContentsDestroyed(WebContents* web_contents) {
     99   DevToolsManager::GetInstance()->ClientHostClosing(frontend_host_.get());
    100   delete this;
    101 }
    102 
    103 void ShellDevToolsFrontend::InspectedContentsClosing() {
    104   frontend_shell_->Close();
    105 }
    106 
    107 }  // namespace content
    108