| 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 #ifndef CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_CUSTOM_DICTIONARY_H_ | 5 #ifndef CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_CUSTOM_DICTIONARY_H_ |
| 6 #define CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_CUSTOM_DICTIONARY_H_ | 6 #define CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_CUSTOM_DICTIONARY_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "chrome/browser/spellchecker/spellcheck_dictionary.h" | 14 #include "chrome/browser/spellchecker/spellcheck_dictionary.h" |
| 15 #include "chrome/common/spellcheck_common.h" | 15 #include "chrome/common/spellcheck_common.h" |
| 16 | 16 |
| 17 // Defines a custom dictionary which users can add their own words to. | 17 // Defines a custom dictionary which users can add their own words to. |
| 18 class SpellcheckCustomDictionary : public SpellcheckDictionary { | 18 class SpellcheckCustomDictionary : public SpellcheckDictionary { |
| 19 public: | 19 public: |
| 20 class Observer { |
| 21 public: |
| 22 virtual void OnCustomDictionaryLoaded() = 0; |
| 23 virtual void OnCustomDictionaryWordAdded(const std::string& word) = 0; |
| 24 virtual void OnCustomDictionaryWordRemoved(const std::string& word) = 0; |
| 25 }; |
| 26 |
| 20 explicit SpellcheckCustomDictionary(Profile* profile); | 27 explicit SpellcheckCustomDictionary(Profile* profile); |
| 21 virtual ~SpellcheckCustomDictionary(); | 28 virtual ~SpellcheckCustomDictionary(); |
| 22 | 29 |
| 30 // Overridden from SpellcheckDictionary: |
| 23 virtual void Load() OVERRIDE; | 31 virtual void Load() OVERRIDE; |
| 24 void WriteWordToCustomDictionary(const std::string& word); | |
| 25 | 32 |
| 26 // Returns true if successful. False otherwise. Takes ownership of | 33 const chrome::spellcheck_common::WordList& GetWords() const; |
| 27 // custom_words and replaces pointer with an empty vector. | 34 |
| 28 bool SetCustomWordList(chrome::spellcheck_common::WordList* custom_words); | |
| 29 const chrome::spellcheck_common::WordList& GetCustomWords() const; | |
| 30 void CustomWordAddedLocally(const std::string& word); | |
| 31 void LoadDictionaryIntoCustomWordList( | 35 void LoadDictionaryIntoCustomWordList( |
| 32 chrome::spellcheck_common::WordList* custom_words); | 36 chrome::spellcheck_common::WordList* custom_words); |
| 33 | 37 |
| 38 // Moves the words from the |custom_words| argument into its own private |
| 39 // member variable. Does not delete the memory at |custom_words|. |
| 40 void SetCustomWordList(chrome::spellcheck_common::WordList* custom_words); |
| 41 |
| 34 // Adds the given word to the custom words list and inform renderer of the | 42 // Adds the given word to the custom words list and inform renderer of the |
| 35 // update. | 43 // update. Returns false for duplicate words. |
| 36 void AddWord(const std::string& word); | 44 bool AddWord(const std::string& word); |
| 37 | 45 |
| 38 // The reply point for PostTaskAndReply. Called when AddWord is finished | 46 // Returns false for duplicate words. |
| 39 // adding a word in the background. | 47 bool CustomWordAddedLocally(const std::string& word); |
| 40 void AddWordComplete(const std::string& word); | 48 |
| 49 void WriteWordToCustomDictionary(const std::string& word); |
| 50 |
| 51 // Removes the given word from the custom words list and inform renderer of |
| 52 // the update. Returns false for words that are not in the dictionary. |
| 53 bool RemoveWord(const std::string& word); |
| 54 |
| 55 // Returns false for words that are not in the dictionary. |
| 56 bool CustomWordRemovedLocally(const std::string& word); |
| 57 |
| 58 void EraseWordFromCustomDictionary(const std::string& word); |
| 59 |
| 60 void AddObserver(Observer* observer); |
| 61 void RemoveObserver(Observer* observer); |
| 41 | 62 |
| 42 private: | 63 private: |
| 64 // Returns a newly allocated list of words read from custom dictionary file. |
| 65 // The caller owns this new list. |
| 66 chrome::spellcheck_common::WordList* LoadDictionary(); |
| 67 |
| 68 // The reply point for PostTaskAndReplyWithResult. Called when LoadDictionary |
| 69 // is finished reading words from custom dictionary file. Moves the strings |
| 70 // from the |custom_words| argument into the private member variable |words_| |
| 71 // and deletes the memory at |custom_words|. |
| 72 void SetCustomWordListAndDelete( |
| 73 chrome::spellcheck_common::WordList* custom_words); |
| 74 |
| 43 // In-memory cache of the custom words file. | 75 // In-memory cache of the custom words file. |
| 44 chrome::spellcheck_common::WordList custom_words_; | 76 chrome::spellcheck_common::WordList words_; |
| 45 | 77 |
| 46 // A path for custom dictionary per profile. | 78 // A path for custom dictionary per profile. |
| 47 FilePath custom_dictionary_path_; | 79 FilePath custom_dictionary_path_; |
| 48 | 80 |
| 49 base::WeakPtrFactory<SpellcheckCustomDictionary> weak_ptr_factory_; | 81 base::WeakPtrFactory<SpellcheckCustomDictionary> weak_ptr_factory_; |
| 50 | 82 |
| 83 std::vector<Observer*> observers_; |
| 84 |
| 51 DISALLOW_COPY_AND_ASSIGN(SpellcheckCustomDictionary); | 85 DISALLOW_COPY_AND_ASSIGN(SpellcheckCustomDictionary); |
| 52 }; | 86 }; |
| 53 | 87 |
| 54 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_CUSTOM_DICTIONARY_H_ | 88 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_CUSTOM_DICTIONARY_H_ |
| OLD | NEW |