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 "chromecast/browser/devtools/cast_dev_tools_delegate.h"
7 #include "base/files/file_path.h"
8 #include "base/macros.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "content/public/browser/devtools_agent_host.h"
11 #include "content/public/browser/devtools_target.h"
12 #include "content/public/browser/favicon_status.h"
13 #include "content/public/browser/navigation_entry.h"
14 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/browser/web_contents_delegate.h"
17 #include "grit/shell_resources.h"
18 #include "ui/base/resource/resource_bundle.h"
20 namespace chromecast
{
25 const char kTargetTypePage
[] = "page";
26 const char kTargetTypeServiceWorker
[] = "service_worker";
27 const char kTargetTypeSharedWorker
[] = "worker";
28 const char kTargetTypeOther
[] = "other";
30 class Target
: public content::DevToolsTarget
{
32 explicit Target(scoped_refptr
<content::DevToolsAgentHost
> agent_host
);
34 std::string
GetId() const override
{ return agent_host_
->GetId(); }
35 std::string
GetParentId() const override
{ return std::string(); }
36 std::string
GetType() const override
{
37 switch (agent_host_
->GetType()) {
38 case content::DevToolsAgentHost::TYPE_WEB_CONTENTS
:
39 return kTargetTypePage
;
40 case content::DevToolsAgentHost::TYPE_SERVICE_WORKER
:
41 return kTargetTypeServiceWorker
;
42 case content::DevToolsAgentHost::TYPE_SHARED_WORKER
:
43 return kTargetTypeSharedWorker
;
47 return kTargetTypeOther
;
49 std::string
GetTitle() const override
{ return agent_host_
->GetTitle(); }
50 std::string
GetDescription() const override
{ return std::string(); }
51 GURL
GetURL() const override
{ return agent_host_
->GetURL(); }
52 GURL
GetFaviconURL() const override
{ return favicon_url_
; }
53 base::TimeTicks
GetLastActivityTime() const override
{
54 return last_activity_time_
;
56 bool IsAttached() const override
{ return agent_host_
->IsAttached(); }
57 scoped_refptr
<content::DevToolsAgentHost
> GetAgentHost() const override
{
60 bool Activate() const override
{ return agent_host_
->Activate(); }
61 bool Close() const override
{ return agent_host_
->Close(); }
64 scoped_refptr
<content::DevToolsAgentHost
> agent_host_
;
66 base::TimeTicks last_activity_time_
;
68 DISALLOW_COPY_AND_ASSIGN(Target
);
71 Target::Target(scoped_refptr
<content::DevToolsAgentHost
> agent_host
)
72 : agent_host_(agent_host
) {
73 if (content::WebContents
* web_contents
= agent_host_
->GetWebContents()) {
74 content::NavigationController
& controller
= web_contents
->GetController();
75 content::NavigationEntry
* entry
= controller
.GetActiveEntry();
76 if (entry
!= NULL
&& entry
->GetURL().is_valid())
77 favicon_url_
= entry
->GetFavicon().url
;
78 last_activity_time_
= web_contents
->GetLastActiveTime();
84 // CastDevToolsDelegate -----------------------------------------------------
86 CastDevToolsDelegate::CastDevToolsDelegate() {
89 CastDevToolsDelegate::~CastDevToolsDelegate() {
92 std::string
CastDevToolsDelegate::GetDiscoveryPageHTML() {
93 #if defined(OS_ANDROID)
96 return ResourceBundle::GetSharedInstance().GetRawDataResource(
97 IDR_CAST_SHELL_DEVTOOLS_DISCOVERY_PAGE
).as_string();
98 #endif // defined(OS_ANDROID)
101 bool CastDevToolsDelegate::BundlesFrontendResources() {
105 base::FilePath
CastDevToolsDelegate::GetDebugFrontendDir() {
106 return base::FilePath();
109 // CastDevToolsManagerDelegate -----------------------------------------------
111 CastDevToolsManagerDelegate::CastDevToolsManagerDelegate() {
114 CastDevToolsManagerDelegate::~CastDevToolsManagerDelegate() {
117 base::DictionaryValue
* CastDevToolsManagerDelegate::HandleCommand(
118 content::DevToolsAgentHost
* agent_host
,
119 base::DictionaryValue
* command
) {
123 std::string
CastDevToolsManagerDelegate::GetPageThumbnailData(
128 scoped_ptr
<content::DevToolsTarget
>
129 CastDevToolsManagerDelegate::CreateNewTarget(const GURL
& url
) {
130 return scoped_ptr
<content::DevToolsTarget
>();
133 void CastDevToolsManagerDelegate::EnumerateTargets(TargetCallback callback
) {
135 content::DevToolsAgentHost::List agents
=
136 content::DevToolsAgentHost::GetOrCreateAll();
137 for (content::DevToolsAgentHost::List::iterator it
= agents
.begin();
138 it
!= agents
.end(); ++it
) {
139 targets
.push_back(new Target(*it
));
141 callback
.Run(targets
);
145 } // namespace chromecast