1 // Copyright (c) 2012 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 "xwalk/runtime/browser/devtools/xwalk_devtools_delegate.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "content/public/browser/devtools_agent_host.h"
11 #include "content/public/browser/devtools_http_handler.h"
12 #include "content/public/browser/devtools_target.h"
13 #include "content/public/browser/favicon_status.h"
14 #include "content/public/browser/navigation_entry.h"
15 #include "content/public/browser/render_view_host.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/common/url_constants.h"
18 #include "grit/xwalk_resources.h"
19 #include "net/socket/tcp_listen_socket.h"
20 #include "ui/base/resource/resource_bundle.h"
21 #include "xwalk/runtime/browser/runtime.h"
23 using content::DevToolsAgentHost;
24 using content::RenderViewHost;
25 using content::WebContents;
29 const char kTargetTypePage[] = "page";
30 const char kTargetTypeServiceWorker[] = "service_worker";
31 const char kTargetTypeOther[] = "other";
33 class Target : public content::DevToolsTarget {
35 explicit Target(scoped_refptr<content::DevToolsAgentHost> agent_host);
37 virtual std::string GetId() const OVERRIDE { return agent_host_->GetId(); }
38 virtual std::string GetType() const OVERRIDE {
39 switch (agent_host_->GetType()) {
40 case content::DevToolsAgentHost::TYPE_WEB_CONTENTS:
41 return kTargetTypePage;
42 case content::DevToolsAgentHost::TYPE_SERVICE_WORKER:
43 return kTargetTypeServiceWorker;
47 return kTargetTypeOther;
49 virtual std::string GetTitle() const OVERRIDE {
50 return agent_host_->GetTitle();
52 virtual std::string GetDescription() const OVERRIDE { return std::string(); }
53 virtual GURL GetURL() const OVERRIDE { return url_; }
54 virtual GURL GetFaviconURL() const OVERRIDE { return favicon_url_; }
55 virtual base::TimeTicks GetLastActivityTime() const OVERRIDE {
56 return last_activity_time_;
58 virtual std::string GetParentId() const OVERRIDE { return std::string(); }
59 virtual bool IsAttached() const OVERRIDE {
60 return agent_host_->IsAttached();
62 virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const OVERRIDE {
65 virtual bool Activate() const OVERRIDE;
66 virtual bool Close() const OVERRIDE;
69 scoped_refptr<DevToolsAgentHost> agent_host_;
74 base::TimeTicks last_activity_time_;
77 Target::Target(scoped_refptr<content::DevToolsAgentHost> agent_host)
78 : agent_host_(agent_host) {
79 if (content::WebContents* web_contents = agent_host_->GetWebContents()) {
80 content::NavigationController& controller = web_contents->GetController();
81 content::NavigationEntry* entry = controller.GetActiveEntry();
82 if (entry != NULL && entry->GetURL().is_valid())
83 favicon_url_ = entry->GetFavicon().url;
84 last_activity_time_ = web_contents->GetLastActiveTime();
88 bool Target::Activate() const {
89 WebContents* web_contents = agent_host_->GetWebContents();
92 web_contents->GetDelegate()->ActivateContents(web_contents);
96 bool Target::Close() const {
97 RenderViewHost* rvh = agent_host_->GetWebContents()->GetRenderViewHost();
108 XWalkDevToolsHttpHandlerDelegate::XWalkDevToolsHttpHandlerDelegate() {
111 XWalkDevToolsHttpHandlerDelegate::~XWalkDevToolsHttpHandlerDelegate() {
114 std::string XWalkDevToolsHttpHandlerDelegate::GetDiscoveryPageHTML() {
115 return ResourceBundle::GetSharedInstance().GetRawDataResource(
116 IDR_DEVTOOLS_FRONTEND_PAGE_HTML).as_string();
119 bool XWalkDevToolsHttpHandlerDelegate::BundlesFrontendResources() {
123 base::FilePath XWalkDevToolsHttpHandlerDelegate::GetDebugFrontendDir() {
124 return base::FilePath();
127 scoped_ptr<net::StreamListenSocket>
128 XWalkDevToolsHttpHandlerDelegate::CreateSocketForTethering(
129 net::StreamListenSocket::Delegate* delegate,
131 return scoped_ptr<net::StreamListenSocket>();
134 XWalkDevToolsDelegate::XWalkDevToolsDelegate(RuntimeContext* runtime_context)
135 : runtime_context_(runtime_context) {
138 XWalkDevToolsDelegate::~XWalkDevToolsDelegate() {
141 base::DictionaryValue* XWalkDevToolsDelegate::HandleCommand(
142 content::DevToolsAgentHost* agent_host,
143 base::DictionaryValue* command_dict) {
147 std::string XWalkDevToolsDelegate::GetPageThumbnailData(const GURL& url) {
148 return std::string();
151 scoped_ptr<content::DevToolsTarget>
152 XWalkDevToolsDelegate::CreateNewTarget(const GURL& url) {
153 Runtime* runtime = Runtime::CreateWithDefaultWindow(
154 runtime_context_, GURL(url::kAboutBlankURL));
155 return scoped_ptr<content::DevToolsTarget>(
156 new Target(DevToolsAgentHost::GetOrCreateFor(runtime->web_contents())));
159 void XWalkDevToolsDelegate::EnumerateTargets(TargetCallback callback) {
161 content::DevToolsAgentHost::List agents =
162 content::DevToolsAgentHost::GetOrCreateAll();
163 for (content::DevToolsAgentHost::List::iterator it = agents.begin();
164 it != agents.end(); ++it) {
166 static_cast<Runtime*>((*it)->GetWebContents()->GetDelegate());
167 if (runtime && runtime->remote_debugging_enabled())
168 targets.push_back(new Target(*it));
170 callback.Run(targets);