summaryrefslogtreecommitdiffstats
path: root/chrome/common/extensions
diff options
context:
space:
mode:
authorgeorged@chromium.org <georged@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-03-18 21:26:24 +0000
committergeorged@chromium.org <georged@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-03-18 21:26:24 +0000
commit3cfbd0eb2afd2cf0b8b248d6bb97ec39b064cd23 (patch)
treeeaa3eddbc15cf6bac88d04c4766778fd545f95c0 /chrome/common/extensions
parent836827406c7135b0225474d24b9d08dd8edff27a (diff)
downloadchromium_src-3cfbd0eb2afd2cf0b8b248d6bb97ec39b064cd23.zip
chromium_src-3cfbd0eb2afd2cf0b8b248d6bb97ec39b064cd23.tar.gz
chromium_src-3cfbd0eb2afd2cf0b8b248d6bb97ec39b064cd23.tar.bz2
Review URL: http://codereview.chromium.org/42288
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@12016 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/extensions')
-rw-r--r--chrome/common/extensions/user_script.cc70
-rw-r--r--chrome/common/extensions/user_script.h86
-rw-r--r--chrome/common/extensions/user_script_unittest.cc20
3 files changed, 146 insertions, 30 deletions
diff --git a/chrome/common/extensions/user_script.cc b/chrome/common/extensions/user_script.cc
index 9a0da2d..b970570 100644
--- a/chrome/common/extensions/user_script.cc
+++ b/chrome/common/extensions/user_script.cc
@@ -7,7 +7,7 @@
#include "base/string_util.h"
bool UserScript::MatchesUrl(const GURL& url) {
- for (std::vector<std::string>::iterator glob = globs_.begin();
+ for (std::vector<std::string>::const_iterator glob = globs_.begin();
glob != globs_.end(); ++glob) {
if (MatchPattern(url.spec(), *glob))
return true;
@@ -22,35 +22,60 @@ bool UserScript::MatchesUrl(const GURL& url) {
return false;
}
-void UserScript::Pickle(::Pickle* pickle) {
+void UserScript::File::Pickle(::Pickle* pickle) const {
pickle->WriteString(url_.spec());
- pickle->WriteInt(run_location_);
+ // Do not write path. It's not needed in the renderer.
+ // Do not write content. It will be serialized by other means.
+}
+
+void UserScript::File::Unpickle(const ::Pickle& pickle, void** iter) {
+ // Read url.
+ std::string url;
+ CHECK(pickle.ReadString(iter, &url));
+ set_url(GURL(url));
+}
- // Don't write path as we don't need that in the renderer.
+void UserScript::Pickle(::Pickle* pickle) const {
+ // Write the run location.
+ pickle->WriteInt(run_location());
+ // Write globs.
pickle->WriteSize(globs_.size());
- for (std::vector<std::string>::iterator glob = globs_.begin();
+ for (std::vector<std::string>::const_iterator glob = globs_.begin();
glob != globs_.end(); ++glob) {
pickle->WriteString(*glob);
}
+ // Write url patterns.
pickle->WriteSize(url_patterns_.size());
- for (std::vector<URLPattern>::iterator pattern = url_patterns_.begin();
+ for (std::vector<URLPattern>::const_iterator pattern = url_patterns_.begin();
pattern != url_patterns_.end(); ++pattern) {
pickle->WriteString(pattern->GetAsString());
}
+
+ // Write js scripts.
+ pickle->WriteSize(js_scripts_.size());
+ for (FileList::const_iterator file = js_scripts_.begin();
+ file != js_scripts_.end(); ++file) {
+ file->Pickle(pickle);
+ }
+
+ // Write css scripts.
+ pickle->WriteSize(css_scripts_.size());
+ for (FileList::const_iterator file = css_scripts_.begin();
+ file != css_scripts_.end(); ++file) {
+ file->Pickle(pickle);
+ }
}
void UserScript::Unpickle(const ::Pickle& pickle, void** iter) {
- std::string url_spec;
- CHECK(pickle.ReadString(iter, &url_spec));
- url_ = GURL(url_spec);
-
+ // Read the run location.
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);
+ CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST);
+ run_location_ = static_cast<RunLocation>(run_location);
+ // Read globs.
size_t num_globs = 0;
CHECK(pickle.ReadSize(iter, &num_globs));
@@ -61,6 +86,7 @@ void UserScript::Unpickle(const ::Pickle& pickle, void** iter) {
globs_.push_back(glob);
}
+ // Read url patterns.
size_t num_patterns = 0;
CHECK(pickle.ReadSize(iter, &num_patterns));
@@ -72,4 +98,24 @@ void UserScript::Unpickle(const ::Pickle& pickle, void** iter) {
CHECK(pattern.Parse(pattern_str));
url_patterns_.push_back(pattern);
}
+
+ // Read js scripts.
+ size_t num_js_files = 0;
+ CHECK(pickle.ReadSize(iter, &num_js_files));
+ js_scripts_.clear();
+ for (size_t i = 0; i < num_js_files; ++i) {
+ File file;
+ file.Unpickle(pickle, iter);
+ js_scripts_.push_back(file);
+ }
+
+ // Read css scripts.
+ size_t num_css_files = 0;
+ CHECK(pickle.ReadSize(iter, &num_css_files));
+ css_scripts_.clear();
+ for (size_t i = 0; i < num_css_files; ++i) {
+ File file;
+ file.Unpickle(pickle, iter);
+ css_scripts_.push_back(file);
+ }
}
diff --git a/chrome/common/extensions/user_script.h b/chrome/common/extensions/user_script.h
index 671f158..bf1ec0d 100644
--- a/chrome/common/extensions/user_script.h
+++ b/chrome/common/extensions/user_script.h
@@ -10,6 +10,7 @@
#include "base/file_path.h"
#include "base/pickle.h"
+#include "base/string_piece.h"
#include "chrome/common/extensions/url_pattern.h"
#include "googleurl/src/gurl.h"
@@ -27,18 +28,62 @@ class UserScript {
RUN_LOCATION_LAST // Leave this as the last item.
};
+ // Holds actual script file info.
+ class File {
+ public:
+ File(const FilePath& path, const GURL& url):
+ path_(path),
+ url_(url) {
+ }
+ File() {}
+
+ const FilePath& path() const { return path_; }
+ void set_path(const FilePath& path) { path_ = path; }
+
+ const GURL& url() const { return url_; }
+ void set_url(const GURL& url) { url_ = url; }
+
+ // If external_content_ is set returns it as content otherwise it returns
+ // content_
+ const StringPiece GetContent() const {
+ if (external_content_.data())
+ return external_content_;
+ else
+ return content_;
+ }
+ void set_external_content(const StringPiece& content) {
+ external_content_ = content;
+ }
+ const void set_content(const StringPiece& content) {
+ content_.assign(content.begin(), content.end());
+ }
+
+ // Serialization support. The content and path_ member will not be
+ // serialized!
+ void Pickle(::Pickle* pickle) const;
+ void Unpickle(const ::Pickle& pickle, void** iter);
+
+ private:
+ // Where is the script file lives on the disk.
+ FilePath path_;
+
+ // The url to this scipt file.
+ GURL url_;
+
+ // The script content. It can be set to either loaded_content_ or
+ // externally allocated string.
+ StringPiece external_content_;
+
+ // Set when the content is loaded by LoadContent
+ std::string content_;
+ };
+
+ typedef std::vector<File> FileList;
+
// 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_; }
- void set_url(const GURL& url) { url_ = url; }
-
- // The path to find the script at.
- 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; }
@@ -57,12 +102,21 @@ class UserScript {
}
void clear_url_patterns() { url_patterns_.clear(); }
+ // List of js scripts for this user script
+ FileList& js_scripts() { return js_scripts_; }
+ const FileList& js_scripts() const { return js_scripts_; }
+
+ // List of css scripts for this user script
+ FileList& css_scripts() { return css_scripts_; }
+ const FileList& css_scripts() const { return css_scripts_; }
+
// Returns true if the script should be applied to the specified URL, false
// otherwise.
bool MatchesUrl(const GURL& url);
- // Serialize the script into a pickle.
- void Pickle(::Pickle* pickle);
+ // Serialize the UserScript into a pickle. The content of the scripts and
+ // paths to UserScript::Files will not be serialized!
+ void Pickle(::Pickle* pickle) const;
// Deserialize the script from a pickle. Note that this always succeeds
// because presumably we were the one that pickled it, and we did it
@@ -70,12 +124,6 @@ class UserScript {
void Unpickle(const ::Pickle& pickle, void** iter);
private:
- // The URL to the content of the script.
- GURL url_;
-
- // The path to the content of the script.
- FilePath path_;
-
// The location to run the script inside the document.
RunLocation run_location_;
@@ -86,6 +134,12 @@ class UserScript {
// URLPatterns that determine pages to inject the script into. These are
// only used with scripts that are part of extensions.
std::vector<URLPattern> url_patterns_;
+
+ // List of js scripts defined in content_scripts
+ FileList js_scripts_;
+
+ // List of css scripts defined in content_scripts
+ FileList css_scripts_;
};
typedef std::vector<UserScript> UserScriptList;
diff --git a/chrome/common/extensions/user_script_unittest.cc b/chrome/common/extensions/user_script_unittest.cc
index 6230497..46014f8 100644
--- a/chrome/common/extensions/user_script_unittest.cc
+++ b/chrome/common/extensions/user_script_unittest.cc
@@ -76,8 +76,17 @@ TEST(UserScriptTest, Pickle) {
ASSERT_TRUE(pattern2.Parse("http://bar/baz*"));
UserScript script1;
- script1.set_url(GURL("chrome-user-script:/foo.user.js"));
+ script1.js_scripts().push_back(UserScript::File(
+ FilePath(FILE_PATH_LITERAL("c:\\foo\\foo.user.js")),
+ GURL("chrome-user-script:/foo.user.js")));
+ script1.css_scripts().push_back(UserScript::File(
+ FilePath(FILE_PATH_LITERAL("c:\\foo\\foo.user.css")),
+ GURL("chrome-user-script:/foo.user.css")));
+ script1.css_scripts().push_back(UserScript::File(
+ FilePath(FILE_PATH_LITERAL("c:\\foo\\foo2.user.css")),
+ GURL("chrome-user-script:/foo2.user.css")));
script1.set_run_location(UserScript::DOCUMENT_START);
+
script1.add_url_pattern(pattern1);
script1.add_url_pattern(pattern2);
@@ -88,7 +97,14 @@ TEST(UserScriptTest, Pickle) {
UserScript script2;
script2.Unpickle(pickle, &iter);
- EXPECT_EQ(script1.url(), script2.url());
+ EXPECT_EQ(1U, script2.js_scripts().size());
+ EXPECT_EQ(script1.js_scripts()[0].url(), script2.js_scripts()[0].url());
+
+ EXPECT_EQ(2U, script2.css_scripts().size());
+ for (size_t i = 0; i < script2.js_scripts().size(); ++i) {
+ EXPECT_EQ(script1.css_scripts()[i].url(), script2.css_scripts()[i].url());
+ }
+
ASSERT_EQ(script1.globs().size(), script2.globs().size());
for (size_t i = 0; i < script1.globs().size(); ++i) {
EXPECT_EQ(script1.globs()[i], script2.globs()[i]);