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
;
156 DevToolsExternalAgentProxyDelegate
*
157 HandleWebSocketConnection(const std::string
& path
) override
;
160 BrowserContext
* browser_context_
;
162 DISALLOW_COPY_AND_ASSIGN(ShellDevToolsDelegate
);
165 ShellDevToolsDelegate::ShellDevToolsDelegate(BrowserContext
* browser_context
)
166 : browser_context_(browser_context
) {
167 devtools_discovery::DevToolsDiscoveryManager::GetInstance()->
168 SetCreateCallback(base::Bind(&CreateNewShellTarget
,
169 base::Unretained(browser_context
)));
172 ShellDevToolsDelegate::~ShellDevToolsDelegate() {
173 devtools_discovery::DevToolsDiscoveryManager::GetInstance()->
175 devtools_discovery::DevToolsDiscoveryManager::CreateCallback());
178 std::string
ShellDevToolsDelegate::GetDiscoveryPageHTML() {
179 #if defined(OS_ANDROID)
180 return std::string();
182 return ResourceBundle::GetSharedInstance().GetRawDataResource(
183 IDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE
).as_string();
187 std::string
ShellDevToolsDelegate::GetFrontendResource(
188 const std::string
& path
) {
189 return content::DevToolsFrontendHost::GetFrontendResource(path
).as_string();
192 std::string
ShellDevToolsDelegate::GetPageThumbnailData(const GURL
& url
) {
193 return std::string();
196 DevToolsExternalAgentProxyDelegate
*
197 ShellDevToolsDelegate::HandleWebSocketConnection(const std::string
& path
) {
203 // ShellDevToolsManagerDelegate ----------------------------------------------
207 ShellDevToolsManagerDelegate::CreateHttpHandler(
208 BrowserContext
* browser_context
) {
209 std::string frontend_url
;
210 #if defined(OS_ANDROID)
211 frontend_url
= base::StringPrintf(kFrontEndURL
, GetWebKitRevision().c_str());
213 return new DevToolsHttpHandler(
214 CreateSocketFactory(),
216 new ShellDevToolsDelegate(browser_context
),
220 GetShellUserAgent());
223 ShellDevToolsManagerDelegate::ShellDevToolsManagerDelegate(
224 BrowserContext
* browser_context
)
225 : browser_context_(browser_context
) {
228 ShellDevToolsManagerDelegate::~ShellDevToolsManagerDelegate() {
231 base::DictionaryValue
* ShellDevToolsManagerDelegate::HandleCommand(
232 DevToolsAgentHost
* agent_host
,
233 base::DictionaryValue
* command
) {
237 } // namespace content