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/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"
26 // DevTools frontend path for inspector LayoutTests.
27 GURL
GetDevToolsPathAsURL() {
28 base::FilePath dir_exe
;
29 if (!PathService::Get(base::DIR_EXE
, &dir_exe
)) {
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("../../..");
39 base::FilePath dev_tools_path
= dir_exe
.AppendASCII(
40 "resources/inspector/devtools.html");
41 return net::FilePathToFileURL(dev_tools_path
);
45 ShellDevToolsFrontend
* ShellDevToolsFrontend::Show(
46 WebContents
* inspected_contents
) {
47 Shell
* shell
= Shell::CreateNewWindow(inspected_contents
->GetBrowserContext(),
52 ShellDevToolsFrontend
* devtools_frontend
= new ShellDevToolsFrontend(
54 DevToolsAgentHost::GetOrCreateFor(inspected_contents
->GetRenderViewHost())
57 ShellDevToolsDelegate
* delegate
= ShellContentBrowserClient::Get()->
58 shell_browser_main_parts()->devtools_delegate();
59 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree
))
60 shell
->LoadURL(GetDevToolsPathAsURL());
62 shell
->LoadURL(delegate
->devtools_http_handler()->GetFrontendURL());
64 devtools_frontend
->Activate();
65 devtools_frontend
->Focus();
67 return devtools_frontend
;
70 void ShellDevToolsFrontend::Activate() {
71 frontend_shell_
->ActivateContents(web_contents());
74 void ShellDevToolsFrontend::Focus() {
75 web_contents()->GetView()->Focus();
78 void ShellDevToolsFrontend::Close() {
79 frontend_shell_
->Close();
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
) {
88 DevToolsClientHost::CreateDevToolsFrontendHost(web_contents(), this));
91 ShellDevToolsFrontend::~ShellDevToolsFrontend() {
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());
103 void ShellDevToolsFrontend::DocumentOnLoadCompletedInMainFrame(int32 page_id
) {
104 web_contents()->GetRenderViewHost()->ExecuteJavascriptInWebFrame(
106 base::ASCIIToUTF16("InspectorFrontendAPI.setUseSoftMenu(true);"));
109 void ShellDevToolsFrontend::WebContentsDestroyed(WebContents
* web_contents
) {
110 DevToolsManager::GetInstance()->ClientHostClosing(frontend_host_
.get());
114 void ShellDevToolsFrontend::InspectedContentsClosing() {
115 frontend_shell_
->Close();
118 } // namespace content