+class GuestLanguageSetCallbackData {
+ public:
+ explicit GuestLanguageSetCallbackData(Profile* profile) : profile(profile) {
+ }
+
+ // Must match SwitchLanguageCallback type.
+ static void Callback(const scoped_ptr<GuestLanguageSetCallbackData>& self,
+ const std::string& locale,
+ const std::string& loaded_locale,
+ bool success);
+
+ Profile* profile;
+};
+
+// static
+void GuestLanguageSetCallbackData::Callback(
+ const scoped_ptr<GuestLanguageSetCallbackData>& self,
+ const std::string& locale,
+ const std::string& loaded_locale,
+ bool success) {
+ input_method::InputMethodManager* const ime_manager =
+ input_method::InputMethodManager::Get();
+ // Active layout must be hardware "login layout".
+ // The previous one must be "locale default layout".
+ // First, enable all hardware input methods.
+ const std::vector<std::string>& input_methods =
+ ime_manager->GetInputMethodUtil()->GetHardwareInputMethodIds();
+ for (size_t i = 0; i < input_methods.size(); ++i)
+ ime_manager->EnableInputMethod(input_methods[i]);
+
+ // Second, enable locale based input methods.
+ const std::string locale_default_input_method =
+ ime_manager->GetInputMethodUtil()->
+ GetLanguageDefaultInputMethodId(loaded_locale);
+ if (!locale_default_input_method.empty()) {
+ PrefService* user_prefs = self->profile->GetPrefs();
+ user_prefs->SetString(prefs::kLanguagePreviousInputMethod,
+ locale_default_input_method);
+ ime_manager->EnableInputMethod(locale_default_input_method);
+ }
+
+ // Finally, activate the first login input method.
+ const std::vector<std::string>& login_input_methods =
+ ime_manager->GetInputMethodUtil()->GetHardwareLoginInputMethodIds();
+ ime_manager->ChangeInputMethod(login_input_methods[0]);
+}
+
+void SetGuestLocale(UserManager* const usermanager, Profile* const profile) {
+ scoped_ptr<GuestLanguageSetCallbackData> data(
+ new GuestLanguageSetCallbackData(profile));
+ scoped_ptr<locale_util::SwitchLanguageCallback> callback(
+ new locale_util::SwitchLanguageCallback(base::Bind(
+ &GuestLanguageSetCallbackData::Callback, base::Passed(data.Pass()))));
+ User* const user = usermanager->GetUserByProfile(profile);
+ usermanager->RespectLocalePreference(profile, user, callback.Pass());
+}
+