summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-01-08 21:47:41 +0000
committeraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-01-08 21:47:41 +0000
commit8f704c814eb2f513ead209887f38c55eea34b636 (patch)
tree28452545c323d7b91f1abcbf44680047755940e6
parentcdc1e9c09df101960cf95600e97c49936e929c2c (diff)
downloadchromium_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
-rw-r--r--chrome/browser/browser_main.cc2
-rw-r--r--chrome/browser/extensions/extension_protocol.cc44
-rw-r--r--chrome/browser/extensions/extension_protocol.h7
-rw-r--r--chrome/browser/greasemonkey_master.h4
-rw-r--r--chrome/browser/net/chrome_url_request_context.cc3
-rw-r--r--chrome/browser/net/chrome_url_request_context.h8
6 files changed, 53 insertions, 15 deletions
diff --git a/chrome/browser/browser_main.cc b/chrome/browser/browser_main.cc
index 21be5b6..d9c3e02 100644
--- a/chrome/browser/browser_main.cc
+++ b/chrome/browser/browser_main.cc
@@ -406,7 +406,7 @@ int BrowserMain(CommandLine &parsed_command_line,
// Register our global network handler for chrome:// and chrome-extension://
// URLs.
RegisterURLRequestChromeJob();
- RegisterExtensionProtocol();
+ RegisterExtensionProtocols();
browser_process->InitBrokerServices(broker_services);
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_
diff --git a/chrome/browser/greasemonkey_master.h b/chrome/browser/greasemonkey_master.h
index cc1c92b..34121dd 100644
--- a/chrome/browser/greasemonkey_master.h
+++ b/chrome/browser/greasemonkey_master.h
@@ -6,6 +6,7 @@
#define CHROME_BROWSER_GREASEMONKEY_MASTER_H_
#include "base/directory_watcher.h"
+#include "base/file_path.h"
#include "base/process.h"
#include "base/scoped_ptr.h"
#include "base/shared_memory.h"
@@ -34,6 +35,9 @@ class GreasemonkeyMaster : public base::RefCounted<GreasemonkeyMaster>,
// Return true if we have any scripts ready.
bool ScriptsReady() const { return shared_memory_.get() != NULL; }
+ // Returns the path to the directory user scripts are stored in.
+ FilePath user_script_dir() const { return *user_script_dir_; }
+
private:
class ScriptReloader;
diff --git a/chrome/browser/net/chrome_url_request_context.cc b/chrome/browser/net/chrome_url_request_context.cc
index f68c2c1..2603b296 100644
--- a/chrome/browser/net/chrome_url_request_context.cc
+++ b/chrome/browser/net/chrome_url_request_context.cc
@@ -9,6 +9,7 @@
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/extensions/extensions_service.h"
+#include "chrome/browser/greasemonkey_master.h"
#include "chrome/browser/profile.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_switches.h"
@@ -111,6 +112,8 @@ ChromeURLRequestContext::ChromeURLRequestContext(Profile* profile)
extension_paths_[(*iter)->id()] = (*iter)->path();
}
+ user_script_dir_path_ = profile->GetGreasemonkeyMaster()->user_script_dir();
+
prefs_->AddPrefObserver(prefs::kAcceptLanguages, this);
prefs_->AddPrefObserver(prefs::kCookieBehavior, this);
diff --git a/chrome/browser/net/chrome_url_request_context.h b/chrome/browser/net/chrome_url_request_context.h
index 7cd95f1..9ddbd47 100644
--- a/chrome/browser/net/chrome_url_request_context.h
+++ b/chrome/browser/net/chrome_url_request_context.h
@@ -39,6 +39,11 @@ class ChromeURLRequestContext : public URLRequestContext,
// Gets the path to the directory for the specified extension.
FilePath GetPathForExtension(const std::string& id);
+ // Gets the path to the directory user scripts are stored in.
+ FilePath user_script_dir_path() const {
+ return user_script_dir_path_;
+ }
+
private:
// Private constructor, use the static factory methods instead. This is
// expected to be called on the UI thread.
@@ -65,6 +70,9 @@ class ChromeURLRequestContext : public URLRequestContext,
// construtor and updated when extensions changed.
ExtensionPaths extension_paths_;
+ // Path to the directory user scripts are stored in.
+ FilePath user_script_dir_path_;
+
scoped_ptr<SQLitePersistentCookieStore> cookie_db_;
PrefService* prefs_;
bool is_off_the_record_;