| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/spellchecker/spellcheck_custom_dictionary.h" | 5 #include "chrome/browser/spellchecker/spellcheck_custom_dictionary.h" |
| 6 | 6 |
| 7 #include <functional> | 7 #include <functional> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/files/important_file_writer.h" | 10 #include "base/files/important_file_writer.h" |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 base::CopyFile(path, path.AddExtension(BACKUP_EXTENSION)); | 122 base::CopyFile(path, path.AddExtension(BACKUP_EXTENSION)); |
| 123 base::ImportantFileWriter::WriteFileAtomically(path, content.str()); | 123 base::ImportantFileWriter::WriteFileAtomically(path, content.str()); |
| 124 } | 124 } |
| 125 | 125 |
| 126 // Removes duplicate and invalid words from |to_add| word list and sorts it. | 126 // Removes duplicate and invalid words from |to_add| word list and sorts it. |
| 127 // Looks for duplicates in both |to_add| and |existing| word lists. Returns a | 127 // Looks for duplicates in both |to_add| and |existing| word lists. Returns a |
| 128 // bitmap of |ChangeSanitationResult| values. | 128 // bitmap of |ChangeSanitationResult| values. |
| 129 int SanitizeWordsToAdd(const WordSet& existing, WordList& to_add) { | 129 int SanitizeWordsToAdd(const WordSet& existing, WordList& to_add) { |
| 130 // Do not add duplicate words. | 130 // Do not add duplicate words. |
| 131 std::sort(to_add.begin(), to_add.end()); | 131 std::sort(to_add.begin(), to_add.end()); |
| 132 WordList new_words; | 132 WordList new_words = base::STLSetDifference<WordList>(to_add, existing); |
| 133 std::set_difference(to_add.begin(), | |
| 134 to_add.end(), | |
| 135 existing.begin(), | |
| 136 existing.end(), | |
| 137 std::back_inserter(new_words)); | |
| 138 new_words.erase(std::unique(new_words.begin(), new_words.end()), | 133 new_words.erase(std::unique(new_words.begin(), new_words.end()), |
| 139 new_words.end()); | 134 new_words.end()); |
| 140 int result = VALID_CHANGE; | 135 int result = VALID_CHANGE; |
| 141 if (to_add.size() != new_words.size()) | 136 if (to_add.size() != new_words.size()) |
| 142 result |= DETECTED_DUPLICATE_WORDS; | 137 result |= DETECTED_DUPLICATE_WORDS; |
| 143 // Do not add invalid words. | 138 // Do not add invalid words. |
| 144 size_t size = new_words.size(); | 139 size_t size = new_words.size(); |
| 145 new_words.erase(std::remove_if(new_words.begin(), | 140 new_words.erase(std::remove_if(new_words.begin(), |
| 146 new_words.end(), | 141 new_words.end(), |
| 147 IsInvalidWord), | 142 IsInvalidWord), |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 | 316 |
| 322 // Add remote words locally. | 317 // Add remote words locally. |
| 323 Change to_change_locally(to_add_locally); | 318 Change to_change_locally(to_add_locally); |
| 324 to_change_locally.Sanitize(GetWords()); | 319 to_change_locally.Sanitize(GetWords()); |
| 325 Apply(to_change_locally); | 320 Apply(to_change_locally); |
| 326 Notify(to_change_locally); | 321 Notify(to_change_locally); |
| 327 Save(to_change_locally); | 322 Save(to_change_locally); |
| 328 | 323 |
| 329 // Add as many as possible local words remotely. | 324 // Add as many as possible local words remotely. |
| 330 std::sort(to_add_locally.begin(), to_add_locally.end()); | 325 std::sort(to_add_locally.begin(), to_add_locally.end()); |
| 331 WordList to_add_remotely; | 326 WordList to_add_remotely = base::STLSetDifference<WordList>(words_, |
| 332 std::set_difference(words_.begin(), | 327 to_add_locally); |
| 333 words_.end(), | |
| 334 to_add_locally.begin(), | |
| 335 to_add_locally.end(), | |
| 336 std::back_inserter(to_add_remotely)); | |
| 337 | 328 |
| 338 // Send local changes to the sync server. | 329 // Send local changes to the sync server. |
| 339 Change to_change_remotely(to_add_remotely); | 330 Change to_change_remotely(to_add_remotely); |
| 340 syncer::SyncMergeResult result(type); | 331 syncer::SyncMergeResult result(type); |
| 341 result.set_error(Sync(to_change_remotely)); | 332 result.set_error(Sync(to_change_remotely)); |
| 342 return result; | 333 return result; |
| 343 } | 334 } |
| 344 | 335 |
| 345 void SpellcheckCustomDictionary::StopSyncing(syncer::ModelType type) { | 336 void SpellcheckCustomDictionary::StopSyncing(syncer::ModelType type) { |
| 346 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 337 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 WordList custom_words; | 415 WordList custom_words; |
| 425 LoadDictionaryFileReliably(custom_words, path); | 416 LoadDictionaryFileReliably(custom_words, path); |
| 426 | 417 |
| 427 // Add words. | 418 // Add words. |
| 428 custom_words.insert(custom_words.end(), | 419 custom_words.insert(custom_words.end(), |
| 429 dictionary_change.to_add().begin(), | 420 dictionary_change.to_add().begin(), |
| 430 dictionary_change.to_add().end()); | 421 dictionary_change.to_add().end()); |
| 431 | 422 |
| 432 // Remove words. | 423 // Remove words. |
| 433 std::sort(custom_words.begin(), custom_words.end()); | 424 std::sort(custom_words.begin(), custom_words.end()); |
| 434 WordList remaining; | 425 WordList remaining = |
| 435 std::set_difference(custom_words.begin(), | 426 base::STLSetDifference<WordList>(custom_words, |
| 436 custom_words.end(), | 427 dictionary_change.to_remove()); |
| 437 dictionary_change.to_remove().begin(), | |
| 438 dictionary_change.to_remove().end(), | |
| 439 std::back_inserter(remaining)); | |
| 440 std::swap(custom_words, remaining); | 428 std::swap(custom_words, remaining); |
| 441 | 429 |
| 442 SaveDictionaryFileReliably(custom_words, path); | 430 SaveDictionaryFileReliably(custom_words, path); |
| 443 } | 431 } |
| 444 | 432 |
| 445 void SpellcheckCustomDictionary::OnLoaded(WordList custom_words) { | 433 void SpellcheckCustomDictionary::OnLoaded(WordList custom_words) { |
| 446 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 434 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 447 Change dictionary_change(custom_words); | 435 Change dictionary_change(custom_words); |
| 448 dictionary_change.Sanitize(GetWords()); | 436 dictionary_change.Sanitize(GetWords()); |
| 449 Apply(dictionary_change); | 437 Apply(dictionary_change); |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 542 | 530 |
| 543 void SpellcheckCustomDictionary::Notify( | 531 void SpellcheckCustomDictionary::Notify( |
| 544 const SpellcheckCustomDictionary::Change& dictionary_change) { | 532 const SpellcheckCustomDictionary::Change& dictionary_change) { |
| 545 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 533 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 546 if (!IsLoaded() || dictionary_change.empty()) | 534 if (!IsLoaded() || dictionary_change.empty()) |
| 547 return; | 535 return; |
| 548 FOR_EACH_OBSERVER(Observer, | 536 FOR_EACH_OBSERVER(Observer, |
| 549 observers_, | 537 observers_, |
| 550 OnCustomDictionaryChanged(dictionary_change)); | 538 OnCustomDictionaryChanged(dictionary_change)); |
| 551 } | 539 } |
| OLD | NEW |