summaryrefslogtreecommitdiffstats
path: root/extensions/common/manifest_handlers
diff options
context:
space:
mode:
authorrockot <rockot@chromium.org>2014-11-12 16:21:22 -0800
committerCommit bot <commit-bot@chromium.org>2014-11-13 00:21:59 +0000
commite5fd3d99ade77d8b9c3df54d6f4d6e8e7bcd9d6a (patch)
treeee4f13834d477164256eb6bce4a39472a38b0b2c /extensions/common/manifest_handlers
parent39698bd5cf1a61e67796b9a855f32321a1d29242 (diff)
downloadchromium_src-e5fd3d99ade77d8b9c3df54d6f4d6e8e7bcd9d6a.zip
chromium_src-e5fd3d99ade77d8b9c3df54d6f4d6e8e7bcd9d6a.tar.gz
chromium_src-e5fd3d99ade77d8b9c3df54d6f4d6e8e7bcd9d6a.tar.bz2
Move default_locale manifest handler out of //chrome
BUG=398671 TBR=kalman@chromium.org,jochen@chromium.org for various #include updates Review URL: https://codereview.chromium.org/719243002 Cr-Commit-Position: refs/heads/master@{#303934}
Diffstat (limited to 'extensions/common/manifest_handlers')
-rw-r--r--extensions/common/manifest_handlers/default_locale_handler.cc119
-rw-r--r--extensions/common/manifest_handlers/default_locale_handler.h46
-rw-r--r--extensions/common/manifest_handlers/default_locale_manifest_unittest.cc23
3 files changed, 188 insertions, 0 deletions
diff --git a/extensions/common/manifest_handlers/default_locale_handler.cc b/extensions/common/manifest_handlers/default_locale_handler.cc
new file mode 100644
index 0000000..debb664
--- /dev/null
+++ b/extensions/common/manifest_handlers/default_locale_handler.cc
@@ -0,0 +1,119 @@
+// Copyright (c) 2013 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 "extensions/common/manifest_handlers/default_locale_handler.h"
+
+#include "base/files/file_enumerator.h"
+#include "base/files/file_util.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/strings/string_util.h"
+#include "base/strings/stringprintf.h"
+#include "base/strings/utf_string_conversions.h"
+#include "base/values.h"
+#include "extensions/common/constants.h"
+#include "extensions/common/extension_l10n_util.h"
+#include "extensions/common/manifest.h"
+#include "extensions/common/manifest_constants.h"
+#include "grit/extensions_strings.h"
+#include "ui/base/l10n/l10n_util.h"
+
+namespace extensions {
+
+namespace keys = manifest_keys;
+namespace errors = manifest_errors;
+
+// static
+const std::string& LocaleInfo::GetDefaultLocale(const Extension* extension) {
+ LocaleInfo* info = static_cast<LocaleInfo*>(
+ extension->GetManifestData(keys::kDefaultLocale));
+ return info ? info->default_locale : base::EmptyString();
+}
+
+DefaultLocaleHandler::DefaultLocaleHandler() {
+}
+
+DefaultLocaleHandler::~DefaultLocaleHandler() {
+}
+
+bool DefaultLocaleHandler::Parse(Extension* extension, base::string16* error) {
+ scoped_ptr<LocaleInfo> info(new LocaleInfo);
+ if (!extension->manifest()->GetString(keys::kDefaultLocale,
+ &info->default_locale) ||
+ !l10n_util::IsValidLocaleSyntax(info->default_locale)) {
+ *error = base::ASCIIToUTF16(manifest_errors::kInvalidDefaultLocale);
+ return false;
+ }
+ extension->SetManifestData(keys::kDefaultLocale, info.release());
+ return true;
+}
+
+bool DefaultLocaleHandler::Validate(
+ const Extension* extension,
+ std::string* error,
+ std::vector<InstallWarning>* warnings) const {
+ // default_locale and _locales have to be both present or both missing.
+ const base::FilePath path = extension->path().Append(kLocaleFolder);
+ bool path_exists = base::PathExists(path);
+ std::string default_locale =
+ extensions::LocaleInfo::GetDefaultLocale(extension);
+
+ // If both default locale and _locales folder are empty, skip verification.
+ if (default_locale.empty() && !path_exists)
+ return true;
+
+ if (default_locale.empty() && path_exists) {
+ *error = l10n_util::GetStringUTF8(
+ IDS_EXTENSION_LOCALES_NO_DEFAULT_LOCALE_SPECIFIED);
+ return false;
+ } else if (!default_locale.empty() && !path_exists) {
+ *error = errors::kLocalesTreeMissing;
+ return false;
+ }
+
+ // Treat all folders under _locales as valid locales.
+ base::FileEnumerator locales(path, false, base::FileEnumerator::DIRECTORIES);
+
+ std::set<std::string> all_locales;
+ extension_l10n_util::GetAllLocales(&all_locales);
+ const base::FilePath default_locale_path = path.AppendASCII(default_locale);
+ bool has_default_locale_message_file = false;
+
+ base::FilePath locale_path;
+ while (!(locale_path = locales.Next()).empty()) {
+ if (extension_l10n_util::ShouldSkipValidation(path, locale_path,
+ all_locales))
+ continue;
+
+ base::FilePath messages_path = locale_path.Append(kMessagesFilename);
+
+ if (!base::PathExists(messages_path)) {
+ *error = base::StringPrintf(
+ "%s %s", errors::kLocalesMessagesFileMissing,
+ base::UTF16ToUTF8(messages_path.LossyDisplayName()).c_str());
+ return false;
+ }
+
+ if (locale_path == default_locale_path)
+ has_default_locale_message_file = true;
+ }
+
+ // Only message file for default locale has to exist.
+ if (!has_default_locale_message_file) {
+ *error = errors::kLocalesNoDefaultMessages;
+ return false;
+ }
+
+ return true;
+}
+
+bool DefaultLocaleHandler::AlwaysValidateForType(Manifest::Type type) const {
+ // Required to validate _locales directory; see Validate.
+ return true;
+}
+
+const std::vector<std::string> DefaultLocaleHandler::Keys() const {
+ return SingleKey(keys::kDefaultLocale);
+}
+
+} // namespace extensions
diff --git a/extensions/common/manifest_handlers/default_locale_handler.h b/extensions/common/manifest_handlers/default_locale_handler.h
new file mode 100644
index 0000000..ee5ff4c
--- /dev/null
+++ b/extensions/common/manifest_handlers/default_locale_handler.h
@@ -0,0 +1,46 @@
+// Copyright (c) 2013 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 EXTENSIONS_COMMON_MANIFEST_HANDLERS_DEFAULT_LOCALE_HANDLER_H_
+#define EXTENSIONS_COMMON_MANIFEST_HANDLERS_DEFAULT_LOCALE_HANDLER_H_
+
+#include <string>
+
+#include "extensions/common/extension.h"
+#include "extensions/common/manifest_handler.h"
+
+namespace extensions {
+
+// A structure to hold the locale information for an extension.
+struct LocaleInfo : public Extension::ManifestData {
+ // Default locale for fall back. Can be empty if extension is not localized.
+ std::string default_locale;
+
+ static const std::string& GetDefaultLocale(const Extension* extension);
+};
+
+// Parses the "default_locale" manifest key.
+class DefaultLocaleHandler : public ManifestHandler {
+ public:
+ DefaultLocaleHandler();
+ ~DefaultLocaleHandler() override;
+
+ bool Parse(Extension* extension, base::string16* error) override;
+
+ // Validates locale info. Doesn't check if messages.json files are valid.
+ bool Validate(const Extension* extension,
+ std::string* error,
+ std::vector<InstallWarning>* warnings) const override;
+
+ bool AlwaysValidateForType(Manifest::Type type) const override;
+
+ private:
+ const std::vector<std::string> Keys() const override;
+
+ DISALLOW_COPY_AND_ASSIGN(DefaultLocaleHandler);
+};
+
+} // namespace extensions
+
+#endif // EXTENSIONS_COMMON_MANIFEST_HANDLERS_DEFAULT_LOCALE_HANDLER_H_
diff --git a/extensions/common/manifest_handlers/default_locale_manifest_unittest.cc b/extensions/common/manifest_handlers/default_locale_manifest_unittest.cc
new file mode 100644
index 0000000..fd700290
--- /dev/null
+++ b/extensions/common/manifest_handlers/default_locale_manifest_unittest.cc
@@ -0,0 +1,23 @@
+// Copyright (c) 2013 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 "extensions/common/manifest_constants.h"
+#include "extensions/common/manifest_handlers/default_locale_handler.h"
+#include "extensions/common/manifest_test.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace extensions {
+
+using DefaultLocaleManifestTest = ManifestTest;
+
+TEST_F(DefaultLocaleManifestTest, DefaultLocale) {
+ LoadAndExpectError("default_locale_invalid.json",
+ manifest_errors::kInvalidDefaultLocale);
+
+ scoped_refptr<Extension> extension(
+ LoadAndExpectSuccess("default_locale_valid.json"));
+ EXPECT_EQ("de-AT", LocaleInfo::GetDefaultLocale(extension.get()));
+}
+
+} // namespace extensions