File: extension_l10n_util.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (174 lines) | stat: -rw-r--r-- 7,301 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2014 The Chromium Authors
// 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 EXTENSIONS_COMMON_EXTENSION_L10N_UTIL_H_
#define EXTENSIONS_COMMON_EXTENSION_L10N_UTIL_H_

#include <set>
#include <string>
#include <string_view>
#include <vector>

#include "base/auto_reset.h"
#include "base/values.h"
#include "extensions/common/manifest.h"
#include "extensions/common/mojom/manifest.mojom-shared.h"

namespace base {
class FilePath;
}

namespace extensions {
class Extension;
class MessageBundle;
}

namespace extension_l10n_util {

enum class GzippedMessagesPermission {
  // Do not allow gzipped locale ('messages.json') files.
  kDisallow,
  // Allow gzipped locale files. This should only be set for trusted sources,
  // e.g. component extensions from the Chrome OS rootfs.
  kAllowForTrustedSource,
};

// Returns GzippedMessagesPermission::kAllowForTrustedSource for component
// extensions, otherwise returns kDisallow.
GzippedMessagesPermission GetGzippedMessagesPermissionForExtension(
    const extensions::Extension* extension);

// Returns GzippedMessagesPermission::kAllowForTrustedSource for trusted
// manifest locations, otherwise returns kDisallow.
GzippedMessagesPermission GetGzippedMessagesPermissionForLocation(
    extensions::mojom::ManifestLocation location);

// Called from tests to temporarily allow loading gzipped messages for non
// component test extensions.
base::AutoReset<bool> AllowGzippedMessagesAllowedForTest();

// 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);

// Sets the preferred locale. This is the user-preferred locale, which may
// differ from the actual process locale in use, like when a preferred locale of
// "en-CA" is mapped to a process locale of "en-GB".
void SetPreferredLocale(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 base::Value::Dict& manifest,
                                         std::string* error);

// Returns true iff the extension was localized, and the current locale
// doesn't match the locale written into info.extension_manifest.
bool ShouldRelocalizeManifest(const base::Value::Dict& manifest);

// Localize extension name, description, browser_action and other fields
// in the manifest.
bool LocalizeManifest(const extensions::MessageBundle& messages,
                      base::Value::Dict* manifest,
                      std::string* error);

// Load message catalogs, localize manifest and attach message bundle to the
// extension. `gzip_permission` will be passed to LoadMessageCatalogs
// (see below for details).
bool LocalizeExtension(const base::FilePath& extension_path,
                       base::Value::Dict* manifest,
                       GzippedMessagesPermission gzip_permission,
                       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<std::string>& chrome_locales,
               const base::FilePath& locale_folder,
               const std::string& locale_name,
               std::set<std::string>* valid_locales,
               std::string* error);

// Returns normalized current locale, or default locale - en_US.
std::string CurrentLocaleOrDefault();

// Extends list of Chrome locales to them and their parents, so we can do
// proper fallback.
void GetAllLocales(std::set<std::string>* all_locales);

// Provides a vector of all fallback locales for message localization.
// The vector is ordered by priority of locale - application locale,
// first_parent, ..., `default_locale`.
void GetAllFallbackLocales(const std::string& default_locale,
                           std::vector<std::string>* all_fallback_locales);

// Fill `valid_locales` with all valid locales under `locale_path`.
// `valid_locales` is the intersection of the set of locales supported by
// Chrome and the set of locales specified by `locale_path`.
// Returns true if valid_locales contains at least one locale, false otherwise.
// `error` contains an error message when a locale is corrupt or missing.
bool GetValidLocales(const base::FilePath& locale_path,
                     std::set<std::string>* valid_locales,
                     std::string* error);

// Loads messages file for the default locale and application locales
// (application locales do not have to exist). Application locales include the
// current locale and its parents. If `gzip_permission` is
// kAllowForTrustedSource, this will look for compressed messages files and
// decompress them if they exist. Returns the message bundle if it can load the
// default locale messages file and all messages are valid. Otherwise returns
// null and sets `error`.
extensions::MessageBundle* LoadMessageCatalogs(
    const base::FilePath& locale_path,
    const std::string& default_locale,
    GzippedMessagesPermission gzip_permission,
    std::string* error);

// Loads message catalogs for all locales to check for validity. Used for
// validating unpacked extensions.
bool ValidateExtensionLocales(const base::FilePath& extension_path,
                              const base::Value::Dict& manifest,
                              std::string* error);

// Returns true if directory has "." in the name (for .svn) or if it doesn't
// belong to Chrome locales.
// `locales_path` is extension_id/_locales
// `locale_path` is extension_id/_locales/xx
// `all_locales` is a set of all valid Chrome locales.
bool ShouldSkipValidation(const base::FilePath& locales_path,
                          const base::FilePath& locale_path,
                          const std::set<std::string>& all_locales);

// Sets the process and preferred locale for the duration of the current scope,
// then reverts back to whatever the current values were before constructing
// this. For testing purposed only!
class ScopedLocaleForTest {
 public:
  // Only revert back to current locale at end of scope, don't set locale.
  ScopedLocaleForTest();

  // Sets temporary locale for the current scope.
  explicit ScopedLocaleForTest(std::string_view locale);

  // Sets process and preferred locales for the current scope.
  ScopedLocaleForTest(std::string_view process_locale,
                      std::string_view preferred_locale);

  ~ScopedLocaleForTest();

 private:
  std::string_view process_locale_;    // The process locale at ctor time.
  std::string_view preferred_locale_;  // The preferred locale at ctor time.
};

// Returns a locale like "en-CA".
const std::string& GetPreferredLocaleForTest();

}  // namespace extension_l10n_util

#endif  // EXTENSIONS_COMMON_EXTENSION_L10N_UTIL_H_