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";
31 class Target : public content::DevToolsTarget {
33 explicit Target(WebContents* web_contents);
35 virtual std::string GetId() const OVERRIDE { return id_; }
36 virtual std::string GetType() const OVERRIDE { return kTargetTypePage; }
37 virtual std::string GetTitle() const OVERRIDE { return title_; }
38 virtual std::string GetDescription() const OVERRIDE { return std::string(); }
39 virtual GURL GetUrl() const OVERRIDE { return url_; }
40 virtual GURL GetFaviconUrl() const OVERRIDE { return favicon_url_; }
41 virtual base::TimeTicks GetLastActivityTime() const OVERRIDE {
42 return last_activity_time_;
44 virtual bool IsAttached() const OVERRIDE {
45 return agent_host_->IsAttached();
47 virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const OVERRIDE {
50 virtual bool Activate() const OVERRIDE;
51 virtual bool Close() const OVERRIDE;
54 scoped_refptr<DevToolsAgentHost> agent_host_;
59 base::TimeTicks last_activity_time_;
62 Target::Target(WebContents* web_contents) {
64 DevToolsAgentHost::GetOrCreateFor(web_contents->GetRenderViewHost());
65 id_ = agent_host_->GetId();
66 title_ = base::UTF16ToUTF8(web_contents->GetTitle());
67 url_ = web_contents->GetURL();
68 content::NavigationController& controller = web_contents->GetController();
69 content::NavigationEntry* entry = controller.GetActiveEntry();
70 if (entry != NULL && entry->GetURL().is_valid())
71 favicon_url_ = entry->GetFavicon().url;
72 last_activity_time_ = web_contents->GetLastActiveTime();
75 bool Target::Activate() const {
76 RenderViewHost* rvh = agent_host_->GetRenderViewHost();
79 WebContents* web_contents = WebContents::FromRenderViewHost(rvh);
82 web_contents->GetDelegate()->ActivateContents(web_contents);
86 bool Target::Close() const {
87 RenderViewHost* rvh = agent_host_->GetRenderViewHost();
98 XWalkDevToolsDelegate::XWalkDevToolsDelegate(RuntimeContext* runtime_context)
99 : runtime_context_(runtime_context) {
102 XWalkDevToolsDelegate::~XWalkDevToolsDelegate() {
105 std::string XWalkDevToolsDelegate::GetDiscoveryPageHTML() {
106 return ResourceBundle::GetSharedInstance().GetRawDataResource(
107 IDR_DEVTOOLS_FRONTEND_PAGE_HTML).as_string();
110 bool XWalkDevToolsDelegate::BundlesFrontendResources() {
114 base::FilePath XWalkDevToolsDelegate::GetDebugFrontendDir() {
115 return base::FilePath();
118 std::string XWalkDevToolsDelegate::GetPageThumbnailData(const GURL& url) {
119 return std::string();
122 scoped_ptr<content::DevToolsTarget>
123 XWalkDevToolsDelegate::CreateNewTarget(const GURL& url) {
124 Runtime* runtime = Runtime::CreateWithDefaultWindow(
125 runtime_context_, GURL(content::kAboutBlankURL));
126 return scoped_ptr<content::DevToolsTarget>(
127 new Target(runtime->web_contents()));
130 void XWalkDevToolsDelegate::EnumerateTargets(TargetCallback callback) {
132 std::vector<RenderViewHost*> rvh_list =
133 content::DevToolsAgentHost::GetValidRenderViewHosts();
134 for (std::vector<RenderViewHost*>::iterator it = rvh_list.begin();
135 it != rvh_list.end(); ++it) {
136 WebContents* web_contents = WebContents::FromRenderViewHost(*it);
138 targets.push_back(new Target(web_contents));
140 callback.Run(targets);
143 scoped_ptr<net::StreamListenSocket>
144 XWalkDevToolsDelegate::CreateSocketForTethering(
145 net::StreamListenSocket::Delegate* delegate,
147 return scoped_ptr<net::StreamListenSocket>();