| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/extension_protocols.h" | 5 #include "chrome/browser/extensions/extension_protocols.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "chrome/browser/extensions/extension.h" | |
| 9 #include "chrome/browser/net/chrome_url_request_context.h" | 8 #include "chrome/browser/net/chrome_url_request_context.h" |
| 10 #include "googleurl/src/url_util.h" | 9 #include "googleurl/src/url_util.h" |
| 11 #include "net/base/net_util.h" | 10 #include "net/base/net_util.h" |
| 12 #include "net/url_request/url_request_file_job.h" | 11 #include "net/url_request/url_request_file_job.h" |
| 13 | 12 |
| 14 const char kExtensionURLScheme[] = "chrome-extension"; | 13 const char kExtensionURLScheme[] = "chrome-extension"; |
| 15 const char kUserScriptURLScheme[] = "chrome-user-script"; | 14 const char kUserScriptURLScheme[] = "chrome-user-script"; |
| 16 | 15 |
| 16 FilePath GetPathForExtensionResource(const FilePath& extension_path, |
| 17 const std::string& url_path) { |
| 18 DCHECK(extension_path.IsAbsolute()); |
| 19 DCHECK(url_path.length() > 0 && url_path[0] == '/'); |
| 20 |
| 21 // Build up a file:// URL and convert that back to a FilePath. This avoids |
| 22 // URL encoding and path separator issues. |
| 23 |
| 24 // Convert the extension's root to a file:// URL. |
| 25 GURL extension_url = net::FilePathToFileURL(extension_path); |
| 26 if (!extension_url.is_valid()) |
| 27 return FilePath(); |
| 28 |
| 29 // Append the requested path. |
| 30 GURL::Replacements replacements; |
| 31 std::string new_path(extension_url.path()); |
| 32 new_path += url_path; |
| 33 replacements.SetPathStr(new_path); |
| 34 GURL file_url = extension_url.ReplaceComponents(replacements); |
| 35 if (!file_url.is_valid()) |
| 36 return FilePath(); |
| 37 |
| 38 // Convert the result back to a FilePath. |
| 39 FilePath ret_val; |
| 40 if (!net::FileURLToFilePath(file_url, &ret_val)) |
| 41 return FilePath(); |
| 42 |
| 43 // Double-check that the path we ended up with is actually inside the |
| 44 // extension root. We can do this with a simple prefix match because: |
| 45 // a) We control the prefix on both sides, and they should match. |
| 46 // b) GURL normalizes things like "../" and "//" before it gets to us. |
| 47 if (ret_val.value().find(extension_path.value() + |
| 48 FilePath::kSeparators[0]) != 0) |
| 49 return FilePath(); |
| 50 |
| 51 return ret_val; |
| 52 } |
| 53 |
| 17 // Factory registered with URLRequest to create URLRequestJobs for extension:// | 54 // Factory registered with URLRequest to create URLRequestJobs for extension:// |
| 18 // URLs. | 55 // URLs. |
| 19 static URLRequestJob* CreateExtensionURLRequestJob(URLRequest* request, | 56 static URLRequestJob* CreateExtensionURLRequestJob(URLRequest* request, |
| 20 const std::string& scheme) { | 57 const std::string& scheme) { |
| 21 ChromeURLRequestContext* context = | 58 ChromeURLRequestContext* context = |
| 22 static_cast<ChromeURLRequestContext*>(request->context()); | 59 static_cast<ChromeURLRequestContext*>(request->context()); |
| 23 | 60 |
| 24 // chrome-extension://extension-id/resource/path.js | 61 // chrome-extension://extension-id/resource/path.js |
| 25 FilePath directory_path = context->GetPathForExtension(request->url().host()); | 62 FilePath directory_path = context->GetPathForExtension(request->url().host()); |
| 26 if (directory_path.value().empty()) | 63 if (directory_path.value().empty()) |
| 27 return NULL; | 64 return NULL; |
| 28 | 65 |
| 29 std::string resource = request->url().path(); | 66 std::string resource = request->url().path(); |
| 30 FilePath path = Extension::GetResourcePath(directory_path, resource); | 67 FilePath path = GetPathForExtensionResource(directory_path, resource); |
| 31 | 68 |
| 32 return new URLRequestFileJob(request, path); | 69 return new URLRequestFileJob(request, path); |
| 33 } | 70 } |
| 34 | 71 |
| 35 // Factory registered with URLRequest to create URLRequestJobs for | 72 // Factory registered with URLRequest to create URLRequestJobs for |
| 36 // chrome-user-script:/ URLs. | 73 // chrome-user-script:/ URLs. |
| 37 static URLRequestJob* CreateUserScriptURLRequestJob(URLRequest* request, | 74 static URLRequestJob* CreateUserScriptURLRequestJob(URLRequest* request, |
| 38 const std::string& scheme) { | 75 const std::string& scheme) { |
| 39 ChromeURLRequestContext* context = | 76 ChromeURLRequestContext* context = |
| 40 static_cast<ChromeURLRequestContext*>(request->context()); | 77 static_cast<ChromeURLRequestContext*>(request->context()); |
| 41 | 78 |
| 42 // chrome-user-script:/user-script-name.user.js | 79 // chrome-user-script:/user-script-name.user.js |
| 43 FilePath directory_path = context->user_script_dir_path(); | 80 FilePath directory_path = context->user_script_dir_path(); |
| 44 std::string resource = request->url().path(); | 81 std::string resource = request->url().path(); |
| 45 | 82 |
| 46 FilePath path = Extension::GetResourcePath(directory_path, resource); | 83 FilePath path = GetPathForExtensionResource(directory_path, resource); |
| 47 return new URLRequestFileJob(request, path); | 84 return new URLRequestFileJob(request, path); |
| 48 } | 85 } |
| 49 | 86 |
| 50 void RegisterExtensionProtocols() { | 87 void RegisterExtensionProtocols() { |
| 51 // Being a standard scheme allows us to resolve relative paths. This is used | 88 // Being a standard scheme allows us to resolve relative paths. This is used |
| 52 // by extensions, but not by standalone user scripts. | 89 // by extensions, but not by standalone user scripts. |
| 53 url_util::AddStandardScheme(kExtensionURLScheme); | 90 url_util::AddStandardScheme(kExtensionURLScheme); |
| 54 | 91 |
| 55 URLRequest::RegisterProtocolFactory(kExtensionURLScheme, | 92 URLRequest::RegisterProtocolFactory(kExtensionURLScheme, |
| 56 &CreateExtensionURLRequestJob); | 93 &CreateExtensionURLRequestJob); |
| 57 URLRequest::RegisterProtocolFactory(kUserScriptURLScheme, | 94 URLRequest::RegisterProtocolFactory(kUserScriptURLScheme, |
| 58 &CreateUserScriptURLRequestJob); | 95 &CreateUserScriptURLRequestJob); |
| 59 } | 96 } |
| OLD | NEW |