| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/input_method/input_method_syncer.h" | 5 #include "chrome/browser/chromeos/input_method/input_method_syncer.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 } | 51 } |
| 52 } | 52 } |
| 53 } | 53 } |
| 54 | 54 |
| 55 // Checks whether each language is supported, replacing locales with variants | 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. | 56 // if they are available. Must be called on a thread that allows IO. |
| 57 std::string CheckAndResolveLocales(const std::string& languages) { | 57 std::string CheckAndResolveLocales(const std::string& languages) { |
| 58 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 58 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 59 if (languages.empty()) | 59 if (languages.empty()) |
| 60 return languages; | 60 return languages; |
| 61 std::vector<std::string> values; | 61 std::vector<std::string> values = base::SplitString( |
| 62 base::SplitString(languages, ',', &values); | 62 languages, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 63 | 63 |
| 64 const std::string app_locale = g_browser_process->GetApplicationLocale(); | 64 const std::string app_locale = g_browser_process->GetApplicationLocale(); |
| 65 | 65 |
| 66 std::vector<std::string> accept_language_codes; | 66 std::vector<std::string> accept_language_codes; |
| 67 l10n_util::GetAcceptLanguagesForLocale(app_locale, &accept_language_codes); | 67 l10n_util::GetAcceptLanguagesForLocale(app_locale, &accept_language_codes); |
| 68 std::sort(accept_language_codes.begin(), accept_language_codes.end()); | 68 std::sort(accept_language_codes.begin(), accept_language_codes.end()); |
| 69 | 69 |
| 70 // Remove unsupported language values. | 70 // Remove unsupported language values. |
| 71 std::vector<std::string>::iterator value_iter = values.begin(); | 71 std::vector<std::string>::iterator value_iter = values.begin(); |
| 72 while (value_iter != values.end()) { | 72 while (value_iter != values.end()) { |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 preferred_languages_syncable_.GetValue(); | 184 preferred_languages_syncable_.GetValue(); |
| 185 std::string preload_engines_syncable = | 185 std::string preload_engines_syncable = |
| 186 preload_engines_syncable_.GetValue(); | 186 preload_engines_syncable_.GetValue(); |
| 187 std::string enabled_extension_imes_syncable = | 187 std::string enabled_extension_imes_syncable = |
| 188 enabled_extension_imes_syncable_.GetValue(); | 188 enabled_extension_imes_syncable_.GetValue(); |
| 189 | 189 |
| 190 std::vector<std::string> synced_tokens; | 190 std::vector<std::string> synced_tokens; |
| 191 std::vector<std::string> new_tokens; | 191 std::vector<std::string> new_tokens; |
| 192 | 192 |
| 193 // First, set the syncable prefs to the union of the local and synced prefs. | 193 // First, set the syncable prefs to the union of the local and synced prefs. |
| 194 base::SplitString( | 194 synced_tokens = |
| 195 preferred_languages_syncable_.GetValue(), ',', &synced_tokens); | 195 base::SplitString(preferred_languages_syncable_.GetValue(), ",", |
| 196 base::SplitString(preferred_languages_.GetValue(), ',', &new_tokens); | 196 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 197 new_tokens = base::SplitString(preferred_languages_.GetValue(), ",", |
| 198 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 197 | 199 |
| 198 // Append the synced values to the current values. | 200 // Append the synced values to the current values. |
| 199 MergeLists(&new_tokens, synced_tokens); | 201 MergeLists(&new_tokens, synced_tokens); |
| 200 preferred_languages_syncable_.SetValue(base::JoinString(new_tokens, ",")); | 202 preferred_languages_syncable_.SetValue(base::JoinString(new_tokens, ",")); |
| 201 | 203 |
| 202 base::SplitString( | 204 synced_tokens = |
| 203 enabled_extension_imes_syncable_.GetValue(), ',', &synced_tokens); | 205 base::SplitString(enabled_extension_imes_syncable_.GetValue(), ",", |
| 204 base::SplitString(enabled_extension_imes_.GetValue(), ',', &new_tokens); | 206 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 207 new_tokens = base::SplitString(enabled_extension_imes_.GetValue(), ",", |
| 208 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 205 | 209 |
| 206 MergeLists(&new_tokens, synced_tokens); | 210 MergeLists(&new_tokens, synced_tokens); |
| 207 enabled_extension_imes_syncable_.SetValue(base::JoinString(new_tokens, ",")); | 211 enabled_extension_imes_syncable_.SetValue(base::JoinString(new_tokens, ",")); |
| 208 | 212 |
| 209 // Revert preload engines to legacy component IDs. | 213 // Revert preload engines to legacy component IDs. |
| 210 base::SplitString(preload_engines_.GetValue(), ',', &new_tokens); | 214 new_tokens = base::SplitString(preload_engines_.GetValue(), ",", |
| 215 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 211 std::transform(new_tokens.begin(), new_tokens.end(), new_tokens.begin(), | 216 std::transform(new_tokens.begin(), new_tokens.end(), new_tokens.begin(), |
| 212 extension_ime_util::GetComponentIDByInputMethodID); | 217 extension_ime_util::GetComponentIDByInputMethodID); |
| 213 base::SplitString( | 218 synced_tokens = |
| 214 preload_engines_syncable_.GetValue(), ',', &synced_tokens); | 219 base::SplitString(preload_engines_syncable_.GetValue(), ",", |
| 220 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 215 | 221 |
| 216 MergeLists(&new_tokens, synced_tokens); | 222 MergeLists(&new_tokens, synced_tokens); |
| 217 preload_engines_syncable_.SetValue(base::JoinString(new_tokens, ",")); | 223 preload_engines_syncable_.SetValue(base::JoinString(new_tokens, ",")); |
| 218 | 224 |
| 219 // Second, set the local prefs, incorporating new values from the sync server. | 225 // Second, set the local prefs, incorporating new values from the sync server. |
| 220 preload_engines_.SetValue( | 226 preload_engines_.SetValue( |
| 221 AddSupportedInputMethodValues(preload_engines_.GetValue(), | 227 AddSupportedInputMethodValues(preload_engines_.GetValue(), |
| 222 preload_engines_syncable, | 228 preload_engines_syncable, |
| 223 prefs::kLanguagePreloadEngines)); | 229 prefs::kLanguagePreloadEngines)); |
| 224 enabled_extension_imes_.SetValue( | 230 enabled_extension_imes_.SetValue( |
| (...skipping 11 matching lines...) Expand all Loading... |
| 236 FROM_HERE, | 242 FROM_HERE, |
| 237 base::Bind(&CheckAndResolveLocales, languages), | 243 base::Bind(&CheckAndResolveLocales, languages), |
| 238 base::Bind(&InputMethodSyncer::FinishMerge, | 244 base::Bind(&InputMethodSyncer::FinishMerge, |
| 239 weak_factory_.GetWeakPtr())); | 245 weak_factory_.GetWeakPtr())); |
| 240 } | 246 } |
| 241 | 247 |
| 242 std::string InputMethodSyncer::AddSupportedInputMethodValues( | 248 std::string InputMethodSyncer::AddSupportedInputMethodValues( |
| 243 const std::string& pref, | 249 const std::string& pref, |
| 244 const std::string& synced_pref, | 250 const std::string& synced_pref, |
| 245 const char* pref_name) { | 251 const char* pref_name) { |
| 246 std::vector<std::string> old_tokens; | 252 std::vector<std::string> old_tokens = |
| 247 std::vector<std::string> new_tokens; | 253 base::SplitString(pref, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 248 base::SplitString(pref, ',', &old_tokens); | 254 std::vector<std::string> new_tokens = base::SplitString( |
| 249 base::SplitString(synced_pref, ',', &new_tokens); | 255 synced_pref, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 250 | 256 |
| 251 // Check and convert the new tokens. | 257 // Check and convert the new tokens. |
| 252 if (pref_name == prefs::kLanguagePreloadEngines || | 258 if (pref_name == prefs::kLanguagePreloadEngines || |
| 253 pref_name == prefs::kLanguageEnabledExtensionImes) { | 259 pref_name == prefs::kLanguageEnabledExtensionImes) { |
| 254 input_method::InputMethodManager* manager = | 260 input_method::InputMethodManager* manager = |
| 255 input_method::InputMethodManager::Get(); | 261 input_method::InputMethodManager::Get(); |
| 256 scoped_ptr<input_method::InputMethodDescriptors> supported_descriptors; | 262 scoped_ptr<input_method::InputMethodDescriptors> supported_descriptors; |
| 257 | 263 |
| 258 if (pref_name == prefs::kLanguagePreloadEngines) { | 264 if (pref_name == prefs::kLanguagePreloadEngines) { |
| 259 // Set the known input methods. | 265 // Set the known input methods. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 if (merging_ || prefs_->GetBoolean(prefs::kLanguageShouldMergeInputMethods)) | 306 if (merging_ || prefs_->GetBoolean(prefs::kLanguageShouldMergeInputMethods)) |
| 301 return; | 307 return; |
| 302 | 308 |
| 303 // Set the language and input prefs at the same time. Otherwise we may, | 309 // Set the language and input prefs at the same time. Otherwise we may, |
| 304 // e.g., use a stale languages setting but push a new preload engines setting. | 310 // e.g., use a stale languages setting but push a new preload engines setting. |
| 305 preferred_languages_syncable_.SetValue(preferred_languages_.GetValue()); | 311 preferred_languages_syncable_.SetValue(preferred_languages_.GetValue()); |
| 306 enabled_extension_imes_syncable_.SetValue(enabled_extension_imes_.GetValue()); | 312 enabled_extension_imes_syncable_.SetValue(enabled_extension_imes_.GetValue()); |
| 307 | 313 |
| 308 // For preload engines, use legacy xkb IDs so the preference can sync | 314 // For preload engines, use legacy xkb IDs so the preference can sync |
| 309 // across Chrome OS and Chromium OS. | 315 // across Chrome OS and Chromium OS. |
| 310 std::vector<std::string> engines; | 316 std::vector<std::string> engines = |
| 311 base::SplitString(preload_engines_.GetValue(), ',', &engines); | 317 base::SplitString(preload_engines_.GetValue(), ",", base::TRIM_WHITESPACE, |
| 318 base::SPLIT_WANT_ALL); |
| 312 std::transform(engines.begin(), engines.end(), engines.begin(), | 319 std::transform(engines.begin(), engines.end(), engines.begin(), |
| 313 extension_ime_util::GetComponentIDByInputMethodID); | 320 extension_ime_util::GetComponentIDByInputMethodID); |
| 314 preload_engines_syncable_.SetValue(base::JoinString(engines, ",")); | 321 preload_engines_syncable_.SetValue(base::JoinString(engines, ",")); |
| 315 } | 322 } |
| 316 | 323 |
| 317 void InputMethodSyncer::OnIsSyncingChanged() { | 324 void InputMethodSyncer::OnIsSyncingChanged() { |
| 318 if (prefs_->GetBoolean(prefs::kLanguageShouldMergeInputMethods) && | 325 if (prefs_->GetBoolean(prefs::kLanguageShouldMergeInputMethods) && |
| 319 prefs_->IsSyncing()) { | 326 prefs_->IsSyncing()) { |
| 320 MergeSyncedPrefs(); | 327 MergeSyncedPrefs(); |
| 321 } | 328 } |
| 322 } | 329 } |
| 323 | 330 |
| 324 } // namespace input_method | 331 } // namespace input_method |
| 325 } // namespace chromeos | 332 } // namespace chromeos |
| OLD | NEW |