Battery Status API: add UMA logging for Linux.
[chromium-blink-merge.git] / android_webview / native / aw_dev_tools_server.cc
blob69046d2883179a74a8f959ac7801399b642244a7
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/socket/unix_domain_server_socket_posix.h"
24 using content::DevToolsAgentHost;
25 using content::RenderViewHost;
26 using content::WebContents;
28 namespace {
30 const char kFrontEndURL[] =
31 "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/devtools.html";
32 const char kSocketNameFormat[] = "webview_devtools_remote_%d";
34 const char kTargetTypePage[] = "page";
35 const char kTargetTypeServiceWorker[] = "service_worker";
36 const char kTargetTypeOther[] = "other";
38 std::string GetViewDescription(WebContents* web_contents);
40 class Target : public content::DevToolsTarget {
41 public:
42 explicit Target(scoped_refptr<DevToolsAgentHost> agent_host);
44 virtual std::string GetId() const OVERRIDE { return agent_host_->GetId(); }
45 virtual std::string GetParentId() const OVERRIDE { return std::string(); }
46 virtual std::string GetType() const OVERRIDE {
47 switch (agent_host_->GetType()) {
48 case DevToolsAgentHost::TYPE_WEB_CONTENTS:
49 return kTargetTypePage;
50 case DevToolsAgentHost::TYPE_SERVICE_WORKER:
51 return kTargetTypeServiceWorker;
52 default:
53 break;
55 return kTargetTypeOther;
57 virtual std::string GetTitle() const OVERRIDE {
58 return agent_host_->GetTitle();
60 virtual std::string GetDescription() const OVERRIDE { return description_; }
61 virtual GURL GetURL() const OVERRIDE { return agent_host_->GetURL(); }
62 virtual GURL GetFaviconURL() const OVERRIDE { return GURL(); }
63 virtual base::TimeTicks GetLastActivityTime() const OVERRIDE {
64 return last_activity_time_;
66 virtual bool IsAttached() const OVERRIDE {
67 return agent_host_->IsAttached();
69 virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const OVERRIDE {
70 return agent_host_;
72 virtual bool Activate() const OVERRIDE { return agent_host_->Activate(); }
73 virtual bool Close() const OVERRIDE { return agent_host_->Close(); }
75 private:
76 scoped_refptr<DevToolsAgentHost> agent_host_;
77 std::string description_;
78 base::TimeTicks last_activity_time_;
81 Target::Target(scoped_refptr<DevToolsAgentHost> agent_host)
82 : agent_host_(agent_host) {
83 if (WebContents* web_contents = agent_host->GetWebContents()) {
84 description_ = GetViewDescription(web_contents);
85 last_activity_time_ = web_contents->GetLastActiveTime();
89 // Delegate implementation for the devtools http handler for WebView. A new
90 // instance of this gets created each time web debugging is enabled.
91 class AwDevToolsServerDelegate : public content::DevToolsHttpHandlerDelegate {
92 public:
93 AwDevToolsServerDelegate() {}
94 virtual ~AwDevToolsServerDelegate() {}
96 // DevToolsHttpProtocolHandler::Delegate overrides.
97 virtual std::string GetDiscoveryPageHTML() OVERRIDE;
99 virtual bool BundlesFrontendResources() OVERRIDE {
100 return false;
103 virtual base::FilePath GetDebugFrontendDir() OVERRIDE {
104 return base::FilePath();
107 virtual std::string GetPageThumbnailData(const GURL&) OVERRIDE {
108 return "";
111 virtual scoped_ptr<content::DevToolsTarget> CreateNewTarget(
112 const GURL&) OVERRIDE {
113 return scoped_ptr<content::DevToolsTarget>();
116 virtual void EnumerateTargets(TargetCallback callback) OVERRIDE {
117 TargetList targets;
118 DevToolsAgentHost::List agents = DevToolsAgentHost::GetOrCreateAll();
119 for (DevToolsAgentHost::List::iterator it = agents.begin();
120 it != agents.end(); ++it) {
121 targets.push_back(new Target(*it));
123 callback.Run(targets);
126 virtual scoped_ptr<net::StreamListenSocket> CreateSocketForTethering(
127 net::StreamListenSocket::Delegate* delegate,
128 std::string* name) OVERRIDE {
129 return scoped_ptr<net::StreamListenSocket>();
132 private:
133 DISALLOW_COPY_AND_ASSIGN(AwDevToolsServerDelegate);
137 std::string AwDevToolsServerDelegate::GetDiscoveryPageHTML() {
138 const char html[] =
139 "<html>"
140 "<head><title>WebView remote debugging</title></head>"
141 "<body>Please use <a href=\'chrome://inspect\'>chrome://inspect</a>"
142 "</body>"
143 "</html>";
144 return html;
147 std::string GetViewDescription(WebContents* web_contents) {
148 const android_webview::BrowserViewRenderer* bvr =
149 android_webview::AwContents::FromWebContents(web_contents)
150 ->GetBrowserViewRenderer();
151 if (!bvr) return "";
152 base::DictionaryValue description;
153 description.SetBoolean("attached", bvr->attached_to_window());
154 description.SetBoolean("visible", bvr->IsVisible());
155 gfx::Rect screen_rect = bvr->GetScreenRect();
156 description.SetInteger("screenX", screen_rect.x());
157 description.SetInteger("screenY", screen_rect.y());
158 description.SetBoolean("empty", screen_rect.size().IsEmpty());
159 if (!screen_rect.size().IsEmpty()) {
160 description.SetInteger("width", screen_rect.width());
161 description.SetInteger("height", screen_rect.height());
163 std::string json;
164 base::JSONWriter::Write(&description, &json);
165 return json;
168 // Factory for UnixDomainServerSocket.
169 class UnixDomainServerSocketFactory
170 : public content::DevToolsHttpHandler::ServerSocketFactory {
171 public:
172 explicit UnixDomainServerSocketFactory(const std::string& socket_name)
173 : content::DevToolsHttpHandler::ServerSocketFactory(socket_name, 0, 1) {}
175 private:
176 // content::DevToolsHttpHandler::ServerSocketFactory.
177 virtual scoped_ptr<net::ServerSocket> Create() const OVERRIDE {
178 return scoped_ptr<net::ServerSocket>(
179 new net::UnixDomainServerSocket(
180 base::Bind(&content::CanUserConnectToDevTools),
181 true /* use_abstract_namespace */));
184 DISALLOW_COPY_AND_ASSIGN(UnixDomainServerSocketFactory);
187 } // namespace
189 namespace android_webview {
191 AwDevToolsServer::AwDevToolsServer()
192 : protocol_handler_(NULL) {
195 AwDevToolsServer::~AwDevToolsServer() {
196 Stop();
199 void AwDevToolsServer::Start() {
200 if (protocol_handler_)
201 return;
203 scoped_ptr<content::DevToolsHttpHandler::ServerSocketFactory> factory(
204 new UnixDomainServerSocketFactory(
205 base::StringPrintf(kSocketNameFormat, getpid())));
206 protocol_handler_ = content::DevToolsHttpHandler::Start(
207 factory.Pass(),
208 base::StringPrintf(kFrontEndURL, content::GetWebKitRevision().c_str()),
209 new AwDevToolsServerDelegate(),
210 base::FilePath());
213 void AwDevToolsServer::Stop() {
214 if (!protocol_handler_)
215 return;
216 // Note that the call to Stop() below takes care of |protocol_handler_|
217 // deletion.
218 protocol_handler_->Stop();
219 protocol_handler_ = NULL;
222 bool AwDevToolsServer::IsStarted() const {
223 return protocol_handler_;
226 bool RegisterAwDevToolsServer(JNIEnv* env) {
227 return RegisterNativesImpl(env);
230 static jlong InitRemoteDebugging(JNIEnv* env,
231 jobject obj) {
232 AwDevToolsServer* server = new AwDevToolsServer();
233 return reinterpret_cast<intptr_t>(server);
236 static void DestroyRemoteDebugging(JNIEnv* env, jobject obj, jlong server) {
237 delete reinterpret_cast<AwDevToolsServer*>(server);
240 static void SetRemoteDebuggingEnabled(JNIEnv* env,
241 jobject obj,
242 jlong server,
243 jboolean enabled) {
244 AwDevToolsServer* devtools_server =
245 reinterpret_cast<AwDevToolsServer*>(server);
246 if (enabled) {
247 devtools_server->Start();
248 } else {
249 devtools_server->Stop();
253 } // namespace android_webview