Extract SIGPIPE ignoring code to a common place.
[chromium-blink-merge.git] / chrome / common / extensions / extension_l10n_util.h
blob24178a076fc4b583dd46733a61a12aa3957eb579
1 // Copyright (c) 2012 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 CHROME_COMMON_EXTENSIONS_EXTENSION_L10N_UTIL_H_
8 #define CHROME_COMMON_EXTENSIONS_EXTENSION_L10N_UTIL_H_
10 #include <set>
11 #include <string>
12 #include <vector>
14 class FilePath;
16 namespace base {
17 class DictionaryValue;
20 namespace extensions {
21 struct ExtensionInfo;
22 class MessageBundle;
25 namespace extension_l10n_util {
27 // Set the locale for this process to a fixed value, rather than using the
28 // normal file-based lookup mechanisms. This is used to set the locale inside
29 // the sandboxed utility process, where file reading is not allowed.
30 void SetProcessLocale(const std::string& locale);
32 // Returns default locale in form "en-US" or "sr" or empty string if
33 // "default_locale" section was not defined in the manifest.json file.
34 std::string GetDefaultLocaleFromManifest(const base::DictionaryValue& manifest,
35 std::string* error);
37 // Returns true iff the extension was localized, and the current locale
38 // doesn't match the locale written into info.extension_manifest.
39 bool ShouldRelocalizeManifest(const extensions::ExtensionInfo& info);
41 // Localize extension name, description, browser_action and other fields
42 // in the manifest.
43 bool LocalizeManifest(const extensions::MessageBundle& messages,
44 base::DictionaryValue* manifest,
45 std::string* error);
47 // Load message catalogs, localize manifest and attach message bundle to the
48 // extension.
49 bool LocalizeExtension(const FilePath& extension_path,
50 base::DictionaryValue* manifest,
51 std::string* error);
53 // Adds locale_name to the extension if it's in chrome_locales, and
54 // if messages file is present (we don't check content of messages file here).
55 // Returns false if locale_name was not found in chrome_locales, and sets
56 // error with locale_name.
57 // If file name starts with . return true (helps testing extensions under svn).
58 bool AddLocale(const std::set<std::string>& chrome_locales,
59 const FilePath& locale_folder,
60 const std::string& locale_name,
61 std::set<std::string>* valid_locales,
62 std::string* error);
64 // Returns normalized current locale, or default locale - en_US.
65 std::string CurrentLocaleOrDefault();
67 // Extends list of Chrome locales to them and their parents, so we can do
68 // proper fallback.
69 void GetAllLocales(std::set<std::string>* all_locales);
71 // Provides a vector of all fallback locales for message localization.
72 // The vector is ordered by priority of locale - |application_locale|,
73 // first_parent, ..., |default_locale|.
74 void GetAllFallbackLocales(const std::string& application_locale,
75 const std::string& default_locale,
76 std::vector<std::string>* all_fallback_locales);
78 // Adds valid locales to the extension.
79 // 1. Do nothing if _locales directory is missing (not an error).
80 // 2. Get list of Chrome locales.
81 // 3. Enumerate all subdirectories of _locales directory.
82 // 4. Intersect both lists, and add intersection to the extension.
83 // Returns false if any of supplied locales don't match chrome list of locales.
84 // Fills out error with offending locale name.
85 bool GetValidLocales(const FilePath& locale_path,
86 std::set<std::string>* locales,
87 std::string* error);
89 // Loads messages file for default locale, and application locales (application
90 // locales doesn't have to exist). Application locale is current locale and its
91 // parents.
92 // Returns message bundle if it can load default locale messages file, and all
93 // messages are valid, else returns NULL and sets error.
94 extensions::MessageBundle* LoadMessageCatalogs(
95 const FilePath& locale_path,
96 const std::string& default_locale,
97 const std::string& app_locale,
98 const std::set<std::string>& valid_locales,
99 std::string* error);
101 // Returns true if directory has "." in the name (for .svn) or if it doesn't
102 // belong to Chrome locales.
103 // |locales_path| is extension_id/_locales
104 // |locale_path| is extension_id/_locales/xx
105 // |all_locales| is a set of all valid Chrome locales.
106 bool ShouldSkipValidation(const FilePath& locales_path,
107 const FilePath& locale_path,
108 const std::set<std::string>& all_locales);
110 // Sets the process locale for the duration of the current scope, then reverts
111 // back to whatever the current locale was before constructing this.
112 // For testing purposed only!
113 class ScopedLocaleForTest {
114 public:
115 // Only revert back to current locale at end of scope, don't set locale.
116 ScopedLocaleForTest();
118 // Set temporary locale for the current scope
119 explicit ScopedLocaleForTest(const std::string& locale);
121 ~ScopedLocaleForTest();
123 private:
124 std::string locale_; // The current locale at ctor time.
128 } // namespace extension_l10n_util
130 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_L10N_UTIL_H_