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" |
8 #include "chrome/browser/net/chrome_url_request_context.h" | 9 #include "chrome/browser/net/chrome_url_request_context.h" |
9 #include "googleurl/src/url_util.h" | 10 #include "googleurl/src/url_util.h" |
10 #include "net/base/net_util.h" | 11 #include "net/base/net_util.h" |
11 #include "net/url_request/url_request_file_job.h" | 12 #include "net/url_request/url_request_file_job.h" |
12 | 13 |
13 const char kExtensionURLScheme[] = "chrome-extension"; | 14 const char kExtensionURLScheme[] = "chrome-extension"; |
14 const char kUserScriptURLScheme[] = "chrome-user-script"; | 15 const char kUserScriptURLScheme[] = "chrome-user-script"; |
15 | 16 |
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 | |
54 // Factory registered with URLRequest to create URLRequestJobs for extension:// | 17 // Factory registered with URLRequest to create URLRequestJobs for extension:// |
55 // URLs. | 18 // URLs. |
56 static URLRequestJob* CreateExtensionURLRequestJob(URLRequest* request, | 19 static URLRequestJob* CreateExtensionURLRequestJob(URLRequest* request, |
57 const std::string& scheme) { | 20 const std::string& scheme) { |
58 ChromeURLRequestContext* context = | 21 ChromeURLRequestContext* context = |
59 static_cast<ChromeURLRequestContext*>(request->context()); | 22 static_cast<ChromeURLRequestContext*>(request->context()); |
60 | 23 |
61 // chrome-extension://extension-id/resource/path.js | 24 // chrome-extension://extension-id/resource/path.js |
62 FilePath directory_path = context->GetPathForExtension(request->url().host()); | 25 FilePath directory_path = context->GetPathForExtension(request->url().host()); |
63 if (directory_path.value().empty()) | 26 if (directory_path.value().empty()) |
64 return NULL; | 27 return NULL; |
65 | 28 |
66 std::string resource = request->url().path(); | 29 std::string resource = request->url().path(); |
67 FilePath path = GetPathForExtensionResource(directory_path, resource); | 30 FilePath path = Extension::GetResourcePath(directory_path, resource); |
68 | 31 |
69 return new URLRequestFileJob(request, path); | 32 return new URLRequestFileJob(request, path); |
70 } | 33 } |
71 | 34 |
72 // Factory registered with URLRequest to create URLRequestJobs for | 35 // Factory registered with URLRequest to create URLRequestJobs for |
73 // chrome-user-script:/ URLs. | 36 // chrome-user-script:/ URLs. |
74 static URLRequestJob* CreateUserScriptURLRequestJob(URLRequest* request, | 37 static URLRequestJob* CreateUserScriptURLRequestJob(URLRequest* request, |
75 const std::string& scheme) { | 38 const std::string& scheme) { |
76 ChromeURLRequestContext* context = | 39 ChromeURLRequestContext* context = |
77 static_cast<ChromeURLRequestContext*>(request->context()); | 40 static_cast<ChromeURLRequestContext*>(request->context()); |
78 | 41 |
79 // chrome-user-script:/user-script-name.user.js | 42 // chrome-user-script:/user-script-name.user.js |
80 FilePath directory_path = context->user_script_dir_path(); | 43 FilePath directory_path = context->user_script_dir_path(); |
81 std::string resource = request->url().path(); | 44 std::string resource = request->url().path(); |
82 | 45 |
83 FilePath path = GetPathForExtensionResource(directory_path, resource); | 46 FilePath path = Extension::GetResourcePath(directory_path, resource); |
84 return new URLRequestFileJob(request, path); | 47 return new URLRequestFileJob(request, path); |
85 } | 48 } |
86 | 49 |
87 void RegisterExtensionProtocols() { | 50 void RegisterExtensionProtocols() { |
88 // Being a standard scheme allows us to resolve relative paths. This is used | 51 // Being a standard scheme allows us to resolve relative paths. This is used |
89 // by extensions, but not by standalone user scripts. | 52 // by extensions, but not by standalone user scripts. |
90 url_util::AddStandardScheme(kExtensionURLScheme); | 53 url_util::AddStandardScheme(kExtensionURLScheme); |
91 | 54 |
92 URLRequest::RegisterProtocolFactory(kExtensionURLScheme, | 55 URLRequest::RegisterProtocolFactory(kExtensionURLScheme, |
93 &CreateExtensionURLRequestJob); | 56 &CreateExtensionURLRequestJob); |
94 URLRequest::RegisterProtocolFactory(kUserScriptURLScheme, | 57 URLRequest::RegisterProtocolFactory(kUserScriptURLScheme, |
95 &CreateUserScriptURLRequestJob); | 58 &CreateUserScriptURLRequestJob); |
96 } | 59 } |
OLD | NEW |