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
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/input_method/input_method_syncer.h"
11 #include "base/bind.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/string_util.h"
14 #include "base/task_runner.h"
15 #include "base/task_runner_util.h"
16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/prefs/pref_service_syncable.h"
18 #include "chrome/common/pref_names.h"
19 #include "components/pref_registry/pref_registry_syncable.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "ui/base/ime/chromeos/component_extension_ime_manager.h"
22 #include "ui/base/ime/chromeos/extension_ime_util.h"
23 #include "ui/base/l10n/l10n_util.h"
26 namespace input_method
{
29 // Checks input method IDs, converting engine IDs to input method IDs and
30 // removing unsupported IDs from |values|.
31 void CheckAndResolveInputMethodIDs(
32 const input_method::InputMethodDescriptors
& supported_descriptors
,
33 std::vector
<std::string
>* values
) {
34 // Extract the supported input method IDs into a set.
35 std::set
<std::string
> supported_input_method_ids
;
36 for (const auto& descriptor
: supported_descriptors
)
37 supported_input_method_ids
.insert(descriptor
.id());
39 // Convert engine IDs to input method extension IDs.
40 std::transform(values
->begin(), values
->end(), values
->begin(),
41 extension_ime_util::GetInputMethodIDByEngineID
);
43 // Remove values that aren't found in the set of supported input method IDs.
44 std::vector
<std::string
>::iterator it
= values
->begin();
45 while (it
!= values
->end()) {
46 if (it
->size() && supported_input_method_ids
.find(*it
) !=
47 supported_input_method_ids
.end()) {
50 it
= values
->erase(it
);
55 // Checks whether each language is supported, replacing locales with variants
56 // if they are available. Must be called on a thread that allows IO.
57 std::string
CheckAndResolveLocales(const std::string
& languages
) {
58 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
59 if (languages
.empty())
61 std::vector
<std::string
> values
;
62 base::SplitString(languages
, ',', &values
);
64 const std::string app_locale
= g_browser_process
->GetApplicationLocale();
66 std::vector
<std::string
> accept_language_codes
;
67 l10n_util::GetAcceptLanguagesForLocale(app_locale
, &accept_language_codes
);
68 std::sort(accept_language_codes
.begin(), accept_language_codes
.end());
70 // Remove unsupported language values.
71 std::vector
<std::string
>::iterator value_iter
= values
.begin();
72 while (value_iter
!= values
.end()) {
73 if (binary_search(accept_language_codes
.begin(),
74 accept_language_codes
.end(),
80 // If a language code resolves to a supported backup locale, replace it
81 // with the resolved locale.
82 std::string resolved_locale
;
83 if (l10n_util::CheckAndResolveLocale(*value_iter
, &resolved_locale
)) {
84 if (binary_search(accept_language_codes
.begin(),
85 accept_language_codes
.end(),
87 *value_iter
= resolved_locale
;
92 value_iter
= values
.erase(value_iter
);
95 return JoinString(values
, ',');
98 // Appends tokens from |src| that are not in |dest| to |dest|.
99 void MergeLists(std::vector
<std::string
>* dest
,
100 const std::vector
<std::string
>& src
) {
101 // Keep track of already-added tokens.
102 std::set
<std::string
> unique_tokens(dest
->begin(), dest
->end());
104 for (const std::string
& token
: src
) {
105 // Skip token if it's already in |dest|.
106 if (binary_search(unique_tokens
.begin(), unique_tokens
.end(), token
))
108 dest
->push_back(token
);
109 unique_tokens
.insert(token
);
113 } // anonymous namespace
115 InputMethodSyncer::InputMethodSyncer(
116 PrefServiceSyncable
* prefs
,
117 scoped_refptr
<input_method::InputMethodManager::State
> ime_state
)
119 ime_state_(ime_state
),
121 weak_factory_(this) {
124 InputMethodSyncer::~InputMethodSyncer() {
125 prefs_
->RemoveObserver(this);
129 void InputMethodSyncer::RegisterProfilePrefs(
130 user_prefs::PrefRegistrySyncable
* registry
) {
131 registry
->RegisterStringPref(
132 prefs::kLanguagePreferredLanguagesSyncable
,
134 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
135 registry
->RegisterStringPref(
136 prefs::kLanguagePreloadEnginesSyncable
,
138 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
139 registry
->RegisterStringPref(
140 prefs::kLanguageEnabledExtensionImesSyncable
,
142 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
143 registry
->RegisterBooleanPref(
144 prefs::kLanguageShouldMergeInputMethods
,
146 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
149 void InputMethodSyncer::Initialize() {
150 // This causes OnIsSyncingChanged to be called when the value of
151 // PrefService::IsSyncing() changes.
152 prefs_
->AddObserver(this);
154 preferred_languages_syncable_
.Init(prefs::kLanguagePreferredLanguagesSyncable
,
156 preload_engines_syncable_
.Init(prefs::kLanguagePreloadEnginesSyncable
,
158 enabled_extension_imes_syncable_
.Init(
159 prefs::kLanguageEnabledExtensionImesSyncable
, prefs_
);
161 BooleanPrefMember::NamedChangeCallback callback
=
162 base::Bind(&InputMethodSyncer::OnPreferenceChanged
,
163 base::Unretained(this));
164 preferred_languages_
.Init(prefs::kLanguagePreferredLanguages
,
166 preload_engines_
.Init(prefs::kLanguagePreloadEngines
,
168 enabled_extension_imes_
.Init(
169 prefs::kLanguageEnabledExtensionImes
, prefs_
, callback
);
171 // If we have already synced but haven't merged input methods yet, do so now.
172 if (prefs_
->GetBoolean(prefs::kLanguageShouldMergeInputMethods
) &&
173 !(preferred_languages_syncable_
.GetValue().empty() &&
174 preload_engines_syncable_
.GetValue().empty() &&
175 enabled_extension_imes_syncable_
.GetValue().empty())) {
180 void InputMethodSyncer::MergeSyncedPrefs() {
181 // This should only be done after the first ever sync.
182 DCHECK(prefs_
->GetBoolean(prefs::kLanguageShouldMergeInputMethods
));
183 prefs_
->SetBoolean(prefs::kLanguageShouldMergeInputMethods
, false);
186 std::string preferred_languages_syncable
=
187 preferred_languages_syncable_
.GetValue();
188 std::string preload_engines_syncable
=
189 preload_engines_syncable_
.GetValue();
190 std::string enabled_extension_imes_syncable
=
191 enabled_extension_imes_syncable_
.GetValue();
193 std::vector
<std::string
> synced_tokens
;
194 std::vector
<std::string
> new_tokens
;
196 // First, set the syncable prefs to the union of the local and synced prefs.
198 preferred_languages_syncable_
.GetValue(), ',', &synced_tokens
);
199 base::SplitString(preferred_languages_
.GetValue(), ',', &new_tokens
);
201 // Append the synced values to the current values.
202 MergeLists(&new_tokens
, synced_tokens
);
203 preferred_languages_syncable_
.SetValue(JoinString(new_tokens
, ','));
206 enabled_extension_imes_syncable_
.GetValue(), ',', &synced_tokens
);
207 base::SplitString(enabled_extension_imes_
.GetValue(), ',', &new_tokens
);
209 MergeLists(&new_tokens
, synced_tokens
);
210 enabled_extension_imes_syncable_
.SetValue(JoinString(new_tokens
, ','));
212 // Revert preload engines to legacy component IDs.
213 base::SplitString(preload_engines_
.GetValue(), ',', &new_tokens
);
214 std::transform(new_tokens
.begin(), new_tokens
.end(), new_tokens
.begin(),
215 extension_ime_util::GetComponentIDByInputMethodID
);
217 preload_engines_syncable_
.GetValue(), ',', &synced_tokens
);
219 MergeLists(&new_tokens
, synced_tokens
);
220 preload_engines_syncable_
.SetValue(JoinString(new_tokens
, ','));
222 // Second, set the local prefs, incorporating new values from the sync server.
223 preload_engines_
.SetValue(
224 AddSupportedInputMethodValues(preload_engines_
.GetValue(),
225 preload_engines_syncable
,
226 prefs::kLanguagePreloadEngines
));
227 enabled_extension_imes_
.SetValue(
228 AddSupportedInputMethodValues(enabled_extension_imes_
.GetValue(),
229 enabled_extension_imes_syncable
,
230 prefs::kLanguageEnabledExtensionImes
));
232 // Remove unsupported locales before updating the local languages preference.
233 std::string
languages(
234 AddSupportedInputMethodValues(preferred_languages_
.GetValue(),
235 preferred_languages_syncable
,
236 prefs::kLanguagePreferredLanguages
));
237 base::PostTaskAndReplyWithResult(
238 content::BrowserThread::GetBlockingPool(),
240 base::Bind(&CheckAndResolveLocales
, languages
),
241 base::Bind(&InputMethodSyncer::FinishMerge
,
242 weak_factory_
.GetWeakPtr()));
245 std::string
InputMethodSyncer::AddSupportedInputMethodValues(
246 const std::string
& pref
,
247 const std::string
& synced_pref
,
248 const char* pref_name
) {
249 std::vector
<std::string
> old_tokens
;
250 std::vector
<std::string
> new_tokens
;
251 base::SplitString(pref
, ',', &old_tokens
);
252 base::SplitString(synced_pref
, ',', &new_tokens
);
254 // Check and convert the new tokens.
255 if (pref_name
== prefs::kLanguagePreloadEngines
||
256 pref_name
== prefs::kLanguageEnabledExtensionImes
) {
257 input_method::InputMethodManager
* manager
=
258 input_method::InputMethodManager::Get();
259 scoped_ptr
<input_method::InputMethodDescriptors
> supported_descriptors
;
261 if (pref_name
== prefs::kLanguagePreloadEngines
) {
262 // Set the known input methods.
263 supported_descriptors
= manager
->GetSupportedInputMethods();
264 // Add the available component extension IMEs.
265 ComponentExtensionIMEManager
* component_extension_manager
=
266 manager
->GetComponentExtensionIMEManager();
267 input_method::InputMethodDescriptors component_descriptors
=
268 component_extension_manager
->GetAllIMEAsInputMethodDescriptor();
269 supported_descriptors
->insert(supported_descriptors
->end(),
270 component_descriptors
.begin(),
271 component_descriptors
.end());
273 supported_descriptors
.reset(new input_method::InputMethodDescriptors
);
274 ime_state_
->GetInputMethodExtensions(supported_descriptors
.get());
276 CheckAndResolveInputMethodIDs(*supported_descriptors
, &new_tokens
);
277 } else if (pref_name
!= prefs::kLanguagePreferredLanguages
) {
278 NOTREACHED() << "Attempting to merge an invalid preference.";
279 // kLanguagePreferredLanguages is checked in CheckAndResolveLocales().
282 // Do the actual merging.
283 MergeLists(&old_tokens
, new_tokens
);
284 return JoinString(old_tokens
, ',');
287 void InputMethodSyncer::FinishMerge(const std::string
& languages
) {
288 // Since the merge only removed locales that are unsupported on this system,
289 // we don't need to update the syncable prefs. If the local preference changes
290 // later, the sync server will lose the values we dropped. That's okay since
291 // the values from this device should then become the new defaults anyway.
292 preferred_languages_
.SetValue(languages
);
294 // We've finished merging.
298 void InputMethodSyncer::OnPreferenceChanged(const std::string
& pref_name
) {
299 DCHECK(pref_name
== prefs::kLanguagePreferredLanguages
||
300 pref_name
== prefs::kLanguagePreloadEngines
||
301 pref_name
== prefs::kLanguageEnabledExtensionImes
);
303 if (merging_
|| prefs_
->GetBoolean(prefs::kLanguageShouldMergeInputMethods
))
306 // Set the language and input prefs at the same time. Otherwise we may,
307 // e.g., use a stale languages setting but push a new preload engines setting.
308 preferred_languages_syncable_
.SetValue(preferred_languages_
.GetValue());
309 enabled_extension_imes_syncable_
.SetValue(enabled_extension_imes_
.GetValue());
311 // For preload engines, use legacy xkb IDs so the preference can sync
312 // across Chrome OS and Chromium OS.
313 std::vector
<std::string
> engines
;
314 base::SplitString(preload_engines_
.GetValue(), ',', &engines
);
315 std::transform(engines
.begin(), engines
.end(), engines
.begin(),
316 extension_ime_util::GetComponentIDByInputMethodID
);
317 preload_engines_syncable_
.SetValue(JoinString(engines
, ','));
320 void InputMethodSyncer::OnIsSyncingChanged() {
321 if (prefs_
->GetBoolean(prefs::kLanguageShouldMergeInputMethods
) &&
322 prefs_
->IsSyncing()) {
327 } // namespace input_method
328 } // namespace chromeos