Chromium Code Reviews| 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 "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/i18n/rtl.h" | 15 #include "base/i18n/rtl.h" |
| 16 #include "base/location.h" | |
| 15 #include "base/logging.h" | 17 #include "base/logging.h" |
| 16 #include "base/strings/string16.h" | 18 #include "base/strings/string16.h" |
| 17 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
| 19 #include "base/values.h" | 21 #include "base/values.h" |
| 20 #include "chrome/browser/browser_process.h" | 22 #include "chrome/browser/browser_process.h" |
| 21 #include "chrome/browser/chromeos/customization_document.h" | 23 #include "chrome/browser/chromeos/customization_document.h" |
| 22 #include "chrome/browser/chromeos/input_method/input_method_util.h" | 24 #include "chrome/browser/chromeos/input_method/input_method_util.h" |
| 23 #include "chromeos/ime/component_extension_ime_manager.h" | 25 #include "chromeos/ime/component_extension_ime_manager.h" |
| 24 #include "chromeos/ime/input_method_descriptor.h" | 26 #include "chromeos/ime/input_method_descriptor.h" |
| 25 #include "chromeos/ime/input_method_manager.h" | 27 #include "chromeos/ime/input_method_manager.h" |
| 28 #include "content/public/browser/browser_thread.h" | |
| 26 #include "grit/generated_resources.h" | 29 #include "grit/generated_resources.h" |
| 27 #include "ui/base/l10n/l10n_util.h" | 30 #include "ui/base/l10n/l10n_util.h" |
| 28 | 31 |
| 29 namespace chromeos { | 32 namespace chromeos { |
| 30 | 33 |
| 31 namespace { | 34 namespace { |
| 32 | 35 |
| 33 base::DictionaryValue* CreateInputMethodsEntry( | 36 scoped_ptr<base::DictionaryValue> CreateInputMethodsEntry( |
| 34 const input_method::InputMethodDescriptor& method, | 37 const input_method::InputMethodDescriptor& method, |
| 35 const std::string selected) { | 38 const std::string selected) { |
| 36 input_method::InputMethodUtil* util = | 39 input_method::InputMethodUtil* util = |
| 37 input_method::InputMethodManager::Get()->GetInputMethodUtil(); | 40 input_method::InputMethodManager::Get()->GetInputMethodUtil(); |
| 38 const std::string& ime_id = method.id(); | 41 const std::string& ime_id = method.id(); |
| 39 scoped_ptr<base::DictionaryValue> input_method(new base::DictionaryValue); | 42 scoped_ptr<base::DictionaryValue> input_method(new base::DictionaryValue); |
| 40 input_method->SetString("value", ime_id); | 43 input_method->SetString("value", ime_id); |
| 41 input_method->SetString("title", util->GetInputMethodLongName(method)); | 44 input_method->SetString("title", util->GetInputMethodLongName(method)); |
| 42 input_method->SetBoolean("selected", ime_id == selected); | 45 input_method->SetBoolean("selected", ime_id == selected); |
| 43 return input_method.release(); | 46 return input_method.Pass(); |
| 44 } | 47 } |
| 45 | 48 |
| 46 // Returns true if element was inserted. | 49 // Returns true if element was inserted. |
| 47 bool InsertString(const std::string& str, std::set<std::string>& to) { | 50 bool InsertString(const std::string& str, std::set<std::string>& to) { |
| 48 const std::pair<std::set<std::string>::iterator, bool> result = | 51 const std::pair<std::set<std::string>::iterator, bool> result = |
| 49 to.insert(str); | 52 to.insert(str); |
| 50 return result.second; | 53 return result.second; |
| 51 } | 54 } |
| 52 | 55 |
| 53 void AddOptgroupOtherLayouts(base::ListValue* input_methods_list) { | 56 void AddOptgroupOtherLayouts(base::ListValue* input_methods_list) { |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 263 dictionary->SetString("code", pair.first); | 266 dictionary->SetString("code", pair.first); |
| 264 dictionary->SetString("displayName", out_display_names[i]); | 267 dictionary->SetString("displayName", out_display_names[i]); |
| 265 dictionary->SetString("textDirection", directionality); | 268 dictionary->SetString("textDirection", directionality); |
| 266 dictionary->SetString("nativeDisplayName", pair.second); | 269 dictionary->SetString("nativeDisplayName", pair.second); |
| 267 language_list->Append(dictionary); | 270 language_list->Append(dictionary); |
| 268 } | 271 } |
| 269 | 272 |
| 270 return language_list.Pass(); | 273 return language_list.Pass(); |
| 271 } | 274 } |
| 272 | 275 |
| 276 // Invokes |callback| with a list of keyboard layouts that can be used for | |
| 277 // |resolved_locale|. | |
| 278 void GetKeyboardLayoutsForResolvedLocale( | |
| 279 const GetKeyboardLayoutsForLocaleCallback& callback, | |
| 280 const std::string& resolved_locale) { | |
| 281 input_method::InputMethodUtil* util = | |
| 282 input_method::InputMethodManager::Get()->GetInputMethodUtil(); | |
| 283 std::vector<std::string> layouts = util->GetHardwareInputMethodIds(); | |
| 284 std::vector<std::string> layouts_from_locale; | |
| 285 util->GetInputMethodIdsFromLanguageCode( | |
| 286 resolved_locale, | |
| 287 input_method::kKeyboardLayoutsOnly, | |
| 288 &layouts_from_locale); | |
| 289 layouts.insert(layouts.end(), layouts_from_locale.begin(), | |
| 290 layouts_from_locale.end()); | |
| 291 | |
| 292 std::string selected; | |
| 293 if (!layouts_from_locale.empty()) { | |
| 294 selected = | |
| 295 util->GetInputMethodDescriptorFromId(layouts_from_locale[0])->id(); | |
| 296 } | |
| 297 | |
| 298 scoped_ptr<base::ListValue> input_methods_list(new base::ListValue); | |
| 299 std::set<std::string> input_methods_added; | |
| 300 for (std::vector<std::string>::const_iterator it = layouts.begin(); | |
| 301 it != layouts.end(); ++it) { | |
| 302 const input_method::InputMethodDescriptor* ime = | |
| 303 util->GetInputMethodDescriptorFromId(*it); | |
| 304 if (!InsertString(ime->id(), input_methods_added)) | |
| 305 continue; | |
| 306 input_methods_list->Append( | |
| 307 CreateInputMethodsEntry(*ime, selected).release()); | |
| 308 } | |
| 309 | |
| 310 callback.Run(input_methods_list.Pass()); | |
| 311 } | |
| 312 | |
| 273 } // namespace | 313 } // namespace |
| 274 | 314 |
| 275 const char kMostRelevantLanguagesDivider[] = "MOST_RELEVANT_LANGUAGES_DIVIDER"; | 315 const char kMostRelevantLanguagesDivider[] = "MOST_RELEVANT_LANGUAGES_DIVIDER"; |
| 276 | 316 |
| 277 scoped_ptr<base::ListValue> GetUILanguageList( | 317 scoped_ptr<base::ListValue> GetUILanguageList( |
| 278 const std::vector<std::string>* most_relevant_language_codes, | 318 const std::vector<std::string>* most_relevant_language_codes, |
| 279 const std::string& selected) { | 319 const std::string& selected) { |
| 280 ComponentExtensionIMEManager* manager = | 320 ComponentExtensionIMEManager* manager = |
| 281 input_method::InputMethodManager::Get()-> | 321 input_method::InputMethodManager::Get()-> |
| 282 GetComponentExtensionIMEManager(); | 322 GetComponentExtensionIMEManager(); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 352 | 392 |
| 353 for (std::vector<std::string>::const_iterator i = | 393 for (std::vector<std::string>::const_iterator i = |
| 354 hardware_login_input_methods.begin(); | 394 hardware_login_input_methods.begin(); |
| 355 i != hardware_login_input_methods.end(); | 395 i != hardware_login_input_methods.end(); |
| 356 ++i) { | 396 ++i) { |
| 357 const input_method::InputMethodDescriptor* ime = | 397 const input_method::InputMethodDescriptor* ime = |
| 358 util->GetInputMethodDescriptorFromId(*i); | 398 util->GetInputMethodDescriptorFromId(*i); |
| 359 // Do not crash in case of misconfiguration. | 399 // Do not crash in case of misconfiguration. |
| 360 if (ime) { | 400 if (ime) { |
| 361 input_methods_added.insert(*i); | 401 input_methods_added.insert(*i); |
| 362 input_methods_list->Append(CreateInputMethodsEntry(*ime, selected)); | 402 input_methods_list->Append( |
| 403 CreateInputMethodsEntry(*ime, selected).release()); | |
| 363 } else { | 404 } else { |
| 364 NOTREACHED(); | 405 NOTREACHED(); |
| 365 } | 406 } |
| 366 } | 407 } |
| 367 | 408 |
| 368 bool optgroup_added = false; | 409 bool optgroup_added = false; |
| 369 for (size_t i = 0; i < input_methods->size(); ++i) { | 410 for (size_t i = 0; i < input_methods->size(); ++i) { |
| 370 // Makes sure the id is in legacy xkb id format. | 411 // Makes sure the id is in legacy xkb id format. |
| 371 const std::string& ime_id = (*input_methods)[i].id(); | 412 const std::string& ime_id = (*input_methods)[i].id(); |
| 372 if (!InsertString(ime_id, input_methods_added)) | 413 if (!InsertString(ime_id, input_methods_added)) |
| 373 continue; | 414 continue; |
| 374 if (!optgroup_added) { | 415 if (!optgroup_added) { |
| 375 optgroup_added = true; | 416 optgroup_added = true; |
| 376 AddOptgroupOtherLayouts(input_methods_list.get()); | 417 AddOptgroupOtherLayouts(input_methods_list.get()); |
| 377 } | 418 } |
| 378 input_methods_list->Append(CreateInputMethodsEntry((*input_methods)[i], | 419 input_methods_list->Append(CreateInputMethodsEntry((*input_methods)[i], |
| 379 selected)); | 420 selected).release()); |
| 380 } | 421 } |
| 381 | 422 |
| 382 // "xkb:us::eng" should always be in the list of available layouts. | 423 // "xkb:us::eng" should always be in the list of available layouts. |
| 383 const std::string us_keyboard_id = | 424 const std::string us_keyboard_id = |
| 384 util->GetFallbackInputMethodDescriptor().id(); | 425 util->GetFallbackInputMethodDescriptor().id(); |
| 385 if (input_methods_added.find(us_keyboard_id) == input_methods_added.end()) { | 426 if (input_methods_added.find(us_keyboard_id) == input_methods_added.end()) { |
| 386 const input_method::InputMethodDescriptor* us_eng_descriptor = | 427 const input_method::InputMethodDescriptor* us_eng_descriptor = |
| 387 util->GetInputMethodDescriptorFromId(us_keyboard_id); | 428 util->GetInputMethodDescriptorFromId(us_keyboard_id); |
| 388 DCHECK(us_eng_descriptor); | 429 DCHECK(us_eng_descriptor); |
| 389 if (!optgroup_added) { | 430 if (!optgroup_added) { |
| 390 optgroup_added = true; | 431 optgroup_added = true; |
| 391 AddOptgroupOtherLayouts(input_methods_list.get()); | 432 AddOptgroupOtherLayouts(input_methods_list.get()); |
| 392 } | 433 } |
| 393 input_methods_list->Append(CreateInputMethodsEntry(*us_eng_descriptor, | 434 input_methods_list->Append(CreateInputMethodsEntry(*us_eng_descriptor, |
| 394 selected)); | 435 selected).release()); |
| 395 } | 436 } |
| 396 return input_methods_list.Pass(); | 437 return input_methods_list.Pass(); |
| 397 } | 438 } |
| 398 | 439 |
| 399 scoped_ptr<base::ListValue> GetKeyboardLayoutsForLocale( | 440 void GetKeyboardLayoutsForLocale( |
| 441 const GetKeyboardLayoutsForLocaleCallback& callback, | |
| 400 const std::string& locale) { | 442 const std::string& locale) { |
| 401 input_method::InputMethodUtil* util = | 443 // Resolve |locale| on the FILE thread, then continue on the UI thread. |
| 402 input_method::InputMethodManager::Get()->GetInputMethodUtil(); | 444 content::BrowserThread::PostTaskAndReplyWithResult( |
| 403 std::vector<std::string> layouts = util->GetHardwareInputMethodIds(); | 445 content::BrowserThread::FILE, |
|
Nikita (slow)
2014/07/30 12:08:47
FILE thread is deprecated
https://groups.google.co
bartfab (slow)
2014/07/30 12:55:17
Done.
| |
| 404 std::vector<std::string> layouts_from_locale; | 446 FROM_HERE, |
| 405 util->GetInputMethodIdsFromLanguageCode( | 447 base::Bind(&l10n_util::GetApplicationLocale, |
| 406 l10n_util::GetApplicationLocale(locale), | 448 locale), |
| 407 input_method::kKeyboardLayoutsOnly, | 449 base::Bind(&GetKeyboardLayoutsForResolvedLocale, |
| 408 &layouts_from_locale); | 450 callback)); |
| 409 layouts.insert(layouts.end(), layouts_from_locale.begin(), | 451 } |
| 410 layouts_from_locale.end()); | |
| 411 | 452 |
| 412 std::string selected; | 453 scoped_ptr<base::DictionaryValue> GetCurrentKeyboardLayout() { |
| 413 if (!layouts_from_locale.empty()) { | 454 const input_method::InputMethodDescriptor current_input_method = |
| 414 selected = | 455 input_method::InputMethodManager::Get()->GetCurrentInputMethod(); |
| 415 util->GetInputMethodDescriptorFromId(layouts_from_locale[0])->id(); | 456 return CreateInputMethodsEntry(current_input_method, |
| 416 } | 457 current_input_method.id()); |
| 417 | |
| 418 scoped_ptr<base::ListValue> input_methods_list(new base::ListValue); | |
| 419 std::set<std::string> input_methods_added; | |
| 420 for (std::vector<std::string>::const_iterator it = layouts.begin(); | |
| 421 it != layouts.end(); ++it) { | |
| 422 const input_method::InputMethodDescriptor* ime = | |
| 423 util->GetInputMethodDescriptorFromId(*it); | |
| 424 if (!InsertString(ime->id(), input_methods_added)) | |
| 425 continue; | |
| 426 input_methods_list->Append(CreateInputMethodsEntry(*ime, selected)); | |
| 427 } | |
| 428 return input_methods_list.Pass(); | |
| 429 } | 458 } |
| 430 | 459 |
| 431 } // namespace chromeos | 460 } // namespace chromeos |
| OLD | NEW |