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 "chromeos/ime/input_method_manager.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "ui/base/resource/resource_bundle.h"
14 #include "ui/gfx/platform_font_pango.h"
20 struct SwitchLanguageData
{
21 SwitchLanguageData(const std::string
& locale
,
22 const bool enableLocaleKeyboardLayouts
,
23 const bool login_layouts_only
,
24 scoped_ptr
<locale_util::SwitchLanguageCallback
> callback
)
25 : callback(callback
.Pass()),
27 enableLocaleKeyboardLayouts(enableLocaleKeyboardLayouts
),
28 login_layouts_only(login_layouts_only
),
31 scoped_ptr
<locale_util::SwitchLanguageCallback
> callback
;
33 const std::string locale
;
34 const bool enableLocaleKeyboardLayouts
;
35 const bool login_layouts_only
;
36 std::string loaded_locale
;
40 // Runs on SequencedWorkerPool thread under PostTaskAndReply().
41 // So data is owned by "Reply" part of PostTaskAndReply() process.
42 void SwitchLanguageDoReloadLocale(SwitchLanguageData
* data
) {
43 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
46 ResourceBundle::GetSharedInstance().ReloadLocaleResources(data
->locale
);
48 data
->success
= !data
->loaded_locale
.empty();
50 ResourceBundle::GetSharedInstance().ReloadFonts();
53 // Callback after SwitchLanguageDoReloadLocale() back in UI thread.
54 void FinishSwitchLanguage(scoped_ptr
<SwitchLanguageData
> data
) {
55 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
57 g_browser_process
->SetApplicationLocale(data
->loaded_locale
);
59 if (data
->enableLocaleKeyboardLayouts
) {
60 // If we have switched the locale, enable the keyboard layouts that
61 // are necessary for the new locale. Change the current input method
62 // to the hardware keyboard layout since the input method currently in
63 // use may not be supported by the new locale (3rd parameter).
64 input_method::InputMethodManager
* manager
=
65 input_method::InputMethodManager::Get();
66 manager
->EnableLoginLayouts(
68 manager
->GetInputMethodUtil()->GetHardwareLoginInputMethodIds());
69 if (!data
->login_layouts_only
) {
70 // Enable all the other layouts
71 std::vector
<std::string
> candidates
;
72 input_method::InputMethodUtil
* util
= manager
->GetInputMethodUtil();
73 // Add input methods associated with the language.
74 util
->GetInputMethodIdsFromLanguageCode(
75 data
->locale
, input_method::kKeyboardLayoutsOnly
, &candidates
);
76 for (std::vector
<std::string
>::const_iterator i
= candidates
.begin();
77 i
!= candidates
.end();
79 manager
->EnableInputMethod(*i
);
83 gfx::PlatformFontPango::ReloadDefaultFont();
85 data
->callback
->Run(data
->locale
, data
->loaded_locale
, data
->success
);
90 namespace locale_util
{
92 void SwitchLanguage(const std::string
& locale
,
93 const bool enableLocaleKeyboardLayouts
,
94 const bool login_layouts_only
,
95 scoped_ptr
<SwitchLanguageCallback
> callback
) {
96 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
97 scoped_ptr
<SwitchLanguageData
> data(
98 new SwitchLanguageData(locale
,
99 enableLocaleKeyboardLayouts
,
102 base::Closure
reloader(
103 base::Bind(&SwitchLanguageDoReloadLocale
, base::Unretained(data
.get())));
104 content::BrowserThread::PostBlockingPoolTaskAndReply(
107 base::Bind(&FinishSwitchLanguage
, base::Passed(data
.Pass())));
110 } // namespace locale_util
111 } // namespace chromeos