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