diff options
author | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-04 02:15:20 +0000 |
---|---|---|
committer | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-04 02:15:20 +0000 |
commit | 6657afa65426f10f5dc51c0ac12d573c4826cb54 (patch) | |
tree | 3b24f83dd2f834bde93b7b7e27327701814379ac /chrome/common/extensions/user_script.cc | |
parent | 6c856ae733bf8d1f187ec5b93e07216ace0c4693 (diff) | |
download | chromium_src-6657afa65426f10f5dc51c0ac12d573c4826cb54.zip chromium_src-6657afa65426f10f5dc51c0ac12d573c4826cb54.tar.gz chromium_src-6657afa65426f10f5dc51c0ac12d573c4826cb54.tar.bz2 |
Add first class support for user scripts.
Original review: http://codereview.chromium.org/340057
TBR=mpcomplete@chromium.org
BUG=22103
TEST=Install a user script (such as from userscripts.org). You should get the extension install UI and the
script should show up in the extension management UI. It should also work, though some scripts use
Firefox-specific APIs and those won't work in Chromium.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@30925 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/extensions/user_script.cc')
-rw-r--r-- | chrome/common/extensions/user_script.cc | 75 |
1 files changed, 65 insertions, 10 deletions
diff --git a/chrome/common/extensions/user_script.cc b/chrome/common/extensions/user_script.cc index df34e52..10d0b60 100644 --- a/chrome/common/extensions/user_script.cc +++ b/chrome/common/extensions/user_script.cc @@ -7,21 +7,59 @@ #include "base/pickle.h" #include "base/string_util.h" -bool UserScript::MatchesUrl(const GURL& url) const { - for (std::vector<std::string>::const_iterator glob = globs_.begin(); - glob != globs_.end(); ++glob) { - if (MatchPattern(url.spec(), *glob)) +namespace { +static bool UrlMatchesPatterns(const UserScript::PatternList* patterns, + const GURL& url) { + for (UserScript::PatternList::const_iterator pattern = patterns->begin(); + pattern != patterns->end(); ++pattern) { + if (pattern->MatchesUrl(url)) return true; } - for (PatternList::const_iterator pattern = url_patterns_.begin(); - pattern != url_patterns_.end(); ++pattern) { - if (pattern->MatchesUrl(url)) + return false; +} + +static bool UrlMatchesGlobs(const std::vector<std::string>* globs, + const GURL& url) { + for (std::vector<std::string>::const_iterator glob = globs->begin(); + glob != globs->end(); ++glob) { + if (MatchPattern(url.spec(), *glob)) return true; } return false; } +} + +const char UserScript::kFileExtension[] = ".user.js"; + +bool UserScript::HasUserScriptFileExtension(const GURL& url) { + return EndsWith(url.ExtractFileName(), kFileExtension, false); +} + +bool UserScript::HasUserScriptFileExtension(const FilePath& path) { + static FilePath extension(FilePath().AppendASCII(kFileExtension)); + return EndsWith(path.BaseName().value(), extension.value(), false); +} + +bool UserScript::MatchesUrl(const GURL& url) const { + if (url_patterns_.size() > 0) { + if (!UrlMatchesPatterns(&url_patterns_, url)) + return false; + } + + if (globs_.size() > 0) { + if (!UrlMatchesGlobs(&globs_, url)) + return false; + } + + if (exclude_globs_.size() > 0) { + if (UrlMatchesGlobs(&exclude_globs_, url)) + return false; + } + + return true; +} void UserScript::File::Pickle(::Pickle* pickle) const { pickle->WriteString(url_.spec()); @@ -43,10 +81,17 @@ void UserScript::Pickle(::Pickle* pickle) const { // Write the extension id. pickle->WriteString(extension_id()); + // Write Greasemonkey emulation. + pickle->WriteBool(emulate_greasemonkey()); + // Write globs. + std::vector<std::string>::const_iterator glob; pickle->WriteSize(globs_.size()); - for (std::vector<std::string>::const_iterator glob = globs_.begin(); - glob != globs_.end(); ++glob) { + for (glob = globs_.begin(); glob != globs_.end(); ++glob) { + pickle->WriteString(*glob); + } + pickle->WriteSize(exclude_globs_.size()); + for (glob = exclude_globs_.begin(); glob != exclude_globs_.end(); ++glob) { pickle->WriteString(*glob); } @@ -82,10 +127,12 @@ void UserScript::Unpickle(const ::Pickle& pickle, void** iter) { // Read the extension ID. CHECK(pickle.ReadString(iter, &extension_id_)); + // Read Greasemonkey emulation. + CHECK(pickle.ReadBool(iter, &emulate_greasemonkey_)); + // Read globs. size_t num_globs = 0; CHECK(pickle.ReadSize(iter, &num_globs)); - globs_.clear(); for (size_t i = 0; i < num_globs; ++i) { std::string glob; @@ -93,6 +140,14 @@ void UserScript::Unpickle(const ::Pickle& pickle, void** iter) { globs_.push_back(glob); } + CHECK(pickle.ReadSize(iter, &num_globs)); + exclude_globs_.clear(); + for (size_t i = 0; i < num_globs; ++i) { + std::string glob; + CHECK(pickle.ReadString(iter, &glob)); + exclude_globs_.push_back(glob); + } + // Read url patterns. size_t num_patterns = 0; CHECK(pickle.ReadSize(iter, &num_patterns)); |