• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "android_webview/native/aw_dev_tools_server.h"
6 
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_socket_posix.h"
23 
24 using content::DevToolsAgentHost;
25 using content::RenderViewHost;
26 using content::WebContents;
27 
28 namespace {
29 
30 const char kFrontEndURL[] =
31     "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/devtools.html";
32 const char kSocketNameFormat[] = "webview_devtools_remote_%d";
33 
34 const char kTargetTypePage[] = "page";
35 
36 std::string GetViewDescription(WebContents* web_contents);
37 
38 class Target : public content::DevToolsTarget {
39  public:
40   explicit Target(WebContents* web_contents);
41 
GetId() const42   virtual std::string GetId() const OVERRIDE { return id_; }
GetParentId() const43   virtual std::string GetParentId() const OVERRIDE { return std::string(); }
GetType() const44   virtual std::string GetType() const OVERRIDE { return kTargetTypePage; }
GetTitle() const45   virtual std::string GetTitle() const OVERRIDE { return title_; }
GetDescription() const46   virtual std::string GetDescription() const OVERRIDE { return description_; }
GetURL() const47   virtual GURL GetURL() const OVERRIDE { return url_; }
GetFaviconURL() const48   virtual GURL GetFaviconURL() const OVERRIDE { return GURL(); }
GetLastActivityTime() const49   virtual base::TimeTicks GetLastActivityTime() const OVERRIDE {
50     return last_activity_time_;
51   }
IsAttached() const52   virtual bool IsAttached() const OVERRIDE {
53     return agent_host_->IsAttached();
54   }
GetAgentHost() const55   virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const OVERRIDE {
56     return agent_host_;
57   }
Activate() const58   virtual bool Activate() const OVERRIDE { return false; }
Close() const59   virtual bool Close() const OVERRIDE { return false; }
60 
61  private:
62   scoped_refptr<DevToolsAgentHost> agent_host_;
63   std::string id_;
64   std::string title_;
65   std::string description_;
66   GURL url_;
67   base::TimeTicks last_activity_time_;
68 };
69 
Target(WebContents * web_contents)70 Target::Target(WebContents* web_contents) {
71   agent_host_ =
72       DevToolsAgentHost::GetOrCreateFor(web_contents->GetRenderViewHost());
73   id_ = agent_host_->GetId();
74   description_ = GetViewDescription(web_contents);
75   title_ = base::UTF16ToUTF8(web_contents->GetTitle());
76   url_ = web_contents->GetURL();
77   last_activity_time_ = web_contents->GetLastActiveTime();
78 }
79 
80 // Delegate implementation for the devtools http handler for WebView. A new
81 // instance of this gets created each time web debugging is enabled.
82 class AwDevToolsServerDelegate : public content::DevToolsHttpHandlerDelegate {
83  public:
AwDevToolsServerDelegate()84   AwDevToolsServerDelegate() {}
~AwDevToolsServerDelegate()85   virtual ~AwDevToolsServerDelegate() {}
86 
87   // DevToolsHttpProtocolHandler::Delegate overrides.
88   virtual std::string GetDiscoveryPageHTML() OVERRIDE;
89 
BundlesFrontendResources()90   virtual bool BundlesFrontendResources() OVERRIDE {
91     return false;
92   }
93 
GetDebugFrontendDir()94   virtual base::FilePath GetDebugFrontendDir() OVERRIDE {
95     return base::FilePath();
96   }
97 
GetPageThumbnailData(const GURL &)98   virtual std::string GetPageThumbnailData(const GURL&) OVERRIDE {
99     return "";
100   }
101 
CreateNewTarget(const GURL &)102   virtual scoped_ptr<content::DevToolsTarget> CreateNewTarget(
103       const GURL&) OVERRIDE {
104     return scoped_ptr<content::DevToolsTarget>();
105   }
106 
EnumerateTargets(TargetCallback callback)107   virtual void EnumerateTargets(TargetCallback callback) OVERRIDE {
108     TargetList targets;
109     std::vector<RenderViewHost*> rvh_list =
110         DevToolsAgentHost::GetValidRenderViewHosts();
111     for (std::vector<RenderViewHost*>::iterator it = rvh_list.begin();
112          it != rvh_list.end(); ++it) {
113       WebContents* web_contents = WebContents::FromRenderViewHost(*it);
114       if (web_contents)
115         targets.push_back(new Target(web_contents));
116     }
117     callback.Run(targets);
118   }
119 
CreateSocketForTethering(net::StreamListenSocket::Delegate * delegate,std::string * name)120   virtual scoped_ptr<net::StreamListenSocket> CreateSocketForTethering(
121       net::StreamListenSocket::Delegate* delegate,
122       std::string* name) OVERRIDE {
123     return scoped_ptr<net::StreamListenSocket>();
124   }
125 
126  private:
127   DISALLOW_COPY_AND_ASSIGN(AwDevToolsServerDelegate);
128 };
129 
130 
GetDiscoveryPageHTML()131 std::string AwDevToolsServerDelegate::GetDiscoveryPageHTML() {
132   const char html[] =
133       "<html>"
134       "<head><title>WebView remote debugging</title></head>"
135       "<body>Please use <a href=\'chrome://inspect\'>chrome://inspect</a>"
136       "</body>"
137       "</html>";
138   return html;
139 }
140 
GetViewDescription(WebContents * web_contents)141 std::string GetViewDescription(WebContents* web_contents) {
142   const android_webview::BrowserViewRenderer* bvr =
143       android_webview::AwContents::FromWebContents(web_contents)
144           ->GetBrowserViewRenderer();
145   if (!bvr) return "";
146   base::DictionaryValue description;
147   description.SetBoolean("attached", bvr->attached_to_window());
148   description.SetBoolean("visible", bvr->IsVisible());
149   gfx::Rect screen_rect = bvr->GetScreenRect();
150   description.SetInteger("screenX", screen_rect.x());
151   description.SetInteger("screenY", screen_rect.y());
152   description.SetBoolean("empty", screen_rect.size().IsEmpty());
153   if (!screen_rect.size().IsEmpty()) {
154     description.SetInteger("width", screen_rect.width());
155     description.SetInteger("height", screen_rect.height());
156   }
157   std::string json;
158   base::JSONWriter::Write(&description, &json);
159   return json;
160 }
161 
162 }  // namespace
163 
164 namespace android_webview {
165 
AwDevToolsServer()166 AwDevToolsServer::AwDevToolsServer()
167     : protocol_handler_(NULL) {
168 }
169 
~AwDevToolsServer()170 AwDevToolsServer::~AwDevToolsServer() {
171   Stop();
172 }
173 
Start()174 void AwDevToolsServer::Start() {
175   if (protocol_handler_)
176     return;
177 
178   protocol_handler_ = content::DevToolsHttpHandler::Start(
179       new net::UnixDomainSocketWithAbstractNamespaceFactory(
180           base::StringPrintf(kSocketNameFormat, getpid()),
181           "",
182           base::Bind(&content::CanUserConnectToDevTools)),
183       base::StringPrintf(kFrontEndURL, content::GetWebKitRevision().c_str()),
184       new AwDevToolsServerDelegate(),
185       base::FilePath());
186 }
187 
Stop()188 void AwDevToolsServer::Stop() {
189   if (!protocol_handler_)
190     return;
191   // Note that the call to Stop() below takes care of |protocol_handler_|
192   // deletion.
193   protocol_handler_->Stop();
194   protocol_handler_ = NULL;
195 }
196 
IsStarted() const197 bool AwDevToolsServer::IsStarted() const {
198   return protocol_handler_;
199 }
200 
RegisterAwDevToolsServer(JNIEnv * env)201 bool RegisterAwDevToolsServer(JNIEnv* env) {
202   return RegisterNativesImpl(env);
203 }
204 
InitRemoteDebugging(JNIEnv * env,jobject obj)205 static jlong InitRemoteDebugging(JNIEnv* env,
206                                 jobject obj) {
207   AwDevToolsServer* server = new AwDevToolsServer();
208   return reinterpret_cast<intptr_t>(server);
209 }
210 
DestroyRemoteDebugging(JNIEnv * env,jobject obj,jlong server)211 static void DestroyRemoteDebugging(JNIEnv* env, jobject obj, jlong server) {
212   delete reinterpret_cast<AwDevToolsServer*>(server);
213 }
214 
SetRemoteDebuggingEnabled(JNIEnv * env,jobject obj,jlong server,jboolean enabled)215 static void SetRemoteDebuggingEnabled(JNIEnv* env,
216                                       jobject obj,
217                                       jlong server,
218                                       jboolean enabled) {
219   AwDevToolsServer* devtools_server =
220       reinterpret_cast<AwDevToolsServer*>(server);
221   if (enabled) {
222     devtools_server->Start();
223   } else {
224     devtools_server->Stop();
225   }
226 }
227 
228 }  // namespace android_webview
229