Fix nullptr crash in OnEmbed
[chromium-blink-merge.git] / extensions / common / extension_l10n_util.h
blob0f7f8c953a55fba8e2963d858039641e29a79dad
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.
7 #ifndef EXTENSIONS_COMMON_EXTENSION_L10N_UTIL_H_
8 #define EXTENSIONS_COMMON_EXTENSION_L10N_UTIL_H_
10 #include <set>
11 #include <string>
12 #include <vector>
14 namespace base {
15 class DictionaryValue;
16 class FilePath;
19 namespace extensions {
20 struct ExtensionInfo;
21 class MessageBundle;
24 namespace extension_l10n_util {
26 // Set the locale for this process to a fixed value, rather than using the
27 // normal file-based lookup mechanisms. This is used to set the locale inside
28 // the sandboxed utility process, where file reading is not allowed.
29 void SetProcessLocale(const std::string& locale);
31 // Returns default locale in form "en-US" or "sr" or empty string if
32 // "default_locale" section was not defined in the manifest.json file.
33 std::string GetDefaultLocaleFromManifest(const base::DictionaryValue& manifest,
34 std::string* error);
36 // Returns true iff the extension was localized, and the current locale
37 // doesn't match the locale written into info.extension_manifest.
38 bool ShouldRelocalizeManifest(const base::DictionaryValue* manifest);
40 // Localize extension name, description, browser_action and other fields
41 // in the manifest.
42 bool LocalizeManifest(const extensions::MessageBundle& messages,
43 base::DictionaryValue* manifest,
44 std::string* error);
46 // Load message catalogs, localize manifest and attach message bundle to the
47 // extension.
48 bool LocalizeExtension(const base::FilePath& extension_path,
49 base::DictionaryValue* manifest,
50 std::string* error);
52 // Adds locale_name to the extension if it's in chrome_locales, and
53 // if messages file is present (we don't check content of messages file here).
54 // Returns false if locale_name was not found in chrome_locales, and sets
55 // error with locale_name.
56 // If file name starts with . return true (helps testing extensions under svn).
57 bool AddLocale(const std::set<std::string>& chrome_locales,
58 const base::FilePath& locale_folder,
59 const std::string& locale_name,
60 std::set<std::string>* valid_locales,
61 std::string* error);
63 // Returns normalized current locale, or default locale - en_US.
64 std::string CurrentLocaleOrDefault();
66 // Extends list of Chrome locales to them and their parents, so we can do
67 // proper fallback.
68 void GetAllLocales(std::set<std::string>* all_locales);
70 // Provides a vector of all fallback locales for message localization.
71 // The vector is ordered by priority of locale - |application_locale|,
72 // first_parent, ..., |default_locale|.
73 void GetAllFallbackLocales(const std::string& application_locale,
74 const std::string& default_locale,
75 std::vector<std::string>* all_fallback_locales);
77 // Fill |valid_locales| with all valid locales under |locale_path|.
78 // |valid_locales| is the intersection of the set of locales supported by
79 // Chrome and the set of locales specified by |locale_path|.
80 // Returns true if vaild_locales contains at least one locale, false otherwise.
81 // |error| contains an error message when a locale is corrupt or missing.
82 bool GetValidLocales(const base::FilePath& locale_path,
83 std::set<std::string>* valid_locales,
84 std::string* error);
86 // Loads messages file for default locale, and application locales (application
87 // locales doesn't have to exist). Application locale is current locale and its
88 // parents.
89 // Returns message bundle if it can load default locale messages file, and all
90 // messages are valid, else returns NULL and sets error.
91 extensions::MessageBundle* LoadMessageCatalogs(
92 const base::FilePath& locale_path,
93 const std::string& default_locale,
94 const std::string& app_locale,
95 std::string* error);
97 // Loads message catalogs for all locales to check for validity.
98 bool ValidateExtensionLocales(const base::FilePath& extension_path,
99 const base::DictionaryValue* manifest,
100 std::string* error);
102 // Returns true if directory has "." in the name (for .svn) or if it doesn't
103 // belong to Chrome locales.
104 // |locales_path| is extension_id/_locales
105 // |locale_path| is extension_id/_locales/xx
106 // |all_locales| is a set of all valid Chrome locales.
107 bool ShouldSkipValidation(const base::FilePath& locales_path,
108 const base::FilePath& locale_path,
109 const std::set<std::string>& all_locales);
111 // Sets the process locale for the duration of the current scope, then reverts
112 // back to whatever the current locale was before constructing this.
113 // For testing purposed only!
114 class ScopedLocaleForTest {
115 public:
116 // Only revert back to current locale at end of scope, don't set locale.
117 ScopedLocaleForTest();
119 // Set temporary locale for the current scope
120 explicit ScopedLocaleForTest(const std::string& locale);
122 ~ScopedLocaleForTest();
124 private:
125 std::string locale_; // The current locale at ctor time.
128 } // namespace extension_l10n_util
130 #endif // EXTENSIONS_COMMON_EXTENSION_L10N_UTIL_H_