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/browser/browser_view_renderer.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 std::string
GetId() const override
{ return agent_host_
->GetId(); }
34 std::string
GetParentId() const override
{ return std::string(); }
35 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 std::string
GetTitle() const override
{ return agent_host_
->GetTitle(); }
47 std::string
GetDescription() const override
{ return description_
; }
48 GURL
GetURL() const override
{ return agent_host_
->GetURL(); }
49 GURL
GetFaviconURL() const override
{ return GURL(); }
50 base::TimeTicks
GetLastActivityTime() const override
{
51 return last_activity_time_
;
53 bool IsAttached() const override
{ return agent_host_
->IsAttached(); }
54 scoped_refptr
<DevToolsAgentHost
> GetAgentHost() const override
{
57 bool Activate() const override
{ return agent_host_
->Activate(); }
58 bool Close() const override
{ return agent_host_
->Close(); }
61 scoped_refptr
<DevToolsAgentHost
> agent_host_
;
62 std::string description_
;
63 base::TimeTicks last_activity_time_
;
66 Target::Target(scoped_refptr
<DevToolsAgentHost
> agent_host
)
67 : agent_host_(agent_host
) {
68 if (WebContents
* web_contents
= agent_host
->GetWebContents()) {
69 description_
= GetViewDescription(web_contents
);
70 last_activity_time_
= web_contents
->GetLastActiveTime();
74 std::string
GetViewDescription(WebContents
* web_contents
) {
75 android_webview::BrowserViewRenderer
* bvr
=
76 android_webview::BrowserViewRenderer::FromWebContents(web_contents
);
78 base::DictionaryValue description
;
79 description
.SetBoolean("attached", bvr
->attached_to_window());
80 description
.SetBoolean("visible", bvr
->IsVisible());
81 gfx::Rect screen_rect
= bvr
->GetScreenRect();
82 description
.SetInteger("screenX", screen_rect
.x());
83 description
.SetInteger("screenY", screen_rect
.y());
84 description
.SetBoolean("empty", screen_rect
.size().IsEmpty());
85 if (!screen_rect
.size().IsEmpty()) {
86 description
.SetInteger("width", screen_rect
.width());
87 description
.SetInteger("height", screen_rect
.height());
90 base::JSONWriter::Write(&description
, &json
);
96 namespace android_webview
{
98 AwDevToolsManagerDelegate::AwDevToolsManagerDelegate() {
101 AwDevToolsManagerDelegate::~AwDevToolsManagerDelegate() {
104 base::DictionaryValue
* AwDevToolsManagerDelegate::HandleCommand(
105 content::DevToolsAgentHost
* agent_host
,
106 base::DictionaryValue
* command_dict
) {
110 void AwDevToolsManagerDelegate::EnumerateTargets(TargetCallback callback
) {
112 DevToolsAgentHost::List agents
= DevToolsAgentHost::GetOrCreateAll();
113 for (DevToolsAgentHost::List::iterator it
= agents
.begin();
114 it
!= agents
.end(); ++it
) {
115 targets
.push_back(new Target(*it
));
117 callback
.Run(targets
);
120 std::string
AwDevToolsManagerDelegate::GetPageThumbnailData(const GURL
&) {
124 scoped_ptr
<content::DevToolsTarget
> AwDevToolsManagerDelegate::CreateNewTarget(
126 return scoped_ptr
<content::DevToolsTarget
>();
129 } // namespace android_webview