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
;
56 DISALLOW_COPY_AND_ASSIGN(AwDevToolsServerDelegate
);
60 std::string
AwDevToolsServerDelegate::GetDiscoveryPageHTML() {
63 "<head><title>WebView remote debugging</title></head>"
64 "<body>Please use <a href=\'chrome://inspect\'>chrome://inspect</a>"
70 std::string
AwDevToolsServerDelegate::GetFrontendResource(
71 const std::string
& path
) {
75 std::string
AwDevToolsServerDelegate::GetPageThumbnailData(const GURL
&) {
79 // Factory for UnixDomainServerSocket.
80 class UnixDomainServerSocketFactory
81 : public DevToolsHttpHandler::ServerSocketFactory
{
83 explicit UnixDomainServerSocketFactory(const std::string
& socket_name
)
84 : socket_name_(socket_name
),
85 last_tethering_socket_(0) {
89 // devtools_http_handler::DevToolsHttpHandler::ServerSocketFactory.
90 scoped_ptr
<net::ServerSocket
> CreateForHttpServer() override
{
91 scoped_ptr
<net::ServerSocket
> socket(
92 new net::UnixDomainServerSocket(
93 base::Bind(&content::CanUserConnectToDevTools
),
94 true /* use_abstract_namespace */));
95 if (socket
->ListenWithAddressAndPort(socket_name_
, 0, kBackLog
) != net::OK
)
96 return scoped_ptr
<net::ServerSocket
>();
101 scoped_ptr
<net::ServerSocket
> CreateForTethering(std::string
* name
) override
{
102 *name
= base::StringPrintf(
103 kTetheringSocketName
, getpid(), ++last_tethering_socket_
);
104 scoped_ptr
<net::UnixDomainServerSocket
> socket(
105 new net::UnixDomainServerSocket(
106 base::Bind(&content::CanUserConnectToDevTools
), true));
107 if (socket
->ListenWithAddressAndPort(*name
, 0, kBackLog
) != net::OK
)
108 return scoped_ptr
<net::ServerSocket
>();
110 return socket
.Pass();
113 std::string socket_name_
;
114 int last_tethering_socket_
;
116 DISALLOW_COPY_AND_ASSIGN(UnixDomainServerSocketFactory
);
121 namespace android_webview
{
123 AwDevToolsServer::AwDevToolsServer() {
126 AwDevToolsServer::~AwDevToolsServer() {
130 void AwDevToolsServer::Start() {
131 if (devtools_http_handler_
)
134 scoped_ptr
<DevToolsHttpHandler::ServerSocketFactory
> factory(
135 new UnixDomainServerSocketFactory(
136 base::StringPrintf(kSocketNameFormat
, getpid())));
137 devtools_http_handler_
.reset(new DevToolsHttpHandler(
139 base::StringPrintf(kFrontEndURL
, content::GetWebKitRevision().c_str()),
140 new AwDevToolsServerDelegate(),
147 void AwDevToolsServer::Stop() {
148 devtools_http_handler_
.reset();
151 bool AwDevToolsServer::IsStarted() const {
152 return devtools_http_handler_
;
155 bool RegisterAwDevToolsServer(JNIEnv
* env
) {
156 return RegisterNativesImpl(env
);
159 static jlong
InitRemoteDebugging(JNIEnv
* env
,
160 const JavaParamRef
<jobject
>& obj
) {
161 AwDevToolsServer
* server
= new AwDevToolsServer();
162 return reinterpret_cast<intptr_t>(server
);
165 static void DestroyRemoteDebugging(JNIEnv
* env
,
166 const JavaParamRef
<jobject
>& obj
,
168 delete reinterpret_cast<AwDevToolsServer
*>(server
);
171 static void SetRemoteDebuggingEnabled(JNIEnv
* env
,
172 const JavaParamRef
<jobject
>& obj
,
175 AwDevToolsServer
* devtools_server
=
176 reinterpret_cast<AwDevToolsServer
*>(server
);
178 devtools_server
->Start();
180 devtools_server
->Stop();
184 } // namespace android_webview