Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / android_webview / native / aw_dev_tools_server.cc
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/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 "content/public/common/user_agent.h"
20 #include "jni/AwDevToolsServer_jni.h"
21 #include "net/socket/unix_domain_socket_posix.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_ = base::UTF16ToUTF8(web_contents->GetTitle());
74   url_ = web_contents->GetURL();
75   last_activity_time_ = web_contents->GetLastActiveTime();
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   const android_webview::BrowserViewRenderer* bvr =
141       android_webview::AwContents::FromWebContents(web_contents)
142           ->GetBrowserViewRenderer();
143   if (!bvr) return "";
144   base::DictionaryValue description;
145   description.SetBoolean("attached", bvr->IsAttachedToWindow());
146   description.SetBoolean("visible", bvr->IsVisible());
147   gfx::Rect screen_rect = bvr->GetScreenRect();
148   description.SetInteger("screenX", screen_rect.x());
149   description.SetInteger("screenY", screen_rect.y());
150   description.SetBoolean("empty", screen_rect.size().IsEmpty());
151   if (!screen_rect.size().IsEmpty()) {
152     description.SetInteger("width", screen_rect.width());
153     description.SetInteger("height", screen_rect.height());
154   }
155   std::string json;
156   base::JSONWriter::Write(&description, &json);
157   return json;
158 }
159
160 }  // namespace
161
162 namespace android_webview {
163
164 AwDevToolsServer::AwDevToolsServer()
165     : protocol_handler_(NULL) {
166 }
167
168 AwDevToolsServer::~AwDevToolsServer() {
169   Stop();
170 }
171
172 void AwDevToolsServer::Start() {
173   if (protocol_handler_)
174     return;
175
176   protocol_handler_ = content::DevToolsHttpHandler::Start(
177       new net::UnixDomainSocketWithAbstractNamespaceFactory(
178           base::StringPrintf(kSocketNameFormat, getpid()),
179           "",
180           base::Bind(&content::CanUserConnectToDevTools)),
181       base::StringPrintf(kFrontEndURL, content::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 jlong InitRemoteDebugging(JNIEnv* env,
203                                 jobject obj) {
204   AwDevToolsServer* server = new AwDevToolsServer();
205   return reinterpret_cast<intptr_t>(server);
206 }
207
208 static void DestroyRemoteDebugging(JNIEnv* env, jobject obj, jlong server) {
209   delete reinterpret_cast<AwDevToolsServer*>(server);
210 }
211
212 static void SetRemoteDebuggingEnabled(JNIEnv* env,
213                                       jobject obj,
214                                       jlong 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