forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextension_l10n_util.cc
576 lines (492 loc) · 20 KB
/
extension_l10n_util.cc
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
// Copyright 2014 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/extension_l10n_util.h"
#include <stddef.h>
#include <set>
#include <string>
#include <vector>
#include "base/files/file_enumerator.h"
#include "base/files/file_util.h"
#include "base/json/json_file_value_serializer.h"
#include "base/json/json_string_value_serializer.h"
#include "base/logging.h"
#include "base/no_destructor.h"
#include "base/stl_util.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/error_utils.h"
#include "extensions/common/extension.h"
#include "extensions/common/extensions_client.h"
#include "extensions/common/file_util.h"
#include "extensions/common/manifest.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/message_bundle.h"
#include "extensions/common/utils/base_string.h"
#include "third_party/icu/source/common/unicode/uloc.h"
#include "third_party/zlib/google/compression_utils.h"
#include "ui/base/l10n/l10n_util.h"
namespace errors = extensions::manifest_errors;
namespace keys = extensions::manifest_keys;
namespace {
bool g_allow_gzipped_messages_for_test = false;
// Loads contents of the messages file for given locale. If file is not found,
// or there was parsing error we return null and set |error|. If
// |gzip_permission| is kAllowForTrustedSource, this will also look for a .gz
// version of the file and if found will decompresses it into a string first.
std::unique_ptr<base::DictionaryValue> LoadMessageFile(
const base::FilePath& locale_path,
const std::string& locale,
std::string* error,
extension_l10n_util::GzippedMessagesPermission gzip_permission) {
base::FilePath file_path =
locale_path.AppendASCII(locale).Append(extensions::kMessagesFilename);
std::unique_ptr<base::DictionaryValue> dictionary;
if (base::PathExists(file_path)) {
JSONFileValueDeserializer messages_deserializer(file_path);
dictionary = base::DictionaryValue::From(
messages_deserializer.Deserialize(nullptr, error));
} else if (gzip_permission == extension_l10n_util::GzippedMessagesPermission::
kAllowForTrustedSource ||
g_allow_gzipped_messages_for_test) {
// If a compressed version of the file exists, load that.
base::FilePath compressed_file_path =
file_path.AddExtension(FILE_PATH_LITERAL(".gz"));
if (base::PathExists(compressed_file_path)) {
std::string compressed_data;
if (!base::ReadFileToString(compressed_file_path, &compressed_data)) {
*error = base::StringPrintf("Failed to read compressed locale %s.",
locale.c_str());
return dictionary;
}
std::string data;
if (!compression::GzipUncompress(compressed_data, &data)) {
*error = base::StringPrintf("Failed to decompress locale %s.",
locale.c_str());
return dictionary;
}
JSONStringValueDeserializer messages_deserializer(data);
dictionary = base::DictionaryValue::From(
messages_deserializer.Deserialize(nullptr, error));
}
} else {
LOG(ERROR) << "Unable to load message file: " << locale_path.AsUTF8Unsafe();
}
if (!dictionary) {
if (error->empty()) {
// JSONFileValueSerializer just returns null if file cannot be read. It
// doesn't set the error, so we have to do it.
*error = base::StringPrintf("Catalog file is missing for locale %s.",
locale.c_str());
} else {
*error = extensions::ErrorUtils::FormatErrorMessage(
errors::kLocalesInvalidLocale,
base::UTF16ToUTF8(file_path.LossyDisplayName()), *error);
}
}
return dictionary;
}
// Localizes manifest value of string type for a given key.
bool LocalizeManifestValue(const std::string& key,
const extensions::MessageBundle& messages,
base::DictionaryValue* manifest,
std::string* error) {
std::string result;
if (!manifest->GetString(key, &result))
return true;
if (!messages.ReplaceMessages(&result, error))
return false;
manifest->SetString(key, result);
return true;
}
// Localizes manifest value of list type for a given key.
bool LocalizeManifestListValue(const std::string& key,
const extensions::MessageBundle& messages,
base::DictionaryValue* manifest,
std::string* error) {
base::ListValue* list = NULL;
if (!manifest->GetList(key, &list))
return true;
bool ret = true;
for (size_t i = 0; i < list->GetSize(); ++i) {
std::string result;
if (list->GetString(i, &result)) {
if (messages.ReplaceMessages(&result, error))
list->Set(i, std::make_unique<base::Value>(result));
else
ret = false;
}
}
return ret;
}
std::string& GetProcessLocale() {
static base::NoDestructor<std::string> process_locale;
return *process_locale;
}
std::string& GetPreferredLocale() {
static base::NoDestructor<std::string> preferred_locale;
return *preferred_locale;
}
// Returns the desired locale to use for localization.
std::string LocaleForLocalization() {
std::string preferred_locale =
l10n_util::NormalizeLocale(GetPreferredLocale());
if (!preferred_locale.empty())
return preferred_locale;
return extension_l10n_util::CurrentLocaleOrDefault();
}
} // namespace
namespace extension_l10n_util {
GzippedMessagesPermission GetGzippedMessagesPermissionForExtension(
const extensions::Extension* extension) {
return extension
? GetGzippedMessagesPermissionForLocation(extension->location())
: GzippedMessagesPermission::kDisallow;
}
GzippedMessagesPermission GetGzippedMessagesPermissionForLocation(
extensions::mojom::ManifestLocation location) {
// Component extensions are part of the chromium or chromium OS source and
// as such are considered a trusted source.
return location == extensions::mojom::ManifestLocation::kComponent
? GzippedMessagesPermission::kAllowForTrustedSource
: GzippedMessagesPermission::kDisallow;
}
base::AutoReset<bool> AllowGzippedMessagesAllowedForTest() {
return base::AutoReset<bool>(&g_allow_gzipped_messages_for_test, true);
}
void SetProcessLocale(const std::string& locale) {
GetProcessLocale() = locale;
}
void SetPreferredLocale(const std::string& locale) {
GetPreferredLocale() = locale;
}
std::string GetDefaultLocaleFromManifest(const base::DictionaryValue& manifest,
std::string* error) {
std::string default_locale;
if (manifest.GetString(keys::kDefaultLocale, &default_locale))
return default_locale;
*error = errors::kInvalidDefaultLocale;
return std::string();
}
bool ShouldRelocalizeManifest(const base::DictionaryValue* manifest) {
if (!manifest)
return false;
if (!manifest->HasKey(keys::kDefaultLocale))
return false;
std::string manifest_current_locale;
manifest->GetString(keys::kCurrentLocale, &manifest_current_locale);
return manifest_current_locale != LocaleForLocalization();
}
bool LocalizeManifest(const extensions::MessageBundle& messages,
base::DictionaryValue* manifest,
std::string* error) {
// Initialize name.
std::string result;
if (!manifest->GetString(keys::kName, &result)) {
*error = errors::kInvalidName;
return false;
}
if (!LocalizeManifestValue(keys::kName, messages, manifest, error)) {
return false;
}
// Initialize short name.
if (!LocalizeManifestValue(keys::kShortName, messages, manifest, error))
return false;
// Initialize description.
if (!LocalizeManifestValue(keys::kDescription, messages, manifest, error))
return false;
// Initialize browser_action.default_title
std::string key(keys::kBrowserAction);
key.append(".");
key.append(keys::kActionDefaultTitle);
if (!LocalizeManifestValue(key, messages, manifest, error))
return false;
// Initialize page_action.default_title
key.assign(keys::kPageAction);
key.append(".");
key.append(keys::kActionDefaultTitle);
if (!LocalizeManifestValue(key, messages, manifest, error))
return false;
// Initialize action.default_title
// TODO(devlin): These could easily use something like base::StrCat().
key.assign(keys::kAction);
key.append(".");
key.append(keys::kActionDefaultTitle);
if (!LocalizeManifestValue(key, messages, manifest, error))
return false;
// Initialize omnibox.keyword.
if (!LocalizeManifestValue(keys::kOmniboxKeyword, messages, manifest, error))
return false;
base::ListValue* file_handlers = NULL;
if (manifest->GetList(keys::kFileBrowserHandlers, &file_handlers)) {
key.assign(keys::kFileBrowserHandlers);
for (size_t i = 0; i < file_handlers->GetSize(); i++) {
base::DictionaryValue* handler = NULL;
if (!file_handlers->GetDictionary(i, &handler)) {
*error = errors::kInvalidFileBrowserHandler;
return false;
}
if (!LocalizeManifestValue(keys::kActionDefaultTitle, messages, handler,
error))
return false;
}
}
// Initialize all input_components
base::ListValue* input_components = NULL;
if (manifest->GetList(keys::kInputComponents, &input_components)) {
for (size_t i = 0; i < input_components->GetSize(); ++i) {
base::DictionaryValue* module = NULL;
if (!input_components->GetDictionary(i, &module)) {
*error = errors::kInvalidInputComponents;
return false;
}
if (!LocalizeManifestValue(keys::kName, messages, module, error))
return false;
if (!LocalizeManifestValue(keys::kDescription, messages, module, error))
return false;
}
}
// Initialize app.launch.local_path.
if (!LocalizeManifestValue(keys::kLaunchLocalPath, messages, manifest, error))
return false;
// Initialize app.launch.web_url.
if (!LocalizeManifestValue(keys::kLaunchWebURL, messages, manifest, error))
return false;
// Initialize description of commmands.
base::DictionaryValue* commands_handler = NULL;
if (manifest->GetDictionary(keys::kCommands, &commands_handler)) {
for (base::DictionaryValue::Iterator iter(*commands_handler);
!iter.IsAtEnd();
iter.Advance()) {
key.assign(
base::StringPrintf("commands.%s.description", iter.key().c_str()));
if (!LocalizeManifestValue(key, messages, manifest, error))
return false;
}
}
// Initialize search_provider fields.
base::DictionaryValue* search_provider = NULL;
if (manifest->GetDictionary(keys::kOverrideSearchProvider,
&search_provider)) {
for (base::DictionaryValue::Iterator iter(*search_provider);
!iter.IsAtEnd();
iter.Advance()) {
key.assign(base::StringPrintf(
"%s.%s", keys::kOverrideSearchProvider, iter.key().c_str()));
bool success =
(key == keys::kSettingsOverrideAlternateUrls)
? LocalizeManifestListValue(key, messages, manifest, error)
: LocalizeManifestValue(key, messages, manifest, error);
if (!success)
return false;
}
}
// Initialize chrome_settings_overrides.homepage.
if (!LocalizeManifestValue(
keys::kOverrideHomepage, messages, manifest, error))
return false;
// Initialize chrome_settings_overrides.startup_pages.
if (!LocalizeManifestListValue(
keys::kOverrideStartupPage, messages, manifest, error))
return false;
// Add desired locale key to the manifest, so we can overwrite prefs
// with new manifest when chrome locale changes.
manifest->SetString(keys::kCurrentLocale, LocaleForLocalization());
return true;
}
bool LocalizeExtension(const base::FilePath& extension_path,
base::DictionaryValue* manifest,
GzippedMessagesPermission gzip_permission,
std::string* error) {
DCHECK(manifest);
std::string default_locale = GetDefaultLocaleFromManifest(*manifest, error);
std::unique_ptr<extensions::MessageBundle> message_bundle(
extensions::file_util::LoadMessageBundle(extension_path, default_locale,
gzip_permission, error));
if (!message_bundle && !error->empty())
return false;
if (message_bundle && !LocalizeManifest(*message_bundle, manifest, error))
return false;
return true;
}
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) {
// Accept name that starts with a . but don't add it to the list of supported
// locales.
if (base::StartsWith(locale_name, ".", base::CompareCase::SENSITIVE))
return true;
if (chrome_locales.find(locale_name) == chrome_locales.end()) {
// Warn if there is an extension locale that's not in the Chrome list,
// but don't fail.
DLOG(WARNING) << base::StringPrintf("Supplied locale %s is not supported.",
locale_name.c_str());
return true;
}
// Check if messages file is actually present (but don't check content).
if (!base::PathExists(locale_folder.Append(extensions::kMessagesFilename))) {
*error = base::StringPrintf("Catalog file is missing for locale %s.",
locale_name.c_str());
return false;
}
valid_locales->insert(locale_name);
return true;
}
std::string CurrentLocaleOrDefault() {
std::string current_locale = l10n_util::NormalizeLocale(GetProcessLocale());
if (current_locale.empty())
current_locale = "en";
return current_locale;
}
void GetAllLocales(std::set<std::string>* all_locales) {
const std::vector<std::string>& available_locales =
l10n_util::GetAvailableICULocales();
// Add all parents of the current locale to the available locales set.
// I.e. for sr_Cyrl_RS we add sr_Cyrl_RS, sr_Cyrl and sr.
for (size_t i = 0; i < available_locales.size(); ++i) {
std::vector<std::string> result;
l10n_util::GetParentLocales(available_locales[i], &result);
all_locales->insert(result.begin(), result.end());
}
}
void GetAllFallbackLocales(const std::string& default_locale,
std::vector<std::string>* all_fallback_locales) {
DCHECK(all_fallback_locales);
std::string application_locale = CurrentLocaleOrDefault();
// Use the preferred locale if available. Otherwise, fall back to the
// application locale or the application locale's parent locales. Thus, a
// preferred locale of "en_CA" with an application locale of "en_GB" will
// first try to use an en_CA locale folder, followed by en_GB, followed by en.
std::string preferred_locale =
l10n_util::NormalizeLocale(GetPreferredLocale());
if (!preferred_locale.empty() && preferred_locale != default_locale &&
preferred_locale != application_locale) {
all_fallback_locales->push_back(preferred_locale);
}
if (!application_locale.empty() && application_locale != default_locale)
l10n_util::GetParentLocales(application_locale, all_fallback_locales);
all_fallback_locales->push_back(default_locale);
}
bool GetValidLocales(const base::FilePath& locale_path,
std::set<std::string>* valid_locales,
std::string* error) {
std::set<std::string> chrome_locales;
GetAllLocales(&chrome_locales);
// Enumerate all supplied locales in the extension.
base::FileEnumerator locales(
locale_path, false, base::FileEnumerator::DIRECTORIES);
base::FilePath locale_folder;
while (!(locale_folder = locales.Next()).empty()) {
std::string locale_name = locale_folder.BaseName().MaybeAsASCII();
if (locale_name.empty()) {
NOTREACHED();
continue; // Not ASCII.
}
if (!AddLocale(
chrome_locales, locale_folder, locale_name, valid_locales, error)) {
valid_locales->clear();
return false;
}
}
if (valid_locales->empty()) {
*error = errors::kLocalesNoValidLocaleNamesListed;
return false;
}
return true;
}
extensions::MessageBundle* LoadMessageCatalogs(
const base::FilePath& locale_path,
const std::string& default_locale,
GzippedMessagesPermission gzip_permission,
std::string* error) {
std::vector<std::string> all_fallback_locales;
GetAllFallbackLocales(default_locale, &all_fallback_locales);
std::vector<std::unique_ptr<base::DictionaryValue>> catalogs;
for (size_t i = 0; i < all_fallback_locales.size(); ++i) {
// Skip all parent locales that are not supplied.
base::FilePath this_locale_path =
locale_path.AppendASCII(all_fallback_locales[i]);
if (!base::PathExists(this_locale_path))
continue;
std::unique_ptr<base::DictionaryValue> catalog = LoadMessageFile(
locale_path, all_fallback_locales[i], error, gzip_permission);
if (!catalog.get()) {
// If locale is valid, but messages.json is corrupted or missing, return
// an error.
return nullptr;
}
catalogs.push_back(std::move(catalog));
}
return extensions::MessageBundle::Create(catalogs, error);
}
bool ValidateExtensionLocales(const base::FilePath& extension_path,
const base::DictionaryValue* manifest,
std::string* error) {
std::string default_locale = GetDefaultLocaleFromManifest(*manifest, error);
if (default_locale.empty())
return true;
base::FilePath locale_path = extension_path.Append(extensions::kLocaleFolder);
std::set<std::string> valid_locales;
if (!GetValidLocales(locale_path, &valid_locales, error))
return false;
for (auto locale = valid_locales.cbegin(); locale != valid_locales.cend();
++locale) {
std::string locale_error;
std::unique_ptr<base::DictionaryValue> catalog =
LoadMessageFile(locale_path, *locale, &locale_error,
GzippedMessagesPermission::kDisallow);
if (!locale_error.empty()) {
if (!error->empty())
error->append(" ");
error->append(locale_error);
}
}
return error->empty();
}
bool ShouldSkipValidation(const base::FilePath& locales_path,
const base::FilePath& locale_path,
const std::set<std::string>& all_locales) {
// Since we use this string as a key in a DictionaryValue, be paranoid about
// skipping any strings with '.'. This happens sometimes, for example with
// '.svn' directories.
base::FilePath relative_path;
if (!locales_path.AppendRelativePath(locale_path, &relative_path)) {
NOTREACHED();
return true;
}
std::string subdir = relative_path.MaybeAsASCII();
if (subdir.empty())
return true; // Non-ASCII.
if (base::Contains(subdir, '.'))
return true;
// On case-insensitive file systems we will load messages by matching them
// with locale names (see LoadMessageCatalogs). Reversed comparison must still
// work here, when we match locale name with file name.
if (!extensions::ContainsStringIgnoreCaseASCII(all_locales, subdir))
return true;
return false;
}
ScopedLocaleForTest::ScopedLocaleForTest()
: process_locale_(GetProcessLocale()),
preferred_locale_(GetPreferredLocale()) {}
ScopedLocaleForTest::ScopedLocaleForTest(base::StringPiece locale)
: ScopedLocaleForTest(locale, locale) {}
ScopedLocaleForTest::ScopedLocaleForTest(base::StringPiece process_locale,
base::StringPiece preferred_locale)
: ScopedLocaleForTest() {
SetProcessLocale(process_locale.as_string());
SetPreferredLocale(preferred_locale.as_string());
}
ScopedLocaleForTest::~ScopedLocaleForTest() {
SetProcessLocale(process_locale_.as_string());
SetPreferredLocale(preferred_locale_.as_string());
}
const std::string& GetPreferredLocaleForTest() {
return GetPreferredLocale();
}
} // namespace extension_l10n_util