Port Android relocation packer to chromium build
[chromium-blink-merge.git] / android_webview / native / aw_dev_tools_server.cc
blob52d474ed0ca7f9da4549d4bb9a75d70a9d229e69
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"
8 #include "base/bind.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;
29 namespace {
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 {
41 public:
42 AwDevToolsServerDelegate() {
45 ~AwDevToolsServerDelegate() override {}
47 // DevToolsHttpProtocolHandler::Delegate overrides.
48 std::string GetDiscoveryPageHTML() override;
50 bool BundlesFrontendResources() override {
51 return false;
54 base::FilePath GetDebugFrontendDir() override {
55 return base::FilePath();
57 private:
59 DISALLOW_COPY_AND_ASSIGN(AwDevToolsServerDelegate);
63 std::string AwDevToolsServerDelegate::GetDiscoveryPageHTML() {
64 const char html[] =
65 "<html>"
66 "<head><title>WebView remote debugging</title></head>"
67 "<body>Please use <a href=\'chrome://inspect\'>chrome://inspect</a>"
68 "</body>"
69 "</html>";
70 return html;
73 // Factory for UnixDomainServerSocket.
74 class UnixDomainServerSocketFactory
75 : public content::DevToolsHttpHandler::ServerSocketFactory {
76 public:
77 explicit UnixDomainServerSocketFactory(const std::string& socket_name)
78 : socket_name_(socket_name),
79 last_tethering_socket_(0) {
82 private:
83 // content::DevToolsHttpHandler::ServerSocketFactory.
84 scoped_ptr<net::ServerSocket> CreateForHttpServer() override {
85 scoped_ptr<net::ServerSocket> socket(
86 new net::UnixDomainServerSocket(
87 base::Bind(&content::CanUserConnectToDevTools),
88 true /* use_abstract_namespace */));
89 if (socket->ListenWithAddressAndPort(socket_name_, 0, kBackLog) != net::OK)
90 return scoped_ptr<net::ServerSocket>();
92 return socket;
95 scoped_ptr<net::ServerSocket> CreateForTethering(std::string* name) override {
96 *name = base::StringPrintf(
97 kTetheringSocketName, getpid(), ++last_tethering_socket_);
98 scoped_ptr<net::UnixDomainServerSocket> socket(
99 new net::UnixDomainServerSocket(
100 base::Bind(&content::CanUserConnectToDevTools), true));
101 if (socket->ListenWithAddressAndPort(*name, 0, kBackLog) != net::OK)
102 return scoped_ptr<net::ServerSocket>();
104 return socket.Pass();
107 std::string socket_name_;
108 int last_tethering_socket_;
110 DISALLOW_COPY_AND_ASSIGN(UnixDomainServerSocketFactory);
113 } // namespace
115 namespace android_webview {
117 AwDevToolsServer::AwDevToolsServer() {
120 AwDevToolsServer::~AwDevToolsServer() {
121 Stop();
124 void AwDevToolsServer::Start() {
125 if (protocol_handler_)
126 return;
128 scoped_ptr<content::DevToolsHttpHandler::ServerSocketFactory> factory(
129 new UnixDomainServerSocketFactory(
130 base::StringPrintf(kSocketNameFormat, getpid())));
131 protocol_handler_.reset(content::DevToolsHttpHandler::Start(
132 factory.Pass(),
133 base::StringPrintf(kFrontEndURL, content::GetWebKitRevision().c_str()),
134 new AwDevToolsServerDelegate(),
135 base::FilePath()));
138 void AwDevToolsServer::Stop() {
139 protocol_handler_.reset();
142 bool AwDevToolsServer::IsStarted() const {
143 return protocol_handler_;
146 bool RegisterAwDevToolsServer(JNIEnv* env) {
147 return RegisterNativesImpl(env);
150 static jlong InitRemoteDebugging(JNIEnv* env,
151 jobject obj) {
152 AwDevToolsServer* server = new AwDevToolsServer();
153 return reinterpret_cast<intptr_t>(server);
156 static void DestroyRemoteDebugging(JNIEnv* env, jobject obj, jlong server) {
157 delete reinterpret_cast<AwDevToolsServer*>(server);
160 static void SetRemoteDebuggingEnabled(JNIEnv* env,
161 jobject obj,
162 jlong server,
163 jboolean enabled) {
164 AwDevToolsServer* devtools_server =
165 reinterpret_cast<AwDevToolsServer*>(server);
166 if (enabled) {
167 devtools_server->Start();
168 } else {
169 devtools_server->Stop();
173 } // namespace android_webview