summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrouslan@chromium.org <rouslan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-11-30 21:16:59 +0000
committerrouslan@chromium.org <rouslan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-11-30 21:16:59 +0000
commit82e58675445b75ab7dd0a39f70a990af61479743 (patch)
tree453c9d752d53eea5ce3f1df987c2d4086a7a9eb0
parent4c58eac90cfc27e21a276c820f3280a96e69d903 (diff)
downloadchromium_src-82e58675445b75ab7dd0a39f70a990af61479743.zip
chromium_src-82e58675445b75ab7dd0a39f70a990af61479743.tar.gz
chromium_src-82e58675445b75ab7dd0a39f70a990af61479743.tar.bz2
Remove duplicate words from custom spelling dictionary on load
BUG=51636 Review URL: https://chromiumcodereview.appspot.com/11434043 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@170540 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/spellchecker/spellcheck_custom_dictionary.cc22
-rw-r--r--chrome/browser/spellchecker/spellcheck_custom_dictionary.h5
-rw-r--r--chrome/browser/spellchecker/spellcheck_custom_dictionary_unittest.cc3
-rw-r--r--chrome/browser/ui/webui/options/language_dictionary_overlay_handler.cc10
4 files changed, 28 insertions, 12 deletions
diff --git a/chrome/browser/spellchecker/spellcheck_custom_dictionary.cc b/chrome/browser/spellchecker/spellcheck_custom_dictionary.cc
index f9e29414..4510332 100644
--- a/chrome/browser/spellchecker/spellcheck_custom_dictionary.cc
+++ b/chrome/browser/spellchecker/spellcheck_custom_dictionary.cc
@@ -57,9 +57,27 @@ void SpellcheckCustomDictionary::LoadDictionaryIntoCustomWordList(
}
base::SplitString(contents, '\n', custom_words);
+
+ // Erase duplicates.
+ std::sort(custom_words->begin(), custom_words->end());
+ custom_words->erase(std::unique(custom_words->begin(), custom_words->end()),
+ custom_words->end());
+
// Clear out empty words.
custom_words->erase(remove_if(custom_words->begin(), custom_words->end(),
mem_fun_ref(&std::string::empty)), custom_words->end());
+
+ // Write out the clean file.
+ std::stringstream ss;
+ for (WordList::iterator it = custom_words->begin();
+ it != custom_words->end();
+ ++it) {
+ ss << *it << '\n';
+ }
+ contents = ss.str();
+ file_util::WriteFile(custom_dictionary_path_,
+ contents.c_str(),
+ contents.length());
}
void SpellcheckCustomDictionary::SetCustomWordList(WordList* custom_words) {
@@ -170,8 +188,8 @@ void SpellcheckCustomDictionary::EraseWordFromCustomDictionary(
WordList custom_words;
LoadDictionaryIntoCustomWordList(&custom_words);
- char empty[] = {'\0'};
- char separator[] = {'\n', '\0'};
+ const char empty[] = {'\0'};
+ const char separator[] = {'\n', '\0'};
file_util::WriteFile(custom_dictionary_path_, empty, 0);
for (WordList::iterator it = custom_words.begin();
it != custom_words.end();
diff --git a/chrome/browser/spellchecker/spellcheck_custom_dictionary.h b/chrome/browser/spellchecker/spellcheck_custom_dictionary.h
index 1c244e7..b1d6045 100644
--- a/chrome/browser/spellchecker/spellcheck_custom_dictionary.h
+++ b/chrome/browser/spellchecker/spellcheck_custom_dictionary.h
@@ -32,6 +32,9 @@ class SpellcheckCustomDictionary : public SpellcheckDictionary {
const chrome::spellcheck_common::WordList& GetWords() const;
+ // Populates the |custom_words| with the list of words in the custom
+ // dictionary file. Makes sure that the custom dictionary file is sorted and
+ // does not have duplicates.
void LoadDictionaryIntoCustomWordList(
chrome::spellcheck_common::WordList* custom_words);
@@ -48,7 +51,7 @@ class SpellcheckCustomDictionary : public SpellcheckDictionary {
void WriteWordToCustomDictionary(const std::string& word);
- // Removes the given word from the custom words list and inform renderer of
+ // Removes the given word from the custom words list and informs renderer of
// the update. Returns false for words that are not in the dictionary.
bool RemoveWord(const std::string& word);
diff --git a/chrome/browser/spellchecker/spellcheck_custom_dictionary_unittest.cc b/chrome/browser/spellchecker/spellcheck_custom_dictionary_unittest.cc
index bdbc15e..1073440 100644
--- a/chrome/browser/spellchecker/spellcheck_custom_dictionary_unittest.cc
+++ b/chrome/browser/spellchecker/spellcheck_custom_dictionary_unittest.cc
@@ -102,6 +102,7 @@ TEST_F(SpellcheckCustomDictionaryTest, SaveAndLoad) {
// The custom word list should include written words.
custom_dictionary->LoadDictionaryIntoCustomWordList(&loaded_custom_words);
+ std::sort(expected.begin(), expected.end());
EXPECT_EQ(loaded_custom_words, expected);
// Load in another instance of SpellCheckService.
@@ -151,10 +152,12 @@ TEST_F(SpellcheckCustomDictionaryTest, MultiProfile) {
WordList actual1;
custom_dictionary->LoadDictionaryIntoCustomWordList(&actual1);
+ std::sort(expected1.begin(), expected1.end());
EXPECT_EQ(actual1, expected1);
WordList actual2;
custom_dictionary2->LoadDictionaryIntoCustomWordList(&actual2);
+ std::sort(expected2.begin(), expected2.end());
EXPECT_EQ(actual2, expected2);
// Flush the loop now to prevent service init tasks from being run during
diff --git a/chrome/browser/ui/webui/options/language_dictionary_overlay_handler.cc b/chrome/browser/ui/webui/options/language_dictionary_overlay_handler.cc
index d9bb121..32a5ecc 100644
--- a/chrome/browser/ui/webui/options/language_dictionary_overlay_handler.cc
+++ b/chrome/browser/ui/webui/options/language_dictionary_overlay_handler.cc
@@ -15,12 +15,6 @@
namespace options {
-namespace {
-bool StringCompare(const std::string& a, const std::string& b) {
- return a.compare(b) < 0;
-}
-} // namespace
-
LanguageDictionaryOverlayHandler::LanguageDictionaryOverlayHandler()
: overlay_initialized_(false),
dictionary_(NULL) {
@@ -99,10 +93,8 @@ void LanguageDictionaryOverlayHandler::ResetDictionaryWords() {
dictionary_->AddObserver(this);
}
- std::vector<std::string> words = dictionary_->GetWords();
- std::sort(words.begin(), words.end(), StringCompare);
ListValue list_value;
- list_value.AppendStrings(words);
+ list_value.AppendStrings(dictionary_->GetWords());
web_ui()->CallJavascriptFunction("EditDictionaryOverlay.setWordList",
list_value);
}