extension_l10n_util.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. //
  5. // This file declares extension specific l10n utils.
  6. #ifndef EXTENSIONS_COMMON_EXTENSION_L10N_UTIL_H_
  7. #define EXTENSIONS_COMMON_EXTENSION_L10N_UTIL_H_
  8. #include <set>
  9. #include <string>
  10. #include <vector>
  11. #include "base/auto_reset.h"
  12. #include "base/strings/string_piece.h"
  13. #include "extensions/common/manifest.h"
  14. #include "extensions/common/mojom/manifest.mojom-shared.h"
  15. namespace base {
  16. class DictionaryValue;
  17. class FilePath;
  18. }
  19. namespace extensions {
  20. class Extension;
  21. class MessageBundle;
  22. }
  23. namespace extension_l10n_util {
  24. enum class GzippedMessagesPermission {
  25. // Do not allow gzipped locale ('messages.json') files.
  26. kDisallow,
  27. // Allow gzipped locale files. This should only be set for trusted sources,
  28. // e.g. component extensions from the Chrome OS rootfs.
  29. kAllowForTrustedSource,
  30. };
  31. // Returns GzippedMessagesPermission::kAllowForTrustedSource for component
  32. // extensions, otherwise returns kDisallow.
  33. GzippedMessagesPermission GetGzippedMessagesPermissionForExtension(
  34. const extensions::Extension* extension);
  35. // Returns GzippedMessagesPermission::kAllowForTrustedSource for trusted
  36. // manifest locations, otherwise returns kDisallow.
  37. GzippedMessagesPermission GetGzippedMessagesPermissionForLocation(
  38. extensions::mojom::ManifestLocation location);
  39. // Called from tests to temporarily allow loading gzipped messages for non
  40. // component test extensions.
  41. base::AutoReset<bool> AllowGzippedMessagesAllowedForTest();
  42. // Set the locale for this process to a fixed value, rather than using the
  43. // normal file-based lookup mechanisms. This is used to set the locale inside
  44. // the sandboxed utility process, where file reading is not allowed.
  45. void SetProcessLocale(const std::string& locale);
  46. // Sets the preferred locale. This is the user-preferred locale, which may
  47. // differ from the actual process locale in use, like when a preferred locale of
  48. // "en-CA" is mapped to a process locale of "en-GB".
  49. void SetPreferredLocale(const std::string& locale);
  50. // Returns default locale in form "en-US" or "sr" or empty string if
  51. // "default_locale" section was not defined in the manifest.json file.
  52. std::string GetDefaultLocaleFromManifest(const base::DictionaryValue& manifest,
  53. std::string* error);
  54. // Returns true iff the extension was localized, and the current locale
  55. // doesn't match the locale written into info.extension_manifest.
  56. bool ShouldRelocalizeManifest(const base::DictionaryValue* manifest);
  57. // Localize extension name, description, browser_action and other fields
  58. // in the manifest.
  59. bool LocalizeManifest(const extensions::MessageBundle& messages,
  60. base::DictionaryValue* manifest,
  61. std::string* error);
  62. // Load message catalogs, localize manifest and attach message bundle to the
  63. // extension. |gzip_permission| will be passed to LoadMessageCatalogs
  64. // (see below for details).
  65. bool LocalizeExtension(const base::FilePath& extension_path,
  66. base::DictionaryValue* manifest,
  67. GzippedMessagesPermission gzip_permission,
  68. std::string* error);
  69. // Adds locale_name to the extension if it's in chrome_locales, and
  70. // if messages file is present (we don't check content of messages file here).
  71. // Returns false if locale_name was not found in chrome_locales, and sets
  72. // error with locale_name.
  73. // If file name starts with . return true (helps testing extensions under svn).
  74. bool AddLocale(const std::set<std::string>& chrome_locales,
  75. const base::FilePath& locale_folder,
  76. const std::string& locale_name,
  77. std::set<std::string>* valid_locales,
  78. std::string* error);
  79. // Returns normalized current locale, or default locale - en_US.
  80. std::string CurrentLocaleOrDefault();
  81. // Extends list of Chrome locales to them and their parents, so we can do
  82. // proper fallback.
  83. void GetAllLocales(std::set<std::string>* all_locales);
  84. // Provides a vector of all fallback locales for message localization.
  85. // The vector is ordered by priority of locale - application locale,
  86. // first_parent, ..., |default_locale|.
  87. void GetAllFallbackLocales(const std::string& default_locale,
  88. std::vector<std::string>* all_fallback_locales);
  89. // Fill |valid_locales| with all valid locales under |locale_path|.
  90. // |valid_locales| is the intersection of the set of locales supported by
  91. // Chrome and the set of locales specified by |locale_path|.
  92. // Returns true if vaild_locales contains at least one locale, false otherwise.
  93. // |error| contains an error message when a locale is corrupt or missing.
  94. bool GetValidLocales(const base::FilePath& locale_path,
  95. std::set<std::string>* valid_locales,
  96. std::string* error);
  97. // Loads messages file for the default locale and application locales
  98. // (application locales do not have to exist). Application locales include the
  99. // current locale and its parents. If |gzip_permission| is
  100. // kAllowForTrustedSource, this will look for compressed messages files and
  101. // decompress them if they exist. Returns the message bundle if it can load the
  102. // default locale messages file and all messages are valid. Otherwise returns
  103. // null and sets |error|.
  104. extensions::MessageBundle* LoadMessageCatalogs(
  105. const base::FilePath& locale_path,
  106. const std::string& default_locale,
  107. GzippedMessagesPermission gzip_permission,
  108. std::string* error);
  109. // Loads message catalogs for all locales to check for validity. Used for
  110. // validating unpacked extensions.
  111. bool ValidateExtensionLocales(const base::FilePath& extension_path,
  112. const base::DictionaryValue* manifest,
  113. std::string* error);
  114. // Returns true if directory has "." in the name (for .svn) or if it doesn't
  115. // belong to Chrome locales.
  116. // |locales_path| is extension_id/_locales
  117. // |locale_path| is extension_id/_locales/xx
  118. // |all_locales| is a set of all valid Chrome locales.
  119. bool ShouldSkipValidation(const base::FilePath& locales_path,
  120. const base::FilePath& locale_path,
  121. const std::set<std::string>& all_locales);
  122. // Sets the process and preferred locale for the duration of the current scope,
  123. // then reverts back to whatever the current values were before constructing
  124. // this. For testing purposed only!
  125. class ScopedLocaleForTest {
  126. public:
  127. // Only revert back to current locale at end of scope, don't set locale.
  128. ScopedLocaleForTest();
  129. // Sets temporary locale for the current scope.
  130. explicit ScopedLocaleForTest(base::StringPiece locale);
  131. // Sets process and preferred locales for the current scope.
  132. ScopedLocaleForTest(base::StringPiece process_locale,
  133. base::StringPiece preferred_locale);
  134. ~ScopedLocaleForTest();
  135. private:
  136. base::StringPiece process_locale_; // The process locale at ctor time.
  137. base::StringPiece preferred_locale_; // The preferred locale at ctor time.
  138. };
  139. // Returns a locale like "en-CA".
  140. const std::string& GetPreferredLocaleForTest();
  141. } // namespace extension_l10n_util
  142. #endif // EXTENSIONS_COMMON_EXTENSION_L10N_UTIL_H_