1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/base/locale_util.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/chromeos/input_method/input_method_util.h"
11 #include "chrome/browser/chromeos/login/session/user_session_manager.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "ui/base/ime/chromeos/input_method_manager.h"
15 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/gfx/platform_font_linux.h"
22 struct SwitchLanguageData
{
23 SwitchLanguageData(const std::string
& locale
,
24 const bool enable_locale_keyboard_layouts
,
25 const bool login_layouts_only
,
26 const locale_util::SwitchLanguageCallback
& callback
,
29 result(locale
, std::string(), false),
30 enable_locale_keyboard_layouts(enable_locale_keyboard_layouts
),
31 login_layouts_only(login_layouts_only
),
34 const locale_util::SwitchLanguageCallback callback
;
36 locale_util::LanguageSwitchResult result
;
37 const bool enable_locale_keyboard_layouts
;
38 const bool login_layouts_only
;
42 // Runs on SequencedWorkerPool thread under PostTaskAndReply().
43 // So data is owned by "Reply" part of PostTaskAndReply() process.
44 void SwitchLanguageDoReloadLocale(SwitchLanguageData
* data
) {
45 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
47 data
->result
.loaded_locale
=
48 ResourceBundle::GetSharedInstance().ReloadLocaleResources(
49 data
->result
.requested_locale
);
51 data
->result
.success
= !data
->result
.loaded_locale
.empty();
53 ResourceBundle::GetSharedInstance().ReloadFonts();
56 // Callback after SwitchLanguageDoReloadLocale() back in UI thread.
57 void FinishSwitchLanguage(scoped_ptr
<SwitchLanguageData
> data
) {
58 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
59 if (data
->result
.success
) {
60 g_browser_process
->SetApplicationLocale(data
->result
.loaded_locale
);
62 if (data
->enable_locale_keyboard_layouts
) {
63 input_method::InputMethodManager
* manager
=
64 input_method::InputMethodManager::Get();
65 scoped_refptr
<input_method::InputMethodManager::State
> ime_state
=
66 UserSessionManager::GetInstance()->GetDefaultIMEState(data
->profile
);
67 if (data
->login_layouts_only
) {
68 // Enable the hardware keyboard layouts and locale-specific layouts
69 // suitable for use on the login screen. This will also switch to the
70 // first hardware keyboard layout since the input method currently in
71 // use may not be supported by the new locale.
72 ime_state
->EnableLoginLayouts(
73 data
->result
.loaded_locale
,
74 manager
->GetInputMethodUtil()->GetHardwareLoginInputMethodIds());
76 // Enable all hardware keyboard layouts. This will also switch to the
77 // first hardware keyboard layout.
78 ime_state
->ReplaceEnabledInputMethods(
79 manager
->GetInputMethodUtil()->GetHardwareInputMethodIds());
81 // Enable all locale-specific layouts.
82 std::vector
<std::string
> input_methods
;
83 manager
->GetInputMethodUtil()->GetInputMethodIdsFromLanguageCode(
84 data
->result
.loaded_locale
,
85 input_method::kKeyboardLayoutsOnly
,
87 for (std::vector
<std::string
>::const_iterator it
=
88 input_methods
.begin(); it
!= input_methods
.end(); ++it
) {
89 ime_state
->EnableInputMethod(*it
);
94 gfx::PlatformFontLinux::ReloadDefaultFont();
95 if (!data
->callback
.is_null())
96 data
->callback
.Run(data
->result
);
101 namespace locale_util
{
103 LanguageSwitchResult::LanguageSwitchResult(const std::string
& requested_locale
,
104 const std::string
& loaded_locale
,
106 : requested_locale(requested_locale
),
107 loaded_locale(loaded_locale
),
111 void SwitchLanguage(const std::string
& locale
,
112 const bool enable_locale_keyboard_layouts
,
113 const bool login_layouts_only
,
114 const SwitchLanguageCallback
& callback
,
116 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
117 scoped_ptr
<SwitchLanguageData
> data(
118 new SwitchLanguageData(locale
, enable_locale_keyboard_layouts
,
119 login_layouts_only
, callback
, profile
));
120 base::Closure
reloader(
121 base::Bind(&SwitchLanguageDoReloadLocale
, base::Unretained(data
.get())));
122 content::BrowserThread::PostBlockingPoolTaskAndReply(
125 base::Bind(&FinishSwitchLanguage
, base::Passed(data
.Pass())));
128 } // namespace locale_util
129 } // namespace chromeos