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

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

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