Roll src/third_party/WebKit 06cb9e9:a978ee5 (svn 202558:202559)
[chromium-blink-merge.git] / android_webview / native / aw_dev_tools_server.cc
blob83ab93253a74327af42e2887a9309e2d04c00db8
1 // Copyright 2013 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 "android_webview/native/aw_dev_tools_server.h"
7 #include "android_webview/common/aw_content_client.h"
8 #include "android_webview/native/aw_contents.h"
9 #include "base/bind.h"
10 #include "base/files/file_path.h"
11 #include "base/json/json_writer.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/values.h"
15 #include "components/devtools_http_handler/devtools_http_handler.h"
16 #include "components/devtools_http_handler/devtools_http_handler_delegate.h"
17 #include "content/public/browser/android/devtools_auth.h"
18 #include "content/public/browser/devtools_agent_host.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/common/user_agent.h"
21 #include "jni/AwDevToolsServer_jni.h"
22 #include "net/base/net_errors.h"
23 #include "net/socket/unix_domain_server_socket_posix.h"
25 using content::DevToolsAgentHost;
26 using content::RenderViewHost;
27 using content::WebContents;
28 using devtools_http_handler::DevToolsHttpHandler;
30 namespace {
32 const char kFrontEndURL[] =
33 "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/inspector.html";
34 const char kSocketNameFormat[] = "webview_devtools_remote_%d";
35 const char kTetheringSocketName[] = "webview_devtools_tethering_%d_%d";
37 const int kBackLog = 10;
39 // Delegate implementation for the devtools http handler for WebView. A new
40 // instance of this gets created each time web debugging is enabled.
41 class AwDevToolsServerDelegate :
42 public devtools_http_handler::DevToolsHttpHandlerDelegate {
43 public:
44 AwDevToolsServerDelegate() {
47 ~AwDevToolsServerDelegate() override {}
49 // devtools_http_handler::DevToolsHttpHandlerDelegate implementation.
50 std::string GetDiscoveryPageHTML() override;
51 std::string GetFrontendResource(const std::string& path) override;
52 std::string GetPageThumbnailData(const GURL&) override;
53 content::DevToolsExternalAgentProxyDelegate*
54 HandleWebSocketConnection(const std::string& path) override;
56 private:
58 DISALLOW_COPY_AND_ASSIGN(AwDevToolsServerDelegate);
62 std::string AwDevToolsServerDelegate::GetDiscoveryPageHTML() {
63 const char html[] =
64 "<html>"
65 "<head><title>WebView remote debugging</title></head>"
66 "<body>Please use <a href=\'chrome://inspect\'>chrome://inspect</a>"
67 "</body>"
68 "</html>";
69 return html;
72 std::string AwDevToolsServerDelegate::GetFrontendResource(
73 const std::string& path) {
74 return std::string();
77 std::string AwDevToolsServerDelegate::GetPageThumbnailData(const GURL&) {
78 return std::string();
81 content::DevToolsExternalAgentProxyDelegate*
82 AwDevToolsServerDelegate::HandleWebSocketConnection(const std::string& path) {
83 return nullptr;
86 // Factory for UnixDomainServerSocket.
87 class UnixDomainServerSocketFactory
88 : public DevToolsHttpHandler::ServerSocketFactory {
89 public:
90 explicit UnixDomainServerSocketFactory(const std::string& socket_name)
91 : socket_name_(socket_name),
92 last_tethering_socket_(0) {
95 private:
96 // devtools_http_handler::DevToolsHttpHandler::ServerSocketFactory.
97 scoped_ptr<net::ServerSocket> CreateForHttpServer() override {
98 scoped_ptr<net::ServerSocket> socket(
99 new net::UnixDomainServerSocket(
100 base::Bind(&content::CanUserConnectToDevTools),
101 true /* use_abstract_namespace */));
102 if (socket->ListenWithAddressAndPort(socket_name_, 0, kBackLog) != net::OK)
103 return scoped_ptr<net::ServerSocket>();
105 return socket;
108 scoped_ptr<net::ServerSocket> CreateForTethering(std::string* name) override {
109 *name = base::StringPrintf(
110 kTetheringSocketName, getpid(), ++last_tethering_socket_);
111 scoped_ptr<net::UnixDomainServerSocket> socket(
112 new net::UnixDomainServerSocket(
113 base::Bind(&content::CanUserConnectToDevTools), true));
114 if (socket->ListenWithAddressAndPort(*name, 0, kBackLog) != net::OK)
115 return scoped_ptr<net::ServerSocket>();
117 return socket.Pass();
120 std::string socket_name_;
121 int last_tethering_socket_;
123 DISALLOW_COPY_AND_ASSIGN(UnixDomainServerSocketFactory);
126 } // namespace
128 namespace android_webview {
130 AwDevToolsServer::AwDevToolsServer() {
133 AwDevToolsServer::~AwDevToolsServer() {
134 Stop();
137 void AwDevToolsServer::Start() {
138 if (devtools_http_handler_)
139 return;
141 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory(
142 new UnixDomainServerSocketFactory(
143 base::StringPrintf(kSocketNameFormat, getpid())));
144 devtools_http_handler_.reset(new DevToolsHttpHandler(
145 factory.Pass(),
146 base::StringPrintf(kFrontEndURL, content::GetWebKitRevision().c_str()),
147 new AwDevToolsServerDelegate(),
148 base::FilePath(),
149 base::FilePath(),
150 GetProduct(),
151 GetUserAgent()));
154 void AwDevToolsServer::Stop() {
155 devtools_http_handler_.reset();
158 bool AwDevToolsServer::IsStarted() const {
159 return devtools_http_handler_;
162 bool RegisterAwDevToolsServer(JNIEnv* env) {
163 return RegisterNativesImpl(env);
166 static jlong InitRemoteDebugging(JNIEnv* env,
167 const JavaParamRef<jobject>& obj) {
168 AwDevToolsServer* server = new AwDevToolsServer();
169 return reinterpret_cast<intptr_t>(server);
172 static void DestroyRemoteDebugging(JNIEnv* env,
173 const JavaParamRef<jobject>& obj,
174 jlong server) {
175 delete reinterpret_cast<AwDevToolsServer*>(server);
178 static void SetRemoteDebuggingEnabled(JNIEnv* env,
179 const JavaParamRef<jobject>& obj,
180 jlong server,
181 jboolean enabled) {
182 AwDevToolsServer* devtools_server =
183 reinterpret_cast<AwDevToolsServer*>(server);
184 if (enabled) {
185 devtools_server->Start();
186 } else {
187 devtools_server->Stop();
191 } // namespace android_webview