summaryrefslogtreecommitdiffstats
path: root/chrome/renderer
diff options
context:
space:
mode:
authoraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-01-17 01:21:47 +0000
committeraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-01-17 01:21:47 +0000
commit0999c45abbe8d57f1c62c65f91459881a4af321a (patch)
tree6844ce69dfaf4cbdfe0e540e6647ef60b3c22451 /chrome/renderer
parent2e6189037048f81f5025125a9884f29c1e5f544d (diff)
downloadchromium_src-0999c45abbe8d57f1c62c65f91459881a4af321a.zip
chromium_src-0999c45abbe8d57f1c62c65f91459881a4af321a.tar.gz
chromium_src-0999c45abbe8d57f1c62c65f91459881a4af321a.tar.bz2
Move parsing of metadata header into browser process. This is a prerequisite
to getting user scripts working in extensions because extensions won't express their metadata using the UserScript header, so parsing can't be done in the renderer. Review URL: http://codereview.chromium.org/18308 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@8249 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/renderer')
-rw-r--r--chrome/renderer/user_script_slave.cc79
-rw-r--r--chrome/renderer/user_script_slave.h36
-rw-r--r--chrome/renderer/user_script_slave_unittest.cc54
3 files changed, 40 insertions, 129 deletions
diff --git a/chrome/renderer/user_script_slave.cc b/chrome/renderer/user_script_slave.cc
index 52d9529..d227d5e 100644
--- a/chrome/renderer/user_script_slave.cc
+++ b/chrome/renderer/user_script_slave.cc
@@ -18,16 +18,6 @@ static const char kUserScriptTail[] = "\n})(window);";
// UserScript
-void UserScript::Parse(const StringPiece& script_text) {
- ParseMetadata(script_text);
-
- // TODO(aa): Set body to just the part after the metadata block? This would
- // significantly cut down on the size of the injected script in some cases.
- // Would require remembering the line number the body begins at, for correct
- // error line number reporting.
- body_ = script_text;
-}
-
bool UserScript::MatchesUrl(const GURL& url) {
for (std::vector<std::string>::iterator pattern = include_patterns_.begin();
pattern != include_patterns_.end(); ++pattern) {
@@ -39,57 +29,6 @@ bool UserScript::MatchesUrl(const GURL& url) {
return false;
}
-void UserScript::ParseMetadata(const StringPiece& script_text) {
- // http://wiki.greasespot.net/Metadata_block
- StringPiece line;
- size_t line_start = 0;
- size_t line_end = 0;
- bool in_metadata = false;
-
- static const StringPiece kUserScriptBegin("// ==UserScript==");
- static const StringPiece kUserScriptEng("// ==/UserScript==");
- static const StringPiece kIncludeDeclaration("// @include ");
-
- while (line_start < script_text.length()) {
- line_end = script_text.find('\n', line_start);
-
- // Handle the case where there is no trailing newline in the file.
- if (line_end == std::string::npos) {
- line_end = script_text.length() - 1;
- }
-
- line.set(script_text.data() + line_start, line_end - line_start);
-
- if (!in_metadata) {
- if (line.starts_with(kUserScriptBegin)) {
- in_metadata = true;
- }
- } else {
- if (line.starts_with(kUserScriptEng)) {
- break;
- }
-
- if (line.starts_with(kIncludeDeclaration)) {
- std::string pattern(line.data() + kIncludeDeclaration.length(),
- line.length() - kIncludeDeclaration.length());
- std::string pattern_trimmed;
- TrimWhitespace(pattern, TRIM_ALL, &pattern_trimmed);
- AddInclude(pattern_trimmed);
- }
-
- // TODO(aa): Handle more types of metadata.
- }
-
- line_start = line_end + 1;
- }
-
- // If no @include patterns were specified, default to @include *.
- // This is what Greasemonkey does.
- if (include_patterns_.size() == 0) {
- AddInclude("*");
- }
-}
-
void UserScript::AddInclude(const std::string &glob_pattern) {
include_patterns_.push_back(EscapeGlob(glob_pattern));
}
@@ -160,12 +99,12 @@ bool UserScriptSlave::UpdateScripts(base::SharedMemoryHandle shared_memory) {
// Unpickle scripts.
void* iter = NULL;
- int num_scripts = 0;
+ size_t num_scripts = 0;
Pickle pickle(reinterpret_cast<char*>(shared_memory_->memory()),
pickle_size);
- pickle.ReadInt(&iter, &num_scripts);
+ pickle.ReadSize(&iter, &num_scripts);
- for (int i = 0; i < num_scripts; ++i) {
+ for (size_t i = 0; i < num_scripts; ++i) {
const char* url = NULL;
int url_length = 0;
const char* body = NULL;
@@ -174,9 +113,17 @@ bool UserScriptSlave::UpdateScripts(base::SharedMemoryHandle shared_memory) {
pickle.ReadData(&iter, &url, &url_length);
pickle.ReadData(&iter, &body, &body_length);
- scripts_.push_back(UserScript(StringPiece(url, url_length)));
+ scripts_.push_back(UserScript(StringPiece(url, url_length),
+ StringPiece(body, body_length)));
UserScript& script = scripts_.back();
- script.Parse(StringPiece(body, body_length));
+
+ size_t num_includes;
+ pickle.ReadSize(&iter, &num_includes);
+ for (size_t j = 0; j < num_includes; ++j) {
+ std::string include;
+ pickle.ReadString(&iter, &include);
+ script.AddInclude(include);
+ }
}
return true;
diff --git a/chrome/renderer/user_script_slave.h b/chrome/renderer/user_script_slave.h
index 21b67dc..b4558a6 100644
--- a/chrome/renderer/user_script_slave.h
+++ b/chrome/renderer/user_script_slave.h
@@ -16,21 +16,23 @@
// Parsed representation of a user script.
class UserScript {
public:
- UserScript(const StringPiece& script_url)
- : url_(script_url) {}
-
- // Gets the script body that should be injected into matching content.
- const StringPiece& GetBody() const {
- return body_;
- }
+ UserScript(const StringPiece& script_url,
+ const StringPiece& body)
+ : url_(script_url), body_(body) {}
// Gets a URL where this script can be found.
const StringPiece& GetURL() const {
return url_;
}
- // Parses the text content of a user script file.
- void Parse(const StringPiece& script_text);
+ // Gets the script body that should be injected into matching content.
+ const StringPiece& GetBody() const {
+ return body_;
+ }
+
+ // Adds an include pattern that will be checked to determine whether to
+ // include a script on a given page.
+ void AddInclude(const std::string &glob_pattern);
// Returns true if the script should be applied to the specified URL, false
// otherwise.
@@ -38,30 +40,20 @@ class UserScript {
private:
FRIEND_TEST(UserScriptSlaveTest, EscapeGlob);
- FRIEND_TEST(UserScriptSlaveTest, Parse1);
- FRIEND_TEST(UserScriptSlaveTest, Parse2);
- FRIEND_TEST(UserScriptSlaveTest, Parse3);
// Helper function to convert the user script glob format to the patterns
// used internally to test URLs.
static std::string EscapeGlob(const std::string& glob);
- // Parses the metadata block from the script.
- void ParseMetadata(const StringPiece& script_text);
-
- // Adds an include pattern that will be checked to determine whether to
- // include a script on a given page.
- void AddInclude(const std::string &glob_pattern);
+ // The url of the file the script came from. This references shared_memory_,
+ // and is valid until that memory is either deleted or Unmap()'d.
+ StringPiece url_;
// The body of the script, which will be injected into content pages. This
// references shared_memory_, and is valid until that memory is either
// deleted or Unmap()'d.
StringPiece body_;
- // The url of the file the script came from. This references shared_memory_,
- // and is valid until that memory is either deleted or Unmap()'d.
- StringPiece url_;
-
// List of patterns to test URLs against for this script. These patterns have
// been escaped for use with MatchPattern() in string_utils ('?' and '\' are
// escaped).
diff --git a/chrome/renderer/user_script_slave_unittest.cc b/chrome/renderer/user_script_slave_unittest.cc
index 57d34a9..eeea749 100644
--- a/chrome/renderer/user_script_slave_unittest.cc
+++ b/chrome/renderer/user_script_slave_unittest.cc
@@ -16,28 +16,11 @@ TEST(UserScriptSlaveTest, EscapeGlob) {
UserScript::EscapeGlob("foo\\bar?hot=dog"));
}
-TEST(UserScriptSlaveTest, Parse1) {
- const std::string text(
- "// This is my awesome script\n"
- "// It does stuff.\n"
- "// ==UserScript== trailing garbage\n"
- "// @name foobar script\n"
- "// @namespace http://www.google.com/\n"
- "// @include *mail.google.com*\n"
- "// \n"
- "// @othergarbage\n"
- "// @include *mail.yahoo.com*\r\n"
- "// @include \t *mail.msn.com*\n" // extra spaces after "@include" OK
- "//@include not-recognized\n" // must have one space after "//"
- "// ==/UserScript== trailing garbage\n"
- "\n"
- "\n"
- "alert('hoo!');\n");
-
- UserScript script("foo");
- script.Parse(text);
- EXPECT_EQ(3U, script.include_patterns_.size());
- EXPECT_EQ(text, script.GetBody());
+TEST(UserScriptSlaveTest, Match1) {
+ UserScript script("foo", "bar");
+ script.AddInclude("*mail.google.com*");
+ script.AddInclude("*mail.yahoo.com*");
+ script.AddInclude("*mail.msn.com*");
EXPECT_TRUE(script.MatchesUrl(GURL("http://mail.google.com")));
EXPECT_TRUE(script.MatchesUrl(GURL("http://mail.google.com/foo")));
EXPECT_TRUE(script.MatchesUrl(GURL("http://mail.yahoo.com/bar")));
@@ -45,27 +28,16 @@ TEST(UserScriptSlaveTest, Parse1) {
EXPECT_FALSE(script.MatchesUrl(GURL("http://www.hotmail.com")));
}
-TEST(UserScriptSlaveTest, Parse2) {
- const std::string text("default to @include *");
-
- UserScript script("foo");
- script.Parse(text);
- EXPECT_EQ(1U, script.include_patterns_.size());
- EXPECT_EQ(text, script.GetBody());
- EXPECT_TRUE(script.MatchesUrl(GURL("foo")));
- EXPECT_TRUE(script.MatchesUrl(GURL("bar")));
+TEST(UserScriptSlaveTest, Match2) {
+ UserScript script("foo", "bar");
+ script.AddInclude("*");
+ EXPECT_TRUE(script.MatchesUrl(GURL("http://foo.com/bar")));
+ EXPECT_TRUE(script.MatchesUrl(GURL("http://hot.com/dog")));
}
-TEST(UserScriptSlaveTest, Parse3) {
- const std::string text(
- "// ==UserScript==\n"
- "// @include *foo*\n"
- "// ==/UserScript=="); // no trailing newline
-
- UserScript script("foo");
- script.Parse(text);
- EXPECT_EQ(1U, script.include_patterns_.size());
- EXPECT_EQ(text, script.GetBody());
+TEST(UserScriptSlaveTest, Match3) {
+ UserScript script("foo", "bar");
+ script.AddInclude("*foo*");
EXPECT_TRUE(script.MatchesUrl(GURL("http://foo.com/bar")));
EXPECT_FALSE(script.MatchesUrl(GURL("http://baz.org")));
}