Upstream version 10.39.225.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 const char kTargetTypeServiceWorker[] = "service_worker";
31 const char kTargetTypeOther[] = "other";
32
33 class Target : public content::DevToolsTarget {
34  public:
35   explicit Target(scoped_refptr<content::DevToolsAgentHost> agent_host);
36
37   virtual std::string GetId() const OVERRIDE { return agent_host_->GetId(); }
38   virtual std::string GetType() const OVERRIDE {
39       switch (agent_host_->GetType()) {
40         case content::DevToolsAgentHost::TYPE_WEB_CONTENTS:
41            return kTargetTypePage;
42          case content::DevToolsAgentHost::TYPE_SERVICE_WORKER:
43            return kTargetTypeServiceWorker;
44          default:
45            break;
46        }
47        return kTargetTypeOther;
48      }
49   virtual std::string GetTitle() const OVERRIDE {
50     return agent_host_->GetTitle();
51   }
52   virtual std::string GetDescription() const OVERRIDE { return std::string(); }
53   virtual GURL GetURL() const OVERRIDE { return url_; }
54   virtual GURL GetFaviconURL() const OVERRIDE { return favicon_url_; }
55   virtual base::TimeTicks GetLastActivityTime() const OVERRIDE {
56     return last_activity_time_;
57   }
58   virtual std::string GetParentId() const OVERRIDE { return std::string(); }
59   virtual bool IsAttached() const OVERRIDE {
60     return agent_host_->IsAttached();
61   }
62   virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const OVERRIDE {
63     return agent_host_;
64   }
65   virtual bool Activate() const OVERRIDE;
66   virtual bool Close() const OVERRIDE;
67
68  private:
69   scoped_refptr<DevToolsAgentHost> agent_host_;
70   std::string id_;
71   std::string title_;
72   GURL url_;
73   GURL favicon_url_;
74   base::TimeTicks last_activity_time_;
75 };
76
77 Target::Target(scoped_refptr<content::DevToolsAgentHost> agent_host)
78     : agent_host_(agent_host) {
79   if (content::WebContents* web_contents = agent_host_->GetWebContents()) {
80     content::NavigationController& controller = web_contents->GetController();
81     content::NavigationEntry* entry = controller.GetActiveEntry();
82     if (entry != NULL && entry->GetURL().is_valid())
83       favicon_url_ = entry->GetFavicon().url;
84     last_activity_time_ = web_contents->GetLastActiveTime();
85   }
86 }
87
88 bool Target::Activate() const {
89   WebContents* web_contents = agent_host_->GetWebContents();
90   if (!web_contents)
91     return false;
92   web_contents->GetDelegate()->ActivateContents(web_contents);
93   return true;
94 }
95
96 bool Target::Close() const {
97   RenderViewHost* rvh = agent_host_->GetWebContents()->GetRenderViewHost();
98   if (!rvh)
99     return false;
100   rvh->ClosePage();
101   return true;
102 }
103
104 }  // namespace
105
106 namespace xwalk {
107
108 XWalkDevToolsHttpHandlerDelegate::XWalkDevToolsHttpHandlerDelegate() {
109 }
110
111 XWalkDevToolsHttpHandlerDelegate::~XWalkDevToolsHttpHandlerDelegate() {
112 }
113
114 std::string XWalkDevToolsHttpHandlerDelegate::GetDiscoveryPageHTML() {
115   return ResourceBundle::GetSharedInstance().GetRawDataResource(
116       IDR_DEVTOOLS_FRONTEND_PAGE_HTML).as_string();
117 }
118
119 bool XWalkDevToolsHttpHandlerDelegate::BundlesFrontendResources() {
120   return true;
121 }
122
123 base::FilePath XWalkDevToolsHttpHandlerDelegate::GetDebugFrontendDir() {
124   return base::FilePath();
125 }
126
127 scoped_ptr<net::StreamListenSocket>
128 XWalkDevToolsHttpHandlerDelegate::CreateSocketForTethering(
129     net::StreamListenSocket::Delegate* delegate,
130     std::string* name) {
131   return scoped_ptr<net::StreamListenSocket>();
132 }
133
134 XWalkDevToolsDelegate::XWalkDevToolsDelegate(RuntimeContext* runtime_context)
135     : runtime_context_(runtime_context) {
136 }
137
138 XWalkDevToolsDelegate::~XWalkDevToolsDelegate() {
139 }
140
141 base::DictionaryValue* XWalkDevToolsDelegate::HandleCommand(
142     content::DevToolsAgentHost* agent_host,
143     base::DictionaryValue* command_dict) {
144   return NULL;
145 }
146
147 std::string XWalkDevToolsDelegate::GetPageThumbnailData(const GURL& url) {
148   return std::string();
149 }
150
151 scoped_ptr<content::DevToolsTarget>
152 XWalkDevToolsDelegate::CreateNewTarget(const GURL& url) {
153   Runtime* runtime = Runtime::CreateWithDefaultWindow(
154       runtime_context_, GURL(url::kAboutBlankURL));
155   return scoped_ptr<content::DevToolsTarget>(
156       new Target(DevToolsAgentHost::GetOrCreateFor(runtime->web_contents())));
157 }
158
159 void XWalkDevToolsDelegate::EnumerateTargets(TargetCallback callback) {
160   TargetList targets;
161     content::DevToolsAgentHost::List agents =
162         content::DevToolsAgentHost::GetOrCreateAll();
163     for (content::DevToolsAgentHost::List::iterator it = agents.begin();
164          it != agents.end(); ++it) {
165     Runtime* runtime =
166         static_cast<Runtime*>((*it)->GetWebContents()->GetDelegate());
167     if (runtime && runtime->remote_debugging_enabled())
168       targets.push_back(new Target(*it));
169   }
170   callback.Run(targets);
171 }
172
173 }  // namespace xwalk