1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Copyright 2013 The Chromium Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
6 #include "xwalk/runtime/browser/android/xwalk_dev_tools_server.h"
12 #include "base/android/jni_string.h"
13 #include "base/basictypes.h"
14 #include "base/bind.h"
15 #include "base/callback.h"
16 #include "base/compiler_specific.h"
17 #include "base/logging.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "content/public/browser/android/devtools_auth.h"
21 #include "content/public/browser/devtools_agent_host.h"
22 #include "content/public/browser/devtools_http_handler.h"
23 #include "content/public/browser/devtools_http_handler_delegate.h"
24 #include "content/public/browser/devtools_target.h"
25 #include "content/public/browser/favicon_status.h"
26 #include "content/public/browser/navigation_entry.h"
27 #include "content/public/browser/render_view_host.h"
28 #include "content/public/browser/web_contents.h"
29 #include "content/public/common/user_agent.h"
30 #include "grit/xwalk_resources.h"
31 #include "jni/XWalkDevToolsServer_jni.h"
32 #include "net/socket/unix_domain_socket_posix.h"
33 #include "ui/base/resource/resource_bundle.h"
35 using content::DevToolsAgentHost;
36 using content::RenderViewHost;
37 using content::WebContents;
41 // FIXME(girish): The frontend URL needs to be served from the domain below
42 // for remote debugging to work in chrome (see chrome's devtools_ui.cc).
43 // Currently, the chrome version is hardcoded because of this dependancy.
44 const char kFrontEndURL[] =
45 "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/devtools.html";
46 const char kTargetTypePage[] = "page";
48 class Target : public content::DevToolsTarget {
50 explicit Target(WebContents* web_contents);
52 virtual std::string GetId() const OVERRIDE { return id_; }
53 virtual std::string GetType() const OVERRIDE { return kTargetTypePage; }
54 virtual std::string GetTitle() const OVERRIDE { return title_; }
55 // TODO(hmin): Get the description about web contents view.
56 virtual std::string GetDescription() const OVERRIDE { return std::string(); }
57 virtual GURL GetUrl() const OVERRIDE { return url_; }
58 virtual GURL GetFaviconUrl() const OVERRIDE { return GURL(); }
59 virtual base::TimeTicks GetLastActivityTime() const OVERRIDE {
60 return last_activity_time_;
62 virtual bool IsAttached() const OVERRIDE {
63 return agent_host_->IsAttached();
65 virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const OVERRIDE {
68 virtual bool Activate() const OVERRIDE { return false; }
69 virtual bool Close() const OVERRIDE { return false; }
72 scoped_refptr<DevToolsAgentHost> agent_host_;
76 base::TimeTicks last_activity_time_;
79 Target::Target(WebContents* web_contents) {
81 DevToolsAgentHost::GetOrCreateFor(web_contents->GetRenderViewHost());
82 id_ = agent_host_->GetId();
83 title_ = UTF16ToUTF8(web_contents->GetTitle());
84 url_ = web_contents->GetURL();
85 last_activity_time_ = web_contents->GetLastActiveTime();
88 // Delegate implementation for the devtools http handler on android. A new
89 // instance of this gets created each time devtools is enabled.
90 class XWalkDevToolsServerDelegate
91 : public content::DevToolsHttpHandlerDelegate {
93 explicit XWalkDevToolsServerDelegate() {
96 virtual std::string GetDiscoveryPageHTML() OVERRIDE {
97 return ResourceBundle::GetSharedInstance().GetRawDataResource(
98 IDR_DEVTOOLS_FRONTEND_PAGE_HTML).as_string();
101 virtual bool BundlesFrontendResources() OVERRIDE {
105 virtual base::FilePath GetDebugFrontendDir() OVERRIDE {
106 return base::FilePath();
109 virtual std::string GetPageThumbnailData(const GURL& url) OVERRIDE {
110 return std::string();
113 virtual scoped_ptr<content::DevToolsTarget> CreateNewTarget(
114 const GURL&) OVERRIDE {
115 return scoped_ptr<content::DevToolsTarget>();
118 virtual scoped_ptr<net::StreamListenSocket> CreateSocketForTethering(
119 net::StreamListenSocket::Delegate* delegate,
120 std::string* name) OVERRIDE {
121 return scoped_ptr<net::StreamListenSocket>();
124 virtual void EnumerateTargets(TargetCallback callback) OVERRIDE {
126 std::vector<RenderViewHost*> rvh_list =
127 DevToolsAgentHost::GetValidRenderViewHosts();
128 for (std::vector<RenderViewHost*>::iterator it = rvh_list.begin();
129 it != rvh_list.end(); ++it) {
130 WebContents* web_contents = WebContents::FromRenderViewHost(*it);
132 targets.push_back(new Target(web_contents));
134 callback.Run(targets);
138 DISALLOW_COPY_AND_ASSIGN(XWalkDevToolsServerDelegate);
145 XWalkDevToolsServer::XWalkDevToolsServer(const std::string& socket_name)
146 : socket_name_(socket_name),
147 protocol_handler_(NULL),
151 XWalkDevToolsServer::~XWalkDevToolsServer() {
155 // Allow connection from uid specified using AllowConnectionFromUid to devtools
156 // server. This supports the XDK usage: debug bridge wrapper runs in a separate
157 // process and connects to the devtools server.
158 bool XWalkDevToolsServer::CanUserConnectToDevTools(uid_t uid, gid_t gid) {
159 if (uid == allowed_uid_)
162 return content::CanUserConnectToDevTools(uid, gid);
165 void XWalkDevToolsServer::Start() {
166 if (protocol_handler_)
169 protocol_handler_ = content::DevToolsHttpHandler::Start(
170 new net::UnixDomainSocketWithAbstractNamespaceFactory(
172 "", // fallback socket name
173 base::Bind(&XWalkDevToolsServer::CanUserConnectToDevTools,
174 base::Unretained(this))),
175 base::StringPrintf(kFrontEndURL, content::GetWebKitRevision().c_str()),
176 new XWalkDevToolsServerDelegate());
179 void XWalkDevToolsServer::Stop() {
180 if (!protocol_handler_)
182 // Note that the call to Stop() below takes care of |protocol_handler_|
184 protocol_handler_->Stop();
185 protocol_handler_ = NULL;
188 bool XWalkDevToolsServer::IsStarted() const {
189 return protocol_handler_;
192 void XWalkDevToolsServer::AllowConnectionFromUid(uid_t uid) {
196 bool RegisterXWalkDevToolsServer(JNIEnv* env) {
197 return RegisterNativesImpl(env);
200 static jlong InitRemoteDebugging(JNIEnv* env,
202 jstring socketName) {
203 XWalkDevToolsServer* server = new XWalkDevToolsServer(
204 base::android::ConvertJavaStringToUTF8(env, socketName));
205 return reinterpret_cast<intptr_t>(server);
208 static void DestroyRemoteDebugging(JNIEnv* env, jobject obj, jlong server) {
209 delete reinterpret_cast<XWalkDevToolsServer*>(server);
212 static jboolean IsRemoteDebuggingEnabled(JNIEnv* env,
215 return reinterpret_cast<XWalkDevToolsServer*>(server)->IsStarted();
218 static void SetRemoteDebuggingEnabled(JNIEnv* env,
222 XWalkDevToolsServer* devtools_server =
223 reinterpret_cast<XWalkDevToolsServer*>(server);
225 devtools_server->Start();
227 devtools_server->Stop();
231 static void AllowConnectionFromUid(JNIEnv* env,
235 XWalkDevToolsServer* devtools_server =
236 reinterpret_cast<XWalkDevToolsServer*>(server);
237 devtools_server->AllowConnectionFromUid((uid_t) uid);