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/history/top_sites_factory.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_iterator.h"
15 #include "chrome/common/chrome_content_client.h"
16 #include "chrome/common/chrome_paths.h"
17 #include "components/devtools_http_handler/devtools_http_handler.h"
18 #include "components/devtools_http_handler/devtools_http_handler_delegate.h"
19 #include "components/history/core/browser/top_sites.h"
20 #include "components/version_info/version_info.h"
21 #include "content/public/browser/devtools_frontend_host.h"
22 #include "grit/browser_resources.h"
23 #include "net/base/net_errors.h"
24 #include "net/socket/tcp_server_socket.h"
25 #include "ui/base/resource/resource_bundle.h"
29 base::LazyInstance
<bool>::Leaky g_tethering_enabled
= LAZY_INSTANCE_INITIALIZER
;
31 const uint16 kMinTetheringPort
= 9333;
32 const uint16 kMaxTetheringPort
= 9444;
33 const int kBackLog
= 10;
35 class TCPServerSocketFactory
36 : public devtools_http_handler::DevToolsHttpHandler::ServerSocketFactory
{
38 TCPServerSocketFactory(const std::string
& address
, uint16 port
)
41 last_tethering_port_(kMinTetheringPort
) {
45 // devtools_http_handler::DevToolsHttpHandler::ServerSocketFactory.
46 scoped_ptr
<net::ServerSocket
> CreateForHttpServer() override
{
47 scoped_ptr
<net::ServerSocket
> socket(
48 new net::TCPServerSocket(nullptr, net::NetLog::Source()));
49 if (socket
->ListenWithAddressAndPort(address_
, port_
, kBackLog
) != net::OK
)
50 return scoped_ptr
<net::ServerSocket
>();
55 scoped_ptr
<net::ServerSocket
> CreateForTethering(std::string
* name
) override
{
56 if (!g_tethering_enabled
.Get())
57 return scoped_ptr
<net::ServerSocket
>();
59 if (last_tethering_port_
== kMaxTetheringPort
)
60 last_tethering_port_
= kMinTetheringPort
;
61 uint16 port
= ++last_tethering_port_
;
62 *name
= base::IntToString(port
);
63 scoped_ptr
<net::TCPServerSocket
> socket(
64 new net::TCPServerSocket(nullptr, net::NetLog::Source()));
65 if (socket
->ListenWithAddressAndPort("127.0.0.1", port
, kBackLog
) !=
67 return scoped_ptr
<net::ServerSocket
>();
74 uint16 last_tethering_port_
;
76 DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory
);
79 class ChromeDevToolsHttpHandlerDelegate
80 : public devtools_http_handler::DevToolsHttpHandlerDelegate
{
82 ChromeDevToolsHttpHandlerDelegate();
83 ~ChromeDevToolsHttpHandlerDelegate() override
;
85 // devtools_http_handler::DevToolsHttpHandlerDelegate implementation.
86 std::string
GetDiscoveryPageHTML() override
;
87 std::string
GetFrontendResource(const std::string
& path
) override
;
88 std::string
GetPageThumbnailData(const GURL
& url
) override
;
91 DISALLOW_COPY_AND_ASSIGN(ChromeDevToolsHttpHandlerDelegate
);
94 ChromeDevToolsHttpHandlerDelegate::ChromeDevToolsHttpHandlerDelegate() {
97 ChromeDevToolsHttpHandlerDelegate::~ChromeDevToolsHttpHandlerDelegate() {
100 std::string
ChromeDevToolsHttpHandlerDelegate::GetDiscoveryPageHTML() {
101 std::set
<Profile
*> profiles
;
102 for (chrome::BrowserIterator it
; !it
.done(); it
.Next())
103 profiles
.insert((*it
)->profile());
105 for (std::set
<Profile
*>::iterator it
= profiles
.begin();
106 it
!= profiles
.end(); ++it
) {
107 scoped_refptr
<history::TopSites
> ts
= TopSitesFactory::GetForProfile(*it
);
109 // TopSites updates itself after a delay. Ask TopSites to update itself
110 // when we're about to show the remote debugging landing page.
111 ts
->SyncWithHistory();
114 return ResourceBundle::GetSharedInstance().GetRawDataResource(
115 IDR_DEVTOOLS_DISCOVERY_PAGE_HTML
).as_string();
118 std::string
ChromeDevToolsHttpHandlerDelegate::GetFrontendResource(
119 const std::string
& path
) {
120 return content::DevToolsFrontendHost::GetFrontendResource(path
).as_string();
123 std::string
ChromeDevToolsHttpHandlerDelegate::GetPageThumbnailData(
125 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
126 Profile
* profile
= (*it
)->profile();
127 scoped_refptr
<history::TopSites
> top_sites
=
128 TopSitesFactory::GetForProfile(profile
);
131 scoped_refptr
<base::RefCountedMemory
> data
;
132 if (top_sites
->GetPageThumbnail(url
, false, &data
))
133 return std::string(data
->front_as
<char>(), data
->size());
135 return std::string();
141 void RemoteDebuggingServer::EnableTetheringForDebug() {
142 g_tethering_enabled
.Get() = true;
145 RemoteDebuggingServer::RemoteDebuggingServer(
146 chrome::HostDesktopType host_desktop_type
,
147 const std::string
& ip
,
149 base::FilePath output_dir
;
151 // The client requested an ephemeral port. Must write the selected
152 // port to a well-known location in the profile directory to
153 // bootstrap the connection process.
154 bool result
= PathService::Get(chrome::DIR_USER_DATA
, &output_dir
);
158 base::FilePath debug_frontend_dir
;
159 #if defined(DEBUG_DEVTOOLS)
160 PathService::Get(chrome::DIR_INSPECTOR
, &debug_frontend_dir
);
163 devtools_http_handler_
.reset(new devtools_http_handler::DevToolsHttpHandler(
164 make_scoped_ptr(new TCPServerSocketFactory(ip
, port
)),
166 new ChromeDevToolsHttpHandlerDelegate(),
169 version_info::GetProductNameAndVersionForUserAgent(),
173 RemoteDebuggingServer::~RemoteDebuggingServer() {
174 // Ensure Profile is alive, because the whole DevTools subsystem
175 // accesses it during shutdown.
176 DCHECK(g_browser_process
->profile_manager());