Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / chrome / browser / devtools / remote_debugging_server.cc
blob49c8f588f178679608d871a9dfde878969df7773
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"
28 namespace {
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 {
38 public:
39 TCPServerSocketFactory(const std::string& address, uint16 port)
40 : address_(address),
41 port_(port),
42 last_tethering_port_(kMinTetheringPort) {
45 private:
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>();
53 return socket;
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) !=
67 net::OK) {
68 return scoped_ptr<net::ServerSocket>();
70 return socket.Pass();
73 std::string address_;
74 uint16 port_;
75 uint16 last_tethering_port_;
77 DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory);
80 class ChromeDevToolsHttpHandlerDelegate
81 : public devtools_http_handler::DevToolsHttpHandlerDelegate {
82 public:
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;
93 private:
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);
111 if (ts) {
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(
127 const GURL& url) {
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);
132 if (!top_sites)
133 continue;
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);
147 } // namespace
149 // static
150 void RemoteDebuggingServer::EnableTetheringForDebug() {
151 g_tethering_enabled.Get() = true;
154 RemoteDebuggingServer::RemoteDebuggingServer(
155 chrome::HostDesktopType host_desktop_type,
156 const std::string& ip,
157 uint16 port) {
158 base::FilePath output_dir;
159 if (!port) {
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);
164 DCHECK(result);
167 base::FilePath debug_frontend_dir;
168 #if defined(DEBUG_DEVTOOLS)
169 PathService::Get(chrome::DIR_INSPECTOR, &debug_frontend_dir);
170 #endif
172 devtools_http_handler_.reset(new devtools_http_handler::DevToolsHttpHandler(
173 make_scoped_ptr(new TCPServerSocketFactory(ip, port)),
174 std::string(),
175 new ChromeDevToolsHttpHandlerDelegate(),
176 output_dir,
177 debug_frontend_dir,
178 version_info::GetProductNameAndVersionForUserAgent(),
179 ::GetUserAgent()));
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());