OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2006-2008 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/extensions/extension_protocol.h" |
| 6 |
| 7 #include "base/string_util.h" |
| 8 #include "chrome/browser/net/chrome_url_request_context.h" |
| 9 #include "googleurl/src/url_util.h" |
| 10 #include "net/base/net_util.h" |
| 11 #include "net/url_request/url_request_file_job.h" |
| 12 |
| 13 static const char kExtensionURLScheme[] = "chrome-extension"; |
| 14 |
| 15 FilePath GetPathForExtensionResource(const FilePath& extension_path, |
| 16 const std::string& url_path) { |
| 17 DCHECK(extension_path.IsAbsolute()); |
| 18 DCHECK(url_path.length() > 0 && url_path[0] == '/'); |
| 19 |
| 20 // Build up a file:// URL and convert that back to a FilePath. This avoids |
| 21 // URL encoding and path separator issues. |
| 22 |
| 23 // Convert the extension's root to a file:// URL. |
| 24 GURL extension_url = net::FilePathToFileURL(extension_path); |
| 25 if (!extension_url.is_valid()) |
| 26 return FilePath(); |
| 27 |
| 28 // Append the requested path. |
| 29 GURL::Replacements replacements; |
| 30 std::string new_path(extension_url.path()); |
| 31 new_path += url_path; |
| 32 replacements.SetPathStr(new_path); |
| 33 GURL file_url = extension_url.ReplaceComponents(replacements); |
| 34 if (!file_url.is_valid()) |
| 35 return FilePath(); |
| 36 |
| 37 // Convert the result back to a FilePath. |
| 38 FilePath ret_val; |
| 39 if (!net::FileURLToFilePath(file_url, &ret_val)) |
| 40 return FilePath(); |
| 41 |
| 42 // Double-check that the path we ended up with is actually inside the |
| 43 // extension root. |
| 44 if (!extension_path.Contains(ret_val)) |
| 45 return FilePath(); |
| 46 |
| 47 return ret_val; |
| 48 } |
| 49 |
| 50 // Creates a URLRequestJob instance for an extension URL. This is the factory |
| 51 // function that is registered with URLRequest. |
| 52 static URLRequestJob* CreateURLRequestJob(URLRequest* request, |
| 53 const std::string& scheme) { |
| 54 ChromeURLRequestContext* context = |
| 55 static_cast<ChromeURLRequestContext*>(request->context()); |
| 56 |
| 57 FilePath extension_path = context->GetPathForExtension(request->url().host()); |
| 58 if (extension_path.value().empty()) |
| 59 return NULL; |
| 60 |
| 61 FilePath path = GetPathForExtensionResource(extension_path, |
| 62 request->url().path()); |
| 63 if (path.value().empty()) |
| 64 return NULL; |
| 65 |
| 66 return new URLRequestFileJob(request, path); |
| 67 } |
| 68 |
| 69 void RegisterExtensionProtocol() { |
| 70 // Being a standard scheme allows us to resolve relative paths |
| 71 url_util::AddStandardScheme(kExtensionURLScheme); |
| 72 |
| 73 URLRequest::RegisterProtocolFactory(kExtensionURLScheme, |
| 74 &CreateURLRequestJob); |
| 75 } |
OLD | NEW |