1 // Copyright 2014 Samsung Electronics. 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 "devtools_delegate_efl.h"
10 #include "base/command_line.h"
11 #include "base/lazy_instance.h"
12 #include "base/logging.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "common/version_info.h"
17 #include "components/devtools_discovery/devtools_target_descriptor.h"
18 #include "components/devtools_http_handler/devtools_http_handler.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/devtools_agent_host.h"
21 #include "content/public/browser/devtools_frontend_host.h"
22 #include "content/public/common/content_client.h"
23 #include "content/public/common/content_switches.h"
24 #include "content/public/common/url_constants.h"
25 #include "content/public/common/user_agent.h"
26 #include "content/shell/browser/shell.h"
27 #include "grit/shell_resources.h"
28 #include "grit/devtools_resources.h"
29 #include "net/base/net_errors.h"
30 #include "net/socket/tcp_server_socket.h"
31 #include "net/base/net_errors.h"
32 #include "ui/base/resource/resource_bundle.h"
37 using content::BrowserThread;
38 using devtools_http_handler::DevToolsHttpHandler;
42 // Copy of internal class implementation from
43 // content/shell/browser/shell_devtools_delegate.cc
45 const uint16 kMinTetheringPort = 9333;
46 const uint16 kMaxTetheringPort = 9444;
47 const int kBackLog = 10;
49 class TCPServerSocketFactory
50 : public DevToolsHttpHandler::ServerSocketFactory {
52 TCPServerSocketFactory(const std::string& address, uint16 port)
57 scoped_ptr<net::ServerSocket> CreateForHttpServer() override {
58 scoped_ptr<net::ServerSocket> socket(
59 new net::TCPServerSocket(nullptr, net::NetLog::Source()));
60 if (socket->ListenWithAddressAndPort(address_, port_, kBackLog) != net::OK)
61 return scoped_ptr<net::ServerSocket>();
73 namespace devtools_http_handler {
75 DevToolsDelegateEfl::DevToolsDelegateEfl(int port)
77 , browser_context_(NULL) {
78 // It's a hacky way to early detected if port is available. The problem is
79 // that the only thing we can do after checking is hope that noone will take
80 // the port until it's initialized on IO thread again. The best approach would
81 // be creating async callbacks that would inform when inspector server started
83 static std::string addr = "0.0.0.0";
84 scoped_ptr<net::ServerSocket> sock(new net::TCPServerSocket(NULL, net::NetLog::Source()));
86 const base::CommandLine* const command_line = base::CommandLine::ForCurrentProcess();
87 // See if the user specified a port on the command line (useful for
88 // automation). If not, use an ephemeral port by specifying 0.
89 if (!port && command_line->HasSwitch(switches::kRemoteDebuggingPort)) {
91 std::string port_str =
92 command_line->GetSwitchValueASCII(switches::kRemoteDebuggingPort);
93 base::StringToInt(port_str, &temp_port);
94 if (temp_port > 0 && temp_port < 65535) {
97 DLOG(WARNING) << "Invalid http debugger port number " << temp_port;
102 if (sock->ListenWithAddressAndPort(addr, port, 1) != net::OK) {
107 net::IPEndPoint givenIp;
108 sock->GetLocalAddress(&givenIp);
109 port_ = givenIp.port();
111 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory(
112 new TCPServerSocketFactory(addr, port_));
114 devtools_http_handler_.reset(
115 new devtools_http_handler::DevToolsHttpHandler(factory.Pass(),
116 std::string(), this, base::FilePath(),
118 EflWebView::VersionInfo::GetInstance()->ProductNameAndVersionForUserAgent(),
119 EflWebView::VersionInfo::GetInstance()->DefaultUserAgent()));
122 DevToolsDelegateEfl::~DevToolsDelegateEfl() {
125 void DevToolsDelegateEfl::Stop() {
126 if (devtools_http_handler_) {
127 // The call below destroys this.
128 devtools_http_handler_.reset();
130 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, this);
134 std::string DevToolsDelegateEfl::GetDiscoveryPageHTML() {
135 return ResourceBundle::GetSharedInstance().GetRawDataResource(
136 IDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE).as_string();
139 std::string DevToolsDelegateEfl::GetFrontendResource(const std::string& path) {
140 return content::DevToolsFrontendHost::GetFrontendResource(path).as_string();
143 std::string DevToolsDelegateEfl::GetPageThumbnailData(const GURL& url) {
144 return std::string();
147 content::DevToolsExternalAgentProxyDelegate*
148 DevToolsDelegateEfl::HandleWebSocketConnection(const std::string& path) {
152 } // namespace content