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> | |
9 #include <vector> | |
10 | |
11 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/gtest_prod_util.h" | |
12 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
13 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
14 #include "base/observer_list.h" | 12 #include "base/observer_list.h" |
15 #include "chrome/browser/spellchecker/spellcheck_dictionary.h" | 13 #include "chrome/browser/spellchecker/spellcheck_dictionary.h" |
16 #include "chrome/common/spellcheck_common.h" | 14 #include "chrome/common/spellcheck_common.h" |
15 #include "sync/api/syncable_service.h" | |
17 | 16 |
18 // Defines a custom dictionary where users can add their own words. All words | 17 // Defines a custom dictionary where users can add their own words. All words |
19 // must be UTF8, between 1 and 128 bytes long, and without ASCII whitespace. | 18 // must be UTF8, between 1 and 99 bytes long, and without ASCII whitespace. The |
20 // The dictionary contains its own checksum when saved on disk. Example | 19 // dictionary contains its own checksum when saved on disk. Example dictionary |
21 // dictionary file contents: | 20 // file contents: |
22 // | 21 // |
23 // bar | 22 // bar |
24 // foo | 23 // foo |
25 // checksum_v1 = ec3df4034567e59e119fcf87f2d9bad4 | 24 // checksum_v1 = ec3df4034567e59e119fcf87f2d9bad4 |
26 // | 25 // |
27 class SpellcheckCustomDictionary : public SpellcheckDictionary { | 26 class SpellcheckCustomDictionary : public SpellcheckDictionary, |
27 public syncer::SyncableService { | |
28 public: | 28 public: |
29 // A change to the dictionary. | |
30 class Change { | |
31 public: | |
32 Change(); | |
33 ~Change(); | |
34 | |
35 // Add |word| in this change. | |
36 void AddWord(const std::string& word); | |
groby-ooo-7-16
2013/01/15 01:19:20
Can we either have AddWord/RemoveWord or AddWords/
please use gerrit instead
2013/01/16 02:06:05
Let's keep only AddWord/RemoveWord.
| |
37 | |
38 // Add |words| in this change. | |
39 void AddWords(const chrome::spellcheck_common::WordList& words); | |
40 | |
41 // Remove |word| in this change. | |
42 void RemoveWord(const std::string& word); | |
43 | |
44 // Remove |words| in this change. | |
45 void RemoveWords(const chrome::spellcheck_common::WordList& words); | |
46 | |
47 // Returns the words to be added in this change. | |
48 const chrome::spellcheck_common::WordList& to_add() const; | |
49 chrome::spellcheck_common::WordList& to_add(); | |
50 | |
51 // Returns the words to be removed in this change. | |
52 const chrome::spellcheck_common::WordList& to_remove() const; | |
53 chrome::spellcheck_common::WordList& to_remove(); | |
54 | |
55 // Returns true if there are no changes to be made. Otherwise returns false. | |
56 bool empty() const; | |
57 | |
58 private: | |
59 // The words to be added. | |
60 chrome::spellcheck_common::WordList to_add_; | |
61 | |
62 // The words to be removed. | |
63 chrome::spellcheck_common::WordList to_remove_; | |
64 | |
65 DISALLOW_COPY_AND_ASSIGN(Change); | |
66 }; | |
67 | |
68 // Interface to implement for dictionary load and change observers. | |
29 class Observer { | 69 class Observer { |
30 public: | 70 public: |
71 // A callback for when the custom dictionary has loaded the words saved in | |
groby-ooo-7-16
2013/01/15 01:19:20
Shorten to "called when the custom dictionary file
please use gerrit instead
2013/01/16 02:06:05
Done.
| |
72 // the custom dictionary file. | |
31 virtual void OnCustomDictionaryLoaded() = 0; | 73 virtual void OnCustomDictionaryLoaded() = 0; |
32 virtual void OnCustomDictionaryWordAdded(const std::string& word) = 0; | 74 |
33 virtual void OnCustomDictionaryWordRemoved(const std::string& word) = 0; | 75 // A callback for when the custom dictionary has been changed. |
groby-ooo-7-16
2013/01/15 01:19:20
"Called when.."
please use gerrit instead
2013/01/16 02:06:05
Done.
| |
76 virtual void OnCustomDictionaryChanged(const Change* dictionary_change) = 0; | |
34 }; | 77 }; |
35 | 78 |
36 explicit SpellcheckCustomDictionary(Profile* profile); | 79 explicit SpellcheckCustomDictionary(Profile* profile); |
37 virtual ~SpellcheckCustomDictionary(); | 80 virtual ~SpellcheckCustomDictionary(); |
38 | 81 |
82 // Returns the in-memory cache of words in the custom dictionary. | |
83 const chrome::spellcheck_common::WordList& GetWords() const; | |
84 | |
85 // Adds |word| to the dictionary, schedules a write to disk, and notifies | |
86 // observers of the change. Returns true if |word| is valid and not a | |
87 // duplicate. Otherwise returns false. | |
88 bool AddWord(const std::string& word); | |
89 | |
90 // Removes |word| from the dictionary, schedules a write to disk, and notifies | |
91 // observers of the change. Returns true if |word| was found. Otherwise | |
92 // returns false. | |
93 bool RemoveWord(const std::string& word); | |
94 | |
95 // Adds |observer| to be notified of dictionary events and changes. | |
96 void AddObserver(Observer* observer); | |
97 | |
98 // Removes |observer| to stop notifications of dictionary events and changes. | |
99 void RemoveObserver(Observer* observer); | |
100 | |
101 // Whether the dictionary is loaded. | |
groby-ooo-7-16
2013/01/15 01:19:20
"Return true if the dictionary is loaded." - I pre
please use gerrit instead
2013/01/16 02:06:05
Done.
| |
102 bool IsLoaded(); | |
103 | |
104 // Whether the dictionary is being synced. | |
105 bool IsSyncing(); | |
groby-ooo-7-16
2013/01/15 01:19:20
See above.
please use gerrit instead
2013/01/16 02:06:05
Done.
| |
106 | |
39 // Overridden from SpellcheckDictionary: | 107 // Overridden from SpellcheckDictionary: |
40 virtual void Load() OVERRIDE; | 108 virtual void Load() OVERRIDE; |
41 | 109 |
42 const chrome::spellcheck_common::WordList& GetWords() const; | 110 // Overridden from syncer::SyncableService: |
43 | 111 virtual syncer::SyncMergeResult MergeDataAndStartSyncing( |
44 // Populates the |custom_words| with the list of words in the custom | 112 syncer::ModelType type, |
45 // dictionary file. Makes sure that the custom dictionary file is sorted, does | 113 const syncer::SyncDataList& initial_sync_data, |
46 // not have duplicates, and contains only valid words. | 114 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, |
47 void LoadDictionaryIntoCustomWordList( | 115 scoped_ptr<syncer::SyncErrorFactory> sync_error_handler) OVERRIDE; |
48 chrome::spellcheck_common::WordList* custom_words); | 116 virtual void StopSyncing(syncer::ModelType type) OVERRIDE; |
49 | 117 virtual syncer::SyncDataList GetAllSyncData( |
50 // Moves the words from the |custom_words| argument into its own private | 118 syncer::ModelType type) const OVERRIDE; |
51 // member variable. Does not delete the memory at |custom_words|. | 119 virtual syncer::SyncError ProcessSyncChanges( |
52 void SetCustomWordList(chrome::spellcheck_common::WordList* custom_words); | 120 const tracked_objects::Location& from_here, |
53 | 121 const syncer::SyncChangeList& change_list) OVERRIDE; |
54 // Adds the given word to the custom words list and informs renderers of the | |
55 // update. Returns false for duplicate and invalid words. | |
56 bool AddWord(const std::string& word); | |
57 | |
58 // Returns false for duplicate words. | |
59 bool CustomWordAddedLocally(const std::string& word); | |
60 | |
61 void WriteWordToCustomDictionary(const std::string& word); | |
62 | |
63 // Removes the given word from the custom words list and informs renderers of | |
64 // the update. Returns false for words that are not in the dictionary and | |
65 // invalid words. | |
66 bool RemoveWord(const std::string& word); | |
67 | |
68 // Returns false for words that are not in the dictionary. | |
69 bool CustomWordRemovedLocally(const std::string& word); | |
70 | |
71 void EraseWordFromCustomDictionary(const std::string& word); | |
72 | |
73 void AddObserver(Observer* observer); | |
74 void RemoveObserver(Observer* observer); | |
75 | 122 |
76 private: | 123 private: |
77 // Returns a newly allocated list of words read from custom dictionary file. | 124 friend class DictionarySyncIntegrationTestHelper; |
78 // The caller owns this new list. | 125 friend class SpellcheckCustomDictionaryTest; |
79 chrome::spellcheck_common::WordList* LoadDictionary(); | |
80 | 126 |
81 // The reply point for PostTaskAndReplyWithResult. Called when LoadDictionary | 127 // Returns the list of words in the custom spellcheck dictionary at |path|. |
82 // is finished reading words from custom dictionary file. Moves the strings | 128 // Makes sure that the custom dictionary file is sorted, does not have |
83 // from the |custom_words| argument into the private member variable |words_| | 129 // duplicates, and contains only valid words. |
84 // and deletes the memory at |custom_words|. | 130 static scoped_ptr<chrome::spellcheck_common::WordList> LoadDictionaryFile( |
groby-ooo-7-16
2013/01/15 01:19:20
Since this doesn't need any member access, and is
please use gerrit instead
2013/01/16 02:06:05
Unit tests access this function directly.
| |
85 void SetCustomWordListAndDelete( | 131 const FilePath& path); |
86 chrome::spellcheck_common::WordList* custom_words); | |
87 | 132 |
88 // Loads the dictionary file into |custom_words|. If the dictionary checksum | 133 // Applies the change in |dictionary_change| to the custom spellcheck |
89 // is not valid, but backup checksum is valid, then restores the backup and | 134 // dictionary. Assumes that |dictionary_change| has already been processed to |
90 // loads that into |custom_words| instead. If the backup is invalid too, then | 135 // clean the words that should not be added or removed. |
91 // clears |custom_words|. | 136 static void UpdateDictionaryFile( |
92 void LoadDictionaryFileReliably( | 137 scoped_ptr<SpellcheckCustomDictionary::Change> dictionary_change, |
93 chrome::spellcheck_common::WordList* custom_words); | 138 const FilePath& path); |
groby-ooo-7-16
2013/01/15 01:19:20
See above re: anon. ns.
please use gerrit instead
2013/01/16 02:06:05
Unit tests access this function directly, too.
| |
94 | 139 |
95 // Backs up the original dictionary, saves |custom_words| and its checksum | 140 // The reply point for PostTaskAndReplyWithResult, called when |
96 // into the dictionary file. | 141 // LoadDictionaryFile finishes reading the dictionary file. |
97 void SaveDictionaryFileReliably( | 142 void OnLoaded(scoped_ptr<chrome::spellcheck_common::WordList> custom_words); |
98 const chrome::spellcheck_common::WordList& custom_words); | 143 |
144 // Applies the |dictionary_change| to the in-memory copy of the dictionary and | |
145 // cleans up |dictionary_change| to be without duplicates, invalid words, and | |
146 // missing words. Does not take ownership of |dictionary_change|. Returns a | |
147 // bitmap of |ChangeResult| values. | |
148 int Apply(Change* dictionary_change); | |
groby-ooo-7-16
2013/01/15 01:19:20
I'm still in favor of sanitizing the changes on th
please use gerrit instead
2013/01/16 02:06:05
Const refs everywhere. Sanitizing the Change objec
| |
149 | |
150 // Schedules a write of |dictionary_change| to disk. | |
151 void Save(scoped_ptr<Change> dictionary_change); | |
groby-ooo-7-16
2013/01/15 01:19:20
"... and takes ownership".
Even better, pass a c
please use gerrit instead
2013/01/16 02:06:05
Done.
| |
152 | |
153 // Notifies the sync service of the |dictionary_change|. Syncs up to the | |
154 // maximum syncable words on the server. Disables syncing of this dictionary | |
155 // if the server contains the maximum number of syncable words. Does not take | |
156 // ownership of |dictionary_change|. | |
157 syncer::SyncError Sync(const Change* dictionary_change); | |
groby-ooo-7-16
2013/01/15 01:19:20
I might be wrong, but at first glance, it doesn't
please use gerrit instead
2013/01/16 02:06:05
Done.
| |
158 | |
159 // Notifies observers of the dictionary change if the dictionary has been | |
160 // changed. Does not take ownership of |dictionary_change|. | |
161 void Notify(const Change* dictionary_change); | |
groby-ooo-7-16
2013/01/15 01:19:20
const-ref?
please use gerrit instead
2013/01/16 02:06:05
Done.
| |
99 | 162 |
100 // In-memory cache of the custom words file. | 163 // In-memory cache of the custom words file. |
101 chrome::spellcheck_common::WordList words_; | 164 chrome::spellcheck_common::WordList words_; |
102 | 165 |
103 // A path for custom dictionary per profile. | 166 // A path for custom dictionary per profile. |
104 FilePath custom_dictionary_path_; | 167 FilePath custom_dictionary_path_; |
105 | 168 |
169 // Used to create weak pointers for an instance of this class. | |
106 base::WeakPtrFactory<SpellcheckCustomDictionary> weak_ptr_factory_; | 170 base::WeakPtrFactory<SpellcheckCustomDictionary> weak_ptr_factory_; |
107 | 171 |
172 // Observers for changes in dictionary load status and content changes. | |
108 ObserverList<Observer> observers_; | 173 ObserverList<Observer> observers_; |
109 | 174 |
175 // Used to send local changes to the sync infrastructure. | |
176 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_; | |
177 | |
178 // Used to send sync-related errors to the sync infrastructure. | |
179 scoped_ptr<syncer::SyncErrorFactory> sync_error_handler_; | |
180 | |
181 // Whether the dictionary has been loaded. | |
182 bool is_loaded_; | |
183 | |
110 DISALLOW_COPY_AND_ASSIGN(SpellcheckCustomDictionary); | 184 DISALLOW_COPY_AND_ASSIGN(SpellcheckCustomDictionary); |
111 }; | 185 }; |
112 | 186 |
113 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_CUSTOM_DICTIONARY_H_ | 187 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_CUSTOM_DICTIONARY_H_ |
OLD | NEW |