From c10cf58384d2808c13808fee0352573bb6069781 Mon Sep 17 00:00:00 2001 From: "aa@chromium.org" Date: Tue, 27 Oct 2009 23:29:49 +0000 Subject: 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 --- chrome/browser/extensions/extension_file_util.cc | 2 +- chrome/browser/extensions/extension_l10n_util.cc | 204 ------------------ chrome/browser/extensions/extension_l10n_util.h | 77 ------- .../extensions/extension_l10n_util_unittest.cc | 232 --------------------- chrome/browser/utility_process_host.cc | 6 + chrome/chrome.gyp | 6 +- chrome/common/extensions/extension.cc | 2 +- chrome/common/extensions/extension_l10n_util.cc | 215 +++++++++++++++++++ chrome/common/extensions/extension_l10n_util.h | 82 ++++++++ .../extensions/extension_l10n_util_unittest.cc | 231 ++++++++++++++++++++ chrome/common/extensions/extension_resource.cc | 2 +- .../extensions/extension_resource_unittest.cc | 2 +- chrome/utility/utility_main.cc | 7 + 13 files changed, 548 insertions(+), 520 deletions(-) delete mode 100644 chrome/browser/extensions/extension_l10n_util.cc delete mode 100644 chrome/browser/extensions/extension_l10n_util.h delete mode 100644 chrome/browser/extensions/extension_l10n_util_unittest.cc create mode 100644 chrome/common/extensions/extension_l10n_util.cc create mode 100644 chrome/common/extensions/extension_l10n_util.h create mode 100644 chrome/common/extensions/extension_l10n_util_unittest.cc (limited to 'chrome') diff --git a/chrome/browser/extensions/extension_file_util.cc b/chrome/browser/extensions/extension_file_util.cc index 6372f71..5b1e16c 100644 --- a/chrome/browser/extensions/extension_file_util.cc +++ b/chrome/browser/extensions/extension_file_util.cc @@ -10,8 +10,8 @@ #include "base/scoped_temp_dir.h" #include "base/string_util.h" #include "chrome/browser/extensions/extension_prefs.h" -#include "chrome/browser/extensions/extension_l10n_util.h" #include "chrome/common/extensions/extension.h" +#include "chrome/common/extensions/extension_l10n_util.h" #include "chrome/common/extensions/extension_constants.h" #include "chrome/common/json_value_serializer.h" #include "net/base/file_stream.h" diff --git a/chrome/browser/extensions/extension_l10n_util.cc b/chrome/browser/extensions/extension_l10n_util.cc deleted file mode 100644 index 1ac93ff..0000000 --- a/chrome/browser/extensions/extension_l10n_util.cc +++ /dev/null @@ -1,204 +0,0 @@ -// 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/browser/extensions/extension_l10n_util.h" - -#include -#include -#include - -#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; - -namespace extension_l10n_util { - -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& chrome_locales, - const FilePath& locale_folder, - const std::string& locale_name, - std::set* 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* parent_locales) { - std::string locale(NormalizeLocale(current_locale)); - - const int kNameCapacity = 256; - char parent[kNameCapacity]; - strncpy(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* all_locales) { - const std::vector& 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 result; - GetParentLocales(available_locales[i], &result); - all_locales->insert(result.begin(), result.end()); - } -} - -bool GetValidLocales(const FilePath& locale_path, - std::set* valid_locales, - std::string* error) { - static std::set 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(dictionary); -} - -ExtensionMessageBundle* LoadMessageCatalogs( - const FilePath& locale_path, - const std::string& default_locale, - const std::string& application_locale, - const std::set& valid_locales, - std::string* error) { - // Order locales to load as current_locale, first_parent, ..., default_locale. - std::vector 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 > 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 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* l10n_paths) { - DCHECK(NULL != l10n_paths); - - std::vector locales; - static const std::string current_locale = - l10n_util::GetApplicationLocale(L""); - 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/browser/extensions/extension_l10n_util.h b/chrome/browser/extensions/extension_l10n_util.h deleted file mode 100644 index 07be73e..0000000 --- a/chrome/browser/extensions/extension_l10n_util.h +++ /dev/null @@ -1,77 +0,0 @@ -// 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_BROWSER_EXTENSIONS_EXTENSION_L10N_UTIL_H_ -#define CHROME_BROWSER_EXTENSIONS_EXTENSION_L10N_UTIL_H_ - -#include -#include -#include - -class DictionaryValue; -class Extension; -class ExtensionMessageBundle; -class FilePath; - -namespace extension_l10n_util { - -// 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& chrome_locales, - const FilePath& locale_folder, - const std::string& locale_name, - std::set* 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* 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* 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& 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* l10n_paths); - -} // namespace extension_l10n_util - -#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_L10N_UTIL_H_ diff --git a/chrome/browser/extensions/extension_l10n_util_unittest.cc b/chrome/browser/extensions/extension_l10n_util_unittest.cc deleted file mode 100644 index 7e7ce27..0000000 --- a/chrome/browser/extensions/extension_l10n_util_unittest.cc +++ /dev/null @@ -1,232 +0,0 @@ -// 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/browser/extensions/extension_l10n_util.h" - -#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 "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 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 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 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 locales; - EXPECT_TRUE(extension_l10n_util::GetValidLocales(install_dir, - &locales, - &error)); - - scoped_ptr 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 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 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 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 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 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 l10n_paths; - extension_l10n_util::GetL10nRelativePaths( - FilePath(FILE_PATH_LITERAL("foo/bar.js")), &l10n_paths); - ASSERT_FALSE(l10n_paths.empty()); - - std::vector 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/browser/utility_process_host.cc b/chrome/browser/utility_process_host.cc index 0e44688..95729b0 100644 --- a/chrome/browser/utility_process_host.cc +++ b/chrome/browser/utility_process_host.cc @@ -4,6 +4,8 @@ #include "chrome/browser/utility_process_host.h" +#include "app/app_switches.h" +#include "app/l10n_util.h" #include "base/command_line.h" #include "base/file_util.h" #include "base/message_loop.h" @@ -89,6 +91,10 @@ bool UtilityProcessHost::StartProcess(const FilePath& exposed_dir) { switches::kUtilityProcess); cmd_line.AppendSwitchWithValue(switches::kProcessChannelID, ASCIIToWide(channel_id())); + // Pass on the browser locale. + std::string locale = l10n_util::GetApplicationLocale(L""); + cmd_line.AppendSwitchWithValue(switches::kLang, ASCIIToWide(locale)); + SetCrashReporterCommandLine(&cmd_line); base::ProcessHandle process; diff --git a/chrome/chrome.gyp b/chrome/chrome.gyp index 8d0bb7e..736d860 100755 --- a/chrome/chrome.gyp +++ b/chrome/chrome.gyp @@ -535,6 +535,8 @@ 'common/extensions/extension_error_utils.h', 'common/extensions/extension_action.cc', 'common/extensions/extension_action.h', + 'common/extensions/extension_l10n_util.cc', + 'common/extensions/extension_l10n_util.h', 'common/extensions/extension_message_bundle.cc', 'common/extensions/extension_message_bundle.h', 'common/extensions/extension_resource.cc', @@ -1323,8 +1325,6 @@ 'browser/extensions/extension_host.h', 'browser/extensions/extension_install_ui.cc', 'browser/extensions/extension_install_ui.h', - 'browser/extensions/extension_l10n_util.cc', - 'browser/extensions/extension_l10n_util.h', 'browser/extensions/extension_message_service.cc', 'browser/extensions/extension_message_service.h', 'browser/extensions/extension_browser_event_router.cc', @@ -4494,7 +4494,6 @@ 'browser/download/save_package_unittest.cc', 'browser/encoding_menu_controller_unittest.cc', 'browser/extensions/extension_file_util_unittest.cc', - 'browser/extensions/extension_l10n_util_unittest.cc', 'browser/extensions/extension_messages_unittest.cc', 'browser/extensions/extension_process_manager_unittest.cc', 'browser/extensions/extension_ui_unittest.cc', @@ -4621,6 +4620,7 @@ 'common/extensions/extension_resource_unittest.cc', 'common/extensions/extension_unittest.cc', 'common/extensions/extension_action_unittest.cc', + 'common/extensions/extension_l10n_util_unittest.cc', 'common/extensions/extension_message_bundle_unittest.cc', 'common/extensions/update_manifest_unittest.cc', 'common/extensions/url_pattern_unittest.cc', 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 +#include +#include + +#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& chrome_locales, + const FilePath& locale_folder, + const std::string& locale_name, + std::set* 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* 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* all_locales) { + const std::vector& 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 result; + GetParentLocales(available_locales[i], &result); + all_locales->insert(result.begin(), result.end()); + } +} + +bool GetValidLocales(const FilePath& locale_path, + std::set* valid_locales, + std::string* error) { + static std::set 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(dictionary); +} + +ExtensionMessageBundle* LoadMessageCatalogs( + const FilePath& locale_path, + const std::string& default_locale, + const std::string& application_locale, + const std::set& valid_locales, + std::string* error) { + // Order locales to load as current_locale, first_parent, ..., default_locale. + std::vector 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 > 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 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* l10n_paths) { + DCHECK(NULL != l10n_paths); + + std::string* current_locale = GetProcessLocale(); + if (current_locale->empty()) + *current_locale = l10n_util::GetApplicationLocale(L""); + + std::vector 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 +#include +#include + +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& chrome_locales, + const FilePath& locale_folder, + const std::string& locale_name, + std::set* 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* 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* 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& 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* 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 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 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 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 locales; + EXPECT_TRUE(extension_l10n_util::GetValidLocales(install_dir, + &locales, + &error)); + + scoped_ptr 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 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 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 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 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 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 l10n_paths; + extension_l10n_util::GetL10nRelativePaths( + FilePath(FILE_PATH_LITERAL("foo/bar.js")), &l10n_paths); + ASSERT_FALSE(l10n_paths.empty()); + + std::vector 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" diff --git a/chrome/utility/utility_main.cc b/chrome/utility/utility_main.cc index bb669a7..b2eaa46 100644 --- a/chrome/utility/utility_main.cc +++ b/chrome/utility/utility_main.cc @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "app/app_switches.h" #include "base/command_line.h" #include "base/message_loop.h" #include "base/string_util.h" @@ -9,6 +10,7 @@ #include "chrome/common/child_process.h" #include "chrome/common/chrome_constants.h" #include "chrome/common/chrome_switches.h" +#include "chrome/common/extensions/extension_l10n_util.h" #include "chrome/common/logging_chrome.h" #include "chrome/common/main_function_params.h" #include "chrome/utility/utility_thread.h" @@ -39,6 +41,11 @@ int UtilityMain(const MainFunctionParams& parameters) { target_services->LowerToken(); #endif + CommandLine* command_line = CommandLine::ForCurrentProcess(); + std::string lang = command_line->GetSwitchValueASCII(switches::kLang); + if (!lang.empty()) + extension_l10n_util::SetProcessLocale(lang); + MessageLoop::current()->Run(); return 0; -- cgit v1.1