diff options
author | erikkay@chromium.org <erikkay@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-13 14:59:01 +0000 |
---|---|---|
committer | erikkay@chromium.org <erikkay@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-13 14:59:01 +0000 |
commit | cb691e8ff78e8bac8b155a5d76625184a06aaa72 (patch) | |
tree | 8567356ad3620c50cc166a40f57e0d53c1ed2efe | |
parent | be3b19a50ec04d881b3d11ae3c7aac4e7cab4261 (diff) | |
download | chromium_src-cb691e8ff78e8bac8b155a5d76625184a06aaa72.zip chromium_src-cb691e8ff78e8bac8b155a5d76625184a06aaa72.tar.gz chromium_src-cb691e8ff78e8bac8b155a5d76625184a06aaa72.tar.bz2 |
Refactor extension constants to improve readability, debugging, and to make it easier to add new constants.
Review URL: http://codereview.chromium.org/155407
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@20483 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/extensions/extension_updater_unittest.cc | 7 | ||||
-rw-r--r-- | chrome/browser/extensions/extensions_service.cc | 8 | ||||
-rw-r--r-- | chrome/browser/extensions/extensions_service_unittest.cc | 7 | ||||
-rw-r--r-- | chrome/chrome.gyp | 2 | ||||
-rw-r--r-- | chrome/common/extensions/extension.cc | 333 | ||||
-rw-r--r-- | chrome/common/extensions/extension.h | 79 | ||||
-rw-r--r-- | chrome/common/extensions/extension_constants.cc | 136 | ||||
-rw-r--r-- | chrome/common/extensions/extension_constants.h | 94 | ||||
-rw-r--r-- | chrome/common/extensions/extension_unittest.cc | 153 | ||||
-rw-r--r-- | chrome/common/extensions/extension_unpacker.cc | 5 |
10 files changed, 436 insertions, 388 deletions
diff --git a/chrome/browser/extensions/extension_updater_unittest.cc b/chrome/browser/extensions/extension_updater_unittest.cc index 05686d4..55acce7 100644 --- a/chrome/browser/extensions/extension_updater_unittest.cc +++ b/chrome/browser/extensions/extension_updater_unittest.cc @@ -8,6 +8,7 @@ #include "chrome/browser/extensions/extensions_service.h" #include "chrome/browser/net/test_url_fetcher_factory.h" #include "chrome/common/extensions/extension.h" +#include "chrome/common/extensions/extension_constants.h" #include "chrome/common/extensions/extension_error_reporter.h" #include "net/url_request/url_request_status.h" #include "testing/gtest/include/gtest/gtest.h" @@ -116,8 +117,10 @@ void CreateTestExtensions(int count, ExtensionList *list) { for (int i = 1; i <= count; i++) { DictionaryValue input; Extension* e = new Extension(); - input.SetString(Extension::kVersionKey, StringPrintf("%d.0.0.0", i)); - input.SetString(Extension::kNameKey, StringPrintf("Extension %d", i)); + input.SetString(extension_manifest_keys::kVersion, + StringPrintf("%d.0.0.0", i)); + input.SetString(extension_manifest_keys::kName, + StringPrintf("Extension %d", i)); std::string error; EXPECT_TRUE(e->InitFromValue(input, false, &error)); list->push_back(e); diff --git a/chrome/browser/extensions/extensions_service.cc b/chrome/browser/extensions/extensions_service.cc index 57247cb..27031d8 100644 --- a/chrome/browser/extensions/extensions_service.cc +++ b/chrome/browser/extensions/extensions_service.cc @@ -32,6 +32,7 @@ #include "chrome/browser/utility_process_host.h" #include "chrome/common/chrome_switches.h" #include "chrome/common/extensions/extension.h" +#include "chrome/common/extensions/extension_constants.h" #include "chrome/common/extensions/extension_error_reporter.h" #include "chrome/common/extensions/extension_unpacker.h" #include "chrome/common/json_value_serializer.h" @@ -164,7 +165,7 @@ class ExtensionsServiceBackend::UnpackerClient // The const_cast is hacky, but seems like the right thing here, rather than // making a full copy just to make this change. const_cast<DictionaryValue*>(&manifest)->SetString( - Extension::kPublicKeyKey, public_key_); + extension_manifest_keys::kPublicKey, public_key_); // The extension was unpacked to the temp dir inside our unpacking dir. FilePath extension_dir = temp_extension_path_.DirName().AppendASCII( @@ -682,7 +683,7 @@ DictionaryValue* ExtensionsServiceBackend::ReadManifest(FilePath manifest_path, return NULL; if (!root->IsType(Value::TYPE_DICTIONARY)) { - *error = Extension::kInvalidManifestError; + *error = extension_manifest_errors::kInvalidManifest; return NULL; } @@ -696,7 +697,8 @@ Extension* ExtensionsServiceBackend::LoadExtension( FilePath manifest_path = extension_path.AppendASCII(Extension::kManifestFilename); if (!file_util::PathExists(manifest_path)) { - ReportExtensionLoadError(extension_path, Extension::kInvalidManifestError); + ReportExtensionLoadError(extension_path, + extension_manifest_errors::kInvalidManifest); return NULL; } diff --git a/chrome/browser/extensions/extensions_service_unittest.cc b/chrome/browser/extensions/extensions_service_unittest.cc index ac668e1f..dd77d47 100644 --- a/chrome/browser/extensions/extensions_service_unittest.cc +++ b/chrome/browser/extensions/extensions_service_unittest.cc @@ -18,6 +18,7 @@ #include "chrome/browser/extensions/external_extension_provider.h" #include "chrome/browser/extensions/external_pref_extension_provider.h" #include "chrome/common/extensions/extension.h" +#include "chrome/common/extensions/extension_constants.h" #include "chrome/common/extensions/url_pattern.h" #include "chrome/common/chrome_paths.h" #include "chrome/common/chrome_switches.h" @@ -604,15 +605,15 @@ TEST_F(ExtensionsServiceTest, LoadAllExtensionsFromDirectoryFail) { EXPECT_TRUE(MatchPattern(GetErrors()[1], std::string("Could not load extension from '*'. ") + - Extension::kInvalidManifestError)) << GetErrors()[1]; + extension_manifest_errors::kInvalidManifest)) << GetErrors()[1]; EXPECT_TRUE(MatchPattern(GetErrors()[2], std::string("Could not load extension from '*'. ") + - Extension::kMissingFileError)) << GetErrors()[2]; + extension_manifest_errors::kMissingFile)) << GetErrors()[2]; EXPECT_TRUE(MatchPattern(GetErrors()[3], std::string("Could not load extension from '*'. ") + - Extension::kInvalidManifestError)) << GetErrors()[3]; + extension_manifest_errors::kInvalidManifest)) << GetErrors()[3]; }; // Test that partially deleted extensions are cleaned up during startup diff --git a/chrome/chrome.gyp b/chrome/chrome.gyp index 0a534ce..58faf97 100644 --- a/chrome/chrome.gyp +++ b/chrome/chrome.gyp @@ -294,6 +294,8 @@ # All .cc, .h, and .mm files under chrome/common except for tests. 'common/extensions/extension.cc', 'common/extensions/extension.h', + 'common/extensions/extension_constants.cc', + 'common/extensions/extension_constants.h', 'common/extensions/extension_error_reporter.cc', 'common/extensions/extension_error_reporter.h', 'common/extensions/extension_error_utils.cc', diff --git a/chrome/common/extensions/extension.cc b/chrome/common/extensions/extension.cc index 6bce681..2dc6519 100644 --- a/chrome/common/extensions/extension.cc +++ b/chrome/common/extensions/extension.cc @@ -13,6 +13,7 @@ #include "base/third_party/nss/blapi.h" #include "base/third_party/nss/sha256.h" #include "chrome/common/chrome_constants.h" +#include "chrome/common/extensions/extension_constants.h" #include "chrome/common/extensions/extension_error_reporter.h" #include "chrome/common/extensions/extension_error_utils.h" #include "chrome/common/extensions/user_script.h" @@ -24,6 +25,10 @@ #include "base/registry.h" #endif +namespace keys = extension_manifest_keys; +namespace values = extension_manifest_values; +namespace errors = extension_manifest_errors; + namespace { const int kPEMOutputColumns = 65; @@ -46,144 +51,22 @@ namespace { } }; +// static int Extension::id_counter_ = 0; const char Extension::kManifestFilename[] = "manifest.json"; -const wchar_t* Extension::kBackgroundKey = L"background_page"; -const wchar_t* Extension::kContentScriptsKey = L"content_scripts"; -const wchar_t* Extension::kCssKey = L"css"; -const wchar_t* Extension::kDescriptionKey = L"description"; -const wchar_t* Extension::kIconPathKey = L"icon"; -const wchar_t* Extension::kIconPathsKey = L"icons"; -const wchar_t* Extension::kJsKey = L"js"; -const wchar_t* Extension::kMatchesKey = L"matches"; -const wchar_t* Extension::kNameKey = L"name"; -const wchar_t* Extension::kPageActionIdKey = L"id"; -const wchar_t* Extension::kPageActionsKey = L"page_actions"; -const wchar_t* Extension::kPermissionsKey = L"permissions"; -const wchar_t* Extension::kPluginsKey = L"plugins"; -const wchar_t* Extension::kPluginsPathKey = L"path"; -const wchar_t* Extension::kPluginsPublicKey = L"public"; -const wchar_t* Extension::kPublicKeyKey = L"key"; -const wchar_t* Extension::kRunAtKey = L"run_at"; -const wchar_t* Extension::kSignatureKey = L"signature"; -const wchar_t* Extension::kThemeKey = L"theme"; -const wchar_t* Extension::kThemeImagesKey = L"images"; -const wchar_t* Extension::kThemeColorsKey = L"colors"; -const wchar_t* Extension::kThemeTintsKey = L"tints"; -const wchar_t* Extension::kThemeDisplayPropertiesKey = L"properties"; -const wchar_t* Extension::kToolstripsKey = L"toolstrips"; -const wchar_t* Extension::kTypeKey = L"type"; -const wchar_t* Extension::kVersionKey = L"version"; -const wchar_t* Extension::kUpdateURLKey = L"update_url"; - -const char* Extension::kRunAtDocumentStartValue = "document_start"; -const char* Extension::kRunAtDocumentEndValue = "document_end"; -const char* Extension::kPageActionTypeTab = "tab"; -const char* Extension::kPageActionTypePermanent = "permanent"; - // A list of all the keys allowed by themes. static const wchar_t* kValidThemeKeys[] = { - Extension::kDescriptionKey, - Extension::kIconPathKey, - Extension::kNameKey, - Extension::kPublicKeyKey, - Extension::kSignatureKey, - Extension::kThemeKey, - Extension::kVersionKey + keys::kDescription, + keys::kIconPath, + keys::kName, + keys::kPublicKey, + keys::kSignature, + keys::kTheme, + keys::kVersion }; -// Extension-related error messages. Some of these are simple patterns, where a -// '*' is replaced at runtime with a specific value. This is used instead of -// printf because we want to unit test them and scanf is hard to make -// cross-platform. -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::kInvalidJsError = - "Invalid value for 'content_scripts[*].js[*]'."; -const char* Extension::kInvalidJsListError = - "Required value 'content_scripts[*].js is invalid."; -const char* Extension::kInvalidKeyError = - "Value 'key' is missing or invalid."; -const char* Extension::kInvalidManifestError = - "Manifest is missing or invalid."; -const char* Extension::kInvalidMatchCountError = - "Invalid value for 'content_scripts[*].matches. There must be at least one " - "match specified."; -const char* Extension::kInvalidMatchError = - "Invalid value for 'content_scripts[*].matches[*]'."; -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::kInvalidPageActionError = - "Invalid value for 'page_actions[*]'."; -const char* Extension::kInvalidPageActionIconPathError = - "Invalid value for 'page_actions[*].icons[*]'."; -const char* Extension::kInvalidPageActionsListError = - "Invalid value for 'page_actions'."; -const char* Extension::kInvalidPageActionIconPathsError = - "Required value 'page_actions[*].icons' is missing or invalid."; -const char* Extension::kInvalidPageActionIdError = - "Required value 'id' is missing or invalid."; -const char* Extension::kInvalidPageActionTypeValueError = - "Invalid value for 'page_actions[*].type', expected 'tab' or 'permanent'."; -const char* Extension::kInvalidPermissionsError = - "Required value 'permissions' is missing or invalid."; -const char* Extension::kInvalidPermissionCountWarning = - "Warning, 'permissions' key found, but array is empty."; -const char* Extension::kInvalidPermissionError = - "Invalid value for 'permissions[*]'."; -const char* Extension::kInvalidPermissionSchemeError = - "Invalid scheme for 'permissions[*]'. Only 'http' and 'https' are " - "allowed."; -const char* Extension::kInvalidPluginsError = - "Invalid value for 'plugins'."; -const char* Extension::kInvalidPluginsPathError = - "Invalid value for 'plugins[*].path'."; -const char* Extension::kInvalidPluginsPublicError = - "Invalid value for 'plugins[*].public'."; -const char* Extension::kInvalidBackgroundError = - "Invalid value for 'background'."; -const char* Extension::kInvalidRunAtError = - "Invalid value for 'content_scripts[*].run_at'."; -const char* Extension::kInvalidSignatureError = - "Value 'signature' is missing or invalid."; -const char* Extension::kInvalidToolstripError = - "Invalid value for 'toolstrips[*]'"; -const char* Extension::kInvalidToolstripsError = - "Invalid value for 'toolstrips'."; -const char* Extension::kInvalidVersionError = - "Required value 'version' is missing or invalid. It must be between 1-4 " - "dot-separated integers."; -const char* Extension::kInvalidZipHashError = - "Required key 'zip_hash' is missing or invalid."; -const char* Extension::kMissingFileError = - "At least one js or css file is required for 'content_scripts[*]'."; -const char* Extension::kInvalidThemeError = - "Invalid value for 'theme'."; -const char* Extension::kInvalidThemeImagesError = - "Invalid value for theme images - images must be strings."; -const char* Extension::kInvalidThemeImagesMissingError = - "Am image specified in the theme is missing."; -const char* Extension::kInvalidThemeColorsError = - "Invalid value for theme colors - colors must be integers"; -const char* Extension::kInvalidThemeTintsError = - "Invalid value for theme images - tints must be decimal numbers."; -const char* Extension::kInvalidUpdateURLError = - "Invalid value for update url: '[*]'."; -const char* Extension::kThemesCannotContainExtensionsError = - "A theme cannot contain extensions code."; - #if defined(OS_WIN) const char* Extension::kExtensionRegistryPath = "Software\\Google\\Chrome\\Extensions"; @@ -282,20 +165,20 @@ bool Extension::LoadUserScriptHelper(const DictionaryValue* content_script, int definition_index, std::string* error, UserScript* result) { // run_at - if (content_script->HasKey(kRunAtKey)) { + if (content_script->HasKey(keys::kRunAt)) { std::string run_location; - if (!content_script->GetString(kRunAtKey, &run_location)) { - *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidRunAtError, + if (!content_script->GetString(keys::kRunAt, &run_location)) { + *error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidRunAt, IntToString(definition_index)); return false; } - if (run_location == kRunAtDocumentStartValue) { + if (run_location == values::kRunAtDocumentStart) { result->set_run_location(UserScript::DOCUMENT_START); - } else if (run_location == kRunAtDocumentEndValue) { + } else if (run_location == values::kRunAtDocumentEnd) { result->set_run_location(UserScript::DOCUMENT_END); } else { - *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidRunAtError, + *error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidRunAt, IntToString(definition_index)); return false; } @@ -303,28 +186,28 @@ bool Extension::LoadUserScriptHelper(const DictionaryValue* content_script, // matches ListValue* matches = NULL; - if (!content_script->GetList(kMatchesKey, &matches)) { - *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidMatchesError, + if (!content_script->GetList(keys::kMatches, &matches)) { + *error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidMatches, IntToString(definition_index)); return false; } if (matches->GetSize() == 0) { - *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidMatchCountError, + *error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidMatchCount, 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 = ExtensionErrorUtils::FormatErrorMessage(kInvalidMatchError, + *error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidMatch, IntToString(definition_index), IntToString(j)); return false; } URLPattern pattern; if (!pattern.Parse(match_str)) { - *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidMatchError, + *error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidMatch, IntToString(definition_index), IntToString(j)); return false; } @@ -334,24 +217,24 @@ bool Extension::LoadUserScriptHelper(const DictionaryValue* content_script, // js and css keys ListValue* js = NULL; - if (content_script->HasKey(kJsKey) && - !content_script->GetList(kJsKey, &js)) { - *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidJsListError, + if (content_script->HasKey(keys::kJs) && + !content_script->GetList(keys::kJs, &js)) { + *error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidJsList, IntToString(definition_index)); return false; } ListValue* css = NULL; - if (content_script->HasKey(kCssKey) && - !content_script->GetList(kCssKey, &css)) { - *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidCssListError, + if (content_script->HasKey(keys::kCss) && + !content_script->GetList(keys::kCss, &css)) { + *error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidCssList, 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 = ExtensionErrorUtils::FormatErrorMessage(kMissingFileError, + *error = ExtensionErrorUtils::FormatErrorMessage(errors::kMissingFile, IntToString(definition_index)); return false; } @@ -362,7 +245,7 @@ bool Extension::LoadUserScriptHelper(const DictionaryValue* content_script, Value* value; std::wstring relative; if (!js->Get(script_index, &value) || !value->GetAsString(&relative)) { - *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidJsError, + *error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidJs, IntToString(definition_index), IntToString(script_index)); return false; } @@ -379,7 +262,7 @@ bool Extension::LoadUserScriptHelper(const DictionaryValue* content_script, Value* value; std::wstring relative; if (!css->Get(script_index, &value) || !value->GetAsString(&relative)) { - *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidCssError, + *error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidCss, IntToString(definition_index), IntToString(script_index)); return false; } @@ -403,11 +286,11 @@ PageAction* Extension::LoadPageActionHelper( ListValue* icons; // Read the page action |icons|. - if (!page_action->HasKey(kIconPathsKey) || - !page_action->GetList(kIconPathsKey, &icons) || + if (!page_action->HasKey(keys::kIconPaths) || + !page_action->GetList(keys::kIconPaths, &icons) || icons->GetSize() == 0) { *error = ExtensionErrorUtils::FormatErrorMessage( - kInvalidPageActionIconPathsError, IntToString(definition_index)); + errors::kInvalidPageActionIconPaths, IntToString(definition_index)); return NULL; } @@ -417,7 +300,7 @@ PageAction* Extension::LoadPageActionHelper( FilePath::StringType path; if (!(*iter)->GetAsString(&path) || path.empty()) { *error = ExtensionErrorUtils::FormatErrorMessage( - kInvalidPageActionIconPathError, + errors::kInvalidPageActionIconPath, IntToString(definition_index), IntToString(icon_count)); return NULL; } @@ -429,17 +312,17 @@ PageAction* Extension::LoadPageActionHelper( // Read the page action |id|. std::string id; - if (!page_action->GetString(kPageActionIdKey, &id)) { - *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidPageActionIdError, - IntToString(definition_index)); + if (!page_action->GetString(keys::kPageActionId, &id)) { + *error = ExtensionErrorUtils::FormatErrorMessage( + errors::kInvalidPageActionId, IntToString(definition_index)); return NULL; } result->set_id(id); // Read the page action |name|. std::string name; - if (!page_action->GetString(kNameKey, &name)) { - *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidNameError, + if (!page_action->GetString(keys::kName, &name)) { + *error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidName, IntToString(definition_index)); return NULL; } @@ -448,15 +331,15 @@ PageAction* Extension::LoadPageActionHelper( // Read the page action |type|. It is optional and set to permanent if // missing. std::string type; - if (!page_action->GetString(kTypeKey, &type)) { + if (!page_action->GetString(keys::kType, &type)) { result->set_type(PageAction::PERMANENT); - } else if (!LowerCaseEqualsASCII(type, kPageActionTypeTab) && - !LowerCaseEqualsASCII(type, kPageActionTypePermanent)) { + } else if (!LowerCaseEqualsASCII(type, values::kPageActionTypeTab) && + !LowerCaseEqualsASCII(type, values::kPageActionTypePermanent)) { *error = ExtensionErrorUtils::FormatErrorMessage( - kInvalidPageActionTypeValueError, IntToString(definition_index)); + errors::kInvalidPageActionTypeValue, IntToString(definition_index)); return NULL; } else { - if (LowerCaseEqualsASCII(type, kPageActionTypeTab)) + if (LowerCaseEqualsASCII(type, values::kPageActionTypeTab)) result->set_type(PageAction::TAB); else result->set_type(PageAction::PERMANENT); @@ -615,16 +498,16 @@ bool Extension::FormatPEMForFileOutput(const std::string input, bool Extension::InitFromValue(const DictionaryValue& source, bool require_id, std::string* error) { - if (source.HasKey(kPublicKeyKey)) { + if (source.HasKey(keys::kPublicKey)) { std::string public_key_bytes; - if (!source.GetString(kPublicKeyKey, &public_key_) || + if (!source.GetString(keys::kPublicKey, &public_key_) || !ParsePEMKeyBytes(public_key_, &public_key_bytes) || !GenerateIdFromPublicKey(public_key_bytes, &id_)) { - *error = kInvalidKeyError; + *error = errors::kInvalidKey; return false; } } else if (require_id) { - *error = kInvalidKeyError; + *error = errors::kInvalidKey; return false; } else { // Generate a random ID @@ -643,70 +526,70 @@ bool Extension::InitFromValue(const DictionaryValue& source, bool require_id, // Initialize version. std::string version_str; - if (!source.GetString(kVersionKey, &version_str)) { - *error = kInvalidVersionError; + if (!source.GetString(keys::kVersion, &version_str)) { + *error = errors::kInvalidVersion; return false; } version_.reset(Version::GetVersionFromString(version_str)); if (!version_.get() || version_->components().size() > 4) { - *error = kInvalidVersionError; + *error = errors::kInvalidVersion; return false; } // Initialize name. - if (!source.GetString(kNameKey, &name_)) { - *error = kInvalidNameError; + if (!source.GetString(keys::kName, &name_)) { + *error = errors::kInvalidName; return false; } // Initialize description (optional). - if (source.HasKey(kDescriptionKey)) { - if (!source.GetString(kDescriptionKey, &description_)) { - *error = kInvalidDescriptionError; + if (source.HasKey(keys::kDescription)) { + if (!source.GetString(keys::kDescription, &description_)) { + *error = errors::kInvalidDescription; return false; } } // Initialize update url (if present). - if (source.HasKey(kUpdateURLKey)) { + if (source.HasKey(keys::kUpdateURL)) { std::string tmp; - if (!source.GetString(kUpdateURLKey, &tmp)) { - *error = - ExtensionErrorUtils::FormatErrorMessage(kInvalidUpdateURLError, ""); + if (!source.GetString(keys::kUpdateURL, &tmp)) { + *error = ExtensionErrorUtils::FormatErrorMessage( + errors::kInvalidUpdateURL, ""); return false; } update_url_ = GURL(tmp); if (!update_url_.is_valid() || update_url_.has_ref()) { - *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidUpdateURLError, - tmp); + *error = ExtensionErrorUtils::FormatErrorMessage( + errors::kInvalidUpdateURL, tmp); return false; } } // Initialize themes. is_theme_ = false; - if (source.HasKey(kThemeKey)) { + if (source.HasKey(keys::kTheme)) { // Themes cannot contain extension keys. if (ContainsNonThemeKeys(source)) { - *error = kThemesCannotContainExtensionsError; + *error = errors::kThemesCannotContainExtensions; return false; } DictionaryValue* theme_value; - if (!source.GetDictionary(kThemeKey, &theme_value)) { - *error = kInvalidThemeError; + if (!source.GetDictionary(keys::kTheme, &theme_value)) { + *error = errors::kInvalidTheme; return false; } is_theme_ = true; DictionaryValue* images_value; - if (theme_value->GetDictionary(kThemeImagesKey, &images_value)) { + if (theme_value->GetDictionary(keys::kThemeImages, &images_value)) { // Validate that the images are all strings DictionaryValue::key_iterator iter = images_value->begin_keys(); while (iter != images_value->end_keys()) { std::string val; if (!images_value->GetString(*iter, &val)) { - *error = kInvalidThemeImagesError; + *error = errors::kInvalidThemeImages; return false; } ++iter; @@ -716,7 +599,7 @@ bool Extension::InitFromValue(const DictionaryValue& source, bool require_id, } DictionaryValue* colors_value; - if (theme_value->GetDictionary(kThemeColorsKey, &colors_value)) { + if (theme_value->GetDictionary(keys::kThemeColors, &colors_value)) { // Validate that the colors are all three-item lists DictionaryValue::key_iterator iter = colors_value->begin_keys(); while (iter != colors_value->end_keys()) { @@ -742,7 +625,7 @@ bool Extension::InitFromValue(const DictionaryValue& source, bool require_id, } } } - *error = kInvalidThemeColorsError; + *error = errors::kInvalidThemeColors; return false; ++iter; } @@ -751,7 +634,7 @@ bool Extension::InitFromValue(const DictionaryValue& source, bool require_id, } DictionaryValue* tints_value; - if (theme_value->GetDictionary(kThemeTintsKey, &tints_value)) { + if (theme_value->GetDictionary(keys::kThemeTints, &tints_value)) { // Validate that the tints are all reals. DictionaryValue::key_iterator iter = tints_value->begin_keys(); while (iter != tints_value->end_keys()) { @@ -762,7 +645,7 @@ bool Extension::InitFromValue(const DictionaryValue& source, bool require_id, !tint_list->GetReal(0, &hue) || !tint_list->GetReal(1, &hue) || !tint_list->GetReal(2, &hue)) { - *error = kInvalidThemeTintsError; + *error = errors::kInvalidThemeTints; return false; } ++iter; @@ -772,7 +655,7 @@ bool Extension::InitFromValue(const DictionaryValue& source, bool require_id, } DictionaryValue* display_properties_value; - if (theme_value->GetDictionary(kThemeDisplayPropertiesKey, + if (theme_value->GetDictionary(keys::kThemeDisplayProperties, &display_properties_value)) { theme_display_properties_.reset( static_cast<DictionaryValue*>(display_properties_value->DeepCopy())); @@ -782,10 +665,10 @@ bool Extension::InitFromValue(const DictionaryValue& source, bool require_id, } // Initialize plugins (optional). - if (source.HasKey(kPluginsKey)) { + if (source.HasKey(keys::kPlugins)) { ListValue* list_value; - if (!source.GetList(kPluginsKey, &list_value)) { - *error = kInvalidPluginsError; + if (!source.GetList(keys::kPlugins, &list_value)) { + *error = errors::kInvalidPlugins; return false; } @@ -795,22 +678,22 @@ bool Extension::InitFromValue(const DictionaryValue& source, bool require_id, bool is_public = false; if (!list_value->GetDictionary(i, &plugin_value)) { - *error = kInvalidPluginsError; + *error = errors::kInvalidPlugins; return false; } // Get plugins[i].path. - if (!plugin_value->GetString(kPluginsPathKey, &path)) { + if (!plugin_value->GetString(keys::kPluginsPath, &path)) { *error = ExtensionErrorUtils::FormatErrorMessage( - kInvalidPluginsPathError, IntToString(i)); + errors::kInvalidPluginsPath, IntToString(i)); return false; } // Get plugins[i].content (optional). - if (plugin_value->HasKey(kPluginsPublicKey)) { - if (!plugin_value->GetBoolean(kPluginsPublicKey, &is_public)) { + if (plugin_value->HasKey(keys::kPluginsPublic)) { + if (!plugin_value->GetBoolean(keys::kPluginsPublic, &is_public)) { *error = ExtensionErrorUtils::FormatErrorMessage( - kInvalidPluginsPublicError, IntToString(i)); + errors::kInvalidPluginsPublic, IntToString(i)); return false; } } @@ -822,28 +705,28 @@ bool Extension::InitFromValue(const DictionaryValue& source, bool require_id, } // Initialize background url (optional). - if (source.HasKey(kBackgroundKey)) { + if (source.HasKey(keys::kBackground)) { std::string background_str; - if (!source.GetString(kBackgroundKey, &background_str)) { - *error = kInvalidBackgroundError; + if (!source.GetString(keys::kBackground, &background_str)) { + *error = errors::kInvalidBackground; return false; } background_url_ = GetResourceURL(background_str); } // Initialize toolstrips (optional). - if (source.HasKey(kToolstripsKey)) { + if (source.HasKey(keys::kToolstrips)) { ListValue* list_value; - if (!source.GetList(kToolstripsKey, &list_value)) { - *error = kInvalidToolstripsError; + if (!source.GetList(keys::kToolstrips, &list_value)) { + *error = errors::kInvalidToolstrips; return false; } for (size_t i = 0; i < list_value->GetSize(); ++i) { std::string toolstrip; if (!list_value->GetString(i, &toolstrip)) { - *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidToolstripError, - IntToString(i)); + *error = ExtensionErrorUtils::FormatErrorMessage( + errors::kInvalidToolstrip, IntToString(i)); return false; } toolstrips_.push_back(toolstrip); @@ -851,10 +734,10 @@ bool Extension::InitFromValue(const DictionaryValue& source, bool require_id, } // Initialize content scripts (optional). - if (source.HasKey(kContentScriptsKey)) { + if (source.HasKey(keys::kContentScripts)) { ListValue* list_value; - if (!source.GetList(kContentScriptsKey, &list_value)) { - *error = kInvalidContentScriptsListError; + if (!source.GetList(keys::kContentScripts, &list_value)) { + *error = errors::kInvalidContentScriptsList; return false; } @@ -862,7 +745,7 @@ bool Extension::InitFromValue(const DictionaryValue& source, bool require_id, DictionaryValue* content_script; if (!list_value->GetDictionary(i, &content_script)) { *error = ExtensionErrorUtils::FormatErrorMessage( - kInvalidContentScriptError, IntToString(i)); + errors::kInvalidContentScript, IntToString(i)); return false; } @@ -875,10 +758,10 @@ bool Extension::InitFromValue(const DictionaryValue& source, bool require_id, } // Initialize page actions (optional). - if (source.HasKey(kPageActionsKey)) { + if (source.HasKey(keys::kPageActions)) { ListValue* list_value; - if (!source.GetList(kPageActionsKey, &list_value)) { - *error = kInvalidPageActionsListError; + if (!source.GetList(keys::kPageActions, &list_value)) { + *error = errors::kInvalidPageActionsList; return false; } @@ -886,7 +769,7 @@ bool Extension::InitFromValue(const DictionaryValue& source, bool require_id, DictionaryValue* page_action_value; if (!list_value->GetDictionary(i, &page_action_value)) { *error = ExtensionErrorUtils::FormatErrorMessage( - kInvalidPageActionError, IntToString(i)); + errors::kInvalidPageAction, IntToString(i)); return false; } @@ -899,31 +782,31 @@ bool Extension::InitFromValue(const DictionaryValue& source, bool require_id, } // Initialize the permissions (optional). - if (source.HasKey(kPermissionsKey)) { + if (source.HasKey(keys::kPermissions)) { ListValue* hosts = NULL; - if (!source.GetList(kPermissionsKey, &hosts)) { + if (!source.GetList(keys::kPermissions, &hosts)) { *error = ExtensionErrorUtils::FormatErrorMessage( - kInvalidPermissionsError, ""); + errors::kInvalidPermissions, ""); return false; } if (hosts->GetSize() == 0) { ExtensionErrorReporter::GetInstance()->ReportError( - kInvalidPermissionCountWarning, false); + errors::kInvalidPermissionCountWarning, false); } for (size_t i = 0; i < hosts->GetSize(); ++i) { std::string host_str; if (!hosts->GetString(i, &host_str)) { *error = ExtensionErrorUtils::FormatErrorMessage( - kInvalidPermissionError, IntToString(i)); + errors::kInvalidPermission, IntToString(i)); return false; } URLPattern pattern; if (!pattern.Parse(host_str)) { *error = ExtensionErrorUtils::FormatErrorMessage( - kInvalidPermissionError, IntToString(i)); + errors::kInvalidPermission, IntToString(i)); return false; } @@ -931,7 +814,7 @@ bool Extension::InitFromValue(const DictionaryValue& source, bool require_id, if ((pattern.scheme() != chrome::kHttpScheme) && (pattern.scheme() != chrome::kHttpsScheme)) { *error = ExtensionErrorUtils::FormatErrorMessage( - kInvalidPermissionSchemeError, IntToString(i)); + errors::kInvalidPermissionScheme, IntToString(i)); return false; } diff --git a/chrome/common/extensions/extension.h b/chrome/common/extensions/extension.h index 0b04281..8add980 100644 --- a/chrome/common/extensions/extension.h +++ b/chrome/common/extensions/extension.h @@ -54,85 +54,6 @@ class Extension { // The name of the manifest inside an extension. static const char kManifestFilename[]; - // Keys used in JSON representation of extensions. - static const wchar_t* kBackgroundKey; - static const wchar_t* kContentScriptsKey; - static const wchar_t* kCssKey; - static const wchar_t* kDescriptionKey; - static const wchar_t* kIconPathKey; - static const wchar_t* kIconPathsKey; - static const wchar_t* kJsKey; - static const wchar_t* kMatchesKey; - static const wchar_t* kNameKey; - static const wchar_t* kPageActionIdKey; - static const wchar_t* kPageActionsKey; - static const wchar_t* kPermissionsKey; - static const wchar_t* kPluginsKey; - static const wchar_t* kPluginsPathKey; - static const wchar_t* kPluginsPublicKey; - static const wchar_t* kPublicKeyKey; - static const wchar_t* kSignatureKey; - static const wchar_t* kRunAtKey; - static const wchar_t* kThemeKey; - static const wchar_t* kThemeImagesKey; - static const wchar_t* kThemeColorsKey; - static const wchar_t* kThemeTintsKey; - static const wchar_t* kThemeDisplayPropertiesKey; - static const wchar_t* kToolstripsKey; - static const wchar_t* kTypeKey; - static const wchar_t* kVersionKey; - static const wchar_t* kUpdateURLKey; - - // Some values expected in manifests. - static const char* kRunAtDocumentStartValue; - static const char* kRunAtDocumentEndValue; - static const char* kPageActionTypeTab; - static const char* kPageActionTypePermanent; - - // Error messages returned from InitFromValue(). - static const char* kInvalidContentScriptError; - static const char* kInvalidContentScriptsListError; - static const char* kInvalidCssError; - static const char* kInvalidCssListError; - static const char* kInvalidDescriptionError; - static const char* kInvalidJsError; - static const char* kInvalidJsListError; - static const char* kInvalidKeyError; - static const char* kInvalidManifestError; - static const char* kInvalidMatchCountError; - static const char* kInvalidMatchError; - static const char* kInvalidMatchesError; - static const char* kInvalidNameError; - static const char* kInvalidPluginsError; - static const char* kInvalidPluginsPathError; - static const char* kInvalidPluginsPublicError; - - static const char* kInvalidBackgroundError; - static const char* kInvalidRunAtError; - static const char* kInvalidSignatureError; - static const char* kInvalidToolstripError; - static const char* kInvalidToolstripsError; - static const char* kInvalidVersionError; - static const char* kInvalidPageActionError; - static const char* kInvalidPageActionsListError; - static const char* kInvalidPageActionIconPathError; - static const char* kInvalidPageActionIconPathsError; - static const char* kInvalidPageActionIdError; - static const char* kInvalidPageActionTypeValueError; - static const char* kInvalidPermissionsError; - static const char* kInvalidPermissionCountWarning; - static const char* kInvalidPermissionError; - static const char* kInvalidPermissionSchemeError; - static const char* kInvalidZipHashError; - static const char* kInvalidThemeError; - static const char* kInvalidThemeImagesMissingError; - static const char* kInvalidThemeImagesError; - static const char* kInvalidThemeColorsError; - static const char* kInvalidThemeTintsError; - static const char* kThemesCannotContainExtensionsError; - static const char* kMissingFileError; - static const char* kInvalidUpdateURLError; - #if defined(OS_WIN) static const char* kExtensionRegistryPath; #endif diff --git a/chrome/common/extensions/extension_constants.cc b/chrome/common/extensions/extension_constants.cc new file mode 100644 index 0000000..87ac2c7 --- /dev/null +++ b/chrome/common/extensions/extension_constants.cc @@ -0,0 +1,136 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/common/extensions/extension_constants.h" + +namespace extension_manifest_keys { + +const wchar_t* kBackground = L"background_page"; +const wchar_t* kContentScripts = L"content_scripts"; +const wchar_t* kCss = L"css"; +const wchar_t* kDescription = L"description"; +const wchar_t* kIconPath = L"icon"; +const wchar_t* kIconPaths = L"icons"; +const wchar_t* kJs = L"js"; +const wchar_t* kMatches = L"matches"; +const wchar_t* kName = L"name"; +const wchar_t* kPageActionId = L"id"; +const wchar_t* kPageActions = L"page_actions"; +const wchar_t* kPermissions = L"permissions"; +const wchar_t* kPlugins = L"plugins"; +const wchar_t* kPluginsPath = L"path"; +const wchar_t* kPluginsPublic = L"public"; +const wchar_t* kPublicKey = L"key"; +const wchar_t* kRunAt = L"run_at"; +const wchar_t* kSignature = L"signature"; +const wchar_t* kTheme = L"theme"; +const wchar_t* kThemeImages = L"images"; +const wchar_t* kThemeColors = L"colors"; +const wchar_t* kThemeTints = L"tints"; +const wchar_t* kThemeDisplayProperties = L"properties"; +const wchar_t* kToolstripPath = L"path"; +const wchar_t* kToolstrips = L"toolstrips"; +const wchar_t* kType = L"type"; +const wchar_t* kVersion = L"version"; +const wchar_t* kUpdateURL = L"update_url"; +} // namespace extension_manifest_keys + +namespace extension_manifest_values { +const char* kRunAtDocumentStart = "document_start"; +const char* kRunAtDocumentEnd = "document_end"; +const char* kPageActionTypeTab = "tab"; +const char* kPageActionTypePermanent = "permanent"; +} // namespace extension_manifest_values + +// Extension-related error messages. Some of these are simple patterns, where a +// '*' is replaced at runtime with a specific value. This is used instead of +// printf because we want to unit test them and scanf is hard to make +// cross-platform. +namespace extension_manifest_errors { +const char* kInvalidContentScript = + "Invalid value for 'content_scripts[*]'."; +const char* kInvalidContentScriptsList = + "Invalid value for 'content_scripts'."; +const char* kInvalidCss = + "Invalid value for 'content_scripts[*].css[*]'."; +const char* kInvalidCssList = + "Required value 'content_scripts[*].css is invalid."; +const char* kInvalidDescription = + "Invalid value for 'description'."; +const char* kInvalidJs = + "Invalid value for 'content_scripts[*].js[*]'."; +const char* kInvalidJsList = + "Required value 'content_scripts[*].js is invalid."; +const char* kInvalidKey = + "Value 'key' is missing or invalid."; +const char* kInvalidManifest = + "Manifest is missing or invalid."; +const char* kInvalidMatchCount = + "Invalid value for 'content_scripts[*].matches. There must be at least one " + "match specified."; +const char* kInvalidMatch = + "Invalid value for 'content_scripts[*].matches[*]'."; +const char* kInvalidMatches = + "Required value 'content_scripts[*].matches' is missing or invalid."; +const char* kInvalidName = + "Required value 'name' is missing or invalid."; +const char* kInvalidPageAction = + "Invalid value for 'page_actions[*]'."; +const char* kInvalidPageActionIconPath = + "Invalid value for 'page_actions[*].icons[*]'."; +const char* kInvalidPageActionsList = + "Invalid value for 'page_actions'."; +const char* kInvalidPageActionIconPaths = + "Required value 'page_actions[*].icons' is missing or invalid."; +const char* kInvalidPageActionId = + "Required value 'id' is missing or invalid."; +const char* kInvalidPageActionTypeValue = + "Invalid value for 'page_actions[*].type', expected 'tab' or 'permanent'."; +const char* kInvalidPermissions = + "Required value 'permissions' is missing or invalid."; +const char* kInvalidPermissionCountWarning = + "Warning, 'permissions' key found, but array is empty."; +const char* kInvalidPermission = + "Invalid value for 'permissions[*]'."; +const char* kInvalidPermissionScheme = + "Invalid scheme for 'permissions[*]'. Only 'http' and 'https' are " + "allowed."; +const char* kInvalidPlugins = + "Invalid value for 'plugins'."; +const char* kInvalidPluginsPath = + "Invalid value for 'plugins[*].path'."; +const char* kInvalidPluginsPublic = + "Invalid value for 'plugins[*].public'."; +const char* kInvalidBackground = + "Invalid value for 'background'."; +const char* kInvalidRunAt = + "Invalid value for 'content_scripts[*].run_at'."; +const char* kInvalidSignature = + "Value 'signature' is missing or invalid."; +const char* kInvalidToolstrip = + "Invalid value for 'toolstrips[*]'"; +const char* kInvalidToolstrips = + "Invalid value for 'toolstrips'."; +const char* kInvalidVersion = + "Required value 'version' is missing or invalid. It must be between 1-4 " + "dot-separated integers."; +const char* kInvalidZipHash = + "Required key 'zip_hash' is missing or invalid."; +const char* kMissingFile = + "At least one js or css file is required for 'content_scripts[*]'."; +const char* kInvalidTheme = + "Invalid value for 'theme'."; +const char* kInvalidThemeImages = + "Invalid value for theme images - images must be strings."; +const char* kInvalidThemeImagesMissing = + "Am image specified in the theme is missing."; +const char* kInvalidThemeColors = + "Invalid value for theme colors - colors must be integers"; +const char* kInvalidThemeTints = + "Invalid value for theme images - tints must be decimal numbers."; +const char* kInvalidUpdateURL = + "Invalid value for update url: '[*]'."; +const char* kThemesCannotContainExtensions = + "A theme cannot contain extensions code."; +} // namespace extension_manifest_errors diff --git a/chrome/common/extensions/extension_constants.h b/chrome/common/extensions/extension_constants.h new file mode 100644 index 0000000..f1324df --- /dev/null +++ b/chrome/common/extensions/extension_constants.h @@ -0,0 +1,94 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_CONSTANTS_H_ +#define CHROME_COMMON_EXTENSIONS_EXTENSION_CONSTANTS_H_ + +// Keys used in JSON representation of extensions. +namespace extension_manifest_keys { + extern const wchar_t* kBackground; + extern const wchar_t* kContentScripts; + extern const wchar_t* kCss; + extern const wchar_t* kDescription; + extern const wchar_t* kIconPath; + extern const wchar_t* kIconPaths; + extern const wchar_t* kJs; + extern const wchar_t* kMatches; + extern const wchar_t* kName; + extern const wchar_t* kPageActionId; + extern const wchar_t* kPageActions; + extern const wchar_t* kPermissions; + extern const wchar_t* kPlugins; + extern const wchar_t* kPluginsPath; + extern const wchar_t* kPluginsPublic; + extern const wchar_t* kPublicKey; + extern const wchar_t* kSignature; + extern const wchar_t* kRunAt; + extern const wchar_t* kTheme; + extern const wchar_t* kThemeImages; + extern const wchar_t* kThemeColors; + extern const wchar_t* kThemeTints; + extern const wchar_t* kThemeDisplayProperties; + extern const wchar_t* kToolstripPath; + extern const wchar_t* kToolstrips; + extern const wchar_t* kType; + extern const wchar_t* kVersion; + extern const wchar_t* kUpdateURL; +} // namespace extension_manifest_keys + +// Some values expected in manifests. +namespace extension_manifest_values { + extern const char* kRunAtDocumentStart; + extern const char* kRunAtDocumentEnd; + extern const char* kPageActionTypeTab; + extern const char* kPageActionTypePermanent; +} // namespace extension_manifest_values + +// Error messages returned from Extension::InitFromValue(). +namespace extension_manifest_errors { + extern const char* kInvalidContentScript; + extern const char* kInvalidContentScriptsList; + extern const char* kInvalidCss; + extern const char* kInvalidCssList; + extern const char* kInvalidDescription; + extern const char* kInvalidJs; + extern const char* kInvalidJsList; + extern const char* kInvalidKey; + extern const char* kInvalidManifest; + extern const char* kInvalidMatchCount; + extern const char* kInvalidMatch; + extern const char* kInvalidMatches; + extern const char* kInvalidName; + extern const char* kInvalidPlugins; + extern const char* kInvalidPluginsPath; + extern const char* kInvalidPluginsPublic; + + extern const char* kInvalidBackground; + extern const char* kInvalidRunAt; + extern const char* kInvalidSignature; + extern const char* kInvalidToolstrip; + extern const char* kInvalidToolstrips; + extern const char* kInvalidVersion; + extern const char* kInvalidPageAction; + extern const char* kInvalidPageActionsList; + extern const char* kInvalidPageActionIconPath; + extern const char* kInvalidPageActionIconPaths; + extern const char* kInvalidPageActionId; + extern const char* kInvalidPageActionTypeValue; + extern const char* kInvalidPermissions; + extern const char* kInvalidPermissionCountWarning; + extern const char* kInvalidPermission; + extern const char* kInvalidPermissionScheme; + extern const char* kInvalidZipHash; + extern const char* kInvalidTheme; + extern const char* kInvalidThemeImagesMissing; + extern const char* kInvalidThemeImages; + extern const char* kInvalidThemeColors; + extern const char* kInvalidThemeTints; + extern const char* kThemesCannotContainExtensions; + extern const char* kMissingFile; + extern const char* kInvalidUpdateURL; +} // namespace extension_manifest_errors + +#endif // CHROME_COMMON_EXTENSIONS_EXTENSION_CONSTANTS_H_ diff --git a/chrome/common/extensions/extension_unittest.cc b/chrome/common/extensions/extension_unittest.cc index 47f8ef8..2b5382c 100644 --- a/chrome/common/extensions/extension_unittest.cc +++ b/chrome/common/extensions/extension_unittest.cc @@ -7,10 +7,15 @@ #include "base/path_service.h" #include "chrome/common/chrome_paths.h" #include "chrome/common/extensions/extension.h" +#include "chrome/common/extensions/extension_constants.h" #include "chrome/common/extensions/extension_error_reporter.h" #include "chrome/common/json_value_serializer.h" #include "testing/gtest/include/gtest/gtest.h" +namespace keys = extension_manifest_keys; +namespace values = extension_manifest_values; +namespace errors = extension_manifest_errors; + class ExtensionTest : public testing::Test { }; @@ -46,146 +51,146 @@ TEST(ExtensionTest, InitFromValueInvalid) { // Test missing and invalid versions input_value.reset(static_cast<DictionaryValue*>(valid_value->DeepCopy())); - input_value->Remove(Extension::kVersionKey, NULL); + input_value->Remove(keys::kVersion, NULL); EXPECT_FALSE(extension.InitFromValue(*input_value, true, &error)); - EXPECT_EQ(Extension::kInvalidVersionError, error); + EXPECT_EQ(errors::kInvalidVersion, error); - input_value->SetInteger(Extension::kVersionKey, 42); + input_value->SetInteger(keys::kVersion, 42); EXPECT_FALSE(extension.InitFromValue(*input_value, true, &error)); - EXPECT_EQ(Extension::kInvalidVersionError, error); + EXPECT_EQ(errors::kInvalidVersion, error); // Test missing and invalid names input_value.reset(static_cast<DictionaryValue*>(valid_value->DeepCopy())); - input_value->Remove(Extension::kNameKey, NULL); + input_value->Remove(keys::kName, NULL); EXPECT_FALSE(extension.InitFromValue(*input_value, true, &error)); - EXPECT_EQ(Extension::kInvalidNameError, error); + EXPECT_EQ(errors::kInvalidName, error); - input_value->SetInteger(Extension::kNameKey, 42); + input_value->SetInteger(keys::kName, 42); EXPECT_FALSE(extension.InitFromValue(*input_value, true, &error)); - EXPECT_EQ(Extension::kInvalidNameError, error); + EXPECT_EQ(errors::kInvalidName, error); // Test invalid description input_value.reset(static_cast<DictionaryValue*>(valid_value->DeepCopy())); - input_value->SetInteger(Extension::kDescriptionKey, 42); + input_value->SetInteger(keys::kDescription, 42); EXPECT_FALSE(extension.InitFromValue(*input_value, true, &error)); - EXPECT_EQ(Extension::kInvalidDescriptionError, error); + EXPECT_EQ(errors::kInvalidDescription, error); // Test invalid user scripts list input_value.reset(static_cast<DictionaryValue*>(valid_value->DeepCopy())); - input_value->SetInteger(Extension::kContentScriptsKey, 42); + input_value->SetInteger(keys::kContentScripts, 42); EXPECT_FALSE(extension.InitFromValue(*input_value, true, &error)); - EXPECT_EQ(Extension::kInvalidContentScriptsListError, error); + EXPECT_EQ(errors::kInvalidContentScriptsList, error); // Test invalid user script item input_value.reset(static_cast<DictionaryValue*>(valid_value->DeepCopy())); ListValue* content_scripts = NULL; - input_value->GetList(Extension::kContentScriptsKey, &content_scripts); + input_value->GetList(keys::kContentScripts, &content_scripts); ASSERT_FALSE(NULL == content_scripts); content_scripts->Set(0, Value::CreateIntegerValue(42)); EXPECT_FALSE(extension.InitFromValue(*input_value, true, &error)); - EXPECT_TRUE(MatchPattern(error, Extension::kInvalidContentScriptError)); + EXPECT_TRUE(MatchPattern(error, errors::kInvalidContentScript)); // Test missing and invalid matches array input_value.reset(static_cast<DictionaryValue*>(valid_value->DeepCopy())); - input_value->GetList(Extension::kContentScriptsKey, &content_scripts); + input_value->GetList(keys::kContentScripts, &content_scripts); DictionaryValue* user_script = NULL; content_scripts->GetDictionary(0, &user_script); - user_script->Remove(Extension::kMatchesKey, NULL); + user_script->Remove(keys::kMatches, NULL); EXPECT_FALSE(extension.InitFromValue(*input_value, true, &error)); - EXPECT_TRUE(MatchPattern(error, Extension::kInvalidMatchesError)); + EXPECT_TRUE(MatchPattern(error, errors::kInvalidMatches)); - user_script->Set(Extension::kMatchesKey, Value::CreateIntegerValue(42)); + user_script->Set(keys::kMatches, Value::CreateIntegerValue(42)); EXPECT_FALSE(extension.InitFromValue(*input_value, true, &error)); - EXPECT_TRUE(MatchPattern(error, Extension::kInvalidMatchesError)); + EXPECT_TRUE(MatchPattern(error, errors::kInvalidMatches)); ListValue* matches = new ListValue; - user_script->Set(Extension::kMatchesKey, matches); + user_script->Set(keys::kMatches, matches); EXPECT_FALSE(extension.InitFromValue(*input_value, true, &error)); - EXPECT_TRUE(MatchPattern(error, Extension::kInvalidMatchCountError)); + EXPECT_TRUE(MatchPattern(error, errors::kInvalidMatchCount)); // Test invalid match element matches->Set(0, Value::CreateIntegerValue(42)); EXPECT_FALSE(extension.InitFromValue(*input_value, true, &error)); - EXPECT_TRUE(MatchPattern(error, Extension::kInvalidMatchError)); + EXPECT_TRUE(MatchPattern(error, errors::kInvalidMatch)); // Test missing and invalid files array input_value.reset(static_cast<DictionaryValue*>(valid_value->DeepCopy())); - input_value->GetList(Extension::kContentScriptsKey, &content_scripts); + input_value->GetList(keys::kContentScripts, &content_scripts); content_scripts->GetDictionary(0, &user_script); - user_script->Remove(Extension::kJsKey, NULL); - user_script->Remove(Extension::kCssKey, NULL); + user_script->Remove(keys::kJs, NULL); + user_script->Remove(keys::kCss, NULL); EXPECT_FALSE(extension.InitFromValue(*input_value, true, &error)); - EXPECT_TRUE(MatchPattern(error, Extension::kMissingFileError)); + EXPECT_TRUE(MatchPattern(error, errors::kMissingFile)); - user_script->Set(Extension::kJsKey, Value::CreateIntegerValue(42)); + user_script->Set(keys::kJs, Value::CreateIntegerValue(42)); EXPECT_FALSE(extension.InitFromValue(*input_value, true, &error)); - EXPECT_TRUE(MatchPattern(error, Extension::kInvalidJsListError)); + EXPECT_TRUE(MatchPattern(error, errors::kInvalidJsList)); - user_script->Set(Extension::kCssKey, new ListValue); - user_script->Set(Extension::kJsKey, new ListValue); + user_script->Set(keys::kCss, new ListValue); + user_script->Set(keys::kJs, new ListValue); EXPECT_FALSE(extension.InitFromValue(*input_value, true, &error)); - EXPECT_TRUE(MatchPattern(error, Extension::kMissingFileError)); - user_script->Remove(Extension::kCssKey, NULL); + EXPECT_TRUE(MatchPattern(error, errors::kMissingFile)); + user_script->Remove(keys::kCss, NULL); ListValue* files = new ListValue; - user_script->Set(Extension::kJsKey, files); + user_script->Set(keys::kJs, files); EXPECT_FALSE(extension.InitFromValue(*input_value, true, &error)); - EXPECT_TRUE(MatchPattern(error, Extension::kMissingFileError)); + EXPECT_TRUE(MatchPattern(error, errors::kMissingFile)); // Test invalid file element files->Set(0, Value::CreateIntegerValue(42)); EXPECT_FALSE(extension.InitFromValue(*input_value, true, &error)); - EXPECT_TRUE(MatchPattern(error, Extension::kInvalidJsError)); + EXPECT_TRUE(MatchPattern(error, errors::kInvalidJs)); - user_script->Remove(Extension::kJsKey, NULL); + user_script->Remove(keys::kJs, NULL); // Test the css element - user_script->Set(Extension::kCssKey, Value::CreateIntegerValue(42)); + user_script->Set(keys::kCss, Value::CreateIntegerValue(42)); EXPECT_FALSE(extension.InitFromValue(*input_value, true, &error)); - EXPECT_TRUE(MatchPattern(error, Extension::kInvalidCssListError)); + EXPECT_TRUE(MatchPattern(error, errors::kInvalidCssList)); // Test invalid file element ListValue* css_files = new ListValue; - user_script->Set(Extension::kCssKey, css_files); + user_script->Set(keys::kCss, css_files); css_files->Set(0, Value::CreateIntegerValue(42)); EXPECT_FALSE(extension.InitFromValue(*input_value, true, &error)); - EXPECT_TRUE(MatchPattern(error, Extension::kInvalidCssError)); + EXPECT_TRUE(MatchPattern(error, errors::kInvalidCss)); // Test missing and invalid permissions array input_value.reset(static_cast<DictionaryValue*>(valid_value->DeepCopy())); EXPECT_TRUE(extension.InitFromValue(*input_value, true, &error)); ListValue* permissions = NULL; - input_value->GetList(Extension::kPermissionsKey, &permissions); + input_value->GetList(keys::kPermissions, &permissions); ASSERT_FALSE(NULL == permissions); permissions = new ListValue; - input_value->Set(Extension::kPermissionsKey, permissions); + input_value->Set(keys::kPermissions, permissions); EXPECT_TRUE(extension.InitFromValue(*input_value, true, &error)); const std::vector<std::string>* error_vector = ExtensionErrorReporter::GetInstance()->GetErrors(); const std::string log_error = error_vector->at(error_vector->size() - 1); EXPECT_TRUE(MatchPattern(log_error, - Extension::kInvalidPermissionCountWarning)); + errors::kInvalidPermissionCountWarning)); - input_value->Set(Extension::kPermissionsKey, Value::CreateIntegerValue(9)); + input_value->Set(keys::kPermissions, Value::CreateIntegerValue(9)); EXPECT_FALSE(extension.InitFromValue(*input_value, true, &error)); - EXPECT_TRUE(MatchPattern(error, Extension::kInvalidPermissionsError)); + EXPECT_TRUE(MatchPattern(error, errors::kInvalidPermissions)); input_value.reset(static_cast<DictionaryValue*>(valid_value->DeepCopy())); - input_value->GetList(Extension::kPermissionsKey, &permissions); + input_value->GetList(keys::kPermissions, &permissions); permissions->Set(0, Value::CreateIntegerValue(24)); EXPECT_FALSE(extension.InitFromValue(*input_value, true, &error)); - EXPECT_TRUE(MatchPattern(error, Extension::kInvalidPermissionError)); + EXPECT_TRUE(MatchPattern(error, errors::kInvalidPermission)); permissions->Set(0, Value::CreateStringValue("www.google.com")); EXPECT_FALSE(extension.InitFromValue(*input_value, true, &error)); - EXPECT_TRUE(MatchPattern(error, Extension::kInvalidPermissionError)); + EXPECT_TRUE(MatchPattern(error, errors::kInvalidPermission)); // Test permissions scheme. input_value.reset(static_cast<DictionaryValue*>(valid_value->DeepCopy())); - input_value->GetList(Extension::kPermissionsKey, &permissions); + input_value->GetList(keys::kPermissions, &permissions); permissions->Set(0, Value::CreateStringValue("file:///C:/foo.txt")); EXPECT_FALSE(extension.InitFromValue(*input_value, true, &error)); - EXPECT_TRUE(MatchPattern(error, Extension::kInvalidPermissionSchemeError)); + EXPECT_TRUE(MatchPattern(error, errors::kInvalidPermissionScheme)); } TEST(ExtensionTest, InitFromValueValid) { @@ -201,8 +206,8 @@ TEST(ExtensionTest, InitFromValueValid) { DictionaryValue input_value; // Test minimal extension - input_value.SetString(Extension::kVersionKey, "1.0.0.0"); - input_value.SetString(Extension::kNameKey, "my extension"); + input_value.SetString(keys::kVersion, "1.0.0.0"); + input_value.SetString(keys::kName, "my extension"); EXPECT_TRUE(extension.InitFromValue(input_value, false, &error)); EXPECT_EQ("", error); @@ -222,8 +227,8 @@ TEST(ExtensionTest, GetResourceURLAndPath) { #endif Extension extension(path); DictionaryValue input_value; - input_value.SetString(Extension::kVersionKey, "1.0.0.0"); - input_value.SetString(Extension::kNameKey, "my extension"); + input_value.SetString(keys::kVersion, "1.0.0.0"); + input_value.SetString(keys::kName, "my extension"); EXPECT_TRUE(extension.InitFromValue(input_value, false, NULL)); EXPECT_EQ(extension.url().spec() + "bar/baz.js", @@ -261,12 +266,12 @@ TEST(ExtensionTest, LoadPageActionHelper) { FilePath::StringType img2 = FILE_PATH_LITERAL("image2.png"); // Add the page_actions dictionary. - input.SetString(Extension::kPageActionIdKey, id); - input.SetString(Extension::kNameKey, name); + input.SetString(keys::kPageActionId, id); + input.SetString(keys::kName, name); ListValue* icons = new ListValue; icons->Set(0, Value::CreateStringValue(img1)); icons->Set(1, Value::CreateStringValue(img2)); - input.Set(Extension::kIconPathsKey, icons); + input.Set(keys::kIconPaths, icons); // Parse the page action and read back the values from the object. page_action.reset(extension.LoadPageActionHelper(&input, 0, &error_msg)); @@ -281,14 +286,14 @@ TEST(ExtensionTest, LoadPageActionHelper) { ASSERT_EQ(PageAction::PERMANENT, page_action->type()); // Explicitly set the same type and parse again. - input.SetString(Extension::kTypeKey, Extension::kPageActionTypePermanent); + input.SetString(keys::kType, values::kPageActionTypePermanent); page_action.reset(extension.LoadPageActionHelper(&input, 0, &error_msg)); ASSERT_TRUE(NULL != page_action.get()); ASSERT_STREQ("", error_msg.c_str()); ASSERT_EQ(PageAction::PERMANENT, page_action->type()); // Explicitly set the TAB type and parse again. - input.SetString(Extension::kTypeKey, Extension::kPageActionTypeTab); + input.SetString(keys::kType, values::kPageActionTypeTab); page_action.reset(extension.LoadPageActionHelper(&input, 0, &error_msg)); ASSERT_TRUE(NULL != page_action.get()); ASSERT_STREQ("", error_msg.c_str()); @@ -300,35 +305,35 @@ TEST(ExtensionTest, LoadPageActionHelper) { // First remove id key. copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); - copy->Remove(Extension::kPageActionIdKey, NULL); + copy->Remove(keys::kPageActionId, NULL); page_action.reset(extension.LoadPageActionHelper(copy.get(), 0, &error_msg)); ASSERT_TRUE(NULL == page_action.get()); ASSERT_TRUE(MatchPattern(error_msg.c_str(), - Extension::kInvalidPageActionIdError)); + errors::kInvalidPageActionId)); // Then remove the name key. copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); - copy->Remove(Extension::kNameKey, NULL); + copy->Remove(keys::kName, NULL); page_action.reset(extension.LoadPageActionHelper(copy.get(), 0, &error_msg)); ASSERT_TRUE(NULL == page_action.get()); ASSERT_TRUE(MatchPattern(error_msg.c_str(), - Extension::kInvalidNameError)); + errors::kInvalidName)); // Then remove the icon paths key. copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); - copy->Remove(Extension::kIconPathsKey, NULL); + copy->Remove(keys::kIconPaths, NULL); page_action.reset(extension.LoadPageActionHelper(copy.get(), 0, &error_msg)); ASSERT_TRUE(NULL == page_action.get()); ASSERT_TRUE(MatchPattern(error_msg.c_str(), - Extension::kInvalidPageActionIconPathsError)); + errors::kInvalidPageActionIconPaths)); // Then set the type to something bogus. copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); - copy->SetString(Extension::kTypeKey, "something_bogus"); + copy->SetString(keys::kType, "something_bogus"); page_action.reset(extension.LoadPageActionHelper(copy.get(), 0, &error_msg)); ASSERT_TRUE(NULL == page_action.get()); ASSERT_TRUE(MatchPattern(error_msg.c_str(), - Extension::kInvalidPageActionTypeValueError)); + errors::kInvalidPageActionTypeValue)); } TEST(ExtensionTest, IdIsValid) { @@ -385,9 +390,9 @@ TEST(ExtensionTest, UpdateUrls) { Extension extension; std::string error; - input_value.SetString(Extension::kVersionKey, "1.0"); - input_value.SetString(Extension::kNameKey, "Test"); - input_value.SetString(Extension::kUpdateURLKey, url.spec()); + input_value.SetString(keys::kVersion, "1.0"); + input_value.SetString(keys::kName, "Test"); + input_value.SetString(keys::kUpdateURL, url.spec()); EXPECT_TRUE(extension.InitFromValue(input_value, false, &error)); } @@ -401,11 +406,11 @@ TEST(ExtensionTest, UpdateUrls) { DictionaryValue input_value; Extension extension; std::string error; - input_value.SetString(Extension::kVersionKey, "1.0"); - input_value.SetString(Extension::kNameKey, "Test"); - input_value.SetString(Extension::kUpdateURLKey, invalid[i]); + input_value.SetString(keys::kVersion, "1.0"); + input_value.SetString(keys::kName, "Test"); + input_value.SetString(keys::kUpdateURL, invalid[i]); EXPECT_FALSE(extension.InitFromValue(input_value, false, &error)); - EXPECT_TRUE(MatchPattern(error, Extension::kInvalidUpdateURLError)); + EXPECT_TRUE(MatchPattern(error, errors::kInvalidUpdateURL)); } } diff --git a/chrome/common/extensions/extension_unpacker.cc b/chrome/common/extensions/extension_unpacker.cc index bae5a7d..06957de 100644 --- a/chrome/common/extensions/extension_unpacker.cc +++ b/chrome/common/extensions/extension_unpacker.cc @@ -12,6 +12,7 @@ #include "base/values.h" #include "net/base/file_stream.h" #include "chrome/common/extensions/extension.h" +#include "chrome/common/extensions/extension_constants.h" #include "chrome/common/ipc_message_utils.h" #include "chrome/common/json_value_serializer.h" #include "chrome/common/notification_service.h" @@ -77,7 +78,7 @@ DictionaryValue* ExtensionUnpacker::ReadManifest() { FilePath manifest_path = temp_install_dir_.AppendASCII(Extension::kManifestFilename); if (!file_util::PathExists(manifest_path)) { - SetError(Extension::kInvalidManifestError); + SetError(extension_manifest_errors::kInvalidManifest); return NULL; } @@ -90,7 +91,7 @@ DictionaryValue* ExtensionUnpacker::ReadManifest() { } if (!root->IsType(Value::TYPE_DICTIONARY)) { - SetError(Extension::kInvalidManifestError); + SetError(extension_manifest_errors::kInvalidManifest); return NULL; } |