Roll src/third_party/hunspell_dictionaries/ to c106afdce
[chromium-blink-merge.git] / android_webview / native / aw_dev_tools_server.cc
blob6b3e82a33859b966450af7cb8d0ea24e451b9dd7
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;
54 private:
56 DISALLOW_COPY_AND_ASSIGN(AwDevToolsServerDelegate);
60 std::string AwDevToolsServerDelegate::GetDiscoveryPageHTML() {
61 const char html[] =
62 "<html>"
63 "<head><title>WebView remote debugging</title></head>"
64 "<body>Please use <a href=\'chrome://inspect\'>chrome://inspect</a>"
65 "</body>"
66 "</html>";
67 return html;
70 std::string AwDevToolsServerDelegate::GetFrontendResource(
71 const std::string& path) {
72 return std::string();
75 std::string AwDevToolsServerDelegate::GetPageThumbnailData(const GURL&) {
76 return std::string();
79 // Factory for UnixDomainServerSocket.
80 class UnixDomainServerSocketFactory
81 : public DevToolsHttpHandler::ServerSocketFactory {
82 public:
83 explicit UnixDomainServerSocketFactory(const std::string& socket_name)
84 : socket_name_(socket_name),
85 last_tethering_socket_(0) {
88 private:
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>();
98 return socket;
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);
119 } // namespace
121 namespace android_webview {
123 AwDevToolsServer::AwDevToolsServer() {
126 AwDevToolsServer::~AwDevToolsServer() {
127 Stop();
130 void AwDevToolsServer::Start() {
131 if (devtools_http_handler_)
132 return;
134 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory(
135 new UnixDomainServerSocketFactory(
136 base::StringPrintf(kSocketNameFormat, getpid())));
137 devtools_http_handler_.reset(new DevToolsHttpHandler(
138 factory.Pass(),
139 base::StringPrintf(kFrontEndURL, content::GetWebKitRevision().c_str()),
140 new AwDevToolsServerDelegate(),
141 base::FilePath(),
142 base::FilePath(),
143 GetProduct(),
144 GetUserAgent()));
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 jobject obj) {
161 AwDevToolsServer* server = new AwDevToolsServer();
162 return reinterpret_cast<intptr_t>(server);
165 static void DestroyRemoteDebugging(JNIEnv* env, jobject obj, jlong server) {
166 delete reinterpret_cast<AwDevToolsServer*>(server);
169 static void SetRemoteDebuggingEnabled(JNIEnv* env,
170 jobject obj,
171 jlong server,
172 jboolean enabled) {
173 AwDevToolsServer* devtools_server =
174 reinterpret_cast<AwDevToolsServer*>(server);
175 if (enabled) {
176 devtools_server->Start();
177 } else {
178 devtools_server->Stop();
182 } // namespace android_webview