Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / shell / browser / shell_devtools_delegate.cc
1 // Copyright 2013 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 "content/shell/browser/shell_devtools_delegate.h"
6
7 #include <vector>
8
9 #include "base/bind.h"
10 #include "base/command_line.h"
11 #include "base/files/file_path.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "content/public/browser/devtools_agent_host.h"
16 #include "content/public/browser/devtools_http_handler.h"
17 #include "content/public/browser/devtools_target.h"
18 #include "content/public/browser/favicon_status.h"
19 #include "content/public/browser/navigation_entry.h"
20 #include "content/public/browser/render_view_host.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/common/content_switches.h"
23 #include "content/public/common/url_constants.h"
24 #include "content/public/common/user_agent.h"
25 #include "content/shell/browser/shell.h"
26 #include "grit/shell_resources.h"
27 #include "net/socket/tcp_listen_socket.h"
28 #include "ui/base/resource/resource_bundle.h"
29
30 #if defined(OS_ANDROID)
31 #include "content/public/browser/android/devtools_auth.h"
32 #include "net/socket/unix_domain_listen_socket_posix.h"
33 #endif
34
35 using content::DevToolsAgentHost;
36 using content::RenderViewHost;
37 using content::WebContents;
38
39 namespace {
40
41 #if defined(OS_ANDROID)
42 const char kFrontEndURL[] =
43     "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/devtools.html";
44 #endif
45 const char kTargetTypePage[] = "page";
46
47 net::StreamListenSocketFactory* CreateSocketFactory() {
48   const CommandLine& command_line = *CommandLine::ForCurrentProcess();
49 #if defined(OS_ANDROID)
50   std::string socket_name = "content_shell_devtools_remote";
51   if (command_line.HasSwitch(switches::kRemoteDebuggingSocketName)) {
52     socket_name = command_line.GetSwitchValueASCII(
53         switches::kRemoteDebuggingSocketName);
54   }
55   return new net::deprecated::
56       UnixDomainListenSocketWithAbstractNamespaceFactory(
57           socket_name, "", base::Bind(&content::CanUserConnectToDevTools));
58 #else
59   // See if the user specified a port on the command line (useful for
60   // automation). If not, use an ephemeral port by specifying 0.
61   int port = 0;
62   if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) {
63     int temp_port;
64     std::string port_str =
65         command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort);
66     if (base::StringToInt(port_str, &temp_port) &&
67         temp_port > 0 && temp_port < 65535) {
68       port = temp_port;
69     } else {
70       DLOG(WARNING) << "Invalid http debugger port number " << temp_port;
71     }
72   }
73   return new net::TCPListenSocketFactory("127.0.0.1", port);
74 #endif
75 }
76
77 class Target : public content::DevToolsTarget {
78  public:
79   explicit Target(WebContents* web_contents);
80
81   virtual std::string GetId() const OVERRIDE { return id_; }
82   virtual std::string GetParentId() const OVERRIDE { return std::string(); }
83   virtual std::string GetType() const OVERRIDE { return kTargetTypePage; }
84   virtual std::string GetTitle() const OVERRIDE { return title_; }
85   virtual std::string GetDescription() const OVERRIDE { return std::string(); }
86   virtual GURL GetURL() const OVERRIDE { return url_; }
87   virtual GURL GetFaviconURL() const OVERRIDE { return favicon_url_; }
88   virtual base::TimeTicks GetLastActivityTime() const OVERRIDE {
89     return last_activity_time_;
90   }
91   virtual bool IsAttached() const OVERRIDE {
92     return agent_host_->IsAttached();
93   }
94   virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const OVERRIDE {
95     return agent_host_;
96   }
97   virtual bool Activate() const OVERRIDE;
98   virtual bool Close() const OVERRIDE;
99
100  private:
101   scoped_refptr<DevToolsAgentHost> agent_host_;
102   std::string id_;
103   std::string title_;
104   GURL url_;
105   GURL favicon_url_;
106   base::TimeTicks last_activity_time_;
107 };
108
109 Target::Target(WebContents* web_contents) {
110   agent_host_ = DevToolsAgentHost::GetOrCreateFor(web_contents);
111   id_ = agent_host_->GetId();
112   title_ = base::UTF16ToUTF8(web_contents->GetTitle());
113   url_ = web_contents->GetURL();
114   content::NavigationController& controller = web_contents->GetController();
115   content::NavigationEntry* entry = controller.GetActiveEntry();
116   if (entry != NULL && entry->GetURL().is_valid())
117     favicon_url_ = entry->GetFavicon().url;
118   last_activity_time_ = web_contents->GetLastActiveTime();
119 }
120
121 bool Target::Activate() const {
122   WebContents* web_contents = agent_host_->GetWebContents();
123   if (!web_contents)
124     return false;
125   web_contents->GetDelegate()->ActivateContents(web_contents);
126   return true;
127 }
128
129 bool Target::Close() const {
130   WebContents* web_contents = agent_host_->GetWebContents();
131   if (!web_contents)
132     return false;
133   web_contents->GetRenderViewHost()->ClosePage();
134   return true;
135 }
136
137 }  // namespace
138
139 namespace content {
140
141 ShellDevToolsDelegate::ShellDevToolsDelegate(BrowserContext* browser_context)
142     : browser_context_(browser_context) {
143   std::string frontend_url;
144 #if defined(OS_ANDROID)
145   frontend_url = base::StringPrintf(kFrontEndURL, GetWebKitRevision().c_str());
146 #endif
147   devtools_http_handler_ =
148       DevToolsHttpHandler::Start(CreateSocketFactory(), frontend_url, this,
149                                  base::FilePath());
150 }
151
152 ShellDevToolsDelegate::~ShellDevToolsDelegate() {
153 }
154
155 void ShellDevToolsDelegate::Stop() {
156   // The call below destroys this.
157   devtools_http_handler_->Stop();
158 }
159
160 std::string ShellDevToolsDelegate::GetDiscoveryPageHTML() {
161 #if defined(OS_ANDROID)
162   return std::string();
163 #else
164   return ResourceBundle::GetSharedInstance().GetRawDataResource(
165       IDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE).as_string();
166 #endif
167 }
168
169 bool ShellDevToolsDelegate::BundlesFrontendResources() {
170 #if defined(OS_ANDROID)
171   return false;
172 #else
173   return true;
174 #endif
175 }
176
177 base::FilePath ShellDevToolsDelegate::GetDebugFrontendDir() {
178   return base::FilePath();
179 }
180
181 std::string ShellDevToolsDelegate::GetPageThumbnailData(const GURL& url) {
182   return std::string();
183 }
184
185 scoped_ptr<DevToolsTarget>
186 ShellDevToolsDelegate::CreateNewTarget(const GURL& url) {
187   Shell* shell = Shell::CreateNewWindow(browser_context_,
188                                         url,
189                                         NULL,
190                                         MSG_ROUTING_NONE,
191                                         gfx::Size());
192   return scoped_ptr<DevToolsTarget>(new Target(shell->web_contents()));
193 }
194
195 void ShellDevToolsDelegate::EnumerateTargets(TargetCallback callback) {
196   TargetList targets;
197   std::vector<WebContents*> wc_list =
198       content::DevToolsAgentHost::GetInspectableWebContents();
199   for (std::vector<WebContents*>::iterator it = wc_list.begin();
200        it != wc_list.end();
201        ++it) {
202     targets.push_back(new Target(*it));
203   }
204   callback.Run(targets);
205 }
206
207 scoped_ptr<net::StreamListenSocket>
208 ShellDevToolsDelegate::CreateSocketForTethering(
209     net::StreamListenSocket::Delegate* delegate,
210     std::string* name) {
211   return scoped_ptr<net::StreamListenSocket>();
212 }
213
214 }  // namespace content