cast_dev_tools_delegate.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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.
4
5#include "chromecast/shell/browser/devtools/cast_dev_tools_delegate.h"
6
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"
19
20namespace chromecast {
21namespace shell {
22
23namespace {
24
25const char kTargetTypePage[] = "page";
26const char kTargetTypeServiceWorker[] = "service_worker";
27const char kTargetTypeSharedWorker[] = "worker";
28const char kTargetTypeOther[] = "other";
29
30class Target : public content::DevToolsTarget {
31 public:
32  explicit Target(scoped_refptr<content::DevToolsAgentHost> agent_host);
33
34  virtual std::string GetId() const OVERRIDE { return agent_host_->GetId(); }
35  virtual std::string GetParentId() const OVERRIDE { return std::string(); }
36  virtual 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;
44      default:
45        break;
46    }
47    return kTargetTypeOther;
48  }
49  virtual std::string GetTitle() const OVERRIDE {
50    return agent_host_->GetTitle();
51  }
52  virtual std::string GetDescription() const OVERRIDE { return std::string(); }
53  virtual GURL GetURL() const OVERRIDE {
54    return agent_host_->GetURL();
55  }
56  virtual GURL GetFaviconURL() const OVERRIDE { return favicon_url_; }
57  virtual base::TimeTicks GetLastActivityTime() const OVERRIDE {
58    return last_activity_time_;
59  }
60  virtual bool IsAttached() const OVERRIDE {
61    return agent_host_->IsAttached();
62  }
63  virtual scoped_refptr<content::DevToolsAgentHost> GetAgentHost()
64      const OVERRIDE {
65    return agent_host_;
66  }
67  virtual bool Activate() const OVERRIDE {
68    return agent_host_->Activate();
69  }
70  virtual bool Close() const OVERRIDE {
71    return agent_host_->Close();
72  }
73
74 private:
75  scoped_refptr<content::DevToolsAgentHost> agent_host_;
76  GURL favicon_url_;
77  base::TimeTicks last_activity_time_;
78
79  DISALLOW_COPY_AND_ASSIGN(Target);
80};
81
82Target::Target(scoped_refptr<content::DevToolsAgentHost> agent_host)
83    : agent_host_(agent_host) {
84  if (content::WebContents* web_contents = agent_host_->GetWebContents()) {
85    content::NavigationController& controller = web_contents->GetController();
86    content::NavigationEntry* entry = controller.GetActiveEntry();
87    if (entry != NULL && entry->GetURL().is_valid())
88      favicon_url_ = entry->GetFavicon().url;
89    last_activity_time_ = web_contents->GetLastActiveTime();
90  }
91}
92
93}  // namespace
94
95// CastDevToolsDelegate -----------------------------------------------------
96
97CastDevToolsDelegate::CastDevToolsDelegate() {
98}
99
100CastDevToolsDelegate::~CastDevToolsDelegate() {
101}
102
103std::string CastDevToolsDelegate::GetDiscoveryPageHTML() {
104  return ResourceBundle::GetSharedInstance().GetRawDataResource(
105      IDR_CAST_SHELL_DEVTOOLS_DISCOVERY_PAGE).as_string();
106}
107
108bool CastDevToolsDelegate::BundlesFrontendResources() {
109  return true;
110}
111
112base::FilePath CastDevToolsDelegate::GetDebugFrontendDir() {
113  return base::FilePath();
114}
115
116scoped_ptr<net::StreamListenSocket>
117CastDevToolsDelegate::CreateSocketForTethering(
118    net::StreamListenSocket::Delegate* delegate,
119    std::string* name) {
120  return scoped_ptr<net::StreamListenSocket>();
121}
122
123// CastDevToolsManagerDelegate -----------------------------------------------
124
125CastDevToolsManagerDelegate::CastDevToolsManagerDelegate() {
126}
127
128CastDevToolsManagerDelegate::~CastDevToolsManagerDelegate() {
129}
130
131base::DictionaryValue* CastDevToolsManagerDelegate::HandleCommand(
132    content::DevToolsAgentHost* agent_host,
133    base::DictionaryValue* command) {
134  return NULL;
135}
136
137std::string CastDevToolsManagerDelegate::GetPageThumbnailData(
138    const GURL& url) {
139  return "";
140}
141
142scoped_ptr<content::DevToolsTarget>
143CastDevToolsManagerDelegate::CreateNewTarget(const GURL& url) {
144  return scoped_ptr<content::DevToolsTarget>();
145}
146
147void CastDevToolsManagerDelegate::EnumerateTargets(TargetCallback callback) {
148  TargetList targets;
149  content::DevToolsAgentHost::List agents =
150      content::DevToolsAgentHost::GetOrCreateAll();
151  for (content::DevToolsAgentHost::List::iterator it = agents.begin();
152       it != agents.end(); ++it) {
153    targets.push_back(new Target(*it));
154  }
155  callback.Run(targets);
156}
157
158}  // namespace shell
159}  // namespace chromecast
160