diff options
author | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-14 04:15:16 +0000 |
---|---|---|
committer | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-14 04:15:16 +0000 |
commit | 0afe827a49501c6801c3c9efbbdb1f64aa010beb (patch) | |
tree | dba6031dd45eb3f0a0435b9750976b1cd432abc1 /chrome/common | |
parent | 27eef9c8edf84061f4e36a3bd26ff7538092a22b (diff) | |
download | chromium_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/common')
-rw-r--r-- | chrome/common/extensions/user_script.cc | 6 | ||||
-rw-r--r-- | chrome/common/extensions/user_script.h | 21 | ||||
-rw-r--r-- | chrome/common/extensions/user_script_unittest.cc | 6 |
3 files changed, 32 insertions, 1 deletions
diff --git a/chrome/common/extensions/user_script.cc b/chrome/common/extensions/user_script.cc index c726ad3..9a0da2d 100644 --- a/chrome/common/extensions/user_script.cc +++ b/chrome/common/extensions/user_script.cc @@ -24,6 +24,7 @@ bool UserScript::MatchesUrl(const GURL& url) { void UserScript::Pickle(::Pickle* pickle) { pickle->WriteString(url_.spec()); + pickle->WriteInt(run_location_); // Don't write path as we don't need that in the renderer. @@ -45,6 +46,11 @@ void UserScript::Unpickle(const ::Pickle& pickle, void** iter) { CHECK(pickle.ReadString(iter, &url_spec)); url_ = GURL(url_spec); + int run_location = 0; + CHECK(pickle.ReadInt(iter, &run_location)); + CHECK(run_location >= 0 && run_location < UserScript::RUN_LOCATION_LAST); + run_location_ = static_cast<UserScript::RunLocation>(run_location); + size_t num_globs = 0; CHECK(pickle.ReadSize(iter, &num_globs)); diff --git a/chrome/common/extensions/user_script.h b/chrome/common/extensions/user_script.h index 6453c8e..671f158 100644 --- a/chrome/common/extensions/user_script.h +++ b/chrome/common/extensions/user_script.h @@ -17,7 +17,19 @@ // extension. class UserScript { public: - UserScript(){} + // Locations that user scripts can be run inside the document. + enum RunLocation { + DOCUMENT_START, // After the documentElemnet is created, but before + // anything else happens. + DOCUMENT_END, // After the entire document is parsed. Same as + // DOMContentLoaded. + + RUN_LOCATION_LAST // Leave this as the last item. + }; + + // Constructor. Default the run location to document end, which is like + // Greasemonkey and probably more useful for typical scripts. + UserScript() : run_location_(DOCUMENT_END) {} // The URL to retrieve the content of this script at. const GURL& url() const { return url_; } @@ -27,6 +39,10 @@ class UserScript { const FilePath& path() const { return path_; } void set_path(const FilePath& path) { path_ = path; } + // The place in the document to run the script. + RunLocation run_location() const { return run_location_; } + void set_run_location(RunLocation location) { run_location_ = location; } + // The globs, if any, that determine which pages this script runs against. // These are only used with "standalone" Greasemonkey-like user scripts. const std::vector<std::string>& globs() const { return globs_; } @@ -60,6 +76,9 @@ class UserScript { // The path to the content of the script. FilePath path_; + // The location to run the script inside the document. + RunLocation run_location_; + // Greasemonkey-style globs that determine pages to inject the script into. // These are only used with standalone scripts. std::vector<std::string> globs_; diff --git a/chrome/common/extensions/user_script_unittest.cc b/chrome/common/extensions/user_script_unittest.cc index 72204f4..2be936a 100644 --- a/chrome/common/extensions/user_script_unittest.cc +++ b/chrome/common/extensions/user_script_unittest.cc @@ -78,6 +78,7 @@ TEST(UserScriptTest, Pickle) { UserScript script1; script1.set_url(GURL("chrome-user-script:/foo.user.js")); + script1.set_run_location(UserScript::DOCUMENT_START); script1.add_url_pattern(pattern1); script1.add_url_pattern(pattern2); @@ -99,3 +100,8 @@ TEST(UserScriptTest, Pickle) { script2.url_patterns()[i].GetAsString()); } } + +TEST(UserScriptTest, Defaults) { + UserScript script; + ASSERT_EQ(UserScript::DOCUMENT_END, script.run_location()); +} |