- add sources.
[platform/framework/web/crosswalk.git] / src / content / shell / browser / shell_devtools_frontend.cc
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.
4
5 #include "content/shell/browser/shell_devtools_frontend.h"
6
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"
21
22 namespace content {
23
24 // DevTools frontend path for inspector LayoutTests.
25 GURL GetDevToolsPathAsURL() {
26   base::FilePath dir_exe;
27   if (!PathService::Get(base::DIR_EXE, &dir_exe)) {
28     NOTREACHED();
29     return GURL();
30   }
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("../../..");
36 #endif
37   base::FilePath dev_tools_path = dir_exe.AppendASCII(
38       "resources/inspector/devtools.html");
39   return net::FilePathToFileURL(dev_tools_path);
40 }
41
42 // static
43 ShellDevToolsFrontend* ShellDevToolsFrontend::Show(
44     WebContents* inspected_contents) {
45   Shell* shell = Shell::CreateNewWindow(inspected_contents->GetBrowserContext(),
46                                         GURL(),
47                                         NULL,
48                                         MSG_ROUTING_NONE,
49                                         gfx::Size());
50   ShellDevToolsFrontend* devtools_frontend = new ShellDevToolsFrontend(
51       shell,
52       DevToolsAgentHost::GetOrCreateFor(inspected_contents->GetRenderViewHost())
53           .get());
54
55   ShellDevToolsDelegate* delegate = ShellContentBrowserClient::Get()->
56       shell_browser_main_parts()->devtools_delegate();
57   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree))
58     shell->LoadURL(GetDevToolsPathAsURL());
59   else
60     shell->LoadURL(delegate->devtools_http_handler()->GetFrontendURL());
61
62   devtools_frontend->Activate();
63   devtools_frontend->Focus();
64
65   return devtools_frontend;
66 }
67
68 void ShellDevToolsFrontend::Activate() {
69   frontend_shell_->ActivateContents(web_contents());
70 }
71
72 void ShellDevToolsFrontend::Focus() {
73   web_contents()->GetView()->Focus();
74 }
75
76 void ShellDevToolsFrontend::Close() {
77   frontend_shell_->Close();
78 }
79
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) {
85   frontend_host_.reset(
86       DevToolsClientHost::CreateDevToolsFrontendHost(web_contents(), this));
87 }
88
89 ShellDevToolsFrontend::~ShellDevToolsFrontend() {
90 }
91
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());
99 }
100
101 void ShellDevToolsFrontend::WebContentsDestroyed(WebContents* web_contents) {
102   DevToolsManager::GetInstance()->ClientHostClosing(frontend_host_.get());
103   delete this;
104 }
105
106 void ShellDevToolsFrontend::InspectedContentsClosing() {
107   frontend_shell_->Close();
108 }
109
110 }  // namespace content