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