Upstream version 5.34.104.0
[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/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"
22
23 using content::DevToolsAgentHost;
24 using content::RenderViewHost;
25 using content::WebContents;
26
27 namespace {
28
29 const char kTargetTypePage[] = "page";
30
31 class Target : public content::DevToolsTarget {
32  public:
33   explicit Target(WebContents* web_contents);
34
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_;
43   }
44   virtual bool IsAttached() const OVERRIDE {
45     return agent_host_->IsAttached();
46   }
47   virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const OVERRIDE {
48     return agent_host_;
49   }
50   virtual bool Activate() const OVERRIDE;
51   virtual bool Close() const OVERRIDE;
52
53  private:
54   scoped_refptr<DevToolsAgentHost> agent_host_;
55   std::string id_;
56   std::string title_;
57   GURL url_;
58   GURL favicon_url_;
59   base::TimeTicks last_activity_time_;
60 };
61
62 Target::Target(WebContents* web_contents) {
63   agent_host_ =
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();
73 }
74
75 bool Target::Activate() const {
76   RenderViewHost* rvh = agent_host_->GetRenderViewHost();
77   if (!rvh)
78     return false;
79   WebContents* web_contents = WebContents::FromRenderViewHost(rvh);
80   if (!web_contents)
81     return false;
82   web_contents->GetDelegate()->ActivateContents(web_contents);
83   return true;
84 }
85
86 bool Target::Close() const {
87   RenderViewHost* rvh = agent_host_->GetRenderViewHost();
88   if (!rvh)
89     return false;
90   rvh->ClosePage();
91   return true;
92 }
93
94 }  // namespace
95
96 namespace xwalk {
97
98 XWalkDevToolsDelegate::XWalkDevToolsDelegate(RuntimeContext* runtime_context)
99     : runtime_context_(runtime_context) {
100 }
101
102 XWalkDevToolsDelegate::~XWalkDevToolsDelegate() {
103 }
104
105 std::string XWalkDevToolsDelegate::GetDiscoveryPageHTML() {
106   return ResourceBundle::GetSharedInstance().GetRawDataResource(
107       IDR_DEVTOOLS_FRONTEND_PAGE_HTML).as_string();
108 }
109
110 bool XWalkDevToolsDelegate::BundlesFrontendResources() {
111   return true;
112 }
113
114 base::FilePath XWalkDevToolsDelegate::GetDebugFrontendDir() {
115   return base::FilePath();
116 }
117
118 std::string XWalkDevToolsDelegate::GetPageThumbnailData(const GURL& url) {
119   return std::string();
120 }
121
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()));
128 }
129
130 void XWalkDevToolsDelegate::EnumerateTargets(TargetCallback callback) {
131   TargetList targets;
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);
137     if (web_contents)
138       targets.push_back(new Target(web_contents));
139   }
140   callback.Run(targets);
141 }
142
143 scoped_ptr<net::StreamListenSocket>
144 XWalkDevToolsDelegate::CreateSocketForTethering(
145       net::StreamListenSocket::Delegate* delegate,
146       std::string* name) {
147   return scoped_ptr<net::StreamListenSocket>();
148 }
149
150 }  // namespace xwalk