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_manager_delegate.h"
10 #include "base/command_line.h"
11 #include "base/files/file_path.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "components/devtools_discovery/basic_target_descriptor.h"
16 #include "components/devtools_discovery/devtools_discovery_manager.h"
17 #include "components/devtools_http_handler/devtools_http_handler.h"
18 #include "content/public/browser/devtools_agent_host.h"
19 #include "content/public/browser/devtools_frontend_host.h"
20 #include "content/public/browser/favicon_status.h"
21 #include "content/public/browser/navigation_entry.h"
22 #include "content/public/browser/render_view_host.h"
23 #include "content/public/browser/web_contents.h"
24 #include "content/public/common/content_switches.h"
25 #include "content/public/common/url_constants.h"
26 #include "content/public/common/user_agent.h"
27 #include "content/shell/browser/shell.h"
28 #include "content/shell/common/shell_content_client.h"
29 #include "grit/shell_resources.h"
30 #include "net/base/net_errors.h"
31 #include "net/socket/tcp_server_socket.h"
32 #include "ui/base/resource/resource_bundle.h"
34 #if defined(OS_ANDROID)
35 #include "content/public/browser/android/devtools_auth.h"
36 #include "net/socket/unix_domain_server_socket_posix.h"
39 using devtools_http_handler::DevToolsHttpHandler
;
45 #if defined(OS_ANDROID)
46 const char kFrontEndURL
[] =
47 "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/inspector.html";
50 const int kBackLog
= 10;
52 #if defined(OS_ANDROID)
53 class UnixDomainServerSocketFactory
54 : public DevToolsHttpHandler::ServerSocketFactory
{
56 explicit UnixDomainServerSocketFactory(const std::string
& socket_name
)
57 : socket_name_(socket_name
) {}
60 // DevToolsHttpHandler::ServerSocketFactory.
61 scoped_ptr
<net::ServerSocket
> CreateForHttpServer() override
{
62 scoped_ptr
<net::ServerSocket
> socket(
63 new net::UnixDomainServerSocket(
64 base::Bind(&CanUserConnectToDevTools
),
65 true /* use_abstract_namespace */));
66 if (socket
->ListenWithAddressAndPort(socket_name_
, 0, kBackLog
) != net::OK
)
67 return scoped_ptr
<net::ServerSocket
>();
72 std::string socket_name_
;
74 DISALLOW_COPY_AND_ASSIGN(UnixDomainServerSocketFactory
);
77 class TCPServerSocketFactory
78 : public DevToolsHttpHandler::ServerSocketFactory
{
80 TCPServerSocketFactory(const std::string
& address
, uint16 port
)
81 : address_(address
), port_(port
) {
85 // DevToolsHttpHandler::ServerSocketFactory.
86 scoped_ptr
<net::ServerSocket
> CreateForHttpServer() override
{
87 scoped_ptr
<net::ServerSocket
> socket(
88 new net::TCPServerSocket(nullptr, net::NetLog::Source()));
89 if (socket
->ListenWithAddressAndPort(address_
, port_
, kBackLog
) != net::OK
)
90 return scoped_ptr
<net::ServerSocket
>();
98 DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory
);
102 scoped_ptr
<DevToolsHttpHandler::ServerSocketFactory
>
103 CreateSocketFactory() {
104 const base::CommandLine
& command_line
=
105 *base::CommandLine::ForCurrentProcess();
106 #if defined(OS_ANDROID)
107 std::string socket_name
= "content_shell_devtools_remote";
108 if (command_line
.HasSwitch(switches::kRemoteDebuggingSocketName
)) {
109 socket_name
= command_line
.GetSwitchValueASCII(
110 switches::kRemoteDebuggingSocketName
);
112 return scoped_ptr
<DevToolsHttpHandler::ServerSocketFactory
>(
113 new UnixDomainServerSocketFactory(socket_name
));
115 // See if the user specified a port on the command line (useful for
116 // automation). If not, use an ephemeral port by specifying 0.
118 if (command_line
.HasSwitch(switches::kRemoteDebuggingPort
)) {
120 std::string port_str
=
121 command_line
.GetSwitchValueASCII(switches::kRemoteDebuggingPort
);
122 if (base::StringToInt(port_str
, &temp_port
) &&
123 temp_port
> 0 && temp_port
< 65535) {
124 port
= static_cast<uint16
>(temp_port
);
126 DLOG(WARNING
) << "Invalid http debugger port number " << temp_port
;
129 return scoped_ptr
<DevToolsHttpHandler::ServerSocketFactory
>(
130 new TCPServerSocketFactory("127.0.0.1", port
));
134 scoped_ptr
<devtools_discovery::DevToolsTargetDescriptor
>
135 CreateNewShellTarget(BrowserContext
* browser_context
, const GURL
& url
) {
136 Shell
* shell
= Shell::CreateNewWindow(browser_context
,
140 return make_scoped_ptr(new devtools_discovery::BasicTargetDescriptor(
141 DevToolsAgentHost::GetOrCreateFor(shell
->web_contents())));
144 // ShellDevToolsDelegate ----------------------------------------------------
146 class ShellDevToolsDelegate
:
147 public devtools_http_handler::DevToolsHttpHandlerDelegate
{
149 explicit ShellDevToolsDelegate(BrowserContext
* browser_context
);
150 ~ShellDevToolsDelegate() override
;
152 // devtools_http_handler::DevToolsHttpHandlerDelegate implementation.
153 std::string
GetDiscoveryPageHTML() override
;
154 std::string
GetFrontendResource(const std::string
& path
) override
;
155 std::string
GetPageThumbnailData(const GURL
& url
) override
;
158 BrowserContext
* browser_context_
;
160 DISALLOW_COPY_AND_ASSIGN(ShellDevToolsDelegate
);
163 ShellDevToolsDelegate::ShellDevToolsDelegate(BrowserContext
* browser_context
)
164 : browser_context_(browser_context
) {
165 devtools_discovery::DevToolsDiscoveryManager::GetInstance()->
166 SetCreateCallback(base::Bind(&CreateNewShellTarget
,
167 base::Unretained(browser_context
)));
170 ShellDevToolsDelegate::~ShellDevToolsDelegate() {
171 devtools_discovery::DevToolsDiscoveryManager::GetInstance()->
173 devtools_discovery::DevToolsDiscoveryManager::CreateCallback());
176 std::string
ShellDevToolsDelegate::GetDiscoveryPageHTML() {
177 #if defined(OS_ANDROID)
178 return std::string();
180 return ResourceBundle::GetSharedInstance().GetRawDataResource(
181 IDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE
).as_string();
185 std::string
ShellDevToolsDelegate::GetFrontendResource(
186 const std::string
& path
) {
187 return content::DevToolsFrontendHost::GetFrontendResource(path
).as_string();
190 std::string
ShellDevToolsDelegate::GetPageThumbnailData(const GURL
& url
) {
191 return std::string();
196 // ShellDevToolsManagerDelegate ----------------------------------------------
200 ShellDevToolsManagerDelegate::CreateHttpHandler(
201 BrowserContext
* browser_context
) {
202 std::string frontend_url
;
203 #if defined(OS_ANDROID)
204 frontend_url
= base::StringPrintf(kFrontEndURL
, GetWebKitRevision().c_str());
206 return new DevToolsHttpHandler(
207 CreateSocketFactory(),
209 new ShellDevToolsDelegate(browser_context
),
213 GetShellUserAgent());
216 ShellDevToolsManagerDelegate::ShellDevToolsManagerDelegate(
217 BrowserContext
* browser_context
)
218 : browser_context_(browser_context
) {
221 ShellDevToolsManagerDelegate::~ShellDevToolsManagerDelegate() {
224 base::DictionaryValue
* ShellDevToolsManagerDelegate::HandleCommand(
225 DevToolsAgentHost
* agent_host
,
226 base::DictionaryValue
* command
) {
230 } // namespace content