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 "content/public/browser/browser_thread.h"
12 #include "ui/base/ime/chromeos/input_method_manager.h"
13 #include "ui/base/resource/resource_bundle.h"
14 #include "ui/gfx/platform_font_linux.h"
20 struct SwitchLanguageData
{
21 SwitchLanguageData(const std::string
& locale
,
22 const bool enable_locale_keyboard_layouts
,
23 const bool login_layouts_only
,
24 const locale_util::SwitchLanguageCallback
& callback
)
26 result(locale
, std::string(), false),
27 enable_locale_keyboard_layouts(enable_locale_keyboard_layouts
),
28 login_layouts_only(login_layouts_only
) {}
30 const locale_util::SwitchLanguageCallback callback
;
32 locale_util::LanguageSwitchResult result
;
33 const bool enable_locale_keyboard_layouts
;
34 const bool login_layouts_only
;
37 // Runs on SequencedWorkerPool thread under PostTaskAndReply().
38 // So data is owned by "Reply" part of PostTaskAndReply() process.
39 void SwitchLanguageDoReloadLocale(SwitchLanguageData
* data
) {
40 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
42 data
->result
.loaded_locale
=
43 ResourceBundle::GetSharedInstance().ReloadLocaleResources(
44 data
->result
.requested_locale
);
46 data
->result
.success
= !data
->result
.loaded_locale
.empty();
48 ResourceBundle::GetSharedInstance().ReloadFonts();
51 // Callback after SwitchLanguageDoReloadLocale() back in UI thread.
52 void FinishSwitchLanguage(scoped_ptr
<SwitchLanguageData
> data
) {
53 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
54 if (data
->result
.success
) {
55 g_browser_process
->SetApplicationLocale(data
->result
.loaded_locale
);
57 if (data
->enable_locale_keyboard_layouts
) {
58 input_method::InputMethodManager
* manager
=
59 input_method::InputMethodManager::Get();
60 scoped_refptr
<input_method::InputMethodManager::State
> ime_state
=
61 manager
->GetActiveIMEState();
62 if (data
->login_layouts_only
) {
63 // Enable the hardware keyboard layouts and locale-specific layouts
64 // suitable for use on the login screen. This will also switch to the
65 // first hardware keyboard layout since the input method currently in
66 // use may not be supported by the new locale.
67 ime_state
->EnableLoginLayouts(
68 data
->result
.loaded_locale
,
69 manager
->GetInputMethodUtil()->GetHardwareLoginInputMethodIds());
71 // Enable all hardware keyboard layouts. This will also switch to the
72 // first hardware keyboard layout.
73 ime_state
->ReplaceEnabledInputMethods(
74 manager
->GetInputMethodUtil()->GetHardwareInputMethodIds());
76 // Enable all locale-specific layouts.
77 std::vector
<std::string
> input_methods
;
78 manager
->GetInputMethodUtil()->GetInputMethodIdsFromLanguageCode(
79 data
->result
.loaded_locale
,
80 input_method::kKeyboardLayoutsOnly
,
82 for (std::vector
<std::string
>::const_iterator it
=
83 input_methods
.begin(); it
!= input_methods
.end(); ++it
) {
84 ime_state
->EnableInputMethod(*it
);
89 gfx::PlatformFontLinux::ReloadDefaultFont();
90 if (!data
->callback
.is_null())
91 data
->callback
.Run(data
->result
);
96 namespace locale_util
{
98 LanguageSwitchResult::LanguageSwitchResult(const std::string
& requested_locale
,
99 const std::string
& loaded_locale
,
101 : requested_locale(requested_locale
),
102 loaded_locale(loaded_locale
),
106 void SwitchLanguage(const std::string
& locale
,
107 const bool enable_locale_keyboard_layouts
,
108 const bool login_layouts_only
,
109 const SwitchLanguageCallback
& callback
) {
110 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
111 scoped_ptr
<SwitchLanguageData
> data(new SwitchLanguageData(
112 locale
, enable_locale_keyboard_layouts
, login_layouts_only
, callback
));
113 base::Closure
reloader(
114 base::Bind(&SwitchLanguageDoReloadLocale
, base::Unretained(data
.get())));
115 content::BrowserThread::PostBlockingPoolTaskAndReply(
118 base::Bind(&FinishSwitchLanguage
, base::Passed(data
.Pass())));
121 } // namespace locale_util
122 } // namespace chromeos