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 "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/browser/shell.h"
15 #include "content/shell/browser/shell_browser_context.h"
16 #include "content/shell/browser/shell_browser_main_parts.h"
17 #include "content/shell/browser/shell_content_browser_client.h"
18 #include "content/shell/browser/shell_devtools_delegate.h"
19 #include "content/shell/common/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(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());
66 devtools_frontend
->Activate();
67 devtools_frontend
->Focus();
69 return devtools_frontend
;
72 void ShellDevToolsFrontend::Activate() {
73 frontend_shell_
->ActivateContents(web_contents());
76 void ShellDevToolsFrontend::Focus() {
77 web_contents()->GetView()->Focus();
80 void ShellDevToolsFrontend::Close() {
81 frontend_shell_
->Close();
84 ShellDevToolsFrontend::ShellDevToolsFrontend(Shell
* frontend_shell
,
85 DevToolsAgentHost
* agent_host
)
86 : WebContentsObserver(frontend_shell
->web_contents()),
87 frontend_shell_(frontend_shell
),
88 agent_host_(agent_host
) {
90 DevToolsClientHost::CreateDevToolsFrontendHost(web_contents(), this));
93 ShellDevToolsFrontend::~ShellDevToolsFrontend() {
96 void ShellDevToolsFrontend::RenderViewCreated(
97 RenderViewHost
* render_view_host
) {
98 DevToolsClientHost::SetupDevToolsFrontendClient(
99 web_contents()->GetRenderViewHost());
100 DevToolsManager
* manager
= DevToolsManager::GetInstance();
101 manager
->RegisterDevToolsClientHostFor(agent_host_
.get(),
102 frontend_host_
.get());
105 void ShellDevToolsFrontend::WebContentsDestroyed(WebContents
* web_contents
) {
106 DevToolsManager::GetInstance()->ClientHostClosing(frontend_host_
.get());
110 void ShellDevToolsFrontend::InspectedContentsClosing() {
111 frontend_shell_
->Close();
114 } // namespace content