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.
5 #include "content/shell/browser/shell_devtools_frontend.h"
7 #include "base/command_line.h"
8 #include "base/path_service.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "content/public/browser/devtools_http_handler.h"
12 #include "content/public/browser/devtools_manager.h"
13 #include "content/public/browser/render_frame_host.h"
14 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/common/content_client.h"
17 #include "content/shell/browser/shell.h"
18 #include "content/shell/browser/shell_browser_context.h"
19 #include "content/shell/browser/shell_browser_main_parts.h"
20 #include "content/shell/browser/shell_content_browser_client.h"
21 #include "content/shell/browser/shell_devtools_delegate.h"
22 #include "content/shell/common/shell_switches.h"
23 #include "net/base/filename_util.h"
27 // DevTools frontend path for inspector LayoutTests.
28 GURL
GetDevToolsPathAsURL(const std::string
& settings
,
29 const std::string
& frontend_url
) {
30 if (!frontend_url
.empty())
31 return GURL(frontend_url
);
32 base::FilePath dir_exe
;
33 if (!PathService::Get(base::DIR_EXE
, &dir_exe
)) {
37 #if defined(OS_MACOSX)
38 // On Mac, the executable is in
39 // out/Release/Content Shell.app/Contents/MacOS/Content Shell.
40 // We need to go up 3 directories to get to out/Release.
41 dir_exe
= dir_exe
.AppendASCII("../../..");
43 base::FilePath dev_tools_path
= dir_exe
.AppendASCII(
44 "resources/inspector/devtools.html");
46 GURL result
= net::FilePathToFileURL(dev_tools_path
);
47 if (!settings
.empty())
48 result
= GURL(base::StringPrintf("%s?settings=%s",
49 result
.spec().c_str(),
55 ShellDevToolsFrontend
* ShellDevToolsFrontend::Show(
56 WebContents
* inspected_contents
) {
57 return ShellDevToolsFrontend::Show(inspected_contents
, "", "");
61 ShellDevToolsFrontend
* ShellDevToolsFrontend::Show(
62 WebContents
* inspected_contents
,
63 const std::string
& settings
,
64 const std::string
& frontend_url
) {
65 scoped_refptr
<DevToolsAgentHost
> agent(
66 DevToolsAgentHost::GetOrCreateFor(
67 inspected_contents
->GetRenderViewHost()));
68 Shell
* shell
= Shell::CreateNewWindow(inspected_contents
->GetBrowserContext(),
73 ShellDevToolsFrontend
* devtools_frontend
= new ShellDevToolsFrontend(
77 ShellDevToolsDelegate
* delegate
= ShellContentBrowserClient::Get()->
78 shell_browser_main_parts()->devtools_delegate();
79 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree
))
80 shell
->LoadURL(GetDevToolsPathAsURL(settings
, frontend_url
));
82 shell
->LoadURL(delegate
->devtools_http_handler()->GetFrontendURL());
84 return devtools_frontend
;
87 void ShellDevToolsFrontend::Activate() {
88 frontend_shell_
->ActivateContents(web_contents());
91 void ShellDevToolsFrontend::Focus() {
92 web_contents()->Focus();
95 void ShellDevToolsFrontend::InspectElementAt(int x
, int y
) {
96 agent_host_
->InspectElement(x
, y
);
99 void ShellDevToolsFrontend::Close() {
100 frontend_shell_
->Close();
103 ShellDevToolsFrontend::ShellDevToolsFrontend(Shell
* frontend_shell
,
104 DevToolsAgentHost
* agent_host
)
105 : WebContentsObserver(frontend_shell
->web_contents()),
106 frontend_shell_(frontend_shell
),
107 agent_host_(agent_host
) {
108 frontend_host_
.reset(
109 DevToolsClientHost::CreateDevToolsFrontendHost(web_contents(), this));
112 ShellDevToolsFrontend::~ShellDevToolsFrontend() {
115 void ShellDevToolsFrontend::RenderViewCreated(
116 RenderViewHost
* render_view_host
) {
117 DevToolsClientHost::SetupDevToolsFrontendClient(
118 web_contents()->GetRenderViewHost());
119 DevToolsManager
* manager
= DevToolsManager::GetInstance();
120 manager
->RegisterDevToolsClientHostFor(agent_host_
.get(),
121 frontend_host_
.get());
124 void ShellDevToolsFrontend::DocumentOnLoadCompletedInMainFrame() {
125 web_contents()->GetMainFrame()->ExecuteJavaScript(
126 base::ASCIIToUTF16("InspectorFrontendAPI.setUseSoftMenu(true);"));
129 void ShellDevToolsFrontend::WebContentsDestroyed() {
130 DevToolsManager::GetInstance()->ClientHostClosing(frontend_host_
.get());
134 void ShellDevToolsFrontend::InspectedContentsClosing() {
135 frontend_shell_
->Close();
138 } // namespace content