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

Side by Side Diff: chrome/browser/ui/webui/chromeos/login/l10n_util.cc

Issue 620563002: ChromeOS NetworkScreenHandler should not call CheckAndResolveLocale on UI thread. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Moved enable_layouts calculation to NetworkScreenHandler.' Created 6 years, 2 months 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
OLDNEW
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 "chrome/browser/ui/webui/chromeos/login/l10n_util.h" 5 #include "chrome/browser/ui/webui/chromeos/login/l10n_util.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <iterator> 8 #include <iterator>
9 #include <map> 9 #include <map>
10 #include <set> 10 #include <set>
(...skipping 17 matching lines...) Expand all
28 #include "chrome/browser/chromeos/input_method/input_method_util.h" 28 #include "chrome/browser/chromeos/input_method/input_method_util.h"
29 #include "chrome/grit/generated_resources.h" 29 #include "chrome/grit/generated_resources.h"
30 #include "chromeos/ime/component_extension_ime_manager.h" 30 #include "chromeos/ime/component_extension_ime_manager.h"
31 #include "chromeos/ime/input_method_descriptor.h" 31 #include "chromeos/ime/input_method_descriptor.h"
32 #include "chromeos/ime/input_method_manager.h" 32 #include "chromeos/ime/input_method_manager.h"
33 #include "content/public/browser/browser_thread.h" 33 #include "content/public/browser/browser_thread.h"
34 #include "ui/base/l10n/l10n_util.h" 34 #include "ui/base/l10n/l10n_util.h"
35 35
36 namespace chromeos { 36 namespace chromeos {
37 37
38 const char kMostRelevantLanguagesDivider[] = "MOST_RELEVANT_LANGUAGES_DIVIDER";
39
38 namespace { 40 namespace {
39 41
40 const char kSequenceToken[] = "chromeos_login_l10n_util"; 42 const char kSequenceToken[] = "chromeos_login_l10n_util";
41 43
42 scoped_ptr<base::DictionaryValue> CreateInputMethodsEntry( 44 scoped_ptr<base::DictionaryValue> CreateInputMethodsEntry(
43 const input_method::InputMethodDescriptor& method, 45 const input_method::InputMethodDescriptor& method,
44 const std::string selected) { 46 const std::string selected) {
45 input_method::InputMethodUtil* util = 47 input_method::InputMethodUtil* util =
46 input_method::InputMethodManager::Get()->GetInputMethodUtil(); 48 input_method::InputMethodManager::Get()->GetInputMethodUtil();
47 const std::string& ime_id = method.id(); 49 const std::string& ime_id = method.id();
(...skipping 16 matching lines...) Expand all
64 // TODO(dpolukhin): crbug.com/407579 66 // TODO(dpolukhin): crbug.com/407579
65 void AddOptgroupOtherLayouts(base::ListValue* input_methods_list) { 67 void AddOptgroupOtherLayouts(base::ListValue* input_methods_list) {
66 scoped_ptr<base::DictionaryValue> optgroup(new base::DictionaryValue); 68 scoped_ptr<base::DictionaryValue> optgroup(new base::DictionaryValue);
67 optgroup->SetString( 69 optgroup->SetString(
68 "optionGroupName", 70 "optionGroupName",
69 l10n_util::GetStringUTF16(IDS_OOBE_OTHER_KEYBOARD_LAYOUTS)); 71 l10n_util::GetStringUTF16(IDS_OOBE_OTHER_KEYBOARD_LAYOUTS));
70 input_methods_list->Append(optgroup.release()); 72 input_methods_list->Append(optgroup.release());
71 } 73 }
72 #endif 74 #endif
73 75
76 base::DictionaryValue* CreateLanguageEntry(
77 const std::string& language_code,
78 const base::string16& language_display_name,
79 const base::string16& language_native_display_name) {
80 base::string16 display_name = language_display_name;
81 const bool markup_removal =
82 base::i18n::UnadjustStringForLocaleDirection(&display_name);
83 DCHECK(markup_removal);
84
85 const bool has_rtl_chars =
86 base::i18n::StringContainsStrongRTLChars(display_name);
87 const std::string directionality = has_rtl_chars ? "rtl" : "ltr";
88
89 scoped_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue());
90 dictionary->SetString("code", language_code);
91 dictionary->SetString("displayName", language_display_name);
92 dictionary->SetString("textDirection", directionality);
93 dictionary->SetString("nativeDisplayName", language_native_display_name);
94 return dictionary.release();
95 }
96
74 // Gets the list of languages with |descriptors| based on |base_language_codes|. 97 // Gets the list of languages with |descriptors| based on |base_language_codes|.
75 // The |most_relevant_language_codes| will be first in the list. If 98 // The |most_relevant_language_codes| will be first in the list. If
76 // |insert_divider| is true, an entry with its "code" attribute set to 99 // |insert_divider| is true, an entry with its "code" attribute set to
77 // kMostRelevantLanguagesDivider is placed between the most relevant languages 100 // kMostRelevantLanguagesDivider is placed between the most relevant languages
78 // and all others. 101 // and all others.
79 scoped_ptr<base::ListValue> GetLanguageList( 102 scoped_ptr<base::ListValue> GetLanguageList(
80 const input_method::InputMethodDescriptors& descriptors, 103 const input_method::InputMethodDescriptors& descriptors,
81 const std::vector<std::string>& base_language_codes, 104 const std::vector<std::string>& base_language_codes,
82 const std::vector<std::string>& most_relevant_language_codes, 105 const std::vector<std::string>& most_relevant_language_codes,
83 bool insert_divider) { 106 bool insert_divider) {
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 for (size_t i = 0; i < out_display_names.size(); ++i) { 271 for (size_t i = 0; i < out_display_names.size(); ++i) {
249 // Sets the directionality of the display language name. 272 // Sets the directionality of the display language name.
250 base::string16 display_name(out_display_names[i]); 273 base::string16 display_name(out_display_names[i]);
251 if (insert_divider && display_name == divider16) { 274 if (insert_divider && display_name == divider16) {
252 // Insert divider. 275 // Insert divider.
253 base::DictionaryValue* dictionary = new base::DictionaryValue(); 276 base::DictionaryValue* dictionary = new base::DictionaryValue();
254 dictionary->SetString("code", kMostRelevantLanguagesDivider); 277 dictionary->SetString("code", kMostRelevantLanguagesDivider);
255 language_list->Append(dictionary); 278 language_list->Append(dictionary);
256 continue; 279 continue;
257 } 280 }
258 const bool markup_removal =
259 base::i18n::UnadjustStringForLocaleDirection(&display_name);
260 DCHECK(markup_removal);
261 const bool has_rtl_chars =
262 base::i18n::StringContainsStrongRTLChars(display_name);
263 const std::string directionality = has_rtl_chars ? "rtl" : "ltr";
264 281
265 const LanguagePair& pair = language_map[out_display_names[i]]; 282 const LanguagePair& pair = language_map[out_display_names[i]];
266 base::DictionaryValue* dictionary = new base::DictionaryValue(); 283 language_list->Append(
267 dictionary->SetString("code", pair.first); 284 CreateLanguageEntry(pair.first, out_display_names[i], pair.second));
268 dictionary->SetString("displayName", out_display_names[i]);
269 dictionary->SetString("textDirection", directionality);
270 dictionary->SetString("nativeDisplayName", pair.second);
271 language_list->Append(dictionary);
272 } 285 }
273 286
274 return language_list.Pass(); 287 return language_list.Pass();
275 } 288 }
276 289
277 // Invokes |callback| with a list of keyboard layouts that can be used for 290 // Invokes |callback| with a list of keyboard layouts that can be used for
278 // |resolved_locale|. 291 // |resolved_locale|.
279 void GetKeyboardLayoutsForResolvedLocale( 292 void GetKeyboardLayoutsForResolvedLocale(
280 const GetKeyboardLayoutsForLocaleCallback& callback, 293 const GetKeyboardLayoutsForLocaleCallback& callback,
281 const std::string& resolved_locale) { 294 const std::string& resolved_locale) {
(...skipping 22 matching lines...) Expand all
304 util->GetInputMethodDescriptorFromId(*it); 317 util->GetInputMethodDescriptorFromId(*it);
305 if (!InsertString(ime->id(), &input_methods_added)) 318 if (!InsertString(ime->id(), &input_methods_added))
306 continue; 319 continue;
307 input_methods_list->Append( 320 input_methods_list->Append(
308 CreateInputMethodsEntry(*ime, selected).release()); 321 CreateInputMethodsEntry(*ime, selected).release());
309 } 322 }
310 323
311 callback.Run(input_methods_list.Pass()); 324 callback.Run(input_methods_list.Pass());
312 } 325 }
313 326
314 } // namespace 327 // For "UI Language" drop-down menu at OOBE screen we need to decide which
328 // entry to mark "selected". If user has just selected "requested_locale",
329 // but "loaded_locale" was actually loaded, we mark original user choice
330 // "selected" only if loaded_locale is a backup for "requested_locale".
331 std::string CalculateSelectedLanguage(const std::string& requested_locale,
332 const std::string& loaded_locale) {
333 std::string resolved_locale;
334 if (!l10n_util::CheckAndResolveLocale(requested_locale, &resolved_locale))
335 return loaded_locale;
315 336
316 const char kMostRelevantLanguagesDivider[] = "MOST_RELEVANT_LANGUAGES_DIVIDER"; 337 if (resolved_locale == loaded_locale)
338 return requested_locale;
317 339
318 scoped_ptr<base::ListValue> GetUILanguageList( 340 return loaded_locale;
319 const std::vector<std::string>* most_relevant_language_codes, 341 }
320 const std::string& selected) {
321 ComponentExtensionIMEManager* manager =
322 input_method::InputMethodManager::Get()->
323 GetComponentExtensionIMEManager();
324 input_method::InputMethodDescriptors descriptors =
325 manager->GetXkbIMEAsInputMethodDescriptor();
326 scoped_ptr<base::ListValue> languages_list(GetLanguageList(
327 descriptors,
328 l10n_util::GetAvailableLocales(),
329 most_relevant_language_codes
330 ? *most_relevant_language_codes
331 : StartupCustomizationDocument::GetInstance()->configured_locales(),
332 true));
333 342
343 void ResolveLanguageListOnBlockingPool(
344 const chromeos::locale_util::LanguageSwitchResult* language_switch_result,
345 scoped_ptr<base::ListValue>* list,
346 std::string* list_locale,
347 std::string* selected_language) {
348 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
349
350 if (!language_switch_result) {
351 selected_language->clear();
352 } else {
353 if (language_switch_result->success) {
354 if (language_switch_result->requested_locale ==
355 language_switch_result->loaded_locale) {
356 *selected_language = language_switch_result->requested_locale;
357 } else {
358 *selected_language =
359 CalculateSelectedLanguage(language_switch_result->requested_locale,
360 language_switch_result->loaded_locale);
361 }
362 } else {
363 *selected_language = language_switch_result->loaded_locale;
364 }
365 }
366 const std::string selected_code =
367 selected_language->empty() ? g_browser_process->GetApplicationLocale()
368 : *selected_language;
369
370 *list_locale = language_switch_result
371 ? language_switch_result->loaded_locale
372 : g_browser_process->GetApplicationLocale();
373 list->reset(chromeos::GetUILanguageList(NULL, selected_code).release());
374 }
375
376 void OnLanguageListResolved(
377 UILanguageListResolvedCallback callback,
378 scoped_ptr<scoped_ptr<base::ListValue>> new_language_list,
379 scoped_ptr<std::string> new_language_list_locale,
380 scoped_ptr<std::string> new_selected_language) {
381 callback.Run(new_language_list->Pass(),
382 *new_language_list_locale,
383 *new_selected_language);
384 }
385
386 void AdjustUILanguageList(const std::string& selected,
387 base::ListValue* languages_list) {
334 for (size_t i = 0; i < languages_list->GetSize(); ++i) { 388 for (size_t i = 0; i < languages_list->GetSize(); ++i) {
335 base::DictionaryValue* language_info = NULL; 389 base::DictionaryValue* language_info = NULL;
336 if (!languages_list->GetDictionary(i, &language_info)) 390 if (!languages_list->GetDictionary(i, &language_info))
337 NOTREACHED(); 391 NOTREACHED();
338 392
339 std::string value; 393 std::string value;
340 language_info->GetString("code", &value); 394 language_info->GetString("code", &value);
341 std::string display_name; 395 std::string display_name;
342 language_info->GetString("displayName", &display_name); 396 language_info->GetString("displayName", &display_name);
343 std::string native_name; 397 std::string native_name;
344 language_info->GetString("nativeDisplayName", &native_name); 398 language_info->GetString("nativeDisplayName", &native_name);
345 399
346 // If it's an option group divider, add field name. 400 // If it's an option group divider, add field name.
347 if (value == kMostRelevantLanguagesDivider) { 401 if (value == kMostRelevantLanguagesDivider) {
348 language_info->SetString( 402 language_info->SetString(
349 "optionGroupName", 403 "optionGroupName",
350 l10n_util::GetStringUTF16(IDS_OOBE_OTHER_LANGUAGES)); 404 l10n_util::GetStringUTF16(IDS_OOBE_OTHER_LANGUAGES));
351 } 405 }
352 if (display_name != native_name) { 406 if (display_name != native_name) {
353 display_name = base::StringPrintf("%s - %s", 407 display_name = base::StringPrintf("%s - %s",
354 display_name.c_str(), 408 display_name.c_str(),
355 native_name.c_str()); 409 native_name.c_str());
356 } 410 }
357 411
358 language_info->SetString("value", value); 412 language_info->SetString("value", value);
359 language_info->SetString("title", display_name); 413 language_info->SetString("title", display_name);
360 if (value == selected) 414 if (value == selected)
361 language_info->SetBoolean("selected", true); 415 language_info->SetBoolean("selected", true);
362 } 416 }
417 }
418
419 } // namespace
420
421 void ResolveUILanguageList(
422 scoped_ptr<chromeos::locale_util::LanguageSwitchResult>
423 language_switch_result,
424 UILanguageListResolvedCallback callback) {
425 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
426
427 scoped_ptr<scoped_ptr<base::ListValue>> new_language_list(
428 new scoped_ptr<base::ListValue>());
429 scoped_ptr<std::string> new_language_list_locale(new std::string);
430 scoped_ptr<std::string> new_selected_language(new std::string);
431
432 content::BrowserThread::GetBlockingPool()->PostTaskAndReply(
433 FROM_HERE,
434 base::Bind(&ResolveLanguageListOnBlockingPool,
435 base::Owned(language_switch_result.release()),
436 base::Unretained(new_language_list.get()),
437 base::Unretained(new_language_list_locale.get()),
438 base::Unretained(new_selected_language.get())),
439 base::Bind(&OnLanguageListResolved,
440 callback,
441 base::Passed(new_language_list.Pass()),
442 base::Passed(new_language_list_locale.Pass()),
443 base::Passed(new_selected_language.Pass())));
444 }
445
446 scoped_ptr<base::ListValue> GetMinimalUILanguageList() {
447 const std::string application_locale =
448 g_browser_process->GetApplicationLocale();
449 base::string16 language_native_display_name =
450 l10n_util::GetDisplayNameForLocale(
451 application_locale, application_locale, true);
452
453 scoped_ptr<base::ListValue> language_list(new base::ListValue());
454 language_list->Append(CreateLanguageEntry(application_locale,
455 language_native_display_name,
456 language_native_display_name));
457 AdjustUILanguageList(std::string(), language_list.get());
458 return language_list.Pass();
459 }
460
461 scoped_ptr<base::ListValue> GetUILanguageList(
462 const std::vector<std::string>* most_relevant_language_codes,
463 const std::string& selected) {
464 ComponentExtensionIMEManager* manager =
465 input_method::InputMethodManager::Get()
466 ->GetComponentExtensionIMEManager();
467 input_method::InputMethodDescriptors descriptors =
468 manager->GetXkbIMEAsInputMethodDescriptor();
469 scoped_ptr<base::ListValue> languages_list(GetLanguageList(
470 descriptors,
471 l10n_util::GetAvailableLocales(),
472 most_relevant_language_codes
473 ? *most_relevant_language_codes
474 : StartupCustomizationDocument::GetInstance()->configured_locales(),
475 true));
476 AdjustUILanguageList(selected, languages_list.get());
363 return languages_list.Pass(); 477 return languages_list.Pass();
364 } 478 }
365 479
366 std::string FindMostRelevantLocale( 480 std::string FindMostRelevantLocale(
367 const std::vector<std::string>& most_relevant_language_codes, 481 const std::vector<std::string>& most_relevant_language_codes,
368 const base::ListValue& available_locales, 482 const base::ListValue& available_locales,
369 const std::string& fallback_locale) { 483 const std::string& fallback_locale) {
370 for (std::vector<std::string>::const_iterator most_relevant_it = 484 for (std::vector<std::string>::const_iterator most_relevant_it =
371 most_relevant_language_codes.begin(); 485 most_relevant_language_codes.begin();
372 most_relevant_it != most_relevant_language_codes.end(); 486 most_relevant_it != most_relevant_language_codes.end();
(...skipping 23 matching lines...) Expand all
396 l10n_util::GetAcceptLanguagesForLocale(app_locale, &accept_language_codes); 510 l10n_util::GetAcceptLanguagesForLocale(app_locale, &accept_language_codes);
397 return GetLanguageList( 511 return GetLanguageList(
398 *input_method::InputMethodManager::Get()->GetSupportedInputMethods(), 512 *input_method::InputMethodManager::Get()->GetSupportedInputMethods(),
399 accept_language_codes, 513 accept_language_codes,
400 StartupCustomizationDocument::GetInstance()->configured_locales(), 514 StartupCustomizationDocument::GetInstance()->configured_locales(),
401 false); 515 false);
402 } 516 }
403 517
404 scoped_ptr<base::ListValue> GetAndActivateLoginKeyboardLayouts( 518 scoped_ptr<base::ListValue> GetAndActivateLoginKeyboardLayouts(
405 const std::string& locale, 519 const std::string& locale,
406 const std::string& selected) { 520 const std::string& selected,
521 bool activate_keyboards) {
407 scoped_ptr<base::ListValue> input_methods_list(new base::ListValue); 522 scoped_ptr<base::ListValue> input_methods_list(new base::ListValue);
408 #if !defined(USE_ATHENA) 523 #if !defined(USE_ATHENA)
409 // TODO(dpolukhin): crbug.com/407579 524 // TODO(dpolukhin): crbug.com/407579
410 input_method::InputMethodManager* manager = 525 input_method::InputMethodManager* manager =
411 input_method::InputMethodManager::Get(); 526 input_method::InputMethodManager::Get();
412 input_method::InputMethodUtil* util = manager->GetInputMethodUtil(); 527 input_method::InputMethodUtil* util = manager->GetInputMethodUtil();
413 528
414 const std::vector<std::string>& hardware_login_input_methods = 529 const std::vector<std::string>& hardware_login_input_methods =
415 util->GetHardwareLoginInputMethodIds(); 530 util->GetHardwareLoginInputMethodIds();
416 531
417 manager->GetActiveIMEState()->EnableLoginLayouts( 532 if (activate_keyboards) {
418 locale, hardware_login_input_methods); 533 manager->GetActiveIMEState()->EnableLoginLayouts(
Nikita (slow) 2014/10/06 18:46:39 nit: Add DCHECK(ProfileHelper::IsSigninProfile(Pr
Alexander Alekseev 2014/10/06 18:57:26 It will crash in DeviceLocalAccountTest.TermsOfSer
Nikita (slow) 2014/10/06 19:11:37 Interesting. I guess that does mean that test need
534 locale, hardware_login_input_methods);
535 }
419 536
420 scoped_ptr<input_method::InputMethodDescriptors> input_methods( 537 scoped_ptr<input_method::InputMethodDescriptors> input_methods(
421 manager->GetActiveIMEState()->GetActiveInputMethods()); 538 manager->GetActiveIMEState()->GetActiveInputMethods());
422 std::set<std::string> input_methods_added; 539 std::set<std::string> input_methods_added;
423 540
424 for (std::vector<std::string>::const_iterator i = 541 for (std::vector<std::string>::const_iterator i =
425 hardware_login_input_methods.begin(); 542 hardware_login_input_methods.begin();
426 i != hardware_login_input_methods.end(); 543 i != hardware_login_input_methods.end();
427 ++i) { 544 ++i) {
428 const input_method::InputMethodDescriptor* ime = 545 const input_method::InputMethodDescriptor* ime =
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
493 scoped_ptr<base::DictionaryValue> GetCurrentKeyboardLayout() { 610 scoped_ptr<base::DictionaryValue> GetCurrentKeyboardLayout() {
494 const input_method::InputMethodDescriptor current_input_method = 611 const input_method::InputMethodDescriptor current_input_method =
495 input_method::InputMethodManager::Get() 612 input_method::InputMethodManager::Get()
496 ->GetActiveIMEState() 613 ->GetActiveIMEState()
497 ->GetCurrentInputMethod(); 614 ->GetCurrentInputMethod();
498 return CreateInputMethodsEntry(current_input_method, 615 return CreateInputMethodsEntry(current_input_method,
499 current_input_method.id()); 616 current_input_method.id());
500 } 617 }
501 618
502 } // namespace chromeos 619 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/chromeos/login/l10n_util.h ('k') | chrome/browser/ui/webui/chromeos/login/network_screen_handler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698