| 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 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/strings/string_piece.h" |
| 12 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
| 13 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 14 #include "base/task_runner.h" | 15 #include "base/task_runner.h" |
| 15 #include "base/task_scheduler/post_task.h" | 16 #include "base/task_scheduler/post_task.h" |
| 16 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
| 17 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
| 18 #include "components/pref_registry/pref_registry_syncable.h" | 19 #include "components/pref_registry/pref_registry_syncable.h" |
| 19 #include "components/sync_preferences/pref_service_syncable.h" | 20 #include "components/sync_preferences/pref_service_syncable.h" |
| 20 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 21 #include "ui/base/ime/chromeos/component_extension_ime_manager.h" | 22 #include "ui/base/ime/chromeos/component_extension_ime_manager.h" |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 continue; | 89 continue; |
| 89 } | 90 } |
| 90 } | 91 } |
| 91 value_iter = values.erase(value_iter); | 92 value_iter = values.erase(value_iter); |
| 92 } | 93 } |
| 93 | 94 |
| 94 return base::JoinString(values, ","); | 95 return base::JoinString(values, ","); |
| 95 } | 96 } |
| 96 | 97 |
| 97 // Appends tokens from |src| that are not in |dest| to |dest|. | 98 // Appends tokens from |src| that are not in |dest| to |dest|. |
| 98 void MergeLists(std::vector<std::string>* dest, | 99 void MergeLists(std::vector<base::StringPiece>* dest, |
| 99 const std::vector<std::string>& src) { | 100 const std::vector<base::StringPiece>& src) { |
| 100 // Keep track of already-added tokens. | 101 // Keep track of already-added tokens. |
| 101 std::set<std::string> unique_tokens(dest->begin(), dest->end()); | 102 std::set<base::StringPiece> unique_tokens(dest->begin(), dest->end()); |
| 102 | 103 |
| 103 for (const std::string& token : src) { | 104 for (auto token : src) { |
| 104 // Skip token if it's already in |dest|. | 105 // Skip token if it's already in |dest|. |
| 105 if (binary_search(unique_tokens.begin(), unique_tokens.end(), token)) | 106 if (binary_search(unique_tokens.begin(), unique_tokens.end(), token)) |
| 106 continue; | 107 continue; |
| 107 dest->push_back(token); | 108 dest->push_back(token); |
| 108 unique_tokens.insert(token); | 109 unique_tokens.insert(token); |
| 109 } | 110 } |
| 110 } | 111 } |
| 111 | 112 |
| 112 } // anonymous namespace | 113 } // anonymous namespace |
| 113 | 114 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 MergeSyncedPrefs(); | 172 MergeSyncedPrefs(); |
| 172 } | 173 } |
| 173 } | 174 } |
| 174 | 175 |
| 175 void InputMethodSyncer::MergeSyncedPrefs() { | 176 void InputMethodSyncer::MergeSyncedPrefs() { |
| 176 // This should only be done after the first ever sync. | 177 // This should only be done after the first ever sync. |
| 177 DCHECK(prefs_->GetBoolean(prefs::kLanguageShouldMergeInputMethods)); | 178 DCHECK(prefs_->GetBoolean(prefs::kLanguageShouldMergeInputMethods)); |
| 178 prefs_->SetBoolean(prefs::kLanguageShouldMergeInputMethods, false); | 179 prefs_->SetBoolean(prefs::kLanguageShouldMergeInputMethods, false); |
| 179 merging_ = true; | 180 merging_ = true; |
| 180 | 181 |
| 182 std::vector<base::StringPiece> synced_tokens; |
| 183 std::vector<base::StringPiece> new_tokens; |
| 184 |
| 185 // First, set the syncable prefs to the union of the local and synced prefs. |
| 181 std::string preferred_languages_syncable = | 186 std::string preferred_languages_syncable = |
| 182 preferred_languages_syncable_.GetValue(); | 187 preferred_languages_syncable_.GetValue(); |
| 183 std::string preload_engines_syncable = | |
| 184 preload_engines_syncable_.GetValue(); | |
| 185 std::string enabled_extension_imes_syncable = | |
| 186 enabled_extension_imes_syncable_.GetValue(); | |
| 187 | |
| 188 std::vector<std::string> synced_tokens; | |
| 189 std::vector<std::string> new_tokens; | |
| 190 | |
| 191 // First, set the syncable prefs to the union of the local and synced prefs. | |
| 192 synced_tokens = | 188 synced_tokens = |
| 193 base::SplitString(preferred_languages_syncable_.GetValue(), ",", | 189 base::SplitStringPiece(preferred_languages_syncable, ",", |
| 194 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 190 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 195 new_tokens = base::SplitString(preferred_languages_.GetValue(), ",", | 191 std::string preferred_languages = preferred_languages_.GetValue(); |
| 196 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 192 new_tokens = base::SplitStringPiece( |
| 193 preferred_languages, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 197 | 194 |
| 198 // Append the synced values to the current values. | 195 // Append the synced values to the current values. |
| 199 MergeLists(&new_tokens, synced_tokens); | 196 MergeLists(&new_tokens, synced_tokens); |
| 200 preferred_languages_syncable_.SetValue(base::JoinString(new_tokens, ",")); | 197 preferred_languages_syncable_.SetValue(base::JoinString(new_tokens, ",")); |
| 201 | 198 |
| 199 std::string enabled_extension_imes_syncable = |
| 200 enabled_extension_imes_syncable_.GetValue(); |
| 202 synced_tokens = | 201 synced_tokens = |
| 203 base::SplitString(enabled_extension_imes_syncable_.GetValue(), ",", | 202 base::SplitStringPiece(enabled_extension_imes_syncable, ",", |
| 204 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 203 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 205 new_tokens = base::SplitString(enabled_extension_imes_.GetValue(), ",", | 204 std::string enabled_extension_imes = enabled_extension_imes_.GetValue(); |
| 206 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 205 new_tokens = base::SplitStringPiece( |
| 206 enabled_extension_imes, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 207 | 207 |
| 208 MergeLists(&new_tokens, synced_tokens); | 208 MergeLists(&new_tokens, synced_tokens); |
| 209 enabled_extension_imes_syncable_.SetValue(base::JoinString(new_tokens, ",")); | 209 enabled_extension_imes_syncable_.SetValue(base::JoinString(new_tokens, ",")); |
| 210 | 210 |
| 211 // Revert preload engines to legacy component IDs. | 211 // Revert preload engines to legacy component IDs. |
| 212 new_tokens = base::SplitString(preload_engines_.GetValue(), ",", | 212 std::string preload_engines = preload_engines_.GetValue(); |
| 213 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 213 new_tokens = base::SplitStringPiece( |
| 214 preload_engines, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 214 std::transform(new_tokens.begin(), new_tokens.end(), new_tokens.begin(), | 215 std::transform(new_tokens.begin(), new_tokens.end(), new_tokens.begin(), |
| 215 extension_ime_util::GetComponentIDByInputMethodID); | 216 extension_ime_util::GetComponentIDByInputMethodID); |
| 217 std::string preload_engines_syncable = preload_engines_syncable_.GetValue(); |
| 216 synced_tokens = | 218 synced_tokens = |
| 217 base::SplitString(preload_engines_syncable_.GetValue(), ",", | 219 base::SplitStringPiece(preload_engines_syncable, ",", |
| 218 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 220 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 219 | 221 |
| 220 MergeLists(&new_tokens, synced_tokens); | 222 MergeLists(&new_tokens, synced_tokens); |
| 221 preload_engines_syncable_.SetValue(base::JoinString(new_tokens, ",")); | 223 preload_engines_syncable_.SetValue(base::JoinString(new_tokens, ",")); |
| 222 | 224 |
| 223 // 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. |
| 224 preload_engines_.SetValue( | 226 preload_engines_.SetValue( |
| 225 AddSupportedInputMethodValues(preload_engines_.GetValue(), | 227 AddSupportedInputMethodValues(preload_engines_.GetValue(), |
| 226 preload_engines_syncable, | 228 preload_engines_syncable, |
| 227 prefs::kLanguagePreloadEngines)); | 229 prefs::kLanguagePreloadEngines)); |
| 228 enabled_extension_imes_.SetValue( | 230 enabled_extension_imes_.SetValue( |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 | 322 |
| 321 void InputMethodSyncer::OnIsSyncingChanged() { | 323 void InputMethodSyncer::OnIsSyncingChanged() { |
| 322 if (prefs_->GetBoolean(prefs::kLanguageShouldMergeInputMethods) && | 324 if (prefs_->GetBoolean(prefs::kLanguageShouldMergeInputMethods) && |
| 323 prefs_->IsSyncing()) { | 325 prefs_->IsSyncing()) { |
| 324 MergeSyncedPrefs(); | 326 MergeSyncedPrefs(); |
| 325 } | 327 } |
| 326 } | 328 } |
| 327 | 329 |
| 328 } // namespace input_method | 330 } // namespace input_method |
| 329 } // namespace chromeos | 331 } // namespace chromeos |
| OLD | NEW |