Upstream version 9.38.198.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 std::string GetParentId() const OVERRIDE { return std::string(); }
45   virtual bool IsAttached() const OVERRIDE {
46     return agent_host_->IsAttached();
47   }
48   virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const OVERRIDE {
49     return agent_host_;
50   }
51   virtual bool Activate() const OVERRIDE;
52   virtual bool Close() const OVERRIDE;
53
54  private:
55   scoped_refptr<DevToolsAgentHost> agent_host_;
56   std::string id_;
57   std::string title_;
58   GURL url_;
59   GURL favicon_url_;
60   base::TimeTicks last_activity_time_;
61 };
62
63 Target::Target(WebContents* web_contents) {
64   agent_host_ =
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();
74 }
75
76 bool Target::Activate() const {
77   WebContents* web_contents = agent_host_->GetWebContents();
78   if (!web_contents)
79     return false;
80   web_contents->GetDelegate()->ActivateContents(web_contents);
81   return true;
82 }
83
84 bool Target::Close() const {
85   RenderViewHost* rvh = agent_host_->GetWebContents()->GetRenderViewHost();
86   if (!rvh)
87     return false;
88   rvh->ClosePage();
89   return true;
90 }
91
92 }  // namespace
93
94 namespace xwalk {
95
96 XWalkDevToolsDelegate::XWalkDevToolsDelegate(RuntimeContext* runtime_context)
97     : runtime_context_(runtime_context) {
98 }
99
100 XWalkDevToolsDelegate::~XWalkDevToolsDelegate() {
101 }
102
103 std::string XWalkDevToolsDelegate::GetDiscoveryPageHTML() {
104   return ResourceBundle::GetSharedInstance().GetRawDataResource(
105       IDR_DEVTOOLS_FRONTEND_PAGE_HTML).as_string();
106 }
107
108 bool XWalkDevToolsDelegate::BundlesFrontendResources() {
109   return true;
110 }
111
112 base::FilePath XWalkDevToolsDelegate::GetDebugFrontendDir() {
113   return base::FilePath();
114 }
115
116 std::string XWalkDevToolsDelegate::GetPageThumbnailData(const GURL& url) {
117   return std::string();
118 }
119
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()));
126 }
127
128 void XWalkDevToolsDelegate::EnumerateTargets(TargetCallback callback) {
129   TargetList targets;
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();
134        ++it) {
135     Runtime* runtime = static_cast<Runtime*>((*it)->GetDelegate());
136     if (runtime && runtime->remote_debugging_enabled())
137       targets.push_back(new Target(*it));
138   }
139   callback.Run(targets);
140 }
141
142 scoped_ptr<net::StreamListenSocket>
143 XWalkDevToolsDelegate::CreateSocketForTethering(
144       net::StreamListenSocket::Delegate* delegate,
145       std::string* name) {
146   return scoped_ptr<net::StreamListenSocket>();
147 }
148
149 }  // namespace xwalk