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/native/aw_contents.h"
9 #include "base/files/file_path.h"
10 #include "base/json/json_writer.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "content/public/browser/android/devtools_auth.h"
15 #include "content/public/browser/devtools_agent_host.h"
16 #include "content/public/browser/devtools_http_handler.h"
17 #include "content/public/browser/devtools_http_handler_delegate.h"
18 #include "content/public/browser/devtools_target.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
;
31 const char kFrontEndURL
[] =
32 "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/inspector.html";
33 const char kSocketNameFormat
[] = "webview_devtools_remote_%d";
34 const char kTetheringSocketName
[] = "webview_devtools_tethering_%d_%d";
36 const int kBackLog
= 10;
38 // Delegate implementation for the devtools http handler for WebView. A new
39 // instance of this gets created each time web debugging is enabled.
40 class AwDevToolsServerDelegate
: public content::DevToolsHttpHandlerDelegate
{
42 AwDevToolsServerDelegate() : last_tethering_socket_(0) {
45 virtual ~AwDevToolsServerDelegate() {}
47 // DevToolsHttpProtocolHandler::Delegate overrides.
48 std::string
GetDiscoveryPageHTML() override
;
50 bool BundlesFrontendResources() override
{
54 base::FilePath
GetDebugFrontendDir() override
{
55 return base::FilePath();
58 scoped_ptr
<net::ServerSocket
>
59 CreateSocketForTethering(std::string
* name
) override
{
60 *name
= base::StringPrintf(
61 kTetheringSocketName
, getpid(), ++last_tethering_socket_
);
62 scoped_ptr
<net::UnixDomainServerSocket
> socket(
63 new net::UnixDomainServerSocket(
64 base::Bind(&content::CanUserConnectToDevTools
), true));
65 if (socket
->ListenWithAddressAndPort(*name
, 0, kBackLog
) != net::OK
)
66 return scoped_ptr
<net::ServerSocket
>();
72 int last_tethering_socket_
;
74 DISALLOW_COPY_AND_ASSIGN(AwDevToolsServerDelegate
);
78 std::string
AwDevToolsServerDelegate::GetDiscoveryPageHTML() {
81 "<head><title>WebView remote debugging</title></head>"
82 "<body>Please use <a href=\'chrome://inspect\'>chrome://inspect</a>"
88 // Factory for UnixDomainServerSocket.
89 class UnixDomainServerSocketFactory
90 : public content::DevToolsHttpHandler::ServerSocketFactory
{
92 explicit UnixDomainServerSocketFactory(const std::string
& socket_name
)
93 : content::DevToolsHttpHandler::ServerSocketFactory(socket_name
, 0, 1) {}
96 // content::DevToolsHttpHandler::ServerSocketFactory.
97 virtual scoped_ptr
<net::ServerSocket
> Create() const override
{
98 return scoped_ptr
<net::ServerSocket
>(
99 new net::UnixDomainServerSocket(
100 base::Bind(&content::CanUserConnectToDevTools
),
101 true /* use_abstract_namespace */));
104 DISALLOW_COPY_AND_ASSIGN(UnixDomainServerSocketFactory
);
109 namespace android_webview
{
111 AwDevToolsServer::AwDevToolsServer() {
114 AwDevToolsServer::~AwDevToolsServer() {
118 void AwDevToolsServer::Start() {
119 if (protocol_handler_
)
122 scoped_ptr
<content::DevToolsHttpHandler::ServerSocketFactory
> factory(
123 new UnixDomainServerSocketFactory(
124 base::StringPrintf(kSocketNameFormat
, getpid())));
125 protocol_handler_
.reset(content::DevToolsHttpHandler::Start(
127 base::StringPrintf(kFrontEndURL
, content::GetWebKitRevision().c_str()),
128 new AwDevToolsServerDelegate(),
132 void AwDevToolsServer::Stop() {
133 protocol_handler_
.reset();
136 bool AwDevToolsServer::IsStarted() const {
137 return protocol_handler_
;
140 bool RegisterAwDevToolsServer(JNIEnv
* env
) {
141 return RegisterNativesImpl(env
);
144 static jlong
InitRemoteDebugging(JNIEnv
* env
,
146 AwDevToolsServer
* server
= new AwDevToolsServer();
147 return reinterpret_cast<intptr_t>(server
);
150 static void DestroyRemoteDebugging(JNIEnv
* env
, jobject obj
, jlong server
) {
151 delete reinterpret_cast<AwDevToolsServer
*>(server
);
154 static void SetRemoteDebuggingEnabled(JNIEnv
* env
,
158 AwDevToolsServer
* devtools_server
=
159 reinterpret_cast<AwDevToolsServer
*>(server
);
161 devtools_server
->Start();
163 devtools_server
->Stop();
167 } // namespace android_webview