diff options
author | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-08 21:47:41 +0000 |
---|---|---|
committer | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-08 21:47:41 +0000 |
commit | 8f704c814eb2f513ead209887f38c55eea34b636 (patch) | |
tree | 28452545c323d7b91f1abcbf44680047755940e6 /chrome/browser/extensions | |
parent | cdc1e9c09df101960cf95600e97c49936e929c2c (diff) | |
download | chromium_src-8f704c814eb2f513ead209887f38c55eea34b636.zip chromium_src-8f704c814eb2f513ead209887f38c55eea34b636.tar.gz chromium_src-8f704c814eb2f513ead209887f38c55eea34b636.tar.bz2 |
Add chrome-user-script:// protocol
This is a step towards getting user scripts working in extensions. It's a bit
janky to use the form chrome-user-script://<script file>/ (with no path), but
GURL assumes that there is always a host, but path is optional, making this
approach simpler than alternatives.
Review URL: http://codereview.chromium.org/16592
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@7759 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions')
-rw-r--r-- | chrome/browser/extensions/extension_protocol.cc | 44 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_protocol.h | 7 |
2 files changed, 37 insertions, 14 deletions
diff --git a/chrome/browser/extensions/extension_protocol.cc b/chrome/browser/extensions/extension_protocol.cc index cb1ee09..6eda9ef 100644 --- a/chrome/browser/extensions/extension_protocol.cc +++ b/chrome/browser/extensions/extension_protocol.cc @@ -11,6 +11,7 @@ #include "net/url_request/url_request_file_job.h" static const char kExtensionURLScheme[] = "chrome-extension"; +static const char kUserScriptURLScheme[] = "chrome-user-script"; FilePath GetPathForExtensionResource(const FilePath& extension_path, const std::string& url_path) { @@ -50,29 +51,46 @@ FilePath GetPathForExtensionResource(const FilePath& extension_path, return ret_val; } -// Creates a URLRequestJob instance for an extension URL. This is the factory -// function that is registered with URLRequest. -static URLRequestJob* CreateURLRequestJob(URLRequest* request, - const std::string& scheme) { +// Factory registered with URLRequest to create URLRequestJobs for extension:// +// URLs. +static URLRequestJob* CreateExtensionURLRequestJob(URLRequest* request, + const std::string& scheme) { ChromeURLRequestContext* context = static_cast<ChromeURLRequestContext*>(request->context()); - FilePath extension_path = context->GetPathForExtension(request->url().host()); - if (extension_path.value().empty()) + // chrome-extension://extension-id/resource/path.js + FilePath directory_path = context->GetPathForExtension(request->url().host()); + if (directory_path.value().empty()) return NULL; - FilePath path = GetPathForExtensionResource(extension_path, - request->url().path()); - if (path.value().empty()) - return NULL; + std::string resource = request->url().path(); + FilePath path = GetPathForExtensionResource(directory_path, resource); + + return new URLRequestFileJob(request, path); +} + +// Factory registered with URLRequest to create URLRequestJobs for +// chrome-user-script:/ URLs. +static URLRequestJob* CreateUserScriptURLRequestJob(URLRequest* request, + const std::string& scheme) { + ChromeURLRequestContext* context = + static_cast<ChromeURLRequestContext*>(request->context()); + + // chrome-user-script:/user-script-name.user.js + FilePath directory_path = context->user_script_dir_path(); + std::string resource = request->url().path(); + FilePath path = GetPathForExtensionResource(directory_path, resource); return new URLRequestFileJob(request, path); } -void RegisterExtensionProtocol() { - // Being a standard scheme allows us to resolve relative paths +void RegisterExtensionProtocols() { + // Being a standard scheme allows us to resolve relative paths. This is used + // by extensions, but not by standalone user scripts. url_util::AddStandardScheme(kExtensionURLScheme); URLRequest::RegisterProtocolFactory(kExtensionURLScheme, - &CreateURLRequestJob); + &CreateExtensionURLRequestJob); + URLRequest::RegisterProtocolFactory(kUserScriptURLScheme, + &CreateUserScriptURLRequestJob); } diff --git a/chrome/browser/extensions/extension_protocol.h b/chrome/browser/extensions/extension_protocol.h index 54254db..49bda17 100644 --- a/chrome/browser/extensions/extension_protocol.h +++ b/chrome/browser/extensions/extension_protocol.h @@ -2,6 +2,9 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_PROTOCOL_H_ +#define CHROME_BROWSER_EXTENSIONS_EXTENSION_PROTOCOL_H_ + #include "base/file_path.h" // Gets a FilePath for a resource inside an extension. |extension_path| is the @@ -11,4 +14,6 @@ FilePath GetPathForExtensionResource(const FilePath& extension_path, const std::string& resource_path); // Registers support for the extension URL scheme. -void RegisterExtensionProtocol(); +void RegisterExtensionProtocols(); + +#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PROTOCOL_H_ |