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.
5 #include "chrome/browser/ui/webui/devtools_ui.h"
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"
25 using content::BrowserThread;
26 using content::WebContents;
30 std::string PathWithoutParams(const std::string& path) {
31 return GURL(std::string("chrome-devtools://devtools/") + path)
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";
40 #if defined(DEBUG_DEVTOOLS)
41 // Local frontend url provided by InspectUI.
42 const char kFallbackFrontendURL[] =
43 "chrome-devtools://devtools/bundled/devtools.html";
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)
50 // FetchRequest ---------------------------------------------------------------
52 class FetchRequest : public net::URLFetcherDelegate {
54 FetchRequest(net::URLRequestContextGetter* request_context,
56 const content::URLDataSource::GotDataCallback& callback);
59 virtual ~FetchRequest() {}
60 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
61 scoped_ptr<net::URLFetcher> fetcher_;
62 content::URLDataSource::GotDataCallback callback_;
65 FetchRequest::FetchRequest(
66 net::URLRequestContextGetter* request_context,
68 const content::URLDataSource::GotDataCallback& callback)
69 : callback_(callback) {
70 if (!url.is_valid()) {
71 OnURLFetchComplete(NULL);
75 fetcher_.reset(net::URLFetcher::Create(url, net::URLFetcher::GET, this));
76 fetcher_->SetRequestContext(request_context);
80 void FetchRequest::OnURLFetchComplete(const net::URLFetcher* source) {
83 source->GetResponseAsString(&response);
85 response = kHttpNotFound;
87 callback_.Run(base::RefCountedString::TakeString(&response));
91 // DevToolsDataSource ---------------------------------------------------------
93 std::string GetMimeTypeForPath(const std::string& path) {
94 std::string filename = PathWithoutParams(path);
95 if (EndsWith(filename, ".html", false)) {
97 } else if (EndsWith(filename, ".css", false)) {
99 } else if (EndsWith(filename, ".js", false)) {
100 return "application/javascript";
101 } else if (EndsWith(filename, ".png", false)) {
103 } else if (EndsWith(filename, ".gif", false)) {
105 } else if (EndsWith(filename, ".manifest", false)) {
106 return "text/cache-manifest";
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 {
118 explicit DevToolsDataSource(net::URLRequestContextGetter* request_context);
120 // content::URLDataSource implementation.
121 virtual std::string GetSource() const OVERRIDE;
123 virtual void StartDataRequest(
124 const std::string& path,
125 int render_process_id,
127 const content::URLDataSource::GotDataCallback& callback) OVERRIDE;
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;
135 // Serves bundled DevTools frontend from ResourceBundle.
136 void StartBundledDataRequest(
137 const std::string& path,
138 int render_process_id,
140 const content::URLDataSource::GotDataCallback& callback);
142 // Serves remote DevTools frontend from hard-coded App Engine domain.
143 void StartRemoteDataRequest(
144 const std::string& path,
145 int render_process_id,
147 const content::URLDataSource::GotDataCallback& callback);
149 virtual ~DevToolsDataSource() {}
150 scoped_refptr<net::URLRequestContextGetter> request_context_;
152 DISALLOW_COPY_AND_ASSIGN(DevToolsDataSource);
155 DevToolsDataSource::DevToolsDataSource(
156 net::URLRequestContextGetter* request_context)
157 : request_context_(request_context) {
160 std::string DevToolsDataSource::GetSource() const {
161 return chrome::kChromeUIDevToolsHost;
164 void DevToolsDataSource::StartDataRequest(
165 const std::string& path,
166 int render_process_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);
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);
190 std::string DevToolsDataSource::GetMimeType(const std::string& path) const {
191 return GetMimeTypeForPath(path);
194 bool DevToolsDataSource::ShouldAddContentSecurityPolicy() const {
198 bool DevToolsDataSource::ShouldServeMimeTypeAsContentTypeHeader() const {
202 void DevToolsDataSource::StartBundledDataRequest(
203 const std::string& path,
204 int render_process_id,
206 const content::URLDataSource::GotDataCallback& callback) {
207 std::string filename = PathWithoutParams(path);
210 content::DevToolsHttpHandler::GetFrontendResourceId(filename);
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(
218 callback.Run(bytes.get());
221 void DevToolsDataSource::StartRemoteDataRequest(
222 const std::string& path,
223 int render_process_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);
233 // DevToolsUI -----------------------------------------------------------------
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()));
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(
253 new DevToolsDataSource(profile->GetRequestContext()));