Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(313)

Side by Side Diff: chrome/browser/chromeos/input_method/input_method_syncer.cc

Issue 2695883003: Change uses of base::JoinString to pass StringPieces where possible. (Closed)
Patch Set: Remove dependency CL. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 (const 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
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 std::vector<std::string> new_token_values;
214 std::transform(new_tokens.begin(), new_tokens.end(), new_tokens.begin(), 214 new_token_values = base::SplitString(
215 preload_engines, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
216 std::transform(new_token_values.begin(), new_token_values.end(),
217 new_token_values.begin(),
215 extension_ime_util::GetComponentIDByInputMethodID); 218 extension_ime_util::GetComponentIDByInputMethodID);
219 std::string preload_engines_syncable = preload_engines_syncable_.GetValue();
216 synced_tokens = 220 synced_tokens =
217 base::SplitString(preload_engines_syncable_.GetValue(), ",", 221 base::SplitStringPiece(preload_engines_syncable, ",",
218 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); 222 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
219 223
224 new_tokens = std::vector<base::StringPiece>(new_token_values.begin(),
225 new_token_values.end());
220 MergeLists(&new_tokens, synced_tokens); 226 MergeLists(&new_tokens, synced_tokens);
221 preload_engines_syncable_.SetValue(base::JoinString(new_tokens, ",")); 227 preload_engines_syncable_.SetValue(base::JoinString(new_tokens, ","));
222 228
223 // Second, set the local prefs, incorporating new values from the sync server. 229 // Second, set the local prefs, incorporating new values from the sync server.
224 preload_engines_.SetValue( 230 preload_engines_.SetValue(
225 AddSupportedInputMethodValues(preload_engines_.GetValue(), 231 AddSupportedInputMethodValues(preload_engines_.GetValue(),
226 preload_engines_syncable, 232 preload_engines_syncable,
227 prefs::kLanguagePreloadEngines)); 233 prefs::kLanguagePreloadEngines));
228 enabled_extension_imes_.SetValue( 234 enabled_extension_imes_.SetValue(
229 AddSupportedInputMethodValues(enabled_extension_imes_.GetValue(), 235 AddSupportedInputMethodValues(enabled_extension_imes_.GetValue(),
230 enabled_extension_imes_syncable, 236 enabled_extension_imes_syncable,
231 prefs::kLanguageEnabledExtensionImes)); 237 prefs::kLanguageEnabledExtensionImes));
232 238
233 // Remove unsupported locales before updating the local languages preference. 239 // Remove unsupported locales before updating the local languages preference.
234 std::string languages( 240 std::string languages(
235 AddSupportedInputMethodValues(preferred_languages_.GetValue(), 241 AddSupportedInputMethodValues(preferred_languages_.GetValue(),
236 preferred_languages_syncable, 242 preferred_languages_syncable,
237 prefs::kLanguagePreferredLanguages)); 243 prefs::kLanguagePreferredLanguages));
238 base::PostTaskWithTraitsAndReplyWithResult( 244 base::PostTaskWithTraitsAndReplyWithResult(
239 FROM_HERE, base::TaskTraits().MayBlock().WithPriority( 245 FROM_HERE, base::TaskTraits().MayBlock().WithPriority(
240 base::TaskPriority::BACKGROUND), 246 base::TaskPriority::BACKGROUND),
241 base::Bind(&CheckAndResolveLocales, languages), 247 base::Bind(&CheckAndResolveLocales, languages),
242 base::Bind(&InputMethodSyncer::FinishMerge, weak_factory_.GetWeakPtr())); 248 base::Bind(&InputMethodSyncer::FinishMerge, weak_factory_.GetWeakPtr()));
243 } 249 }
244 250
245 std::string InputMethodSyncer::AddSupportedInputMethodValues( 251 std::string InputMethodSyncer::AddSupportedInputMethodValues(
246 const std::string& pref, 252 const std::string& pref,
247 const std::string& synced_pref, 253 const std::string& synced_pref,
248 const char* pref_name) { 254 const char* pref_name) {
249 std::vector<std::string> old_tokens = 255 std::vector<base::StringPiece> old_tokens = base::SplitStringPiece(
250 base::SplitString(pref, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); 256 pref, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
251 std::vector<std::string> new_tokens = base::SplitString( 257 std::vector<std::string> new_token_values = base::SplitString(
252 synced_pref, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); 258 synced_pref, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
253 259
254 // Check and convert the new tokens. 260 // Check and convert the new tokens.
255 if (pref_name == prefs::kLanguagePreloadEngines || 261 if (pref_name == prefs::kLanguagePreloadEngines ||
256 pref_name == prefs::kLanguageEnabledExtensionImes) { 262 pref_name == prefs::kLanguageEnabledExtensionImes) {
257 input_method::InputMethodManager* manager = 263 input_method::InputMethodManager* manager =
258 input_method::InputMethodManager::Get(); 264 input_method::InputMethodManager::Get();
259 std::unique_ptr<input_method::InputMethodDescriptors> supported_descriptors; 265 std::unique_ptr<input_method::InputMethodDescriptors> supported_descriptors;
260 266
261 if (pref_name == prefs::kLanguagePreloadEngines) { 267 if (pref_name == prefs::kLanguagePreloadEngines) {
262 // Set the known input methods. 268 // Set the known input methods.
263 supported_descriptors = manager->GetSupportedInputMethods(); 269 supported_descriptors = manager->GetSupportedInputMethods();
264 // Add the available component extension IMEs. 270 // Add the available component extension IMEs.
265 ComponentExtensionIMEManager* component_extension_manager = 271 ComponentExtensionIMEManager* component_extension_manager =
266 manager->GetComponentExtensionIMEManager(); 272 manager->GetComponentExtensionIMEManager();
267 input_method::InputMethodDescriptors component_descriptors = 273 input_method::InputMethodDescriptors component_descriptors =
268 component_extension_manager->GetAllIMEAsInputMethodDescriptor(); 274 component_extension_manager->GetAllIMEAsInputMethodDescriptor();
269 supported_descriptors->insert(supported_descriptors->end(), 275 supported_descriptors->insert(supported_descriptors->end(),
270 component_descriptors.begin(), 276 component_descriptors.begin(),
271 component_descriptors.end()); 277 component_descriptors.end());
272 } else { 278 } else {
273 supported_descriptors.reset(new input_method::InputMethodDescriptors); 279 supported_descriptors.reset(new input_method::InputMethodDescriptors);
274 ime_state_->GetInputMethodExtensions(supported_descriptors.get()); 280 ime_state_->GetInputMethodExtensions(supported_descriptors.get());
275 } 281 }
276 CheckAndResolveInputMethodIDs(*supported_descriptors, &new_tokens); 282 CheckAndResolveInputMethodIDs(*supported_descriptors, &new_token_values);
277 } else if (pref_name != prefs::kLanguagePreferredLanguages) { 283 } else if (pref_name != prefs::kLanguagePreferredLanguages) {
278 NOTREACHED() << "Attempting to merge an invalid preference."; 284 NOTREACHED() << "Attempting to merge an invalid preference.";
279 // kLanguagePreferredLanguages is checked in CheckAndResolveLocales(). 285 // kLanguagePreferredLanguages is checked in CheckAndResolveLocales().
280 } 286 }
281 287
282 // Do the actual merging. 288 // Do the actual merging.
289 std::vector<base::StringPiece> new_tokens(new_token_values.begin(),
290 new_token_values.end());
283 MergeLists(&old_tokens, new_tokens); 291 MergeLists(&old_tokens, new_tokens);
284 return base::JoinString(old_tokens, ","); 292 return base::JoinString(old_tokens, ",");
285 } 293 }
286 294
287 void InputMethodSyncer::FinishMerge(const std::string& languages) { 295 void InputMethodSyncer::FinishMerge(const std::string& languages) {
288 // Since the merge only removed locales that are unsupported on this system, 296 // 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 297 // 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 298 // 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. 299 // the values from this device should then become the new defaults anyway.
292 preferred_languages_.SetValue(languages); 300 preferred_languages_.SetValue(languages);
(...skipping 27 matching lines...) Expand all
320 328
321 void InputMethodSyncer::OnIsSyncingChanged() { 329 void InputMethodSyncer::OnIsSyncingChanged() {
322 if (prefs_->GetBoolean(prefs::kLanguageShouldMergeInputMethods) && 330 if (prefs_->GetBoolean(prefs::kLanguageShouldMergeInputMethods) &&
323 prefs_->IsSyncing()) { 331 prefs_->IsSyncing()) {
324 MergeSyncedPrefs(); 332 MergeSyncedPrefs();
325 } 333 }
326 } 334 }
327 335
328 } // namespace input_method 336 } // namespace input_method
329 } // namespace chromeos 337 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698