| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/devtools/remote_debugging_server.h" | 5 #include "chrome/browser/devtools/remote_debugging_server.h" |
| 6 | 6 |
| 7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "base/macros.h" |
| 8 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 9 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 10 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/devtools/devtools_window.h" | 12 #include "chrome/browser/devtools/devtools_window.h" |
| 12 #include "chrome/browser/history/top_sites_factory.h" | 13 #include "chrome/browser/history/top_sites_factory.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/browser/ui/browser_iterator.h" | 16 #include "chrome/browser/ui/browser_iterator.h" |
| 16 #include "chrome/common/chrome_content_client.h" | 17 #include "chrome/common/chrome_content_client.h" |
| 17 #include "chrome/common/chrome_paths.h" | 18 #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.h" |
| 19 #include "components/devtools_http_handler/devtools_http_handler_delegate.h" | 20 #include "components/devtools_http_handler/devtools_http_handler_delegate.h" |
| 20 #include "components/history/core/browser/top_sites.h" | 21 #include "components/history/core/browser/top_sites.h" |
| 21 #include "components/version_info/version_info.h" | 22 #include "components/version_info/version_info.h" |
| 22 #include "content/public/browser/devtools_frontend_host.h" | 23 #include "content/public/browser/devtools_frontend_host.h" |
| 23 #include "grit/browser_resources.h" | 24 #include "grit/browser_resources.h" |
| 24 #include "net/base/net_errors.h" | 25 #include "net/base/net_errors.h" |
| 25 #include "net/socket/tcp_server_socket.h" | 26 #include "net/socket/tcp_server_socket.h" |
| 26 #include "ui/base/resource/resource_bundle.h" | 27 #include "ui/base/resource/resource_bundle.h" |
| 27 | 28 |
| 28 namespace { | 29 namespace { |
| 29 | 30 |
| 30 base::LazyInstance<bool>::Leaky g_tethering_enabled = LAZY_INSTANCE_INITIALIZER; | 31 base::LazyInstance<bool>::Leaky g_tethering_enabled = LAZY_INSTANCE_INITIALIZER; |
| 31 | 32 |
| 32 const uint16 kMinTetheringPort = 9333; | 33 const uint16_t kMinTetheringPort = 9333; |
| 33 const uint16 kMaxTetheringPort = 9444; | 34 const uint16_t kMaxTetheringPort = 9444; |
| 34 const int kBackLog = 10; | 35 const int kBackLog = 10; |
| 35 | 36 |
| 36 class TCPServerSocketFactory | 37 class TCPServerSocketFactory |
| 37 : public devtools_http_handler::DevToolsHttpHandler::ServerSocketFactory { | 38 : public devtools_http_handler::DevToolsHttpHandler::ServerSocketFactory { |
| 38 public: | 39 public: |
| 39 TCPServerSocketFactory(const std::string& address, uint16 port) | 40 TCPServerSocketFactory(const std::string& address, uint16_t port) |
| 40 : address_(address), | 41 : address_(address), |
| 41 port_(port), | 42 port_(port), |
| 42 last_tethering_port_(kMinTetheringPort) { | 43 last_tethering_port_(kMinTetheringPort) {} |
| 43 } | |
| 44 | 44 |
| 45 private: | 45 private: |
| 46 // devtools_http_handler::DevToolsHttpHandler::ServerSocketFactory. | 46 // devtools_http_handler::DevToolsHttpHandler::ServerSocketFactory. |
| 47 scoped_ptr<net::ServerSocket> CreateForHttpServer() override { | 47 scoped_ptr<net::ServerSocket> CreateForHttpServer() override { |
| 48 scoped_ptr<net::ServerSocket> socket( | 48 scoped_ptr<net::ServerSocket> socket( |
| 49 new net::TCPServerSocket(nullptr, net::NetLog::Source())); | 49 new net::TCPServerSocket(nullptr, net::NetLog::Source())); |
| 50 if (socket->ListenWithAddressAndPort(address_, port_, kBackLog) != net::OK) | 50 if (socket->ListenWithAddressAndPort(address_, port_, kBackLog) != net::OK) |
| 51 return scoped_ptr<net::ServerSocket>(); | 51 return scoped_ptr<net::ServerSocket>(); |
| 52 | 52 |
| 53 return socket; | 53 return socket; |
| 54 } | 54 } |
| 55 | 55 |
| 56 scoped_ptr<net::ServerSocket> CreateForTethering(std::string* name) override { | 56 scoped_ptr<net::ServerSocket> CreateForTethering(std::string* name) override { |
| 57 if (!g_tethering_enabled.Get()) | 57 if (!g_tethering_enabled.Get()) |
| 58 return scoped_ptr<net::ServerSocket>(); | 58 return scoped_ptr<net::ServerSocket>(); |
| 59 | 59 |
| 60 if (last_tethering_port_ == kMaxTetheringPort) | 60 if (last_tethering_port_ == kMaxTetheringPort) |
| 61 last_tethering_port_ = kMinTetheringPort; | 61 last_tethering_port_ = kMinTetheringPort; |
| 62 uint16 port = ++last_tethering_port_; | 62 uint16_t port = ++last_tethering_port_; |
| 63 *name = base::UintToString(port); | 63 *name = base::UintToString(port); |
| 64 scoped_ptr<net::TCPServerSocket> socket( | 64 scoped_ptr<net::TCPServerSocket> socket( |
| 65 new net::TCPServerSocket(nullptr, net::NetLog::Source())); | 65 new net::TCPServerSocket(nullptr, net::NetLog::Source())); |
| 66 if (socket->ListenWithAddressAndPort("127.0.0.1", port, kBackLog) != | 66 if (socket->ListenWithAddressAndPort("127.0.0.1", port, kBackLog) != |
| 67 net::OK) { | 67 net::OK) { |
| 68 return scoped_ptr<net::ServerSocket>(); | 68 return scoped_ptr<net::ServerSocket>(); |
| 69 } | 69 } |
| 70 return socket.Pass(); | 70 return socket.Pass(); |
| 71 } | 71 } |
| 72 | 72 |
| 73 std::string address_; | 73 std::string address_; |
| 74 uint16 port_; | 74 uint16_t port_; |
| 75 uint16 last_tethering_port_; | 75 uint16_t last_tethering_port_; |
| 76 | 76 |
| 77 DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory); | 77 DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory); |
| 78 }; | 78 }; |
| 79 | 79 |
| 80 class ChromeDevToolsHttpHandlerDelegate | 80 class ChromeDevToolsHttpHandlerDelegate |
| 81 : public devtools_http_handler::DevToolsHttpHandlerDelegate { | 81 : public devtools_http_handler::DevToolsHttpHandlerDelegate { |
| 82 public: | 82 public: |
| 83 ChromeDevToolsHttpHandlerDelegate(); | 83 ChromeDevToolsHttpHandlerDelegate(); |
| 84 ~ChromeDevToolsHttpHandlerDelegate() override; | 84 ~ChromeDevToolsHttpHandlerDelegate() override; |
| 85 | 85 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 } // namespace | 147 } // namespace |
| 148 | 148 |
| 149 // static | 149 // static |
| 150 void RemoteDebuggingServer::EnableTetheringForDebug() { | 150 void RemoteDebuggingServer::EnableTetheringForDebug() { |
| 151 g_tethering_enabled.Get() = true; | 151 g_tethering_enabled.Get() = true; |
| 152 } | 152 } |
| 153 | 153 |
| 154 RemoteDebuggingServer::RemoteDebuggingServer( | 154 RemoteDebuggingServer::RemoteDebuggingServer( |
| 155 chrome::HostDesktopType host_desktop_type, | 155 chrome::HostDesktopType host_desktop_type, |
| 156 const std::string& ip, | 156 const std::string& ip, |
| 157 uint16 port) { | 157 uint16_t port) { |
| 158 base::FilePath output_dir; | 158 base::FilePath output_dir; |
| 159 if (!port) { | 159 if (!port) { |
| 160 // The client requested an ephemeral port. Must write the selected | 160 // The client requested an ephemeral port. Must write the selected |
| 161 // port to a well-known location in the profile directory to | 161 // port to a well-known location in the profile directory to |
| 162 // bootstrap the connection process. | 162 // bootstrap the connection process. |
| 163 bool result = PathService::Get(chrome::DIR_USER_DATA, &output_dir); | 163 bool result = PathService::Get(chrome::DIR_USER_DATA, &output_dir); |
| 164 DCHECK(result); | 164 DCHECK(result); |
| 165 } | 165 } |
| 166 | 166 |
| 167 base::FilePath debug_frontend_dir; | 167 base::FilePath debug_frontend_dir; |
| 168 #if defined(DEBUG_DEVTOOLS) | 168 #if defined(DEBUG_DEVTOOLS) |
| 169 PathService::Get(chrome::DIR_INSPECTOR, &debug_frontend_dir); | 169 PathService::Get(chrome::DIR_INSPECTOR, &debug_frontend_dir); |
| 170 #endif | 170 #endif |
| 171 | 171 |
| 172 devtools_http_handler_.reset(new devtools_http_handler::DevToolsHttpHandler( | 172 devtools_http_handler_.reset(new devtools_http_handler::DevToolsHttpHandler( |
| 173 make_scoped_ptr(new TCPServerSocketFactory(ip, port)), | 173 make_scoped_ptr(new TCPServerSocketFactory(ip, port)), |
| 174 std::string(), | 174 std::string(), |
| 175 new ChromeDevToolsHttpHandlerDelegate(), | 175 new ChromeDevToolsHttpHandlerDelegate(), |
| 176 output_dir, | 176 output_dir, |
| 177 debug_frontend_dir, | 177 debug_frontend_dir, |
| 178 version_info::GetProductNameAndVersionForUserAgent(), | 178 version_info::GetProductNameAndVersionForUserAgent(), |
| 179 ::GetUserAgent())); | 179 ::GetUserAgent())); |
| 180 } | 180 } |
| 181 | 181 |
| 182 RemoteDebuggingServer::~RemoteDebuggingServer() { | 182 RemoteDebuggingServer::~RemoteDebuggingServer() { |
| 183 // Ensure Profile is alive, because the whole DevTools subsystem | 183 // Ensure Profile is alive, because the whole DevTools subsystem |
| 184 // accesses it during shutdown. | 184 // accesses it during shutdown. |
| 185 DCHECK(g_browser_process->profile_manager()); | 185 DCHECK(g_browser_process->profile_manager()); |
| 186 } | 186 } |
| OLD | NEW |