IndexedDB: Protect against use-after-free in ChainedBlobWriter.
[chromium-blink-merge.git] / android_webview / native / aw_dev_tools_server.cc
blob1e11f28139825e9153a2e3241f8301e3a8e36aef
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/browser/aw_dev_tools_manager_delegate.h"
8 #include "android_webview/common/aw_content_client.h"
9 #include "android_webview/native/aw_contents.h"
10 #include "base/bind.h"
11 #include "base/files/file_path.h"
12 #include "base/json/json_writer.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/values.h"
16 #include "components/devtools_http_handler/devtools_http_handler.h"
17 #include "components/devtools_http_handler/devtools_http_handler_delegate.h"
18 #include "content/public/browser/android/devtools_auth.h"
19 #include "content/public/browser/devtools_agent_host.h"
20 #include "content/public/browser/devtools_target.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/common/user_agent.h"
23 #include "jni/AwDevToolsServer_jni.h"
24 #include "net/base/net_errors.h"
25 #include "net/socket/unix_domain_server_socket_posix.h"
27 using content::DevToolsAgentHost;
28 using content::RenderViewHost;
29 using content::WebContents;
30 using devtools_http_handler::DevToolsHttpHandler;
32 namespace {
34 const char kFrontEndURL[] =
35 "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/inspector.html";
36 const char kSocketNameFormat[] = "webview_devtools_remote_%d";
37 const char kTetheringSocketName[] = "webview_devtools_tethering_%d_%d";
39 const int kBackLog = 10;
41 // Delegate implementation for the devtools http handler for WebView. A new
42 // instance of this gets created each time web debugging is enabled.
43 class AwDevToolsServerDelegate :
44 public devtools_http_handler::DevToolsHttpHandlerDelegate {
45 public:
46 AwDevToolsServerDelegate() {
49 ~AwDevToolsServerDelegate() override {}
51 // devtools_http_handler::DevToolsHttpHandlerDelegate implementation.
52 std::string GetDiscoveryPageHTML() override;
53 std::string GetFrontendResource(const std::string& path) override;
55 private:
57 DISALLOW_COPY_AND_ASSIGN(AwDevToolsServerDelegate);
61 std::string AwDevToolsServerDelegate::GetDiscoveryPageHTML() {
62 const char html[] =
63 "<html>"
64 "<head><title>WebView remote debugging</title></head>"
65 "<body>Please use <a href=\'chrome://inspect\'>chrome://inspect</a>"
66 "</body>"
67 "</html>";
68 return html;
71 std::string AwDevToolsServerDelegate::GetFrontendResource(
72 const std::string& path) {
73 return std::string();
76 // Factory for UnixDomainServerSocket.
77 class UnixDomainServerSocketFactory
78 : public DevToolsHttpHandler::ServerSocketFactory {
79 public:
80 explicit UnixDomainServerSocketFactory(const std::string& socket_name)
81 : socket_name_(socket_name),
82 last_tethering_socket_(0) {
85 private:
86 // devtools_http_handler::DevToolsHttpHandler::ServerSocketFactory.
87 scoped_ptr<net::ServerSocket> CreateForHttpServer() override {
88 scoped_ptr<net::ServerSocket> socket(
89 new net::UnixDomainServerSocket(
90 base::Bind(&content::CanUserConnectToDevTools),
91 true /* use_abstract_namespace */));
92 if (socket->ListenWithAddressAndPort(socket_name_, 0, kBackLog) != net::OK)
93 return scoped_ptr<net::ServerSocket>();
95 return socket;
98 scoped_ptr<net::ServerSocket> CreateForTethering(std::string* name) override {
99 *name = base::StringPrintf(
100 kTetheringSocketName, getpid(), ++last_tethering_socket_);
101 scoped_ptr<net::UnixDomainServerSocket> socket(
102 new net::UnixDomainServerSocket(
103 base::Bind(&content::CanUserConnectToDevTools), true));
104 if (socket->ListenWithAddressAndPort(*name, 0, kBackLog) != net::OK)
105 return scoped_ptr<net::ServerSocket>();
107 return socket.Pass();
110 std::string socket_name_;
111 int last_tethering_socket_;
113 DISALLOW_COPY_AND_ASSIGN(UnixDomainServerSocketFactory);
116 } // namespace
118 namespace android_webview {
120 AwDevToolsServer::AwDevToolsServer() {
123 AwDevToolsServer::~AwDevToolsServer() {
124 Stop();
127 void AwDevToolsServer::Start() {
128 if (devtools_http_handler_)
129 return;
131 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory(
132 new UnixDomainServerSocketFactory(
133 base::StringPrintf(kSocketNameFormat, getpid())));
134 manager_delegate_.reset(new AwDevToolsManagerDelegate());
135 devtools_http_handler_.reset(new DevToolsHttpHandler(
136 factory.Pass(),
137 base::StringPrintf(kFrontEndURL, content::GetWebKitRevision().c_str()),
138 new AwDevToolsServerDelegate(),
139 manager_delegate_.get(),
140 base::FilePath(),
141 base::FilePath(),
142 GetProduct(),
143 GetUserAgent()));
146 void AwDevToolsServer::Stop() {
147 devtools_http_handler_.reset();
148 manager_delegate_.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