diff options
author | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-27 23:29:49 +0000 |
---|---|---|
committer | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-27 23:29:49 +0000 |
commit | c10cf58384d2808c13808fee0352573bb6069781 (patch) | |
tree | eff79c3f08085544694d5f573364f182526c238d /chrome/common/extensions | |
parent | 48abb0046f935690ea83d0290dc6ff0a60d99b27 (diff) | |
download | chromium_src-c10cf58384d2808c13808fee0352573bb6069781.zip chromium_src-c10cf58384d2808c13808fee0352573bb6069781.tar.gz chromium_src-c10cf58384d2808c13808fee0352573bb6069781.tar.bz2 |
Fix bug where many extensions don't install due to sandbox.
FWIW, I tracked down why our tests didn't find this. We do
have coverage for the code path that was getting executed,
but the sandbox is disabled in our browser tests, so it did
not expose this issue.
BUG=25865
TEST=Install any extension that has a content script or icons (test/data/extensions/good.crx is one example). Go to chrome://extensions/. You should see the extension successfully installed.
Review URL: http://codereview.chromium.org/337041
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@30282 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/extensions')
-rw-r--r-- | chrome/common/extensions/extension.cc | 2 | ||||
-rw-r--r-- | chrome/common/extensions/extension_l10n_util.cc | 215 | ||||
-rw-r--r-- | chrome/common/extensions/extension_l10n_util.h | 82 | ||||
-rw-r--r-- | chrome/common/extensions/extension_l10n_util_unittest.cc | 231 | ||||
-rw-r--r-- | chrome/common/extensions/extension_resource.cc | 2 | ||||
-rw-r--r-- | chrome/common/extensions/extension_resource_unittest.cc | 2 |
6 files changed, 531 insertions, 3 deletions
diff --git a/chrome/common/extensions/extension.cc b/chrome/common/extensions/extension.cc index c7207c1..db05c4b 100644 --- a/chrome/common/extensions/extension.cc +++ b/chrome/common/extensions/extension.cc @@ -13,11 +13,11 @@ #include "base/stl_util-inl.h" #include "base/third_party/nss/blapi.h" #include "base/third_party/nss/sha256.h" -#include "chrome/browser/extensions/extension_l10n_util.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/extension_l10n_util.h" #include "chrome/common/extensions/user_script.h" #include "chrome/common/notification_service.h" #include "chrome/common/url_constants.h" diff --git a/chrome/common/extensions/extension_l10n_util.cc b/chrome/common/extensions/extension_l10n_util.cc new file mode 100644 index 0000000..8ff7ec6 --- /dev/null +++ b/chrome/common/extensions/extension_l10n_util.cc @@ -0,0 +1,215 @@ +// 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_l10n_util.h" + +#include <set> +#include <string> +#include <vector> + +#include "app/l10n_util.h" +#include "base/file_util.h" +#include "base/linked_ptr.h" +#include "base/string_util.h" +#include "base/values.h" +#include "chrome/common/extensions/extension.h" +#include "chrome/common/extensions/extension_constants.h" +#include "chrome/common/extensions/extension_message_bundle.h" +#include "chrome/common/json_value_serializer.h" + +namespace errors = extension_manifest_errors; +namespace keys = extension_manifest_keys; + +static std::string* GetProcessLocale() { + static std::string locale; + return &locale; +} + +namespace extension_l10n_util { + +void SetProcessLocale(const std::string& locale) { + *(GetProcessLocale()) = locale; +} + +std::string GetDefaultLocaleFromManifest(const DictionaryValue& manifest, + std::string* error) { + std::string default_locale; + if (!manifest.GetString(keys::kDefaultLocale, &default_locale)) { + *error = errors::kInvalidDefaultLocale; + return ""; + } + + return default_locale; +} + +bool AddLocale(const std::set<std::string>& chrome_locales, + const FilePath& locale_folder, + const std::string& locale_name, + std::set<std::string>* valid_locales, + std::string* error) { + // Accept name that starts with a . but don't add it to the list of supported + // locales. + if (locale_name.find(".") == 0) + return true; + if (chrome_locales.find(locale_name) == chrome_locales.end()) { + // Fail if there is an extension locale that's not in the Chrome list. + *error = StringPrintf("Supplied locale %s is not supported.", + locale_name.c_str()); + return false; + } + // Check if messages file is actually present (but don't check content). + if (file_util::PathExists( + locale_folder.AppendASCII(Extension::kMessagesFilename))) { + valid_locales->insert(locale_name); + } else { + *error = StringPrintf("Catalog file is missing for locale %s.", + locale_name.c_str()); + return false; + } + + return true; +} + +std::string NormalizeLocale(const std::string& locale) { + std::string normalized_locale(locale); + std::replace(normalized_locale.begin(), normalized_locale.end(), '-', '_'); + + return normalized_locale; +} + +void GetParentLocales(const std::string& current_locale, + std::vector<std::string>* parent_locales) { + std::string locale(NormalizeLocale(current_locale)); + + const int kNameCapacity = 256; + char parent[kNameCapacity]; + base::strlcpy(parent, locale.c_str(), kNameCapacity); + parent_locales->push_back(parent); + UErrorCode err = U_ZERO_ERROR; + while (uloc_getParent(parent, parent, kNameCapacity, &err) > 0) { + if (err != U_ZERO_ERROR) + break; + parent_locales->push_back(parent); + } +} + +// Extends list of Chrome locales to them and their parents, so we can do +// proper fallback. +static void GetAllLocales(std::set<std::string>* all_locales) { + const std::vector<std::string>& available_locales = + l10n_util::GetAvailableLocales(); + // Add all parents of the current locale to the available locales set. + // I.e. for sr_Cyrl_RS we add sr_Cyrl_RS, sr_Cyrl and sr. + for (size_t i = 0; i < available_locales.size(); ++i) { + std::vector<std::string> result; + GetParentLocales(available_locales[i], &result); + all_locales->insert(result.begin(), result.end()); + } +} + +bool GetValidLocales(const FilePath& locale_path, + std::set<std::string>* valid_locales, + std::string* error) { + static std::set<std::string> chrome_locales; + GetAllLocales(&chrome_locales); + + // Enumerate all supplied locales in the extension. + file_util::FileEnumerator locales(locale_path, + false, + file_util::FileEnumerator::DIRECTORIES); + FilePath locale_folder; + while (!(locale_folder = locales.Next()).empty()) { + std::string locale_name = + WideToASCII(locale_folder.BaseName().ToWStringHack()); + if (!AddLocale(chrome_locales, + locale_folder, + locale_name, + valid_locales, + error)) { + return false; + } + } + + if (valid_locales->empty()) { + *error = extension_manifest_errors::kLocalesNoValidLocaleNamesListed; + return false; + } + + return true; +} + +// Loads contents of the messages file for given locale. If file is not found, +// or there was parsing error we return NULL and set |error|. +// Caller owns the returned object. +static DictionaryValue* LoadMessageFile(const FilePath& locale_path, + const std::string& locale, + std::string* error) { + + std::string extension_locale = locale; + FilePath file = locale_path.AppendASCII(extension_locale) + .AppendASCII(Extension::kMessagesFilename); + JSONFileValueSerializer messages_serializer(file); + Value *dictionary = messages_serializer.Deserialize(error); + if (!dictionary && error->empty()) { + // JSONFileValueSerializer just returns NULL if file cannot be found. It + // doesn't set the error, so we have to do it. + *error = StringPrintf("Catalog file is missing for locale %s.", + extension_locale.c_str()); + } + + return static_cast<DictionaryValue*>(dictionary); +} + +ExtensionMessageBundle* LoadMessageCatalogs( + const FilePath& locale_path, + const std::string& default_locale, + const std::string& application_locale, + const std::set<std::string>& valid_locales, + std::string* error) { + // Order locales to load as current_locale, first_parent, ..., default_locale. + std::vector<std::string> all_fallback_locales; + if (!application_locale.empty() && application_locale != default_locale) + GetParentLocales(application_locale, &all_fallback_locales); + all_fallback_locales.push_back(default_locale); + + std::vector<linked_ptr<DictionaryValue> > catalogs; + for (size_t i = 0; i < all_fallback_locales.size(); ++i) { + // Skip all parent locales that are not supplied. + if (valid_locales.find(all_fallback_locales[i]) == valid_locales.end()) + continue; + linked_ptr<DictionaryValue> catalog( + LoadMessageFile(locale_path, all_fallback_locales[i], error)); + if (!catalog.get()) { + // If locale is valid, but messages.json is corrupted or missing, return + // an error. + return false; + } else { + catalogs.push_back(catalog); + } + } + + return ExtensionMessageBundle::Create(catalogs, error); +} + +void GetL10nRelativePaths(const FilePath& relative_resource_path, + std::vector<FilePath>* l10n_paths) { + DCHECK(NULL != l10n_paths); + + std::string* current_locale = GetProcessLocale(); + if (current_locale->empty()) + *current_locale = l10n_util::GetApplicationLocale(L""); + + std::vector<std::string> locales; + GetParentLocales(*current_locale, &locales); + + FilePath locale_relative_path; + for (size_t i = 0; i < locales.size(); ++i) { + l10n_paths->push_back(locale_relative_path + .AppendASCII(Extension::kLocaleFolder) + .AppendASCII(locales[i]) + .Append(relative_resource_path)); + } +} + +} // namespace extension_l10n_util diff --git a/chrome/common/extensions/extension_l10n_util.h b/chrome/common/extensions/extension_l10n_util.h new file mode 100644 index 0000000..87ad43f --- /dev/null +++ b/chrome/common/extensions/extension_l10n_util.h @@ -0,0 +1,82 @@ +// 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. +// +// This file declares extension specific l10n utils. + +#ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_L10N_UTIL_H_ +#define CHROME_COMMON_EXTENSIONS_EXTENSION_L10N_UTIL_H_ + +#include <set> +#include <string> +#include <vector> + +class DictionaryValue; +class Extension; +class ExtensionMessageBundle; +class FilePath; + +namespace extension_l10n_util { + +// Set the locale for this process to a fixed value, rather than using the +// normal file-based lookup mechanisms. This is used to set the locale inside +// the sandboxed utility process, where file reading is not allowed. +void SetProcessLocale(const std::string& locale); + +// Returns default locale in form "en-US" or "sr" or empty string if +// "default_locale" section was not defined in the manifest.json file. +std::string GetDefaultLocaleFromManifest(const DictionaryValue& manifest, + std::string* error); + +// Adds locale_name to the extension if it's in chrome_locales, and +// if messages file is present (we don't check content of messages file here). +// Returns false if locale_name was not found in chrome_locales, and sets +// error with locale_name. +// If file name starts with . return true (helps testing extensions under svn). +bool AddLocale(const std::set<std::string>& chrome_locales, + const FilePath& locale_folder, + const std::string& locale_name, + std::set<std::string>* valid_locales, + std::string* error); + +// Converts all - into _, to be consistent with ICU and file system names. +std::string NormalizeLocale(const std::string& locale); + +// Produce a vector of parent locales for given locale. +// It includes the current locale in the result. +// sr_Cyrl_RS generates sr_Cyrl_RS, sr_Cyrl and sr. +void GetParentLocales(const std::string& current_locale, + std::vector<std::string>* parent_locales); + +// Adds valid locales to the extension. +// 1. Do nothing if _locales directory is missing (not an error). +// 2. Get list of Chrome locales. +// 3. Enumerate all subdirectories of _locales directory. +// 4. Intersect both lists, and add intersection to the extension. +// Returns false if any of supplied locales don't match chrome list of locales. +// Fills out error with offending locale name. +bool GetValidLocales(const FilePath& locale_path, + std::set<std::string>* locales, + std::string* error); + +// Loads messages file for default locale, and application locales (application +// locales doesn't have to exist). Application locale is current locale and its +// parents. +// Returns message bundle if it can load default locale messages file, and all +// messages are valid, else returns NULL and sets error. +ExtensionMessageBundle* LoadMessageCatalogs( + const FilePath& locale_path, + const std::string& default_locale, + const std::string& app_locale, + const std::set<std::string>& valid_locales, + std::string* error); + +// Returns relative l10n paths to the resource. +// Returned vector starts with more specific locale path, and ends with the +// least specific one. +void GetL10nRelativePaths(const FilePath& relative_resource_path, + std::vector<FilePath>* l10n_paths); + +} // namespace extension_l10n_util + +#endif // CHROME_COMMON_EXTENSIONS_EXTENSION_L10N_UTIL_H_ diff --git a/chrome/common/extensions/extension_l10n_util_unittest.cc b/chrome/common/extensions/extension_l10n_util_unittest.cc new file mode 100644 index 0000000..d6e5ad3 --- /dev/null +++ b/chrome/common/extensions/extension_l10n_util_unittest.cc @@ -0,0 +1,231 @@ +// 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 "app/l10n_util.h" +#include "base/file_path.h" +#include "base/file_util.h" +#include "base/path_service.h" +#include "base/scoped_ptr.h" +#include "base/scoped_temp_dir.h" +#include "base/values.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_l10n_util.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +TEST(Extensio8nL10nUtil, GetValidLocalesEmptyLocaleFolder) { + ScopedTempDir temp; + ASSERT_TRUE(temp.CreateUniqueTempDir()); + + FilePath src_path = temp.path().AppendASCII(Extension::kLocaleFolder); + ASSERT_TRUE(file_util::CreateDirectory(src_path)); + + std::string error; + std::set<std::string> locales; + EXPECT_FALSE(extension_l10n_util::GetValidLocales(src_path, + &locales, + &error)); + + EXPECT_TRUE(locales.empty()); +} + +TEST(ExtensionL10nUtil, GetValidLocalesWithValidLocaleNoMessagesFile) { + ScopedTempDir temp; + ASSERT_TRUE(temp.CreateUniqueTempDir()); + + FilePath src_path = temp.path().AppendASCII(Extension::kLocaleFolder); + ASSERT_TRUE(file_util::CreateDirectory(src_path)); + ASSERT_TRUE(file_util::CreateDirectory(src_path.AppendASCII("sr"))); + + std::string error; + std::set<std::string> locales; + EXPECT_FALSE(extension_l10n_util::GetValidLocales(src_path, + &locales, + &error)); + + EXPECT_TRUE(locales.empty()); +} + +TEST(ExtensionL10nUtil, GetValidLocalesWithValidLocalesAndMessagesFile) { + FilePath install_dir; + ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); + install_dir = install_dir.AppendASCII("extensions") + .AppendASCII("good") + .AppendASCII("Extensions") + .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") + .AppendASCII("1.0.0.0") + .AppendASCII(Extension::kLocaleFolder); + + std::string error; + std::set<std::string> locales; + EXPECT_TRUE(extension_l10n_util::GetValidLocales(install_dir, + &locales, + &error)); + EXPECT_EQ(3U, locales.size()); + EXPECT_TRUE(locales.find("sr") != locales.end()); + EXPECT_TRUE(locales.find("en") != locales.end()); + EXPECT_TRUE(locales.find("en_US") != locales.end()); +} + +TEST(ExtensionL10nUtil, LoadMessageCatalogsValidFallback) { + FilePath install_dir; + ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); + install_dir = install_dir.AppendASCII("extensions") + .AppendASCII("good") + .AppendASCII("Extensions") + .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") + .AppendASCII("1.0.0.0") + .AppendASCII(Extension::kLocaleFolder); + + std::string error; + std::set<std::string> locales; + EXPECT_TRUE(extension_l10n_util::GetValidLocales(install_dir, + &locales, + &error)); + + scoped_ptr<ExtensionMessageBundle> bundle( + extension_l10n_util::LoadMessageCatalogs( + install_dir, "sr", "en_US", locales, &error)); + ASSERT_FALSE(NULL == bundle.get()); + EXPECT_TRUE(error.empty()); + EXPECT_EQ("Color", bundle->GetL10nMessage("color")); + EXPECT_EQ("Not in the US or GB.", bundle->GetL10nMessage("not_in_US_or_GB")); +} + +TEST(ExtensionL10nUtil, LoadMessageCatalogsMissingFiles) { + ScopedTempDir temp; + ASSERT_TRUE(temp.CreateUniqueTempDir()); + + FilePath src_path = temp.path().AppendASCII(Extension::kLocaleFolder); + ASSERT_TRUE(file_util::CreateDirectory(src_path)); + + std::set<std::string> valid_locales; + valid_locales.insert("sr"); + valid_locales.insert("en"); + std::string error; + EXPECT_TRUE(NULL == extension_l10n_util::LoadMessageCatalogs(src_path, + "en", + "sr", + valid_locales, + &error)); + EXPECT_FALSE(error.empty()); +} + +TEST(ExtensionL10nUtil, LoadMessageCatalogsBadJSONFormat) { + ScopedTempDir temp; + ASSERT_TRUE(temp.CreateUniqueTempDir()); + + FilePath src_path = temp.path().AppendASCII(Extension::kLocaleFolder); + ASSERT_TRUE(file_util::CreateDirectory(src_path)); + + FilePath locale = src_path.AppendASCII("sr"); + ASSERT_TRUE(file_util::CreateDirectory(locale)); + + std::string data = "{ \"name\":"; + ASSERT_TRUE( + file_util::WriteFile(locale.AppendASCII(Extension::kMessagesFilename), + data.c_str(), data.length())); + + std::set<std::string> valid_locales; + valid_locales.insert("sr"); + valid_locales.insert("en_US"); + std::string error; + EXPECT_TRUE(NULL == extension_l10n_util::LoadMessageCatalogs(src_path, + "en_US", + "sr", + valid_locales, + &error)); + EXPECT_EQ("Line: 1, column: 10, Syntax error.", error); +} + +TEST(ExtensionL10nUtil, LoadMessageCatalogsDuplicateKeys) { + ScopedTempDir temp; + ASSERT_TRUE(temp.CreateUniqueTempDir()); + + FilePath src_path = temp.path().AppendASCII(Extension::kLocaleFolder); + ASSERT_TRUE(file_util::CreateDirectory(src_path)); + + FilePath locale_1 = src_path.AppendASCII("en"); + ASSERT_TRUE(file_util::CreateDirectory(locale_1)); + + std::string data = + "{ \"name\": { \"message\": \"something\" }, " + "\"name\": { \"message\": \"something else\" } }"; + ASSERT_TRUE( + file_util::WriteFile(locale_1.AppendASCII(Extension::kMessagesFilename), + data.c_str(), data.length())); + + FilePath locale_2 = src_path.AppendASCII("sr"); + ASSERT_TRUE(file_util::CreateDirectory(locale_2)); + + ASSERT_TRUE( + file_util::WriteFile(locale_2.AppendASCII(Extension::kMessagesFilename), + data.c_str(), data.length())); + + std::set<std::string> valid_locales; + valid_locales.insert("sr"); + valid_locales.insert("en"); + std::string error; + // JSON parser hides duplicates. We are going to get only one key/value + // pair at the end. + scoped_ptr<ExtensionMessageBundle> message_bundle( + extension_l10n_util::LoadMessageCatalogs(src_path, + "en", + "sr", + valid_locales, + &error)); + EXPECT_TRUE(NULL != message_bundle.get()); + EXPECT_TRUE(error.empty()); +} + +TEST(ExtensionL10nUtil, GetParentLocales) { + std::vector<std::string> locales; + const std::string top_locale("sr_Cyrl_RS"); + extension_l10n_util::GetParentLocales(top_locale, &locales); + + ASSERT_EQ(3U, locales.size()); + EXPECT_EQ("sr_Cyrl_RS", locales[0]); + EXPECT_EQ("sr_Cyrl", locales[1]); + EXPECT_EQ("sr", locales[2]); +} + +bool PathsAreEqual(const FilePath& path1, const FilePath& path2) { + FilePath::StringType path1_str = path1.value(); + std::replace(path1_str.begin(), path1_str.end(), '\\', '/'); + + FilePath::StringType path2_str = path2.value(); + std::replace(path2_str.begin(), path2_str.end(), '\\', '/'); + + if (path1_str == path2_str) { + return true; + } else { + return false; + } +} + +TEST(ExtensionL10nUtil, GetL10nRelativePath) { + static std::string current_locale = + extension_l10n_util::NormalizeLocale(l10n_util::GetApplicationLocale(L"")); + + std::vector<FilePath> l10n_paths; + extension_l10n_util::GetL10nRelativePaths( + FilePath(FILE_PATH_LITERAL("foo/bar.js")), &l10n_paths); + ASSERT_FALSE(l10n_paths.empty()); + + std::vector<std::string> locales; + extension_l10n_util::GetParentLocales(current_locale, &locales); + ASSERT_EQ(locales.size(), l10n_paths.size()); + + for (size_t i = 0; i < locales.size(); ++i) { + FilePath tmp; + tmp = tmp.AppendASCII(Extension::kLocaleFolder) + .AppendASCII(locales[i]).AppendASCII("foo/bar.js"); + EXPECT_TRUE(PathsAreEqual(tmp, l10n_paths[i])); + } +} + +} // namespace diff --git a/chrome/common/extensions/extension_resource.cc b/chrome/common/extensions/extension_resource.cc index e92a8d0..7b2a833 100644 --- a/chrome/common/extensions/extension_resource.cc +++ b/chrome/common/extensions/extension_resource.cc @@ -7,7 +7,7 @@ #include "base/file_path.h" #include "base/file_util.h" #include "base/string_util.h" -#include "chrome/browser/extensions/extension_l10n_util.h" +#include "chrome/common/extensions/extension_l10n_util.h" #include "googleurl/src/gurl.h" #include "net/base/net_util.h" diff --git a/chrome/common/extensions/extension_resource_unittest.cc b/chrome/common/extensions/extension_resource_unittest.cc index 6954408..f467e47 100644 --- a/chrome/common/extensions/extension_resource_unittest.cc +++ b/chrome/common/extensions/extension_resource_unittest.cc @@ -8,9 +8,9 @@ #include "base/file_util.h" #include "base/path_service.h" #include "base/scoped_temp_dir.h" -#include "chrome/browser/extensions/extension_l10n_util.h" #include "chrome/common/chrome_paths.h" #include "chrome/common/extensions/extension.h" +#include "chrome/common/extensions/extension_l10n_util.h" #include "chrome/common/extensions/extension_resource.h" #include "testing/gtest/include/gtest/gtest.h" |