Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / devtools_ui.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 "chrome/browser/ui/webui/devtools_ui.h"
6
7 #include <string>
8
9 #include "base/memory/ref_counted_memory.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/url_constants.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/devtools_http_handler.h"
17 #include "content/public/browser/url_data_source.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/browser/web_ui.h"
20 #include "net/url_request/url_fetcher.h"
21 #include "net/url_request/url_fetcher_delegate.h"
22 #include "net/url_request/url_request_context_getter.h"
23 #include "ui/base/resource/resource_bundle.h"
24
25 using content::BrowserThread;
26 using content::WebContents;
27
28 namespace {
29
30 std::string PathWithoutParams(const std::string& path) {
31   return GURL(std::string("chrome-devtools://devtools/") + path)
32       .path().substr(1);
33 }
34
35 const char kRemoteFrontendDomain[] = "chrome-devtools-frontend.appspot.com";
36 const char kRemoteFrontendBase[] =
37     "https://chrome-devtools-frontend.appspot.com/";
38 const char kHttpNotFound[] = "HTTP/1.1 404 Not Found\n\n";
39
40 #if defined(DEBUG_DEVTOOLS)
41 // Local frontend url provided by InspectUI.
42 const char kFallbackFrontendURL[] =
43     "chrome-devtools://devtools/bundled/devtools.html";
44 #else
45 // URL causing the DevTools window to display a plain text warning.
46 const char kFallbackFrontendURL[] =
47     "data:text/plain,Cannot load DevTools frontend from an untrusted origin";
48 #endif  // defined(DEBUG_DEVTOOLS)
49
50 // FetchRequest ---------------------------------------------------------------
51
52 class FetchRequest : public net::URLFetcherDelegate {
53  public:
54   FetchRequest(net::URLRequestContextGetter* request_context,
55                const GURL& url,
56                const content::URLDataSource::GotDataCallback& callback);
57
58  private:
59   virtual ~FetchRequest() {}
60   virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
61   scoped_ptr<net::URLFetcher> fetcher_;
62   content::URLDataSource::GotDataCallback callback_;
63 };
64
65 FetchRequest::FetchRequest(
66     net::URLRequestContextGetter* request_context,
67     const GURL& url,
68     const content::URLDataSource::GotDataCallback& callback)
69     : callback_(callback) {
70   if (!url.is_valid()) {
71     OnURLFetchComplete(NULL);
72     return;
73   }
74
75   fetcher_.reset(net::URLFetcher::Create(url, net::URLFetcher::GET, this));
76   fetcher_->SetRequestContext(request_context);
77   fetcher_->Start();
78 }
79
80 void FetchRequest::OnURLFetchComplete(const net::URLFetcher* source) {
81   std::string response;
82   if (source)
83     source->GetResponseAsString(&response);
84   else
85     response = kHttpNotFound;
86
87   callback_.Run(base::RefCountedString::TakeString(&response));
88   delete this;
89 }
90
91 // DevToolsDataSource ---------------------------------------------------------
92
93 std::string GetMimeTypeForPath(const std::string& path) {
94   std::string filename = PathWithoutParams(path);
95   if (EndsWith(filename, ".html", false)) {
96     return "text/html";
97   } else if (EndsWith(filename, ".css", false)) {
98     return "text/css";
99   } else if (EndsWith(filename, ".js", false)) {
100     return "application/javascript";
101   } else if (EndsWith(filename, ".png", false)) {
102     return "image/png";
103   } else if (EndsWith(filename, ".gif", false)) {
104     return "image/gif";
105   } else if (EndsWith(filename, ".manifest", false)) {
106     return "text/cache-manifest";
107   }
108   NOTREACHED();
109   return "text/plain";
110 }
111
112 // An URLDataSource implementation that handles chrome-devtools://devtools/
113 // requests. Three types of requests could be handled based on the URL path:
114 // 1. /bundled/: bundled DevTools frontend is served.
115 // 2. /remote/: Remote DevTools frontend is served from App Engine.
116 class DevToolsDataSource : public content::URLDataSource {
117  public:
118   explicit DevToolsDataSource(net::URLRequestContextGetter* request_context);
119
120   // content::URLDataSource implementation.
121   virtual std::string GetSource() const OVERRIDE;
122
123   virtual void StartDataRequest(
124       const std::string& path,
125       int render_process_id,
126       int render_frame_id,
127       const content::URLDataSource::GotDataCallback& callback) OVERRIDE;
128
129  private:
130   // content::URLDataSource overrides.
131   virtual std::string GetMimeType(const std::string& path) const OVERRIDE;
132   virtual bool ShouldAddContentSecurityPolicy() const OVERRIDE;
133   virtual bool ShouldServeMimeTypeAsContentTypeHeader() const OVERRIDE;
134
135   // Serves bundled DevTools frontend from ResourceBundle.
136   void StartBundledDataRequest(
137       const std::string& path,
138       int render_process_id,
139       int render_frame_id,
140       const content::URLDataSource::GotDataCallback& callback);
141
142   // Serves remote DevTools frontend from hard-coded App Engine domain.
143   void StartRemoteDataRequest(
144       const std::string& path,
145       int render_process_id,
146       int render_frame_id,
147       const content::URLDataSource::GotDataCallback& callback);
148
149   virtual ~DevToolsDataSource() {}
150   scoped_refptr<net::URLRequestContextGetter> request_context_;
151
152   DISALLOW_COPY_AND_ASSIGN(DevToolsDataSource);
153 };
154
155 DevToolsDataSource::DevToolsDataSource(
156     net::URLRequestContextGetter* request_context)
157     : request_context_(request_context) {
158 }
159
160 std::string DevToolsDataSource::GetSource() const {
161   return chrome::kChromeUIDevToolsHost;
162 }
163
164 void DevToolsDataSource::StartDataRequest(
165     const std::string& path,
166     int render_process_id,
167     int render_frame_id,
168     const content::URLDataSource::GotDataCallback& callback) {
169   // Serve request from local bundle.
170   std::string bundled_path_prefix(chrome::kChromeUIDevToolsBundledPath);
171   bundled_path_prefix += "/";
172   if (StartsWithASCII(path, bundled_path_prefix, false)) {
173     StartBundledDataRequest(path.substr(bundled_path_prefix.length()),
174                             render_process_id, render_frame_id, callback);
175     return;
176   }
177
178   // Serve request from remote location.
179   std::string remote_path_prefix(chrome::kChromeUIDevToolsRemotePath);
180   remote_path_prefix += "/";
181   if (StartsWithASCII(path, remote_path_prefix, false)) {
182     StartRemoteDataRequest(path.substr(remote_path_prefix.length()),
183                            render_process_id, render_frame_id, callback);
184     return;
185   }
186
187   callback.Run(NULL);
188 }
189
190 std::string DevToolsDataSource::GetMimeType(const std::string& path) const {
191   return GetMimeTypeForPath(path);
192 }
193
194 bool DevToolsDataSource::ShouldAddContentSecurityPolicy() const {
195   return false;
196 }
197
198 bool DevToolsDataSource::ShouldServeMimeTypeAsContentTypeHeader() const {
199   return true;
200 }
201
202 void DevToolsDataSource::StartBundledDataRequest(
203     const std::string& path,
204     int render_process_id,
205     int render_frame_id,
206     const content::URLDataSource::GotDataCallback& callback) {
207   std::string filename = PathWithoutParams(path);
208
209   int resource_id =
210       content::DevToolsHttpHandler::GetFrontendResourceId(filename);
211
212   DLOG_IF(WARNING, -1 == resource_id) << "Unable to find dev tool resource: "
213       << filename << ". If you compiled with debug_devtools=1, try running"
214       " with --debug-devtools.";
215   const ResourceBundle& rb = ResourceBundle::GetSharedInstance();
216   scoped_refptr<base::RefCountedStaticMemory> bytes(rb.LoadDataResourceBytes(
217       resource_id));
218   callback.Run(bytes.get());
219 }
220
221 void DevToolsDataSource::StartRemoteDataRequest(
222     const std::string& path,
223     int render_process_id,
224     int render_frame_id,
225     const content::URLDataSource::GotDataCallback& callback) {
226   GURL url = GURL(kRemoteFrontendBase + path);
227   CHECK_EQ(url.host(), kRemoteFrontendDomain);
228   new FetchRequest(request_context_, url, callback);
229 }
230
231 }  // namespace
232
233 // DevToolsUI -----------------------------------------------------------------
234
235 // static
236 GURL DevToolsUI::GetProxyURL(const std::string& frontend_url) {
237   GURL url(frontend_url);
238   if (!url.is_valid() || url.host() != kRemoteFrontendDomain)
239     return GURL(kFallbackFrontendURL);
240   return GURL(base::StringPrintf("%s://%s/%s/%s",
241               content::kChromeDevToolsScheme,
242               chrome::kChromeUIDevToolsHost,
243               chrome::kChromeUIDevToolsRemotePath,
244               url.path().substr(1).c_str()));
245 }
246
247 DevToolsUI::DevToolsUI(content::WebUI* web_ui)
248     : WebUIController(web_ui) {
249   web_ui->SetBindings(0);
250   Profile* profile = Profile::FromWebUI(web_ui);
251   content::URLDataSource::Add(
252       profile,
253       new DevToolsDataSource(profile->GetRequestContext()));
254 }