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"
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
;
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
{
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
;
58 DISALLOW_COPY_AND_ASSIGN(AwDevToolsServerDelegate
);
62 std::string
AwDevToolsServerDelegate::GetDiscoveryPageHTML() {
65 "<head><title>WebView remote debugging</title></head>"
66 "<body>Please use <a href=\'chrome://inspect\'>chrome://inspect</a>"
72 std::string
AwDevToolsServerDelegate::GetFrontendResource(
73 const std::string
& path
) {
77 std::string
AwDevToolsServerDelegate::GetPageThumbnailData(const GURL
&) {
81 content::DevToolsExternalAgentProxyDelegate
*
82 AwDevToolsServerDelegate::HandleWebSocketConnection(const std::string
& path
) {
86 // Factory for UnixDomainServerSocket.
87 class UnixDomainServerSocketFactory
88 : public DevToolsHttpHandler::ServerSocketFactory
{
90 explicit UnixDomainServerSocketFactory(const std::string
& socket_name
)
91 : socket_name_(socket_name
),
92 last_tethering_socket_(0) {
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
>();
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
);
128 namespace android_webview
{
130 AwDevToolsServer::AwDevToolsServer() {
133 AwDevToolsServer::~AwDevToolsServer() {
137 void AwDevToolsServer::Start() {
138 if (devtools_http_handler_
)
141 scoped_ptr
<DevToolsHttpHandler::ServerSocketFactory
> factory(
142 new UnixDomainServerSocketFactory(
143 base::StringPrintf(kSocketNameFormat
, getpid())));
144 devtools_http_handler_
.reset(new DevToolsHttpHandler(
146 base::StringPrintf(kFrontEndURL
, content::GetWebKitRevision().c_str()),
147 new AwDevToolsServerDelegate(),
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
,
175 delete reinterpret_cast<AwDevToolsServer
*>(server
);
178 static void SetRemoteDebuggingEnabled(JNIEnv
* env
,
179 const JavaParamRef
<jobject
>& obj
,
182 AwDevToolsServer
* devtools_server
=
183 reinterpret_cast<AwDevToolsServer
*>(server
);
185 devtools_server
->Start();
187 devtools_server
->Stop();
191 } // namespace android_webview