Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(380)

Side by Side Diff: chrome/common/extensions/extension_l10n_util.cc

Issue 5643002: Add utility function to determine if a locale is valid syntax; this will... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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 <set> 7 #include <set>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 } 233 }
234 } 234 }
235 235
236 if (valid_locales->empty()) { 236 if (valid_locales->empty()) {
237 *error = extension_manifest_errors::kLocalesNoValidLocaleNamesListed; 237 *error = extension_manifest_errors::kLocalesNoValidLocaleNamesListed;
238 return false; 238 return false;
239 } 239 }
240 240
241 return true; 241 return true;
242 } 242 }
243
244 bool IsValidLocaleSyntax(const std::string& locale) {
245 // This implements a simple approximation of RFC 5646; it will accept all
jungshik at Google 2010/12/10 18:32:17 nit: BCP 47 (Best Current Practice 47 : http://www
246 // valid strings and reject (but not all) invalid ones.
247
248 // Check that the length is plausible.
249 if (locale.size() < 2 || locale.size() >= ULOC_FULLNAME_CAPACITY)
250 return false;
251
252 // Check that all characters are alphanumeric, hyphen, or underscore.
Nebojša Ćirić 2010/12/10 17:19:13 What about @ and = signs in de-DE@collation=phoneb
253 for (size_t i = 0; i < locale.size(); i++) {
254 char ch = locale[i];
255 if (!isalnum(ch) && ch != '-' && ch != '_')
jungshik at Google 2010/12/10 18:32:17 isalnum is locale-dependent (C-library locale) and
Nebojša Ćirić 2010/12/10 18:38:50 I think IsAsciiAlpha && IsAsciiDigit would work he
256 return false;
257 }
258
259 // Check that the initial token before the first hyphen or underscore is
260 // 1, 2, or 3 characters, and all others are 1 - 8 characters.
261 // (Tokenize/StringTokenizer don't work here, they collapse multiple
262 // delimiters into one.)
263 int token_len = 0;
264 int token_index = 0;
265 for (size_t i = 0; i < locale.size(); i++) {
266 char ch = locale[i];
267 if (ch == '-' || ch == '_') {
268 if (token_index == 0 && (token_len < 1 || token_len > 3)) {
jungshik at Google 2010/12/10 18:32:17 can you tighten up a bit here? although BCP 47 res
269 return false;
270 } else if (token_len < 1 || token_len > 8) {
271 return false;
272 }
273 token_index++;
274 token_len = 0;
275 } else {
276 token_len++;
277 }
278 }
279 if (token_index == 0 && (token_len < 1 || token_len > 3)) {
280 return false;
281 } else if (token_len < 1 || token_len > 8) {
282 return false;
283 }
284
285 return true;
286 }
Nebojša Ćirić 2010/12/10 17:19:13 I think app/l10n_util would be a better place for
jungshik at Google 2010/12/10 18:32:17 I agree
287
243 // Loads contents of the messages file for given locale. If file is not found, 288 // Loads contents of the messages file for given locale. If file is not found,
244 // or there was parsing error we return NULL and set |error|. 289 // or there was parsing error we return NULL and set |error|.
245 // Caller owns the returned object. 290 // Caller owns the returned object.
246 static DictionaryValue* LoadMessageFile(const FilePath& locale_path, 291 static DictionaryValue* LoadMessageFile(const FilePath& locale_path,
247 const std::string& locale, 292 const std::string& locale,
248 std::string* error) { 293 std::string* error) {
249 std::string extension_locale = locale; 294 std::string extension_locale = locale;
250 FilePath file = locale_path.AppendASCII(extension_locale) 295 FilePath file = locale_path.AppendASCII(extension_locale)
251 .Append(Extension::kMessagesFilename); 296 .Append(Extension::kMessagesFilename);
252 JSONFileValueSerializer messages_serializer(file); 297 JSONFileValueSerializer messages_serializer(file);
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 if (std::find(subdir.begin(), subdir.end(), L'.') != subdir.end()) 350 if (std::find(subdir.begin(), subdir.end(), L'.') != subdir.end())
306 return true; 351 return true;
307 352
308 if (all_locales.find(WideToASCII(subdir)) == all_locales.end()) 353 if (all_locales.find(WideToASCII(subdir)) == all_locales.end())
309 return true; 354 return true;
310 355
311 return false; 356 return false;
312 } 357 }
313 358
314 } // namespace extension_l10n_util 359 } // namespace extension_l10n_util
OLDNEW
« no previous file with comments | « chrome/common/extensions/extension_l10n_util.h ('k') | chrome/common/extensions/extension_l10n_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698