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"
24 // DevTools frontend path for inspector LayoutTests.
25 GURL GetDevToolsPathAsURL() {
26 base::FilePath dir_exe;
27 if (!PathService::Get(base::DIR_EXE, &dir_exe)) {
31 #if defined(OS_MACOSX)
32 // On Mac, the executable is in
33 // out/Release/Content Shell.app/Contents/MacOS/Content Shell.
34 // We need to go up 3 directories to get to out/Release.
35 dir_exe = dir_exe.AppendASCII("../../..");
37 base::FilePath dev_tools_path = dir_exe.AppendASCII(
38 "resources/inspector/devtools.html");
39 return net::FilePathToFileURL(dev_tools_path);
43 ShellDevToolsFrontend* ShellDevToolsFrontend::Show(
44 WebContents* inspected_contents) {
45 Shell* shell = Shell::CreateNewWindow(inspected_contents->GetBrowserContext(),
50 ShellDevToolsFrontend* devtools_frontend = new ShellDevToolsFrontend(
52 DevToolsAgentHost::GetOrCreateFor(inspected_contents->GetRenderViewHost())
55 ShellDevToolsDelegate* delegate = ShellContentBrowserClient::Get()->
56 shell_browser_main_parts()->devtools_delegate();
57 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree))
58 shell->LoadURL(GetDevToolsPathAsURL());
60 shell->LoadURL(delegate->devtools_http_handler()->GetFrontendURL());
62 devtools_frontend->Activate();
63 devtools_frontend->Focus();
65 return devtools_frontend;
68 void ShellDevToolsFrontend::Activate() {
69 frontend_shell_->ActivateContents(web_contents());
72 void ShellDevToolsFrontend::Focus() {
73 web_contents()->GetView()->Focus();
76 void ShellDevToolsFrontend::Close() {
77 frontend_shell_->Close();
80 ShellDevToolsFrontend::ShellDevToolsFrontend(Shell* frontend_shell,
81 DevToolsAgentHost* agent_host)
82 : WebContentsObserver(frontend_shell->web_contents()),
83 frontend_shell_(frontend_shell),
84 agent_host_(agent_host) {
86 DevToolsClientHost::CreateDevToolsFrontendHost(web_contents(), this));
89 ShellDevToolsFrontend::~ShellDevToolsFrontend() {
92 void ShellDevToolsFrontend::RenderViewCreated(
93 RenderViewHost* render_view_host) {
94 DevToolsClientHost::SetupDevToolsFrontendClient(
95 web_contents()->GetRenderViewHost());
96 DevToolsManager* manager = DevToolsManager::GetInstance();
97 manager->RegisterDevToolsClientHostFor(agent_host_.get(),
98 frontend_host_.get());
101 void ShellDevToolsFrontend::WebContentsDestroyed(WebContents* web_contents) {
102 DevToolsManager::GetInstance()->ClientHostClosing(frontend_host_.get());
106 void ShellDevToolsFrontend::InspectedContentsClosing() {
107 frontend_shell_->Close();
110 } // namespace content