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.
5 #include "content/shell/shell_devtools_frontend.h"
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/shell.h"
15 #include "content/shell/shell_browser_context.h"
16 #include "content/shell/shell_browser_main_parts.h"
17 #include "content/shell/shell_content_browser_client.h"
18 #include "content/shell/shell_devtools_delegate.h"
19 #include "content/shell/shell_switches.h"
20 #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
);
47 ShellDevToolsFrontend
* ShellDevToolsFrontend::Show(
48 WebContents
* inspected_contents
) {
49 Shell
* shell
= Shell::CreateNewWindow(inspected_contents
->GetBrowserContext(),
54 ShellDevToolsFrontend
* devtools_frontend
= new ShellDevToolsFrontend(
56 DevToolsAgentHost::GetOrCreateFor(
57 inspected_contents
->GetRenderViewHost()));
59 ShellDevToolsDelegate
* delegate
= ShellContentBrowserClient::Get()->
60 shell_browser_main_parts()->devtools_delegate();
61 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree
))
62 shell
->LoadURL(GetDevToolsPathAsURL());
64 shell
->LoadURL(delegate
->devtools_http_handler()->GetFrontendURL(NULL
));
66 return devtools_frontend
;
69 void ShellDevToolsFrontend::Focus() {
70 web_contents()->GetView()->Focus();
73 void ShellDevToolsFrontend::Close() {
74 frontend_shell_
->Close();
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
) {
83 DevToolsClientHost::CreateDevToolsFrontendHost(web_contents(), this));
86 ShellDevToolsFrontend::~ShellDevToolsFrontend() {
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());
98 void ShellDevToolsFrontend::WebContentsDestroyed(WebContents
* web_contents
) {
99 DevToolsManager::GetInstance()->ClientHostClosing(frontend_host_
.get());
103 void ShellDevToolsFrontend::InspectedContentsClosing() {
104 frontend_shell_
->Close();
107 } // namespace content