[M49_2623] Chromium upversion to m49_2623 branch.
[platform/framework/web/chromium-efl.git] / tizen_src / ewk / efl_integration / devtools_delegate_efl.cc
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.
4
5 #include "devtools_delegate_efl.h"
6
7 #include <vector>
8
9 #include "base/bind.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"
33 #include <stdlib.h>
34 #include <time.h>
35 #include <unistd.h>
36
37 using content::BrowserThread;
38 using devtools_http_handler::DevToolsHttpHandler;
39
40 namespace {
41
42 // Copy of internal class implementation from
43 // content/shell/browser/shell_devtools_delegate.cc
44
45 const uint16_t kMinTetheringPort = 9333;
46 const uint16_t kMaxTetheringPort = 9444;
47 const int kBackLog = 10;
48
49 class TCPServerSocketFactory
50     : public DevToolsHttpHandler::ServerSocketFactory {
51  public:
52   TCPServerSocketFactory(const std::string& address, uint16_t port)
53       : address_(address),
54         port_(port) {}
55
56  private:
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>();
62
63     return socket;
64   }
65
66   std::string address_;
67   uint16_t port_;
68 };
69
70 }  // namespace
71
72
73 namespace devtools_http_handler {
74
75 DevToolsDelegateEfl::DevToolsDelegateEfl(int port)
76     : port_(0)
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
82   // and on which port.
83   static std::string addr = "0.0.0.0";
84   scoped_ptr<net::ServerSocket> sock(new net::TCPServerSocket(NULL, net::NetLog::Source()));
85
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)) {
90     int temp_port = 0;
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) {
95       port = temp_port;
96     } else {
97       DLOG(WARNING) << "Invalid http debugger port number " << temp_port;
98     }
99   }
100
101   // why backlog is 1?
102   if (sock->ListenWithAddressAndPort(addr, port, 1) != net::OK) {
103     port_ = 0;
104     return;
105   }
106
107   net::IPEndPoint givenIp;
108   sock->GetLocalAddress(&givenIp);
109   port_ = givenIp.port();
110
111   scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory(
112       new TCPServerSocketFactory(addr, port_));
113
114   devtools_http_handler_.reset(new devtools_http_handler::DevToolsHttpHandler(
115       std::move(factory), std::string(), this, base::FilePath(),
116       base::FilePath(), EflWebView::VersionInfo::GetInstance()
117                             ->ProductNameAndVersionForUserAgent(),
118       EflWebView::VersionInfo::GetInstance()->DefaultUserAgent()));
119 }
120
121 DevToolsDelegateEfl::~DevToolsDelegateEfl() {
122 }
123
124 void DevToolsDelegateEfl::Stop() {
125   if (devtools_http_handler_) {
126     // The call below destroys this.
127     devtools_http_handler_.reset();
128   } else {
129     BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, this);
130   }
131 }
132
133 std::string DevToolsDelegateEfl::GetDiscoveryPageHTML() {
134   return ResourceBundle::GetSharedInstance().GetRawDataResource(
135       IDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE).as_string();
136 }
137
138 std::string DevToolsDelegateEfl::GetFrontendResource(const std::string& path) {
139   return content::DevToolsFrontendHost::GetFrontendResource(path).as_string();
140 }
141
142 std::string DevToolsDelegateEfl::GetPageThumbnailData(const GURL& url) {
143   return std::string();
144 }
145
146 content::DevToolsExternalAgentProxyDelegate*
147 DevToolsDelegateEfl::HandleWebSocketConnection(const std::string& path) {
148   return nullptr;
149 }
150
151 }  // namespace content