diff options
author | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-05 22:51:54 +0000 |
---|---|---|
committer | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-05 22:51:54 +0000 |
commit | d2a69e2fccc65e8c74b770343cecad7a1136f9fd (patch) | |
tree | efc684e21d7ef65d72dcb31fba6a01184df8407b /chrome/browser/extensions | |
parent | a60551c712b36d6241c234cd81ae2a847f323ec5 (diff) | |
download | chromium_src-d2a69e2fccc65e8c74b770343cecad7a1136f9fd.zip chromium_src-d2a69e2fccc65e8c74b770343cecad7a1136f9fd.tar.gz chromium_src-d2a69e2fccc65e8c74b770343cecad7a1136f9fd.tar.bz2 |
Integrate URLPattern with Extension user scripts.
Also refactored the UserScript class in
UserScriptSlave and the UserScriptInfo structure
in UserScriptMaster into a common location.
Review URL: http://codereview.chromium.org/21070
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@9270 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions')
-rw-r--r-- | chrome/browser/extensions/extension.cc | 22 | ||||
-rw-r--r-- | chrome/browser/extensions/extensions_service_unittest.cc | 16 | ||||
-rw-r--r-- | chrome/browser/extensions/user_script_master.cc | 50 | ||||
-rw-r--r-- | chrome/browser/extensions/user_script_master.h | 20 | ||||
-rw-r--r-- | chrome/browser/extensions/user_script_master_unittest.cc | 28 |
5 files changed, 65 insertions, 71 deletions
diff --git a/chrome/browser/extensions/extension.cc b/chrome/browser/extensions/extension.cc index f932eff..11cab38 100644 --- a/chrome/browser/extensions/extension.cc +++ b/chrome/browser/extensions/extension.cc @@ -8,6 +8,7 @@ #include "base/logging.h" #include "base/string_util.h" #include "net/base/net_util.h" +#include "chrome/common/extensions/user_script.h" const char kExtensionURLScheme[] = "chrome-extension"; const char kUserScriptURLScheme[] = "chrome-user-script"; @@ -262,16 +263,23 @@ bool Extension::InitFromValue(const DictionaryValue& source, return false; } - UserScriptInfo script_info; + UserScript script; for (size_t j = 0; j < matches->GetSize(); ++j) { - std::string match; - if (!matches->GetString(j, &match)) { + std::string match_str; + if (!matches->GetString(j, &match_str)) { *error = FormatErrorMessage(kInvalidMatchError, IntToString(i), IntToString(j)); return false; } - script_info.matches.push_back(match); + URLPattern pattern; + if (!pattern.Parse(match_str)) { + *error = FormatErrorMessage(kInvalidMatchError, IntToString(i), + IntToString(j)); + return false; + } + + script.add_url_pattern(pattern); } // TODO(aa): Support multiple files. @@ -281,10 +289,10 @@ bool Extension::InitFromValue(const DictionaryValue& source, IntToString(0)); return false; } - script_info.path = Extension::GetResourcePath(path(), file); - script_info.url = Extension::GetResourceURL(url(), file); + script.set_path(Extension::GetResourcePath(path(), file)); + script.set_url(Extension::GetResourceURL(url(), file)); - user_scripts_.push_back(script_info); + user_scripts_.push_back(script); } } diff --git a/chrome/browser/extensions/extensions_service_unittest.cc b/chrome/browser/extensions/extensions_service_unittest.cc index ed86b6b..49e37a3 100644 --- a/chrome/browser/extensions/extensions_service_unittest.cc +++ b/chrome/browser/extensions/extensions_service_unittest.cc @@ -154,15 +154,17 @@ TEST_F(ExtensionsServiceTest, LoadAllExtensionsFromDirectory) { Extension* extension = frontend->extensions()->at(0); const UserScriptList& scripts = extension->user_scripts(); ASSERT_EQ(2u, scripts.size()); - EXPECT_EQ(2u, scripts[0].matches.size()); - EXPECT_EQ("http://*.google.com/*", scripts[0].matches[0]); - EXPECT_EQ("https://*.google.com/*", scripts[0].matches[1]); + EXPECT_EQ(2u, scripts[0].url_patterns().size()); + EXPECT_EQ("http://*.google.com/*", + scripts[0].url_patterns()[0].GetAsString()); + EXPECT_EQ("https://*.google.com/*", + scripts[0].url_patterns()[1].GetAsString()); EXPECT_EQ(extension->path().AppendASCII("script1.js").value(), - scripts[0].path.value()); - EXPECT_EQ(1u, scripts[1].matches.size()); - EXPECT_EQ("http://*.yahoo.com/*", scripts[1].matches[0]); + scripts[0].path().value()); + EXPECT_EQ(1u, scripts[1].url_patterns().size()); + EXPECT_EQ("http://*.yahoo.com/*", scripts[1].url_patterns()[0].GetAsString()); EXPECT_EQ(extension->path().AppendASCII("script2.js").value(), - scripts[1].path.value()); + scripts[1].path().value()); EXPECT_EQ(std::string("com.google.myextension2"), frontend->extensions()->at(1)->id()); diff --git a/chrome/browser/extensions/user_script_master.cc b/chrome/browser/extensions/user_script_master.cc index 3dafcbc..4ca10fb 100644 --- a/chrome/browser/extensions/user_script_master.cc +++ b/chrome/browser/extensions/user_script_master.cc @@ -22,7 +22,7 @@ extern const char kUserScriptURLScheme[]; // static void UserScriptMaster::ScriptReloader::ParseMetadataHeader( - const StringPiece& script_text, std::vector<std::string> *includes) { + const StringPiece& script_text, UserScript* script) { // http://wiki.greasespot.net/Metadata_block StringPiece line; size_t line_start = 0; @@ -57,7 +57,12 @@ void UserScriptMaster::ScriptReloader::ParseMetadataHeader( line.length() - kIncludeDeclaration.length()); std::string pattern_trimmed; TrimWhitespace(pattern, TRIM_ALL, &pattern_trimmed); - includes->push_back(pattern_trimmed); + + // We escape some characters that MatchPattern() considers special. + ReplaceSubstringsAfterOffset(&pattern_trimmed, 0, "\\", "\\\\"); + ReplaceSubstringsAfterOffset(&pattern_trimmed, 0, "?", "\\?"); + + script->add_glob(pattern_trimmed); } // TODO(aa): Handle more types of metadata. @@ -68,8 +73,8 @@ void UserScriptMaster::ScriptReloader::ParseMetadataHeader( // If no @include patterns were specified, default to @include *. // This is what Greasemonkey does. - if (includes->size() == 0) { - includes->push_back("*"); + if (script->globs().size() == 0) { + script->add_glob("*"); } } @@ -121,11 +126,11 @@ base::SharedMemory* UserScriptMaster::ScriptReloader::GetNewScripts( FILE_PATH_LITERAL("*.user.js")); for (FilePath file = enumerator.Next(); !file.value().empty(); file = enumerator.Next()) { - all_scripts.push_back(UserScriptInfo()); - UserScriptInfo& info = all_scripts.back(); - info.url = GURL(std::string(kUserScriptURLScheme) + ":/" + - net::FilePathToFileURL(file.ToWStringHack()).ExtractFileName()); - info.path = file; + all_scripts.push_back(UserScript()); + UserScript& info = all_scripts.back(); + info.set_url(GURL(std::string(kUserScriptURLScheme) + ":/" + + net::FilePathToFileURL(file.ToWStringHack()).ExtractFileName())); + info.set_path(file); } } @@ -137,21 +142,25 @@ base::SharedMemory* UserScriptMaster::ScriptReloader::GetNewScripts( lone_scripts.end()); // Load and pickle each script. Look for a metadata header if there are no - // matches specified already. + // url_patterns specified already. Pickle pickle; pickle.WriteSize(all_scripts.size()); for (UserScriptList::iterator iter = all_scripts.begin(); iter != all_scripts.end(); ++iter) { // TODO(aa): Support unicode script files. std::string contents; - file_util::ReadFileToString(iter->path.ToWStringHack(), &contents); + file_util::ReadFileToString(iter->path().ToWStringHack(), &contents); - if (iter->matches.empty()) { + if (iter->url_patterns().empty()) { // TODO(aa): Handle errors parsing header. - ParseMetadataHeader(contents, &iter->matches); + ParseMetadataHeader(contents, &(*iter)); } - PickleScriptData(*iter, contents, &pickle); + iter->Pickle(&pickle); + + // Write scripts as 'data' so that we can read it out in the slave without + // allocating a new string. + pickle.WriteData(contents.c_str(), contents.length()); } // Create the shared memory object. @@ -174,19 +183,6 @@ base::SharedMemory* UserScriptMaster::ScriptReloader::GetNewScripts( return shared_memory.release(); } -void UserScriptMaster::ScriptReloader::PickleScriptData( - const UserScriptInfo& script, const std::string& contents, Pickle* pickle) { - // Write scripts as 'data' so that we can read it out in the slave without - // allocating a new string. - pickle->WriteData(script.url.spec().c_str(), script.url.spec().length()); - pickle->WriteData(contents.c_str(), contents.length()); - pickle->WriteSize(script.matches.size()); - for (std::vector<std::string>::const_iterator iter = script.matches.begin(); - iter != script.matches.end(); ++iter) { - pickle->WriteString(*iter); - } -} - UserScriptMaster::UserScriptMaster(MessageLoop* worker_loop, const FilePath& script_dir) : user_script_dir_(script_dir), diff --git a/chrome/browser/extensions/user_script_master.h b/chrome/browser/extensions/user_script_master.h index 1c1b095..8969281 100644 --- a/chrome/browser/extensions/user_script_master.h +++ b/chrome/browser/extensions/user_script_master.h @@ -12,17 +12,9 @@ #include "base/scoped_ptr.h" #include "base/shared_memory.h" #include "base/string_piece.h" +#include "chrome/common/extensions/user_script.h" #include "googleurl/src/gurl.h" -class Pickle; - -struct UserScriptInfo { - GURL url; - FilePath path; - std::vector<std::string> matches; -}; -typedef std::vector<UserScriptInfo> UserScriptList; - // Manages a segment of shared memory that contains the user scripts the user // has installed. Lives on the UI thread. class UserScriptMaster : public base::RefCounted<UserScriptMaster>, @@ -35,7 +27,7 @@ class UserScriptMaster : public base::RefCounted<UserScriptMaster>, ~UserScriptMaster(); // Add a single user script that exists outside the script directory. - void AddLoneScript(const UserScriptInfo& script) { + void AddLoneScript(const UserScript& script) { lone_scripts_.push_back(script); } @@ -76,8 +68,8 @@ class UserScriptMaster : public base::RefCounted<UserScriptMaster>, : public base::RefCounted<UserScriptMaster::ScriptReloader> { public: // Parses the includes out of |script| and returns them in |includes|. - static void ParseMetadataHeader(const StringPiece& script, - std::vector<std::string>* includes); + static void ParseMetadataHeader(const StringPiece& script_text, + UserScript* script); ScriptReloader(UserScriptMaster* master) : master_(master), master_message_loop_(MessageLoop::current()) {} @@ -116,10 +108,6 @@ class UserScriptMaster : public base::RefCounted<UserScriptMaster>, base::SharedMemory* GetNewScripts(const FilePath& script_dir, const UserScriptList& lone_scripts); - // Serialize script metadata and contents into the specified pickle. - void PickleScriptData(const UserScriptInfo& script, - const std::string& contents, Pickle* pickle); - // A pointer back to our master. // May be NULL if DisownMaster() is called. UserScriptMaster* master_; diff --git a/chrome/browser/extensions/user_script_master_unittest.cc b/chrome/browser/extensions/user_script_master_unittest.cc index af60cb8..8ce2391 100644 --- a/chrome/browser/extensions/user_script_master_unittest.cc +++ b/chrome/browser/extensions/user_script_master_unittest.cc @@ -139,21 +139,21 @@ TEST_F(UserScriptMasterTest, Parse1) { "\n" "alert('hoo!');\n"); - std::vector<std::string> includes; - UserScriptMaster::ScriptReloader::ParseMetadataHeader(text, &includes); - EXPECT_EQ(3U, includes.size()); - EXPECT_EQ("*mail.google.com*", includes[0]); - EXPECT_EQ("*mail.yahoo.com*", includes[1]); - EXPECT_EQ("*mail.msn.com*", includes[2]); + UserScript script; + UserScriptMaster::ScriptReloader::ParseMetadataHeader(text, &script); + EXPECT_EQ(3U, script.globs().size()); + EXPECT_EQ("*mail.google.com*", script.globs()[0]); + EXPECT_EQ("*mail.yahoo.com*", script.globs()[1]); + EXPECT_EQ("*mail.msn.com*", script.globs()[2]); } TEST_F(UserScriptMasterTest, Parse2) { const std::string text("default to @include *"); - std::vector<std::string> includes; - UserScriptMaster::ScriptReloader::ParseMetadataHeader(text, &includes); - EXPECT_EQ(1U, includes.size()); - EXPECT_EQ("*", includes[0]); + UserScript script; + UserScriptMaster::ScriptReloader::ParseMetadataHeader(text, &script); + EXPECT_EQ(1U, script.globs().size()); + EXPECT_EQ("*", script.globs()[0]); } TEST_F(UserScriptMasterTest, Parse3) { @@ -162,8 +162,8 @@ TEST_F(UserScriptMasterTest, Parse3) { "// @include *foo*\n" "// ==/UserScript=="); // no trailing newline - std::vector<std::string> includes; - UserScriptMaster::ScriptReloader::ParseMetadataHeader(text, &includes); - EXPECT_EQ(1U, includes.size()); - EXPECT_EQ("*foo*", includes[0]); + UserScript script; + UserScriptMaster::ScriptReloader::ParseMetadataHeader(text, &script); + EXPECT_EQ(1U, script.globs().size()); + EXPECT_EQ("*foo*", script.globs()[0]); } |