Chromium Code Reviews| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <functional> | 8 #include <functional> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/files/important_file_writer.h" | 12 #include "base/files/important_file_writer.h" |
| 13 #include "base/md5.h" | 13 #include "base/md5.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/string_split.h" | 15 #include "base/strings/string_split.h" |
| 16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 17 #include "chrome/browser/spellchecker/set_difference_container.h" | |
| 17 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" | 18 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" |
| 18 #include "chrome/common/chrome_constants.h" | 19 #include "chrome/common/chrome_constants.h" |
| 19 #include "chrome/common/spellcheck_common.h" | 20 #include "chrome/common/spellcheck_common.h" |
| 20 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 21 #include "sync/api/sync_change.h" | 22 #include "sync/api/sync_change.h" |
| 22 #include "sync/api/sync_error_factory.h" | 23 #include "sync/api/sync_error_factory.h" |
| 23 #include "sync/protocol/sync.pb.h" | 24 #include "sync/protocol/sync.pb.h" |
| 24 | 25 |
| 25 using content::BrowserThread; | 26 using content::BrowserThread; |
| 26 | 27 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 89 base::TrimWhitespaceASCII(word, base::TRIM_ALL, &tmp); | 90 base::TrimWhitespaceASCII(word, base::TRIM_ALL, &tmp); |
| 90 } | 91 } |
| 91 | 92 |
| 92 // Removes duplicate and invalid words from |to_add| word list. Looks for | 93 // Removes duplicate and invalid words from |to_add| word list. Looks for |
| 93 // duplicates in both |to_add| and |existing| word lists. Returns a bitmap of | 94 // duplicates in both |to_add| and |existing| word lists. Returns a bitmap of |
| 94 // |ChangeSanitationResult| values. | 95 // |ChangeSanitationResult| values. |
| 95 int SanitizeWordsToAdd(const std::set<std::string>& existing, | 96 int SanitizeWordsToAdd(const std::set<std::string>& existing, |
| 96 std::set<std::string>* to_add) { | 97 std::set<std::string>* to_add) { |
| 97 DCHECK(to_add); | 98 DCHECK(to_add); |
| 98 // Do not add duplicate words. | 99 // Do not add duplicate words. |
| 99 std::set<std::string> new_words = | 100 spellcheck::set_difference_container<std::set<std::string>, |
| 100 base::STLSetDifference<std::set<std::string>>(*to_add, existing); | 101 std::set<std::string> > |
|
please use gerrit instead
2016/02/03 23:59:11
No space inside of ">>".
Kevin Bailey
2016/02/04 16:34:12
Done.
| |
| 102 new_words(*to_add, existing); | |
| 101 int result = VALID_CHANGE; | 103 int result = VALID_CHANGE; |
| 102 if (to_add->size() != new_words.size()) | 104 unsigned new_words_size = 0; |
|
please use gerrit instead
2016/02/03 23:59:11
size_t, since we're comparing it to std::set::size
Kevin Bailey
2016/02/04 16:34:12
If true, the unsigned would be promoted. I think y
groby-ooo-7-16
2016/02/04 19:38:36
We're trying to avoid implicit promotions.
Quoth
| |
| 103 result |= DETECTED_DUPLICATE_WORDS; | |
| 104 // Do not add invalid words. | 105 // Do not add invalid words. |
| 105 std::set<std::string> valid_new_words; | 106 std::set<std::string> valid_new_words; |
| 106 for (const std::string& word : new_words) { | 107 for (const std::string& word : new_words) { |
| 108 ++new_words_size; | |
| 107 if (IsValidWord(word)) | 109 if (IsValidWord(word)) |
| 108 valid_new_words.insert(valid_new_words.end(), word); | 110 valid_new_words.insert(valid_new_words.end(), word); |
| 109 } | 111 } |
| 110 if (valid_new_words.size() != new_words.size()) | 112 if (to_add->size() != new_words_size) |
| 113 result |= DETECTED_DUPLICATE_WORDS; | |
| 114 if (valid_new_words.size() != new_words_size) | |
| 111 result |= DETECTED_INVALID_WORDS; | 115 result |= DETECTED_INVALID_WORDS; |
| 112 // Save the sanitized words to be added. | 116 // Save the sanitized words to be added. |
| 113 std::swap(*to_add, valid_new_words); | 117 std::swap(*to_add, valid_new_words); |
| 114 return result; | 118 return result; |
| 115 } | 119 } |
| 116 | 120 |
| 117 // Loads and returns the custom spellcheck dictionary from |path|. Must be | 121 // Loads and returns the custom spellcheck dictionary from |path|. Must be |
| 118 // called on the file thread. | 122 // called on the file thread. |
| 119 scoped_ptr<SpellcheckCustomDictionary::LoadFileResult> | 123 scoped_ptr<SpellcheckCustomDictionary::LoadFileResult> |
| 120 LoadDictionaryFileReliably(const base::FilePath& path) { | 124 LoadDictionaryFileReliably(const base::FilePath& path) { |
| (...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 438 fix_invalid_file_.callback()); | 442 fix_invalid_file_.callback()); |
| 439 } | 443 } |
| 440 } | 444 } |
| 441 | 445 |
| 442 void SpellcheckCustomDictionary::Apply(const Change& dictionary_change) { | 446 void SpellcheckCustomDictionary::Apply(const Change& dictionary_change) { |
| 443 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 447 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 444 if (!dictionary_change.to_add().empty()) { | 448 if (!dictionary_change.to_add().empty()) { |
| 445 words_.insert(dictionary_change.to_add().begin(), | 449 words_.insert(dictionary_change.to_add().begin(), |
| 446 dictionary_change.to_add().end()); | 450 dictionary_change.to_add().end()); |
| 447 } | 451 } |
| 448 if (!dictionary_change.to_remove().empty()) { | 452 for (const auto& word : dictionary_change.to_remove()) |
| 449 std::set<std::string> updated_words = | 453 words_.erase(word); |
|
please use gerrit instead
2016/02/03 23:59:11
Let's not switch this algorithm, because it's O(n*
Kevin Bailey
2016/02/04 16:34:12
The old one is very O(n ln(n)). :) Consider:
line
groby-ooo-7-16
2016/02/04 19:38:36
Even if it were an O(n) vs O(n ln n) difference -
| |
| 450 base::STLSetDifference<std::set<std::string>>( | |
| 451 words_, dictionary_change.to_remove()); | |
| 452 std::swap(words_, updated_words); | |
| 453 } | |
| 454 } | 454 } |
| 455 | 455 |
| 456 void SpellcheckCustomDictionary::FixInvalidFile( | 456 void SpellcheckCustomDictionary::FixInvalidFile( |
| 457 scoped_ptr<LoadFileResult> load_file_result) { | 457 scoped_ptr<LoadFileResult> load_file_result) { |
| 458 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 458 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 459 BrowserThread::PostTask( | 459 BrowserThread::PostTask( |
| 460 BrowserThread::FILE, FROM_HERE, | 460 BrowserThread::FILE, FROM_HERE, |
| 461 base::Bind(&SavePassedWordsToDictionaryFileReliably, | 461 base::Bind(&SavePassedWordsToDictionaryFileReliably, |
| 462 custom_dictionary_path_, base::Passed(&load_file_result))); | 462 custom_dictionary_path_, base::Passed(&load_file_result))); |
| 463 } | 463 } |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 526 } | 526 } |
| 527 | 527 |
| 528 void SpellcheckCustomDictionary::Notify(const Change& dictionary_change) { | 528 void SpellcheckCustomDictionary::Notify(const Change& dictionary_change) { |
| 529 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 529 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 530 if (!IsLoaded() || dictionary_change.empty()) | 530 if (!IsLoaded() || dictionary_change.empty()) |
| 531 return; | 531 return; |
| 532 FOR_EACH_OBSERVER(Observer, | 532 FOR_EACH_OBSERVER(Observer, |
| 533 observers_, | 533 observers_, |
| 534 OnCustomDictionaryChanged(dictionary_change)); | 534 OnCustomDictionaryChanged(dictionary_change)); |
| 535 } | 535 } |
| OLD | NEW |