OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "extensions/common/extension_l10n_util.h" | 5 #include "extensions/common/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> |
(...skipping 21 matching lines...) Expand all Loading... |
32 | 32 |
33 // Loads contents of the messages file for given locale. If file is not found, | 33 // Loads contents of the messages file for given locale. If file is not found, |
34 // or there was parsing error we return NULL and set |error|. | 34 // or there was parsing error we return NULL and set |error|. |
35 // Caller owns the returned object. | 35 // Caller owns the returned object. |
36 base::DictionaryValue* LoadMessageFile(const base::FilePath& locale_path, | 36 base::DictionaryValue* LoadMessageFile(const base::FilePath& locale_path, |
37 const std::string& locale, | 37 const std::string& locale, |
38 std::string* error) { | 38 std::string* error) { |
39 base::FilePath file = | 39 base::FilePath file = |
40 locale_path.AppendASCII(locale).Append(extensions::kMessagesFilename); | 40 locale_path.AppendASCII(locale).Append(extensions::kMessagesFilename); |
41 JSONFileValueSerializer messages_serializer(file); | 41 JSONFileValueSerializer messages_serializer(file); |
42 base::Value* dictionary = messages_serializer.Deserialize(NULL, error); | 42 base::Value* dictionary = messages_serializer.Deserialize(nullptr, error); |
43 if (!dictionary) { | 43 if (!dictionary) { |
44 if (error->empty()) { | 44 if (error->empty()) { |
45 // JSONFileValueSerializer just returns NULL if file cannot be found. It | 45 // JSONFileValueSerializer just returns NULL if file cannot be found. It |
46 // doesn't set the error, so we have to do it. | 46 // doesn't set the error, so we have to do it. |
47 *error = base::StringPrintf("Catalog file is missing for locale %s.", | 47 *error = base::StringPrintf("Catalog file is missing for locale %s.", |
48 locale.c_str()); | 48 locale.c_str()); |
49 } else { | 49 } else { |
50 *error = extensions::ErrorUtils::FormatErrorMessage( | 50 *error = extensions::ErrorUtils::FormatErrorMessage( |
51 errors::kLocalesInvalidLocale, | 51 errors::kLocalesInvalidLocale, |
52 base::UTF16ToUTF8(file.LossyDisplayName()), | 52 base::UTF16ToUTF8(file.LossyDisplayName()), |
(...skipping 18 matching lines...) Expand all Loading... |
71 | 71 |
72 manifest->SetString(key, result); | 72 manifest->SetString(key, result); |
73 return true; | 73 return true; |
74 } | 74 } |
75 | 75 |
76 // Localizes manifest value of list type for a given key. | 76 // Localizes manifest value of list type for a given key. |
77 bool LocalizeManifestListValue(const std::string& key, | 77 bool LocalizeManifestListValue(const std::string& key, |
78 const extensions::MessageBundle& messages, | 78 const extensions::MessageBundle& messages, |
79 base::DictionaryValue* manifest, | 79 base::DictionaryValue* manifest, |
80 std::string* error) { | 80 std::string* error) { |
81 base::ListValue* list = NULL; | 81 base::ListValue* list = nullptr; |
82 if (!manifest->GetList(key, &list)) | 82 if (!manifest->GetList(key, &list)) |
83 return true; | 83 return true; |
84 | 84 |
85 bool ret = true; | 85 bool ret = true; |
86 for (size_t i = 0; i < list->GetSize(); ++i) { | 86 for (size_t i = 0; i < list->GetSize(); ++i) { |
87 std::string result; | 87 std::string result; |
88 if (list->GetString(i, &result)) { | 88 if (list->GetString(i, &result)) { |
89 if (messages.ReplaceMessages(&result, error)) | 89 if (messages.ReplaceMessages(&result, error)) |
90 list->Set(i, new base::StringValue(result)); | 90 list->Set(i, new base::StringValue(result)); |
91 else | 91 else |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 key.assign(keys::kPageAction); | 162 key.assign(keys::kPageAction); |
163 key.append("."); | 163 key.append("."); |
164 key.append(keys::kPageActionDefaultTitle); | 164 key.append(keys::kPageActionDefaultTitle); |
165 if (!LocalizeManifestValue(key, messages, manifest, error)) | 165 if (!LocalizeManifestValue(key, messages, manifest, error)) |
166 return false; | 166 return false; |
167 | 167 |
168 // Initialize omnibox.keyword. | 168 // Initialize omnibox.keyword. |
169 if (!LocalizeManifestValue(keys::kOmniboxKeyword, messages, manifest, error)) | 169 if (!LocalizeManifestValue(keys::kOmniboxKeyword, messages, manifest, error)) |
170 return false; | 170 return false; |
171 | 171 |
172 base::ListValue* file_handlers = NULL; | 172 base::ListValue* file_handlers = nullptr; |
173 if (manifest->GetList(keys::kFileBrowserHandlers, &file_handlers)) { | 173 if (manifest->GetList(keys::kFileBrowserHandlers, &file_handlers)) { |
174 key.assign(keys::kFileBrowserHandlers); | 174 key.assign(keys::kFileBrowserHandlers); |
175 for (size_t i = 0; i < file_handlers->GetSize(); i++) { | 175 for (size_t i = 0; i < file_handlers->GetSize(); i++) { |
176 base::DictionaryValue* handler = NULL; | 176 base::DictionaryValue* handler = nullptr; |
177 if (!file_handlers->GetDictionary(i, &handler)) { | 177 if (!file_handlers->GetDictionary(i, &handler)) { |
178 *error = errors::kInvalidFileBrowserHandler; | 178 *error = errors::kInvalidFileBrowserHandler; |
179 return false; | 179 return false; |
180 } | 180 } |
181 if (!LocalizeManifestValue( | 181 if (!LocalizeManifestValue( |
182 keys::kPageActionDefaultTitle, messages, handler, error)) | 182 keys::kPageActionDefaultTitle, messages, handler, error)) |
183 return false; | 183 return false; |
184 } | 184 } |
185 } | 185 } |
186 | 186 |
187 // Initialize all input_components | 187 // Initialize all input_components |
188 base::ListValue* input_components = NULL; | 188 base::ListValue* input_components = nullptr; |
189 if (manifest->GetList(keys::kInputComponents, &input_components)) { | 189 if (manifest->GetList(keys::kInputComponents, &input_components)) { |
190 for (size_t i = 0; i < input_components->GetSize(); ++i) { | 190 for (size_t i = 0; i < input_components->GetSize(); ++i) { |
191 base::DictionaryValue* module = NULL; | 191 base::DictionaryValue* module = nullptr; |
192 if (!input_components->GetDictionary(i, &module)) { | 192 if (!input_components->GetDictionary(i, &module)) { |
193 *error = errors::kInvalidInputComponents; | 193 *error = errors::kInvalidInputComponents; |
194 return false; | 194 return false; |
195 } | 195 } |
196 if (!LocalizeManifestValue(keys::kName, messages, module, error)) | 196 if (!LocalizeManifestValue(keys::kName, messages, module, error)) |
197 return false; | 197 return false; |
198 if (!LocalizeManifestValue(keys::kDescription, messages, module, error)) | 198 if (!LocalizeManifestValue(keys::kDescription, messages, module, error)) |
199 return false; | 199 return false; |
200 } | 200 } |
201 } | 201 } |
202 | 202 |
203 // Initialize app.launch.local_path. | 203 // Initialize app.launch.local_path. |
204 if (!LocalizeManifestValue(keys::kLaunchLocalPath, messages, manifest, error)) | 204 if (!LocalizeManifestValue(keys::kLaunchLocalPath, messages, manifest, error)) |
205 return false; | 205 return false; |
206 | 206 |
207 // Initialize app.launch.web_url. | 207 // Initialize app.launch.web_url. |
208 if (!LocalizeManifestValue(keys::kLaunchWebURL, messages, manifest, error)) | 208 if (!LocalizeManifestValue(keys::kLaunchWebURL, messages, manifest, error)) |
209 return false; | 209 return false; |
210 | 210 |
211 // Initialize description of commmands. | 211 // Initialize description of commmands. |
212 base::DictionaryValue* commands_handler = NULL; | 212 base::DictionaryValue* commands_handler = nullptr; |
213 if (manifest->GetDictionary(keys::kCommands, &commands_handler)) { | 213 if (manifest->GetDictionary(keys::kCommands, &commands_handler)) { |
214 for (base::DictionaryValue::Iterator iter(*commands_handler); | 214 for (base::DictionaryValue::Iterator iter(*commands_handler); |
215 !iter.IsAtEnd(); | 215 !iter.IsAtEnd(); |
216 iter.Advance()) { | 216 iter.Advance()) { |
217 key.assign( | 217 key.assign( |
218 base::StringPrintf("commands.%s.description", iter.key().c_str())); | 218 base::StringPrintf("commands.%s.description", iter.key().c_str())); |
219 if (!LocalizeManifestValue(key, messages, manifest, error)) | 219 if (!LocalizeManifestValue(key, messages, manifest, error)) |
220 return false; | 220 return false; |
221 } | 221 } |
222 } | 222 } |
223 | 223 |
224 // Initialize search_provider fields. | 224 // Initialize search_provider fields. |
225 base::DictionaryValue* search_provider = NULL; | 225 base::DictionaryValue* search_provider = nullptr; |
226 if (manifest->GetDictionary(keys::kOverrideSearchProvider, | 226 if (manifest->GetDictionary(keys::kOverrideSearchProvider, |
227 &search_provider)) { | 227 &search_provider)) { |
228 for (base::DictionaryValue::Iterator iter(*search_provider); | 228 for (base::DictionaryValue::Iterator iter(*search_provider); |
229 !iter.IsAtEnd(); | 229 !iter.IsAtEnd(); |
230 iter.Advance()) { | 230 iter.Advance()) { |
231 key.assign(base::StringPrintf( | 231 key.assign(base::StringPrintf( |
232 "%s.%s", keys::kOverrideSearchProvider, iter.key().c_str())); | 232 "%s.%s", keys::kOverrideSearchProvider, iter.key().c_str())); |
233 bool success = | 233 bool success = |
234 (key == keys::kSettingsOverrideAlternateUrls) | 234 (key == keys::kSettingsOverrideAlternateUrls) |
235 ? LocalizeManifestListValue(key, messages, manifest, error) | 235 ? LocalizeManifestListValue(key, messages, manifest, error) |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 std::vector<linked_ptr<base::DictionaryValue> > catalogs; | 376 std::vector<linked_ptr<base::DictionaryValue> > catalogs; |
377 for (size_t i = 0; i < all_fallback_locales.size(); ++i) { | 377 for (size_t i = 0; i < all_fallback_locales.size(); ++i) { |
378 // Skip all parent locales that are not supplied. | 378 // Skip all parent locales that are not supplied. |
379 if (valid_locales.find(all_fallback_locales[i]) == valid_locales.end()) | 379 if (valid_locales.find(all_fallback_locales[i]) == valid_locales.end()) |
380 continue; | 380 continue; |
381 linked_ptr<base::DictionaryValue> catalog( | 381 linked_ptr<base::DictionaryValue> catalog( |
382 LoadMessageFile(locale_path, all_fallback_locales[i], error)); | 382 LoadMessageFile(locale_path, all_fallback_locales[i], error)); |
383 if (!catalog.get()) { | 383 if (!catalog.get()) { |
384 // If locale is valid, but messages.json is corrupted or missing, return | 384 // If locale is valid, but messages.json is corrupted or missing, return |
385 // an error. | 385 // an error. |
386 return NULL; | 386 return nullptr; |
387 } else { | 387 } else { |
388 catalogs.push_back(catalog); | 388 catalogs.push_back(catalog); |
389 } | 389 } |
390 } | 390 } |
391 | 391 |
392 return extensions::MessageBundle::Create(catalogs, error); | 392 return extensions::MessageBundle::Create(catalogs, error); |
393 } | 393 } |
394 | 394 |
395 bool ValidateExtensionLocales(const base::FilePath& extension_path, | 395 bool ValidateExtensionLocales(const base::FilePath& extension_path, |
396 const base::DictionaryValue* manifest, | 396 const base::DictionaryValue* manifest, |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 ScopedLocaleForTest::ScopedLocaleForTest(const std::string& locale) | 453 ScopedLocaleForTest::ScopedLocaleForTest(const std::string& locale) |
454 : locale_(extension_l10n_util::CurrentLocaleOrDefault()) { | 454 : locale_(extension_l10n_util::CurrentLocaleOrDefault()) { |
455 extension_l10n_util::SetProcessLocale(locale); | 455 extension_l10n_util::SetProcessLocale(locale); |
456 } | 456 } |
457 | 457 |
458 ScopedLocaleForTest::~ScopedLocaleForTest() { | 458 ScopedLocaleForTest::~ScopedLocaleForTest() { |
459 extension_l10n_util::SetProcessLocale(locale_); | 459 extension_l10n_util::SetProcessLocale(locale_); |
460 } | 460 } |
461 | 461 |
462 } // namespace extension_l10n_util | 462 } // namespace extension_l10n_util |
OLD | NEW |