Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3514)

Unified Diff: chrome/common/extensions/user_script.cc

Issue 340057: Add first-class support for user scripts (Closed)
Patch Set: newness Created 11 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/common/extensions/user_script.cc
diff --git a/chrome/common/extensions/user_script.cc b/chrome/common/extensions/user_script.cc
index df34e529114551555287fd7cffd219ee65df13fb..10d0b60eef0d06167703de1bec38604108cc5b4b 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));

Powered by Google App Engine
This is Rietveld 408576698