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"
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"
24 using content::DevToolsAgentHost
;
25 using content::RenderViewHost
;
26 using content::WebContents
;
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";
36 std::string
GetViewDescription(WebContents
* web_contents
);
38 class Target
: public content::DevToolsTarget
{
40 explicit Target(WebContents
* web_contents
);
42 virtual std::string
GetId() const OVERRIDE
{ return id_
; }
43 virtual std::string
GetParentId() const OVERRIDE
{ return std::string(); }
44 virtual std::string
GetType() const OVERRIDE
{ return kTargetTypePage
; }
45 virtual std::string
GetTitle() const OVERRIDE
{ return title_
; }
46 virtual std::string
GetDescription() const OVERRIDE
{ return description_
; }
47 virtual GURL
GetURL() const OVERRIDE
{ return url_
; }
48 virtual GURL
GetFaviconURL() const OVERRIDE
{ return GURL(); }
49 virtual base::TimeTicks
GetLastActivityTime() const OVERRIDE
{
50 return last_activity_time_
;
52 virtual bool IsAttached() const OVERRIDE
{
53 return agent_host_
->IsAttached();
55 virtual scoped_refptr
<DevToolsAgentHost
> GetAgentHost() const OVERRIDE
{
58 virtual bool Activate() const OVERRIDE
{ return false; }
59 virtual bool Close() const OVERRIDE
{ return false; }
62 scoped_refptr
<DevToolsAgentHost
> agent_host_
;
65 std::string description_
;
67 base::TimeTicks last_activity_time_
;
70 Target::Target(WebContents
* web_contents
) {
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();
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
{
84 AwDevToolsServerDelegate() {}
85 virtual ~AwDevToolsServerDelegate() {}
87 // DevToolsHttpProtocolHandler::Delegate overrides.
88 virtual std::string
GetDiscoveryPageHTML() OVERRIDE
;
90 virtual bool BundlesFrontendResources() OVERRIDE
{
94 virtual base::FilePath
GetDebugFrontendDir() OVERRIDE
{
95 return base::FilePath();
98 virtual std::string
GetPageThumbnailData(const GURL
&) OVERRIDE
{
102 virtual scoped_ptr
<content::DevToolsTarget
> CreateNewTarget(
103 const GURL
&) OVERRIDE
{
104 return scoped_ptr
<content::DevToolsTarget
>();
107 virtual void EnumerateTargets(TargetCallback callback
) OVERRIDE
{
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
);
115 targets
.push_back(new Target(web_contents
));
117 callback
.Run(targets
);
120 virtual scoped_ptr
<net::StreamListenSocket
> CreateSocketForTethering(
121 net::StreamListenSocket::Delegate
* delegate
,
122 std::string
* name
) OVERRIDE
{
123 return scoped_ptr
<net::StreamListenSocket
>();
127 DISALLOW_COPY_AND_ASSIGN(AwDevToolsServerDelegate
);
131 std::string
AwDevToolsServerDelegate::GetDiscoveryPageHTML() {
134 "<head><title>WebView remote debugging</title></head>"
135 "<body>Please use <a href=\'chrome://inspect\'>chrome://inspect</a>"
141 std::string
GetViewDescription(WebContents
* web_contents
) {
142 const android_webview::BrowserViewRenderer
* bvr
=
143 android_webview::AwContents::FromWebContents(web_contents
)
144 ->GetBrowserViewRenderer();
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());
158 base::JSONWriter::Write(&description
, &json
);
164 namespace android_webview
{
166 AwDevToolsServer::AwDevToolsServer()
167 : protocol_handler_(NULL
) {
170 AwDevToolsServer::~AwDevToolsServer() {
174 void AwDevToolsServer::Start() {
175 if (protocol_handler_
)
178 protocol_handler_
= content::DevToolsHttpHandler::Start(
179 new net::UnixDomainSocketWithAbstractNamespaceFactory(
180 base::StringPrintf(kSocketNameFormat
, getpid()),
182 base::Bind(&content::CanUserConnectToDevTools
)),
183 base::StringPrintf(kFrontEndURL
, content::GetWebKitRevision().c_str()),
184 new AwDevToolsServerDelegate(),
188 void AwDevToolsServer::Stop() {
189 if (!protocol_handler_
)
191 // Note that the call to Stop() below takes care of |protocol_handler_|
193 protocol_handler_
->Stop();
194 protocol_handler_
= NULL
;
197 bool AwDevToolsServer::IsStarted() const {
198 return protocol_handler_
;
201 bool RegisterAwDevToolsServer(JNIEnv
* env
) {
202 return RegisterNativesImpl(env
);
205 static jlong
InitRemoteDebugging(JNIEnv
* env
,
207 AwDevToolsServer
* server
= new AwDevToolsServer();
208 return reinterpret_cast<intptr_t>(server
);
211 static void DestroyRemoteDebugging(JNIEnv
* env
, jobject obj
, jlong server
) {
212 delete reinterpret_cast<AwDevToolsServer
*>(server
);
215 static void SetRemoteDebuggingEnabled(JNIEnv
* env
,
219 AwDevToolsServer
* devtools_server
=
220 reinterpret_cast<AwDevToolsServer
*>(server
);
222 devtools_server
->Start();
224 devtools_server
->Stop();
228 } // namespace android_webview