summaryrefslogtreecommitdiffstats
path: root/chrome/browser
diff options
context:
space:
mode:
authoraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-02-14 04:15:16 +0000
committeraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-02-14 04:15:16 +0000
commit0afe827a49501c6801c3c9efbbdb1f64aa010beb (patch)
treedba6031dd45eb3f0a0435b9750976b1cd432abc1 /chrome/browser
parent27eef9c8edf84061f4e36a3bd26ff7538092a22b (diff)
downloadchromium_src-0afe827a49501c6801c3c9efbbdb1f64aa010beb.zip
chromium_src-0afe827a49501c6801c3c9efbbdb1f64aa010beb.tar.gz
chromium_src-0afe827a49501c6801c3c9efbbdb1f64aa010beb.tar.bz2
Add early-injection capability to user scripts.
Review URL: http://codereview.chromium.org/19624 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@9822 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rw-r--r--chrome/browser/extensions/extension.cc23
-rw-r--r--chrome/browser/extensions/extension.h6
-rw-r--r--chrome/browser/extensions/user_script_master.cc8
3 files changed, 37 insertions, 0 deletions
diff --git a/chrome/browser/extensions/extension.cc b/chrome/browser/extensions/extension.cc
index 11cab38..d1d52a51 100644
--- a/chrome/browser/extensions/extension.cc
+++ b/chrome/browser/extensions/extension.cc
@@ -22,9 +22,13 @@ const wchar_t* Extension::kIdKey = L"id";
const wchar_t* Extension::kMatchesKey = L"matches";
const wchar_t* Extension::kNameKey = L"name";
const wchar_t* Extension::kUserScriptsKey = L"user_scripts";
+const wchar_t* Extension::kRunAtKey = L"run_at";
const wchar_t* Extension::kVersionKey = L"version";
const wchar_t* Extension::kZipHashKey = L"zip_hash";
+const char* Extension::kRunAtDocumentStartValue = "document_start";
+const char* Extension::kRunAtDocumentEndValue = "document_end";
+
// Extension-related error messages. Some of these are simple patterns, where a
// '*' is replaced at runtime with a specific value. This is used instead of
// printf because we want to unit test them and scanf is hard to make
@@ -53,6 +57,8 @@ const char* Extension::kInvalidMatchesError =
"Required value 'user_scripts[*].matches' is missing or invalid.";
const char* Extension::kInvalidNameError =
"Required value 'name' is missing or invalid.";
+const char* Extension::kInvalidRunAtError =
+ "Invalid value for 'user_scripts[*].run_at'.";
const char* Extension::kInvalidUserScriptError =
"Invalid value for 'user_scripts[*]'.";
const char* Extension::kInvalidUserScriptsListError =
@@ -264,6 +270,23 @@ bool Extension::InitFromValue(const DictionaryValue& source,
}
UserScript script;
+ if (user_script->HasKey(kRunAtKey)) {
+ std::string run_location;
+ if (!user_script->GetString(kRunAtKey, &run_location)) {
+ *error = FormatErrorMessage(kInvalidRunAtError, IntToString(i));
+ return false;
+ }
+
+ if (run_location == kRunAtDocumentStartValue) {
+ script.set_run_location(UserScript::DOCUMENT_START);
+ } else if (run_location == kRunAtDocumentEndValue) {
+ script.set_run_location(UserScript::DOCUMENT_END);
+ } else {
+ *error = FormatErrorMessage(kInvalidRunAtError, IntToString(i));
+ return false;
+ }
+ }
+
for (size_t j = 0; j < matches->GetSize(); ++j) {
std::string match_str;
if (!matches->GetString(j, &match_str)) {
diff --git a/chrome/browser/extensions/extension.h b/chrome/browser/extensions/extension.h
index 22e8fd6..5964ad4 100644
--- a/chrome/browser/extensions/extension.h
+++ b/chrome/browser/extensions/extension.h
@@ -44,9 +44,14 @@ class Extension {
static const wchar_t* kMatchesKey;
static const wchar_t* kNameKey;
static const wchar_t* kUserScriptsKey;
+ static const wchar_t* kRunAtKey;
static const wchar_t* kVersionKey;
static const wchar_t* kZipHashKey;
+ // Some values expected in manifests.
+ static const char* kRunAtDocumentStartValue;
+ static const char* kRunAtDocumentEndValue;
+
// Error messages returned from InitFromValue().
static const char* kInvalidDescriptionError;
static const char* kInvalidFileCountError;
@@ -59,6 +64,7 @@ class Extension {
static const char* kInvalidMatchError;
static const char* kInvalidMatchesError;
static const char* kInvalidNameError;
+ static const char* kInvalidRunAtError;
static const char* kInvalidUserScriptError;
static const char* kInvalidUserScriptsListError;
static const char* kInvalidVersionError;
diff --git a/chrome/browser/extensions/user_script_master.cc b/chrome/browser/extensions/user_script_master.cc
index 7164dc1..23ef66f 100644
--- a/chrome/browser/extensions/user_script_master.cc
+++ b/chrome/browser/extensions/user_script_master.cc
@@ -45,6 +45,9 @@ bool UserScriptMaster::ScriptReloader::ParseMetadataHeader(
static const StringPiece kUserScriptEng("// ==/UserScript==");
static const StringPiece kIncludeDeclaration("// @include ");
static const StringPiece kMatchDeclaration("// @match ");
+ static const StringPiece kRunAtDeclaration("// @run-at ");
+ static const StringPiece kRunAtDocumentStartValue("document-start");
+ static const StringPiece kRunAtDocumentEndValue("document-end");
while (line_start < script_text.length()) {
line_end = script_text.find('\n', line_start);
@@ -76,6 +79,11 @@ bool UserScriptMaster::ScriptReloader::ParseMetadataHeader(
if (!pattern.Parse(value))
return false;
script->add_url_pattern(pattern);
+ } else if (GetDeclarationValue(line, kRunAtDeclaration, &value)) {
+ if (value == kRunAtDocumentStartValue)
+ script->set_run_location(UserScript::DOCUMENT_START);
+ else if (value != kRunAtDocumentEndValue)
+ return false;
}
// TODO(aa): Handle more types of metadata.