1 // Copyright (c) 2012 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 "chrome/browser/devtools/remote_debugging_server.h"
7 #include "base/lazy_instance.h"
8 #include "base/path_service.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/devtools/devtools_window.h"
12 #include "chrome/browser/history/top_sites_factory.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_iterator.h"
16 #include "chrome/common/chrome_content_client.h"
17 #include "chrome/common/chrome_paths.h"
18 #include "components/devtools_http_handler/devtools_http_handler.h"
19 #include "components/devtools_http_handler/devtools_http_handler_delegate.h"
20 #include "components/history/core/browser/top_sites.h"
21 #include "components/version_info/version_info.h"
22 #include "content/public/browser/devtools_frontend_host.h"
23 #include "grit/browser_resources.h"
24 #include "net/base/net_errors.h"
25 #include "net/socket/tcp_server_socket.h"
26 #include "ui/base/resource/resource_bundle.h"
30 base::LazyInstance
<bool>::Leaky g_tethering_enabled
= LAZY_INSTANCE_INITIALIZER
;
32 const uint16 kMinTetheringPort
= 9333;
33 const uint16 kMaxTetheringPort
= 9444;
34 const int kBackLog
= 10;
36 class TCPServerSocketFactory
37 : public devtools_http_handler::DevToolsHttpHandler::ServerSocketFactory
{
39 TCPServerSocketFactory(const std::string
& address
, uint16 port
)
42 last_tethering_port_(kMinTetheringPort
) {
46 // devtools_http_handler::DevToolsHttpHandler::ServerSocketFactory.
47 scoped_ptr
<net::ServerSocket
> CreateForHttpServer() override
{
48 scoped_ptr
<net::ServerSocket
> socket(
49 new net::TCPServerSocket(nullptr, net::NetLog::Source()));
50 if (socket
->ListenWithAddressAndPort(address_
, port_
, kBackLog
) != net::OK
)
51 return scoped_ptr
<net::ServerSocket
>();
56 scoped_ptr
<net::ServerSocket
> CreateForTethering(std::string
* name
) override
{
57 if (!g_tethering_enabled
.Get())
58 return scoped_ptr
<net::ServerSocket
>();
60 if (last_tethering_port_
== kMaxTetheringPort
)
61 last_tethering_port_
= kMinTetheringPort
;
62 uint16 port
= ++last_tethering_port_
;
63 *name
= base::IntToString(port
);
64 scoped_ptr
<net::TCPServerSocket
> socket(
65 new net::TCPServerSocket(nullptr, net::NetLog::Source()));
66 if (socket
->ListenWithAddressAndPort("127.0.0.1", port
, kBackLog
) !=
68 return scoped_ptr
<net::ServerSocket
>();
75 uint16 last_tethering_port_
;
77 DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory
);
80 class ChromeDevToolsHttpHandlerDelegate
81 : public devtools_http_handler::DevToolsHttpHandlerDelegate
{
83 ChromeDevToolsHttpHandlerDelegate();
84 ~ChromeDevToolsHttpHandlerDelegate() override
;
86 // devtools_http_handler::DevToolsHttpHandlerDelegate implementation.
87 std::string
GetDiscoveryPageHTML() override
;
88 std::string
GetFrontendResource(const std::string
& path
) override
;
89 std::string
GetPageThumbnailData(const GURL
& url
) override
;
90 content::DevToolsExternalAgentProxyDelegate
*
91 HandleWebSocketConnection(const std::string
& path
) override
;
94 DISALLOW_COPY_AND_ASSIGN(ChromeDevToolsHttpHandlerDelegate
);
97 ChromeDevToolsHttpHandlerDelegate::ChromeDevToolsHttpHandlerDelegate() {
100 ChromeDevToolsHttpHandlerDelegate::~ChromeDevToolsHttpHandlerDelegate() {
103 std::string
ChromeDevToolsHttpHandlerDelegate::GetDiscoveryPageHTML() {
104 std::set
<Profile
*> profiles
;
105 for (chrome::BrowserIterator it
; !it
.done(); it
.Next())
106 profiles
.insert((*it
)->profile());
108 for (std::set
<Profile
*>::iterator it
= profiles
.begin();
109 it
!= profiles
.end(); ++it
) {
110 scoped_refptr
<history::TopSites
> ts
= TopSitesFactory::GetForProfile(*it
);
112 // TopSites updates itself after a delay. Ask TopSites to update itself
113 // when we're about to show the remote debugging landing page.
114 ts
->SyncWithHistory();
117 return ResourceBundle::GetSharedInstance().GetRawDataResource(
118 IDR_DEVTOOLS_DISCOVERY_PAGE_HTML
).as_string();
121 std::string
ChromeDevToolsHttpHandlerDelegate::GetFrontendResource(
122 const std::string
& path
) {
123 return content::DevToolsFrontendHost::GetFrontendResource(path
).as_string();
126 std::string
ChromeDevToolsHttpHandlerDelegate::GetPageThumbnailData(
128 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
129 Profile
* profile
= (*it
)->profile();
130 scoped_refptr
<history::TopSites
> top_sites
=
131 TopSitesFactory::GetForProfile(profile
);
134 scoped_refptr
<base::RefCountedMemory
> data
;
135 if (top_sites
->GetPageThumbnail(url
, false, &data
))
136 return std::string(data
->front_as
<char>(), data
->size());
138 return std::string();
141 content::DevToolsExternalAgentProxyDelegate
*
142 ChromeDevToolsHttpHandlerDelegate::HandleWebSocketConnection(
143 const std::string
& path
) {
144 return DevToolsWindow::CreateWebSocketAPIChannel(path
);
150 void RemoteDebuggingServer::EnableTetheringForDebug() {
151 g_tethering_enabled
.Get() = true;
154 RemoteDebuggingServer::RemoteDebuggingServer(
155 chrome::HostDesktopType host_desktop_type
,
156 const std::string
& ip
,
158 base::FilePath output_dir
;
160 // The client requested an ephemeral port. Must write the selected
161 // port to a well-known location in the profile directory to
162 // bootstrap the connection process.
163 bool result
= PathService::Get(chrome::DIR_USER_DATA
, &output_dir
);
167 base::FilePath debug_frontend_dir
;
168 #if defined(DEBUG_DEVTOOLS)
169 PathService::Get(chrome::DIR_INSPECTOR
, &debug_frontend_dir
);
172 devtools_http_handler_
.reset(new devtools_http_handler::DevToolsHttpHandler(
173 make_scoped_ptr(new TCPServerSocketFactory(ip
, port
)),
175 new ChromeDevToolsHttpHandlerDelegate(),
178 version_info::GetProductNameAndVersionForUserAgent(),
182 RemoteDebuggingServer::~RemoteDebuggingServer() {
183 // Ensure Profile is alive, because the whole DevTools subsystem
184 // accesses it during shutdown.
185 DCHECK(g_browser_process
->profile_manager());