1 // Copyright 2014 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/browser/aw_dev_tools_manager_delegate.h"
7 #include "android_webview/native/aw_contents.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/devtools_agent_host.h"
14 #include "content/public/browser/devtools_target.h"
15 #include "content/public/browser/web_contents.h"
17 using content::DevToolsAgentHost
;
18 using content::RenderViewHost
;
19 using content::WebContents
;
23 const char kTargetTypePage
[] = "page";
24 const char kTargetTypeServiceWorker
[] = "service_worker";
25 const char kTargetTypeOther
[] = "other";
27 std::string
GetViewDescription(WebContents
* web_contents
);
29 class Target
: public content::DevToolsTarget
{
31 explicit Target(scoped_refptr
<DevToolsAgentHost
> agent_host
);
33 virtual std::string
GetId() const override
{ return agent_host_
->GetId(); }
34 virtual std::string
GetParentId() const override
{ return std::string(); }
35 virtual std::string
GetType() const override
{
36 switch (agent_host_
->GetType()) {
37 case DevToolsAgentHost::TYPE_WEB_CONTENTS
:
38 return kTargetTypePage
;
39 case DevToolsAgentHost::TYPE_SERVICE_WORKER
:
40 return kTargetTypeServiceWorker
;
44 return kTargetTypeOther
;
46 virtual std::string
GetTitle() const override
{
47 return agent_host_
->GetTitle();
49 virtual std::string
GetDescription() const override
{ return description_
; }
50 virtual GURL
GetURL() const override
{ return agent_host_
->GetURL(); }
51 virtual GURL
GetFaviconURL() const override
{ return GURL(); }
52 virtual base::TimeTicks
GetLastActivityTime() const override
{
53 return last_activity_time_
;
55 virtual bool IsAttached() const override
{
56 return agent_host_
->IsAttached();
58 virtual scoped_refptr
<DevToolsAgentHost
> GetAgentHost() const override
{
61 virtual bool Activate() const override
{ return agent_host_
->Activate(); }
62 virtual bool Close() const override
{ return agent_host_
->Close(); }
65 scoped_refptr
<DevToolsAgentHost
> agent_host_
;
66 std::string description_
;
67 base::TimeTicks last_activity_time_
;
70 Target::Target(scoped_refptr
<DevToolsAgentHost
> agent_host
)
71 : agent_host_(agent_host
) {
72 if (WebContents
* web_contents
= agent_host
->GetWebContents()) {
73 description_
= GetViewDescription(web_contents
);
74 last_activity_time_
= web_contents
->GetLastActiveTime();
78 std::string
GetViewDescription(WebContents
* web_contents
) {
79 const android_webview::BrowserViewRenderer
* bvr
=
80 android_webview::AwContents::FromWebContents(web_contents
)
81 ->GetBrowserViewRenderer();
83 base::DictionaryValue description
;
84 description
.SetBoolean("attached", bvr
->attached_to_window());
85 description
.SetBoolean("visible", bvr
->IsVisible());
86 gfx::Rect screen_rect
= bvr
->GetScreenRect();
87 description
.SetInteger("screenX", screen_rect
.x());
88 description
.SetInteger("screenY", screen_rect
.y());
89 description
.SetBoolean("empty", screen_rect
.size().IsEmpty());
90 if (!screen_rect
.size().IsEmpty()) {
91 description
.SetInteger("width", screen_rect
.width());
92 description
.SetInteger("height", screen_rect
.height());
95 base::JSONWriter::Write(&description
, &json
);
101 namespace android_webview
{
103 AwDevToolsManagerDelegate::AwDevToolsManagerDelegate() {
106 AwDevToolsManagerDelegate::~AwDevToolsManagerDelegate() {
109 base::DictionaryValue
* AwDevToolsManagerDelegate::HandleCommand(
110 content::DevToolsAgentHost
* agent_host
,
111 base::DictionaryValue
* command_dict
) {
115 void AwDevToolsManagerDelegate::EnumerateTargets(TargetCallback callback
) {
117 DevToolsAgentHost::List agents
= DevToolsAgentHost::GetOrCreateAll();
118 for (DevToolsAgentHost::List::iterator it
= agents
.begin();
119 it
!= agents
.end(); ++it
) {
120 targets
.push_back(new Target(*it
));
122 callback
.Run(targets
);
125 std::string
AwDevToolsManagerDelegate::GetPageThumbnailData(const GURL
&) {
129 scoped_ptr
<content::DevToolsTarget
> AwDevToolsManagerDelegate::CreateNewTarget(
131 return scoped_ptr
<content::DevToolsTarget
>();
134 } // namespace android_webview