summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension.cc
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/browser/extensions/extension.cc
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/browser/extensions/extension.cc')
-rw-r--r--chrome/browser/extensions/extension.cc220
1 files changed, 145 insertions, 75 deletions
diff --git a/chrome/browser/extensions/extension.cc b/chrome/browser/extensions/extension.cc
index 75d8930..924c4ed 100644
--- a/chrome/browser/extensions/extension.cc
+++ b/chrome/browser/extensions/extension.cc
@@ -19,6 +19,7 @@ const wchar_t* Extension::kDescriptionKey = L"description";
const wchar_t* Extension::kFormatVersionKey = L"format_version";
const wchar_t* Extension::kIdKey = L"id";
const wchar_t* Extension::kJsKey = L"js";
+const wchar_t* Extension::kCssKey = L"css";
const wchar_t* Extension::kMatchesKey = L"matches";
const wchar_t* Extension::kNameKey = L"name";
const wchar_t* Extension::kRunAtKey = L"run_at";
@@ -39,6 +40,10 @@ const char* Extension::kInvalidContentScriptError =
"Invalid value for 'content_scripts[*]'.";
const char* Extension::kInvalidContentScriptsListError =
"Invalid value for 'content_scripts'.";
+const char* Extension::kInvalidCssError =
+ "Invalid value for 'content_scripts[*].css[*]'.";
+const char* Extension::kInvalidCssListError =
+ "Required value 'content_scripts[*].css is invalid.";
const char* Extension::kInvalidDescriptionError =
"Invalid value for 'description'.";
const char* Extension::kInvalidFormatVersionError =
@@ -51,7 +56,7 @@ const char* Extension::kInvalidJsCountError =
const char* Extension::kInvalidJsError =
"Invalid value for 'content_scripts[*].js[*]'.";
const char* Extension::kInvalidJsListError =
- "Required value 'content_scripts[*].js is missing or invalid.";
+ "Required value 'content_scripts[*].js is invalid.";
const char* Extension::kInvalidManifestError =
"Manifest is missing or invalid.";
const char* Extension::kInvalidMatchCountError =
@@ -63,16 +68,18 @@ const char* Extension::kInvalidMatchesError =
"Required value 'content_scripts[*].matches' is missing or invalid.";
const char* Extension::kInvalidNameError =
"Required value 'name' is missing or invalid.";
+const char* Extension::kInvalidPluginsDirError =
+ "Invalid value for 'plugins_dir'.";
const char* Extension::kInvalidRunAtError =
"Invalid value for 'content_scripts[*].run_at'.";
+const char* Extension::kInvalidToolstripError =
+ "Invalid value for 'toolstrip'.";
const char* Extension::kInvalidVersionError =
"Required value 'version' is missing or invalid.";
const char* Extension::kInvalidZipHashError =
"Required key 'zip_hash' is missing or invalid.";
-const char* Extension::kInvalidPluginsDirError =
- "Invalid value for 'plugins_dir'.";
-const char* Extension::kInvalidToolstripError =
- "Invalid value for 'toolstrip'.";
+const char* Extension::kMissingFileError =
+ "At least one js or css file is required for 'content_scripts[*]'.";
const size_t Extension::kIdSize = 20; // SHA1 (160 bits) == 20 bytes
@@ -185,6 +192,135 @@ Extension::Extension(const FilePath& path) {
#endif
}
+// Helper method that loads a UserScript object from a dictionary in the
+// content_script list of the manifest.
+bool Extension::LoadUserScriptHelper(const DictionaryValue* content_script,
+ int definition_index, std::string* error,
+ UserScript* result) {
+ // run_at
+ if (content_script->HasKey(kRunAtKey)) {
+ std::string run_location;
+ if (!content_script->GetString(kRunAtKey, &run_location)) {
+ *error = FormatErrorMessage(kInvalidRunAtError,
+ IntToString(definition_index));
+ return false;
+ }
+
+ if (run_location == kRunAtDocumentStartValue) {
+ result->set_run_location(UserScript::DOCUMENT_START);
+ } else if (run_location == kRunAtDocumentEndValue) {
+ result->set_run_location(UserScript::DOCUMENT_END);
+ } else {
+ *error = FormatErrorMessage(kInvalidRunAtError,
+ IntToString(definition_index));
+ return false;
+ }
+ }
+
+ // matches
+ ListValue* matches = NULL;
+ if (!content_script->GetList(kMatchesKey, &matches)) {
+ *error = FormatErrorMessage(kInvalidMatchesError,
+ IntToString(definition_index));
+ return false;
+ }
+
+ if (matches->GetSize() == 0) {
+ *error = FormatErrorMessage(kInvalidMatchCountError,
+ IntToString(definition_index));
+ return false;
+ }
+ for (size_t j = 0; j < matches->GetSize(); ++j) {
+ std::string match_str;
+ if (!matches->GetString(j, &match_str)) {
+ *error = FormatErrorMessage(kInvalidMatchError,
+ IntToString(definition_index),
+ IntToString(j));
+ return false;
+ }
+
+ URLPattern pattern;
+ if (!pattern.Parse(match_str)) {
+ *error = FormatErrorMessage(kInvalidMatchError,
+ IntToString(definition_index),
+ IntToString(j));
+ return false;
+ }
+
+ result->add_url_pattern(pattern);
+ }
+
+ // js and css keys
+ ListValue* js = NULL;
+ if (content_script->HasKey(kJsKey) &&
+ !content_script->GetList(kJsKey, &js)) {
+ *error = FormatErrorMessage(kInvalidJsListError,
+ IntToString(definition_index));
+ return false;
+ }
+
+ ListValue* css = NULL;
+ if (content_script->HasKey(kCssKey) &&
+ !content_script->GetList(kCssKey, &css)) {
+ *error = FormatErrorMessage(kInvalidCssListError,
+ IntToString(definition_index));
+ return false;
+ }
+
+ // The manifest needs to have at least one js or css user script definition.
+ if (((js ? js->GetSize() : 0) + (css ? css->GetSize() : 0)) == 0) {
+ *error = FormatErrorMessage(kMissingFileError,
+ IntToString(definition_index));
+ return false;
+ }
+
+ // NOTE: Only one js file is supported for now.
+ // TODO(aa): Add support for multiple js files.
+ if (js && js->GetSize() != 1) {
+ *error = FormatErrorMessage(kInvalidJsCountError,
+ IntToString(definition_index));
+ return false;
+ }
+
+ if (js) {
+ for (size_t script_index = 0; script_index < js->GetSize();
+ ++script_index) {
+ Value* value;
+ std::wstring relative;
+ if (!js->Get(script_index, &value) || !value->GetAsString(&relative)) {
+ *error = FormatErrorMessage(kInvalidJsError,
+ IntToString(definition_index),
+ IntToString(script_index));
+ return false;
+ }
+ // TODO(georged): Make GetResourceURL accept wstring too
+ GURL url = GetResourceURL(WideToUTF8(relative));
+ FilePath path = GetResourcePath(WideToUTF8(relative));
+ result->js_scripts().push_back(UserScript::File(path, url));
+ }
+ }
+
+ if (css) {
+ for (size_t script_index = 0; script_index < css->GetSize();
+ ++script_index) {
+ Value* value;
+ std::wstring relative;
+ if (!css->Get(script_index, &value) || !value->GetAsString(&relative)) {
+ *error = FormatErrorMessage(kInvalidCssError,
+ IntToString(definition_index),
+ IntToString(script_index));
+ return false;
+ }
+ // TODO(georged): Make GetResourceURL accept wstring too
+ GURL url = GetResourceURL(WideToUTF8(relative));
+ FilePath path = GetResourcePath(WideToUTF8(relative));
+ result->css_scripts().push_back(UserScript::File(path, url));
+ }
+ }
+
+ return true;
+}
+
bool Extension::InitFromValue(const DictionaryValue& source,
std::string* error) {
// Check format version.
@@ -300,80 +436,14 @@ bool Extension::InitFromValue(const DictionaryValue& source,
for (size_t i = 0; i < list_value->GetSize(); ++i) {
DictionaryValue* content_script;
if (!list_value->GetDictionary(i, &content_script)) {
- *error = FormatErrorMessage(kInvalidContentScriptError, IntToString(i));
- return false;
- }
-
- ListValue* matches;
- ListValue* js;
-
- if (!content_script->GetList(kMatchesKey, &matches)) {
- *error = FormatErrorMessage(kInvalidMatchesError, IntToString(i));
- return false;
- }
-
- if (!content_script->GetList(kJsKey, &js)) {
- *error = FormatErrorMessage(kInvalidJsListError, IntToString(i));
- return false;
- }
-
- if (matches->GetSize() == 0) {
- *error = FormatErrorMessage(kInvalidMatchCountError, IntToString(i));
- return false;
- }
-
- // NOTE: Only one file is supported right now.
- if (js->GetSize() != 1) {
- *error = FormatErrorMessage(kInvalidJsCountError, IntToString(i));
+ *error = FormatErrorMessage(kInvalidContentScriptError,
+ IntToString(i));
return false;
}
UserScript script;
- if (content_script->HasKey(kRunAtKey)) {
- std::string run_location;
- if (!content_script->GetString(kRunAtKey, &run_location)) {
- *error = FormatErrorMessage(kInvalidRunAtError, IntToString(i));
- return false;
- }
-
- if (run_location == kRunAtDocumentStartValue) {
- script.set_run_location(UserScript::DOCUMENT_START);
- } else if (run_location == kRunAtDocumentEndValue) {
- script.set_run_location(UserScript::DOCUMENT_END);
- } else {
- *error = FormatErrorMessage(kInvalidRunAtError, IntToString(i));
- return false;
- }
- }
-
- for (size_t j = 0; j < matches->GetSize(); ++j) {
- std::string match_str;
- if (!matches->GetString(j, &match_str)) {
- *error = FormatErrorMessage(kInvalidMatchError, IntToString(i),
- IntToString(j));
- return false;
- }
-
- 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.
- std::string file;
- if (!js->GetString(0, &file)) {
- *error = FormatErrorMessage(kInvalidJsError, IntToString(i),
- IntToString(0));
- return false;
- }
- script.set_path(Extension::GetResourcePath(path(), file));
- script.set_url(Extension::GetResourceURL(url(), file));
-
+ if (!LoadUserScriptHelper(content_script, i, error, &script))
+ return false; // Failed to parse script context definition
content_scripts_.push_back(script);
}
}