Add ICU message format support
[chromium-blink-merge.git] / content / shell / browser / shell_devtools_manager_delegate.cc
blobeab3849b1ee3f71cb282ff17dcc1c148e8d19e9e
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"
7 #include <vector>
9 #include "base/bind.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"
37 #endif
39 using devtools_http_handler::DevToolsHttpHandler;
41 namespace content {
43 namespace {
45 #if defined(OS_ANDROID)
46 const char kFrontEndURL[] =
47 "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/inspector.html";
48 #endif
50 const int kBackLog = 10;
52 #if defined(OS_ANDROID)
53 class UnixDomainServerSocketFactory
54 : public DevToolsHttpHandler::ServerSocketFactory {
55 public:
56 explicit UnixDomainServerSocketFactory(const std::string& socket_name)
57 : socket_name_(socket_name) {}
59 private:
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>();
69 return socket;
72 std::string socket_name_;
74 DISALLOW_COPY_AND_ASSIGN(UnixDomainServerSocketFactory);
76 #else
77 class TCPServerSocketFactory
78 : public DevToolsHttpHandler::ServerSocketFactory {
79 public:
80 TCPServerSocketFactory(const std::string& address, uint16 port)
81 : address_(address), port_(port) {
84 private:
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>();
92 return socket;
95 std::string address_;
96 uint16 port_;
98 DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory);
100 #endif
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));
114 #else
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.
117 uint16 port = 0;
118 if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) {
119 int temp_port;
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);
125 } else {
126 DLOG(WARNING) << "Invalid http debugger port number " << temp_port;
129 return scoped_ptr<DevToolsHttpHandler::ServerSocketFactory>(
130 new TCPServerSocketFactory("127.0.0.1", port));
131 #endif
134 scoped_ptr<devtools_discovery::DevToolsTargetDescriptor>
135 CreateNewShellTarget(BrowserContext* browser_context, const GURL& url) {
136 Shell* shell = Shell::CreateNewWindow(browser_context,
137 url,
138 nullptr,
139 gfx::Size());
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 {
148 public:
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;
157 private:
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()->
172 SetCreateCallback(
173 devtools_discovery::DevToolsDiscoveryManager::CreateCallback());
176 std::string ShellDevToolsDelegate::GetDiscoveryPageHTML() {
177 #if defined(OS_ANDROID)
178 return std::string();
179 #else
180 return ResourceBundle::GetSharedInstance().GetRawDataResource(
181 IDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE).as_string();
182 #endif
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();
194 } // namespace
196 // ShellDevToolsManagerDelegate ----------------------------------------------
198 // static
199 DevToolsHttpHandler*
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());
205 #endif
206 return new DevToolsHttpHandler(
207 CreateSocketFactory(),
208 frontend_url,
209 new ShellDevToolsDelegate(browser_context),
210 base::FilePath(),
211 base::FilePath(),
212 std::string(),
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) {
227 return NULL;
230 } // namespace content