bdfec662eeabf5b59f505fb4fe9244bfcabc3172
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / devtools / xwalk_devtools_delegate.cc
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.
4
5 #include "xwalk/runtime/browser/devtools/xwalk_devtools_delegate.h"
6
7 #include <string>
8
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"
26
27 using content::DevToolsAgentHost;
28 using content::RenderViewHost;
29 using content::RenderWidgetHostView;
30 using content::WebContents;
31
32 namespace {
33
34 const char kTargetTypePage[] = "page";
35 const char kTargetTypeServiceWorker[] = "service_worker";
36 const char kTargetTypeOther[] = "other";
37
38 class Target : public content::DevToolsTarget {
39  public:
40   explicit Target(scoped_refptr<content::DevToolsAgentHost> agent_host);
41
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;
49          default:
50            break;
51        }
52        return kTargetTypeOther;
53      }
54   virtual std::string GetTitle() const OVERRIDE {
55     return agent_host_->GetTitle();
56   }
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_;
62   }
63   virtual std::string GetParentId() const OVERRIDE { return std::string(); }
64   virtual bool IsAttached() const OVERRIDE {
65     return agent_host_->IsAttached();
66   }
67   virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const OVERRIDE {
68     return agent_host_;
69   }
70   virtual bool Activate() const OVERRIDE;
71   virtual bool Close() const OVERRIDE;
72
73  private:
74   GURL GetFaviconDataURL(WebContents* web_contents) const;
75
76   scoped_refptr<DevToolsAgentHost> agent_host_;
77   std::string id_;
78   std::string title_;
79   GURL favicon_url_;
80   base::TimeTicks last_activity_time_;
81 };
82
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();
93   }
94 }
95
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())
101     return GURL();
102   scoped_refptr<base::RefCountedMemory> icon_bytes =
103       runtime->app_icon().Copy1xPNGBytes();
104   std::string str_url;
105   str_url.append(reinterpret_cast<const char*>(icon_bytes->front()),
106                  icon_bytes->size());
107   base::Base64Encode(str_url, &str_url);
108   str_url.insert(0, "data:image/png;base64,");
109   return GURL(str_url);
110 }
111
112 bool Target::Activate() const {
113   return agent_host_->Activate();
114 }
115
116 bool Target::Close() const {
117   return agent_host_->Close();
118 }
119
120 }  // namespace
121
122 namespace xwalk {
123
124 namespace {
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));
133   runtime->Show();
134 #endif
135   return runtime;
136 }
137 }  // namespace
138
139 XWalkDevToolsHttpHandlerDelegate::XWalkDevToolsHttpHandlerDelegate() {
140 }
141
142 XWalkDevToolsHttpHandlerDelegate::~XWalkDevToolsHttpHandlerDelegate() {
143 }
144
145 std::string XWalkDevToolsHttpHandlerDelegate::GetDiscoveryPageHTML() {
146   return ResourceBundle::GetSharedInstance().GetRawDataResource(
147       IDR_DEVTOOLS_FRONTEND_PAGE_HTML).as_string();
148 }
149
150 void XWalkDevToolsDelegate::ProcessAndSaveThumbnail(
151     const GURL& url,
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]),
155                               png_data.size());
156   thumbnail_map_[url] = png_string_data;
157 }
158
159 bool XWalkDevToolsHttpHandlerDelegate::BundlesFrontendResources() {
160   return true;
161 }
162
163 base::FilePath XWalkDevToolsHttpHandlerDelegate::GetDebugFrontendDir() {
164   return base::FilePath();
165 }
166
167 scoped_ptr<net::StreamListenSocket>
168 XWalkDevToolsHttpHandlerDelegate::CreateSocketForTethering(
169     net::StreamListenSocket::Delegate* delegate,
170     std::string* name) {
171   return scoped_ptr<net::StreamListenSocket>();
172 }
173
174 XWalkDevToolsDelegate::XWalkDevToolsDelegate(XWalkBrowserContext* context)
175     : browser_context_(context),
176       weak_factory_(this) {
177 }
178
179 XWalkDevToolsDelegate::~XWalkDevToolsDelegate() {
180 }
181
182 base::DictionaryValue* XWalkDevToolsDelegate::HandleCommand(
183     content::DevToolsAgentHost* agent_host,
184     base::DictionaryValue* command_dict) {
185   return NULL;
186 }
187
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)
200         continue;
201       gfx::Rect snapshot_bounds(
202         render_widget_host_view->GetViewBounds().size());
203       ui::GrabViewSnapshotAsync(
204         render_widget_host_view->GetNativeView(),
205         snapshot_bounds,
206         base::ThreadTaskRunnerHandle::Get(),
207         base::Bind(&XWalkDevToolsDelegate::ProcessAndSaveThumbnail,
208                    weak_factory_.GetWeakPtr(),
209                    url));
210         break;
211     }
212   }
213   return std::string();
214 }
215
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())));
222 }
223
224 void XWalkDevToolsDelegate::EnumerateTargets(TargetCallback callback) {
225   TargetList targets;
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)
231     Runtime* runtime =
232         static_cast<Runtime*>((*it)->GetWebContents()->GetDelegate());
233     if (runtime && runtime->remote_debugging_enabled())
234 #endif
235       targets.push_back(new Target(*it));
236   }
237   callback.Run(targets);
238 }
239
240 void XWalkDevToolsDelegate::OnNewRuntimeAdded(Runtime* runtime) {
241   runtime->set_observer(this);
242   runtime->set_ui_delegate(DefaultRuntimeUIDelegate::Create(runtime));
243   runtime->Show();
244 }
245
246 void XWalkDevToolsDelegate::OnRuntimeClosed(Runtime* runtime) {
247   delete runtime;
248 }
249
250 }  // namespace xwalk