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 std::string GetParentId() const OVERRIDE { return std::string(); }
45 virtual bool IsAttached() const OVERRIDE {
46 return agent_host_->IsAttached();
48 virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const OVERRIDE {
51 virtual bool Activate() const OVERRIDE;
52 virtual bool Close() const OVERRIDE;
55 scoped_refptr<DevToolsAgentHost> agent_host_;
60 base::TimeTicks last_activity_time_;
63 Target::Target(WebContents* web_contents) {
65 DevToolsAgentHost::GetOrCreateFor(web_contents);
66 id_ = agent_host_->GetId();
67 title_ = base::UTF16ToUTF8(web_contents->GetTitle());
68 url_ = web_contents->GetURL();
69 content::NavigationController& controller = web_contents->GetController();
70 content::NavigationEntry* entry = controller.GetActiveEntry();
71 if (entry != NULL && entry->GetURL().is_valid())
72 favicon_url_ = entry->GetFavicon().url;
73 last_activity_time_ = web_contents->GetLastActiveTime();
76 bool Target::Activate() const {
77 WebContents* web_contents = agent_host_->GetWebContents();
80 web_contents->GetDelegate()->ActivateContents(web_contents);
84 bool Target::Close() const {
85 RenderViewHost* rvh = agent_host_->GetWebContents()->GetRenderViewHost();
96 XWalkDevToolsDelegate::XWalkDevToolsDelegate(RuntimeContext* runtime_context)
97 : runtime_context_(runtime_context) {
100 XWalkDevToolsDelegate::~XWalkDevToolsDelegate() {
103 std::string XWalkDevToolsDelegate::GetDiscoveryPageHTML() {
104 return ResourceBundle::GetSharedInstance().GetRawDataResource(
105 IDR_DEVTOOLS_FRONTEND_PAGE_HTML).as_string();
108 bool XWalkDevToolsDelegate::BundlesFrontendResources() {
112 base::FilePath XWalkDevToolsDelegate::GetDebugFrontendDir() {
113 return base::FilePath();
116 std::string XWalkDevToolsDelegate::GetPageThumbnailData(const GURL& url) {
117 return std::string();
120 scoped_ptr<content::DevToolsTarget>
121 XWalkDevToolsDelegate::CreateNewTarget(const GURL& url) {
122 Runtime* runtime = Runtime::CreateWithDefaultWindow(
123 runtime_context_, GURL(url::kAboutBlankURL));
124 return scoped_ptr<content::DevToolsTarget>(
125 new Target(runtime->web_contents()));
128 void XWalkDevToolsDelegate::EnumerateTargets(TargetCallback callback) {
130 std::vector<WebContents*> web_contents_list =
131 content::DevToolsAgentHost::GetInspectableWebContents();
132 for (std::vector<WebContents*>::iterator it = web_contents_list.begin();
133 it != web_contents_list.end();
135 Runtime* runtime = static_cast<Runtime*>((*it)->GetDelegate());
136 if (runtime && runtime->remote_debugging_enabled())
137 targets.push_back(new Target(*it));
139 callback.Run(targets);
142 scoped_ptr<net::StreamListenSocket>
143 XWalkDevToolsDelegate::CreateSocketForTethering(
144 net::StreamListenSocket::Delegate* delegate,
146 return scoped_ptr<net::StreamListenSocket>();