OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/common/extensions/extension_l10n_util.h" | 5 #include "chrome/common/extensions/extension_l10n_util.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
13 #include "base/json/json_file_value_serializer.h" | 13 #include "base/json/json_file_value_serializer.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/memory/linked_ptr.h" | 15 #include "base/memory/linked_ptr.h" |
16 #include "base/stringprintf.h" | 16 #include "base/stringprintf.h" |
17 #include "base/values.h" | 17 #include "base/values.h" |
18 #include "chrome/common/extensions/extension.h" | 18 #include "chrome/common/extensions/extension.h" |
19 #include "chrome/common/extensions/extension_manifest_constants.h" | 19 #include "chrome/common/extensions/extension_manifest_constants.h" |
20 #include "chrome/common/extensions/extension_file_util.h" | 20 #include "chrome/common/extensions/extension_file_util.h" |
21 #include "chrome/common/extensions/extension_message_bundle.h" | 21 #include "chrome/common/extensions/message_bundle.h" |
22 #include "chrome/common/url_constants.h" | 22 #include "chrome/common/url_constants.h" |
23 #include "ui/base/l10n/l10n_util.h" | 23 #include "ui/base/l10n/l10n_util.h" |
24 #include "unicode/uloc.h" | 24 #include "unicode/uloc.h" |
25 | 25 |
26 namespace errors = extension_manifest_errors; | 26 namespace errors = extension_manifest_errors; |
27 namespace keys = extension_manifest_keys; | 27 namespace keys = extension_manifest_keys; |
28 | 28 |
29 static std::string& GetProcessLocale() { | 29 static std::string& GetProcessLocale() { |
30 CR_DEFINE_STATIC_LOCAL(std::string, locale, ()); | 30 CR_DEFINE_STATIC_LOCAL(std::string, locale, ()); |
31 return locale; | 31 return locale; |
(...skipping 24 matching lines...) Expand all Loading... |
56 if (!manifest->HasKey(keys::kDefaultLocale)) | 56 if (!manifest->HasKey(keys::kDefaultLocale)) |
57 return false; | 57 return false; |
58 | 58 |
59 std::string manifest_current_locale; | 59 std::string manifest_current_locale; |
60 manifest->GetString(keys::kCurrentLocale, &manifest_current_locale); | 60 manifest->GetString(keys::kCurrentLocale, &manifest_current_locale); |
61 return manifest_current_locale != CurrentLocaleOrDefault(); | 61 return manifest_current_locale != CurrentLocaleOrDefault(); |
62 } | 62 } |
63 | 63 |
64 // Localizes manifest value for a given key. | 64 // Localizes manifest value for a given key. |
65 static bool LocalizeManifestValue(const std::string& key, | 65 static bool LocalizeManifestValue(const std::string& key, |
66 const ExtensionMessageBundle& messages, | 66 const extensions::MessageBundle& messages, |
67 DictionaryValue* manifest, | 67 DictionaryValue* manifest, |
68 std::string* error) { | 68 std::string* error) { |
69 std::string result; | 69 std::string result; |
70 if (!manifest->GetString(key, &result)) | 70 if (!manifest->GetString(key, &result)) |
71 return true; | 71 return true; |
72 | 72 |
73 if (!messages.ReplaceMessages(&result, error)) | 73 if (!messages.ReplaceMessages(&result, error)) |
74 return false; | 74 return false; |
75 | 75 |
76 manifest->SetString(key, result); | 76 manifest->SetString(key, result); |
77 return true; | 77 return true; |
78 } | 78 } |
79 | 79 |
80 bool LocalizeManifest(const ExtensionMessageBundle& messages, | 80 bool LocalizeManifest(const extensions::MessageBundle& messages, |
81 DictionaryValue* manifest, | 81 DictionaryValue* manifest, |
82 std::string* error) { | 82 std::string* error) { |
83 // Initialize name. | 83 // Initialize name. |
84 std::string result; | 84 std::string result; |
85 if (!manifest->GetString(keys::kName, &result)) { | 85 if (!manifest->GetString(keys::kName, &result)) { |
86 *error = errors::kInvalidName; | 86 *error = errors::kInvalidName; |
87 return false; | 87 return false; |
88 } | 88 } |
89 if (!LocalizeManifestValue(keys::kName, messages, manifest, error)) { | 89 if (!LocalizeManifestValue(keys::kName, messages, manifest, error)) { |
90 return false; | 90 return false; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 return true; | 167 return true; |
168 } | 168 } |
169 | 169 |
170 bool LocalizeExtension(const FilePath& extension_path, | 170 bool LocalizeExtension(const FilePath& extension_path, |
171 DictionaryValue* manifest, | 171 DictionaryValue* manifest, |
172 std::string* error) { | 172 std::string* error) { |
173 DCHECK(manifest); | 173 DCHECK(manifest); |
174 | 174 |
175 std::string default_locale = GetDefaultLocaleFromManifest(*manifest, error); | 175 std::string default_locale = GetDefaultLocaleFromManifest(*manifest, error); |
176 | 176 |
177 scoped_ptr<ExtensionMessageBundle> message_bundle( | 177 scoped_ptr<extensions::MessageBundle> message_bundle( |
178 extension_file_util::LoadExtensionMessageBundle( | 178 extension_file_util::LoadMessageBundle( |
179 extension_path, default_locale, error)); | 179 extension_path, default_locale, error)); |
180 | 180 |
181 if (!message_bundle.get() && !error->empty()) | 181 if (!message_bundle.get() && !error->empty()) |
182 return false; | 182 return false; |
183 | 183 |
184 if (message_bundle.get() && | 184 if (message_bundle.get() && |
185 !LocalizeManifest(*message_bundle, manifest, error)) | 185 !LocalizeManifest(*message_bundle, manifest, error)) |
186 return false; | 186 return false; |
187 | 187 |
188 return true; | 188 return true; |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 if (!dictionary && error->empty()) { | 285 if (!dictionary && error->empty()) { |
286 // JSONFileValueSerializer just returns NULL if file cannot be found. It | 286 // JSONFileValueSerializer just returns NULL if file cannot be found. It |
287 // doesn't set the error, so we have to do it. | 287 // doesn't set the error, so we have to do it. |
288 *error = base::StringPrintf("Catalog file is missing for locale %s.", | 288 *error = base::StringPrintf("Catalog file is missing for locale %s.", |
289 extension_locale.c_str()); | 289 extension_locale.c_str()); |
290 } | 290 } |
291 | 291 |
292 return static_cast<DictionaryValue*>(dictionary); | 292 return static_cast<DictionaryValue*>(dictionary); |
293 } | 293 } |
294 | 294 |
295 ExtensionMessageBundle* LoadMessageCatalogs( | 295 extensions::MessageBundle* LoadMessageCatalogs( |
296 const FilePath& locale_path, | 296 const FilePath& locale_path, |
297 const std::string& default_locale, | 297 const std::string& default_locale, |
298 const std::string& application_locale, | 298 const std::string& application_locale, |
299 const std::set<std::string>& valid_locales, | 299 const std::set<std::string>& valid_locales, |
300 std::string* error) { | 300 std::string* error) { |
301 // Order locales to load as current_locale, first_parent, ..., default_locale. | 301 // Order locales to load as current_locale, first_parent, ..., default_locale. |
302 std::vector<std::string> all_fallback_locales; | 302 std::vector<std::string> all_fallback_locales; |
303 if (!application_locale.empty() && application_locale != default_locale) | 303 if (!application_locale.empty() && application_locale != default_locale) |
304 l10n_util::GetParentLocales(application_locale, &all_fallback_locales); | 304 l10n_util::GetParentLocales(application_locale, &all_fallback_locales); |
305 all_fallback_locales.push_back(default_locale); | 305 all_fallback_locales.push_back(default_locale); |
306 | 306 |
307 std::vector<linked_ptr<DictionaryValue> > catalogs; | 307 std::vector<linked_ptr<DictionaryValue> > catalogs; |
308 for (size_t i = 0; i < all_fallback_locales.size(); ++i) { | 308 for (size_t i = 0; i < all_fallback_locales.size(); ++i) { |
309 // Skip all parent locales that are not supplied. | 309 // Skip all parent locales that are not supplied. |
310 if (valid_locales.find(all_fallback_locales[i]) == valid_locales.end()) | 310 if (valid_locales.find(all_fallback_locales[i]) == valid_locales.end()) |
311 continue; | 311 continue; |
312 linked_ptr<DictionaryValue> catalog( | 312 linked_ptr<DictionaryValue> catalog( |
313 LoadMessageFile(locale_path, all_fallback_locales[i], error)); | 313 LoadMessageFile(locale_path, all_fallback_locales[i], error)); |
314 if (!catalog.get()) { | 314 if (!catalog.get()) { |
315 // If locale is valid, but messages.json is corrupted or missing, return | 315 // If locale is valid, but messages.json is corrupted or missing, return |
316 // an error. | 316 // an error. |
317 return NULL; | 317 return NULL; |
318 } else { | 318 } else { |
319 catalogs.push_back(catalog); | 319 catalogs.push_back(catalog); |
320 } | 320 } |
321 } | 321 } |
322 | 322 |
323 return ExtensionMessageBundle::Create(catalogs, error); | 323 return extensions::MessageBundle::Create(catalogs, error); |
324 } | 324 } |
325 | 325 |
326 bool ShouldSkipValidation(const FilePath& locales_path, | 326 bool ShouldSkipValidation(const FilePath& locales_path, |
327 const FilePath& locale_path, | 327 const FilePath& locale_path, |
328 const std::set<std::string>& all_locales) { | 328 const std::set<std::string>& all_locales) { |
329 // Since we use this string as a key in a DictionaryValue, be paranoid about | 329 // Since we use this string as a key in a DictionaryValue, be paranoid about |
330 // skipping any strings with '.'. This happens sometimes, for example with | 330 // skipping any strings with '.'. This happens sometimes, for example with |
331 // '.svn' directories. | 331 // '.svn' directories. |
332 FilePath relative_path; | 332 FilePath relative_path; |
333 if (!locales_path.AppendRelativePath(locale_path, &relative_path)) { | 333 if (!locales_path.AppendRelativePath(locale_path, &relative_path)) { |
334 NOTREACHED(); | 334 NOTREACHED(); |
335 return true; | 335 return true; |
336 } | 336 } |
337 std::string subdir = relative_path.MaybeAsASCII(); | 337 std::string subdir = relative_path.MaybeAsASCII(); |
338 if (subdir.empty()) | 338 if (subdir.empty()) |
339 return true; // Non-ASCII. | 339 return true; // Non-ASCII. |
340 | 340 |
341 if (std::find(subdir.begin(), subdir.end(), '.') != subdir.end()) | 341 if (std::find(subdir.begin(), subdir.end(), '.') != subdir.end()) |
342 return true; | 342 return true; |
343 | 343 |
344 if (all_locales.find(subdir) == all_locales.end()) | 344 if (all_locales.find(subdir) == all_locales.end()) |
345 return true; | 345 return true; |
346 | 346 |
347 return false; | 347 return false; |
348 } | 348 } |
349 | 349 |
350 } // namespace extension_l10n_util | 350 } // namespace extension_l10n_util |
OLD | NEW |