• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 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/browser/shell_devtools_frontend.h"
6 
7 #include "base/command_line.h"
8 #include "base/path_service.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "content/public/browser/devtools_http_handler.h"
11 #include "content/public/browser/devtools_manager.h"
12 #include "content/public/browser/render_view_host.h"
13 #include "content/public/browser/web_contents.h"
14 #include "content/public/browser/web_contents_view.h"
15 #include "content/public/common/content_client.h"
16 #include "content/shell/browser/shell.h"
17 #include "content/shell/browser/shell_browser_context.h"
18 #include "content/shell/browser/shell_browser_main_parts.h"
19 #include "content/shell/browser/shell_content_browser_client.h"
20 #include "content/shell/browser/shell_devtools_delegate.h"
21 #include "content/shell/common/shell_switches.h"
22 #include "net/base/net_util.h"
23 
24 namespace content {
25 
26 // DevTools frontend path for inspector LayoutTests.
GetDevToolsPathAsURL()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 // static
Show(WebContents * inspected_contents)45 ShellDevToolsFrontend* ShellDevToolsFrontend::Show(
46     WebContents* inspected_contents) {
47   Shell* shell = Shell::CreateNewWindow(inspected_contents->GetBrowserContext(),
48                                         GURL(),
49                                         NULL,
50                                         MSG_ROUTING_NONE,
51                                         gfx::Size());
52   ShellDevToolsFrontend* devtools_frontend = new ShellDevToolsFrontend(
53       shell,
54       DevToolsAgentHost::GetOrCreateFor(inspected_contents->GetRenderViewHost())
55           .get());
56 
57   ShellDevToolsDelegate* delegate = ShellContentBrowserClient::Get()->
58       shell_browser_main_parts()->devtools_delegate();
59   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree))
60     shell->LoadURL(GetDevToolsPathAsURL());
61   else
62     shell->LoadURL(delegate->devtools_http_handler()->GetFrontendURL());
63 
64   devtools_frontend->Activate();
65   devtools_frontend->Focus();
66 
67   return devtools_frontend;
68 }
69 
Activate()70 void ShellDevToolsFrontend::Activate() {
71   frontend_shell_->ActivateContents(web_contents());
72 }
73 
Focus()74 void ShellDevToolsFrontend::Focus() {
75   web_contents()->GetView()->Focus();
76 }
77 
Close()78 void ShellDevToolsFrontend::Close() {
79   frontend_shell_->Close();
80 }
81 
ShellDevToolsFrontend(Shell * frontend_shell,DevToolsAgentHost * agent_host)82 ShellDevToolsFrontend::ShellDevToolsFrontend(Shell* frontend_shell,
83                                              DevToolsAgentHost* agent_host)
84     : WebContentsObserver(frontend_shell->web_contents()),
85       frontend_shell_(frontend_shell),
86       agent_host_(agent_host) {
87   frontend_host_.reset(
88       DevToolsClientHost::CreateDevToolsFrontendHost(web_contents(), this));
89 }
90 
~ShellDevToolsFrontend()91 ShellDevToolsFrontend::~ShellDevToolsFrontend() {
92 }
93 
RenderViewCreated(RenderViewHost * render_view_host)94 void ShellDevToolsFrontend::RenderViewCreated(
95     RenderViewHost* render_view_host) {
96   DevToolsClientHost::SetupDevToolsFrontendClient(
97       web_contents()->GetRenderViewHost());
98   DevToolsManager* manager = DevToolsManager::GetInstance();
99   manager->RegisterDevToolsClientHostFor(agent_host_.get(),
100                                          frontend_host_.get());
101 }
102 
DocumentOnLoadCompletedInMainFrame(int32 page_id)103 void ShellDevToolsFrontend::DocumentOnLoadCompletedInMainFrame(int32 page_id) {
104   web_contents()->GetRenderViewHost()->ExecuteJavascriptInWebFrame(
105       base::string16(),
106       ASCIIToUTF16("InspectorFrontendAPI.setUseSoftMenu(true);"));
107 }
108 
WebContentsDestroyed(WebContents * web_contents)109 void ShellDevToolsFrontend::WebContentsDestroyed(WebContents* web_contents) {
110   DevToolsManager::GetInstance()->ClientHostClosing(frontend_host_.get());
111   delete this;
112 }
113 
InspectedContentsClosing()114 void ShellDevToolsFrontend::InspectedContentsClosing() {
115   frontend_shell_->Close();
116 }
117 
118 }  // namespace content
119