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/base64.h"
10 #include "base/memory/ref_counted_memory.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "content/public/browser/devtools_agent_host.h"
14 #include "content/public/browser/devtools_http_handler.h"
15 #include "content/public/browser/devtools_target.h"
16 #include "content/public/browser/favicon_status.h"
17 #include "content/public/browser/navigation_entry.h"
18 #include "content/public/browser/render_view_host.h"
19 #include "content/public/browser/render_widget_host_view.h"
20 #include "content/public/browser/web_contents.h"
21 #include "content/public/common/url_constants.h"
22 #include "grit/xwalk_resources.h"
23 #include "net/socket/tcp_listen_socket.h"
24 #include "ui/base/resource/resource_bundle.h"
25 #include "ui/snapshot/snapshot.h"
27 using content::DevToolsAgentHost;
28 using content::RenderViewHost;
29 using content::RenderWidgetHostView;
30 using content::WebContents;
34 const char kTargetTypePage[] = "page";
35 const char kTargetTypeServiceWorker[] = "service_worker";
36 const char kTargetTypeOther[] = "other";
38 class Target : public content::DevToolsTarget {
40 explicit Target(scoped_refptr<content::DevToolsAgentHost> agent_host);
42 virtual std::string GetId() const OVERRIDE { return agent_host_->GetId(); }
43 virtual std::string GetType() const OVERRIDE {
44 switch (agent_host_->GetType()) {
45 case content::DevToolsAgentHost::TYPE_WEB_CONTENTS:
46 return kTargetTypePage;
47 case content::DevToolsAgentHost::TYPE_SERVICE_WORKER:
48 return kTargetTypeServiceWorker;
52 return kTargetTypeOther;
54 virtual std::string GetTitle() const OVERRIDE {
55 return agent_host_->GetTitle();
57 virtual std::string GetDescription() const OVERRIDE { return std::string(); }
58 virtual GURL GetURL() const OVERRIDE { return agent_host_->GetURL(); }
59 virtual GURL GetFaviconURL() const OVERRIDE { return favicon_url_; }
60 virtual base::TimeTicks GetLastActivityTime() const OVERRIDE {
61 return last_activity_time_;
63 virtual std::string GetParentId() const OVERRIDE { return std::string(); }
64 virtual bool IsAttached() const OVERRIDE {
65 return agent_host_->IsAttached();
67 virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const OVERRIDE {
70 virtual bool Activate() const OVERRIDE;
71 virtual bool Close() const OVERRIDE;
74 GURL GetFaviconDataURL(WebContents* web_contents) const;
76 scoped_refptr<DevToolsAgentHost> agent_host_;
80 base::TimeTicks last_activity_time_;
83 Target::Target(scoped_refptr<content::DevToolsAgentHost> agent_host)
84 : agent_host_(agent_host) {
85 if (content::WebContents* web_contents = agent_host_->GetWebContents()) {
86 content::NavigationController& controller = web_contents->GetController();
87 content::NavigationEntry* entry = controller.GetActiveEntry();
88 if (entry != NULL && entry->GetURL().is_valid())
89 favicon_url_ = entry->GetFavicon().url;
90 if (favicon_url_.is_empty())
91 favicon_url_ = GetFaviconDataURL(web_contents);
92 last_activity_time_ = web_contents->GetLastActiveTime();
96 GURL Target::GetFaviconDataURL(WebContents* web_contents) const {
97 // Convert icon image to "data:" url.
98 xwalk::Runtime* runtime =
99 static_cast<xwalk::Runtime*>(web_contents->GetDelegate());
100 if (!runtime || runtime->app_icon().IsEmpty())
102 scoped_refptr<base::RefCountedMemory> icon_bytes =
103 runtime->app_icon().Copy1xPNGBytes();
105 str_url.append(reinterpret_cast<const char*>(icon_bytes->front()),
107 base::Base64Encode(str_url, &str_url);
108 str_url.insert(0, "data:image/png;base64,");
109 return GURL(str_url);
112 bool Target::Activate() const {
113 return agent_host_->Activate();
116 bool Target::Close() const {
117 return agent_host_->Close();
125 Runtime* CreateWithDefaultWindow(
126 XWalkBrowserContext* browser_context, const GURL& url,
127 Runtime::Observer* observer) {
128 Runtime* runtime = Runtime::Create(browser_context);
129 runtime->set_observer(observer);
130 runtime->LoadURL(url);
131 #if !defined(OS_ANDROID)
132 runtime->set_ui_delegate(DefaultRuntimeUIDelegate::Create(runtime));
139 XWalkDevToolsHttpHandlerDelegate::XWalkDevToolsHttpHandlerDelegate() {
142 XWalkDevToolsHttpHandlerDelegate::~XWalkDevToolsHttpHandlerDelegate() {
145 std::string XWalkDevToolsHttpHandlerDelegate::GetDiscoveryPageHTML() {
146 return ResourceBundle::GetSharedInstance().GetRawDataResource(
147 IDR_DEVTOOLS_FRONTEND_PAGE_HTML).as_string();
150 void XWalkDevToolsDelegate::ProcessAndSaveThumbnail(
152 scoped_refptr<base::RefCountedBytes> png) {
153 const std::vector<unsigned char>& png_data = png->data();
154 std::string png_string_data(reinterpret_cast<const char*>(&png_data[0]),
156 thumbnail_map_[url] = png_string_data;
159 bool XWalkDevToolsHttpHandlerDelegate::BundlesFrontendResources() {
163 base::FilePath XWalkDevToolsHttpHandlerDelegate::GetDebugFrontendDir() {
164 return base::FilePath();
167 scoped_ptr<net::StreamListenSocket>
168 XWalkDevToolsHttpHandlerDelegate::CreateSocketForTethering(
169 net::StreamListenSocket::Delegate* delegate,
171 return scoped_ptr<net::StreamListenSocket>();
174 XWalkDevToolsDelegate::XWalkDevToolsDelegate(XWalkBrowserContext* context)
175 : browser_context_(context),
176 weak_factory_(this) {
179 XWalkDevToolsDelegate::~XWalkDevToolsDelegate() {
182 base::DictionaryValue* XWalkDevToolsDelegate::HandleCommand(
183 content::DevToolsAgentHost* agent_host,
184 base::DictionaryValue* command_dict) {
188 std::string XWalkDevToolsDelegate::GetPageThumbnailData(const GURL& url) {
189 if (thumbnail_map_.find(url) != thumbnail_map_.end())
190 return thumbnail_map_[url];
191 // TODO(YangangHan): Support real time thumbnail.
192 content::DevToolsAgentHost::List agents =
193 content::DevToolsAgentHost::GetOrCreateAll();
194 for (auto& it : agents) {
195 WebContents* web_contents = it.get()->GetWebContents();
196 if (web_contents && web_contents->GetURL() == url) {
197 RenderWidgetHostView* render_widget_host_view =
198 web_contents->GetRenderWidgetHostView();
199 if (!render_widget_host_view)
201 gfx::Rect snapshot_bounds(
202 render_widget_host_view->GetViewBounds().size());
203 ui::GrabViewSnapshotAsync(
204 render_widget_host_view->GetNativeView(),
206 base::ThreadTaskRunnerHandle::Get(),
207 base::Bind(&XWalkDevToolsDelegate::ProcessAndSaveThumbnail,
208 weak_factory_.GetWeakPtr(),
213 return std::string();
216 scoped_ptr<content::DevToolsTarget>
217 XWalkDevToolsDelegate::CreateNewTarget(const GURL& url) {
218 Runtime* runtime = CreateWithDefaultWindow(
219 browser_context_, GURL(url::kAboutBlankURL), this);
220 return scoped_ptr<content::DevToolsTarget>(
221 new Target(DevToolsAgentHost::GetOrCreateFor(runtime->web_contents())));
224 void XWalkDevToolsDelegate::EnumerateTargets(TargetCallback callback) {
226 content::DevToolsAgentHost::List agents =
227 content::DevToolsAgentHost::GetOrCreateAll();
228 for (content::DevToolsAgentHost::List::iterator it = agents.begin();
229 it != agents.end(); ++it) {
230 #if !defined(OS_ANDROID)
232 static_cast<Runtime*>((*it)->GetWebContents()->GetDelegate());
233 if (runtime && runtime->remote_debugging_enabled())
235 targets.push_back(new Target(*it));
237 callback.Run(targets);
240 void XWalkDevToolsDelegate::OnNewRuntimeAdded(Runtime* runtime) {
241 runtime->set_observer(this);
242 runtime->set_ui_delegate(DefaultRuntimeUIDelegate::Create(runtime));
246 void XWalkDevToolsDelegate::OnRuntimeClosed(Runtime* runtime) {