Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(806)

Side by Side Diff: chrome/browser/spellchecker/spellcheck_custom_dictionary.h

Issue 11445002: Sync user's custom spellcheck dictionary (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Add server-side size limit tests Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
12 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h" 10 #include "base/memory/weak_ptr.h"
14 #include "base/observer_list.h" 11 #include "base/observer_list.h"
15 #include "chrome/browser/spellchecker/spellcheck_dictionary.h" 12 #include "chrome/browser/spellchecker/spellcheck_dictionary.h"
16 #include "chrome/common/spellcheck_common.h" 13 #include "chrome/common/spellcheck_common.h"
14 #include "sync/api/syncable_service.h"
15
16 namespace custom_dictionary {
17
18 // Returns the list of words in the custom spellcheck dictionary at |path|.
19 // Makes sure that the custom dictionary file is sorted, does not have
20 // duplicates, and contains only valid words. Must be called on the file thread.
21 scoped_ptr<chrome::spellcheck_common::WordList> LoadDictionary(
groby-ooo-7-16 2013/01/10 19:53:03 Why are these APIs exposed? Isn't CustomDictionary
please use gerrit instead 2013/01/12 02:50:46 These APIs were used for unit-tests. I have friend
22 const FilePath& path);
23
24 // Adds |word| to the custom spellcheck dictionary at |path|. Must be called on
25 // the file thread.
26 void WriteWordToCustomDictionary(const std::string& word, const FilePath& path);
27
28 // Removes |word| from the custom spellcheck dictionary at |path|. Must be
29 // called on the file thread.
30 void EraseWordFromCustomDictionary(
31 const std::string& word,
32 const FilePath& path);
33
34 } // namespace custom_dictionary
17 35
18 // Defines a custom dictionary where users can add their own words. All words 36 // 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. 37 // 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 38 // dictionary contains its own checksum when saved on disk. Example dictionary
21 // dictionary file contents: 39 // file contents:
22 // 40 //
23 // bar 41 // bar
24 // foo 42 // foo
25 // checksum_v1 = ec3df4034567e59e119fcf87f2d9bad4 43 // checksum_v1 = ec3df4034567e59e119fcf87f2d9bad4
26 // 44 //
27 class SpellcheckCustomDictionary : public SpellcheckDictionary { 45 class SpellcheckCustomDictionary : public SpellcheckDictionary,
46 public syncer::SyncableService {
28 public: 47 public:
48
49 // Dictionary change.
50 class Change {
51 public:
52
53 // Result of dictionary change.
54 class Result {
55 public:
56 Result();
57 Result(const Result& other);
58 ~Result();
59
60 void set_detected_invalid_words() { invalid_ = true; }
groby-ooo-7-16 2013/01/10 22:04:44 As discussed - let's just make this a struct, with
please use gerrit instead 2013/01/12 02:50:46 Converted into an enum.
61 void set_detected_duplicate_words() { duplicate_ = true; }
62 void set_detected_missing_words() { missing_ = true; }
63 bool detected_invalid_words() const { return invalid_; }
64 bool detected_duplicate_words() const { return duplicate_; }
65 bool detected_missing_words() const { return missing_; }
66
67 private:
68 bool invalid_;
69 bool duplicate_;
70 bool missing_;
71 };
72
73 Change();
74 explicit Change(const Change* other);
75 ~Change();
76
77 void AddWord(const std::string& word);
78 void RemoveWord(const std::string& word);
79 void AddWords(const chrome::spellcheck_common::WordList* words);
groby-ooo-7-16 2013/01/10 22:04:44 Since we copy those words over to internal vectors
please use gerrit instead 2013/01/12 02:50:46 Done.
80 void RemoveWords(const chrome::spellcheck_common::WordList* words);
81 const chrome::spellcheck_common::WordList& to_add() const
82 { return to_add_; }
83 const chrome::spellcheck_common::WordList& to_remove() const
84 { return to_remove_; }
85 chrome::spellcheck_common::WordList& to_add() { return to_add_; }
86 chrome::spellcheck_common::WordList& to_remove() { return to_remove_; }
87 Result& result() { return result_; }
88 bool empty() const { return to_add_.empty() && to_remove_.empty(); }
89
90 private:
91 Result result_;
92 chrome::spellcheck_common::WordList to_add_;
93 chrome::spellcheck_common::WordList to_remove_;
94 DISALLOW_COPY_AND_ASSIGN(Change);
95 };
96
97 // Interface to implement for dictionary change and load observers.
29 class Observer { 98 class Observer {
30 public: 99 public:
31 virtual void OnCustomDictionaryLoaded() = 0; 100 virtual void OnCustomDictionaryLoaded() = 0;
32 virtual void OnCustomDictionaryWordAdded(const std::string& word) = 0; 101 virtual void OnCustomDictionaryChanged(const Change* dictionary_change) = 0;
33 virtual void OnCustomDictionaryWordRemoved(const std::string& word) = 0;
34 }; 102 };
35 103
36 explicit SpellcheckCustomDictionary(Profile* profile); 104 explicit SpellcheckCustomDictionary(Profile* profile);
37 virtual ~SpellcheckCustomDictionary(); 105 virtual ~SpellcheckCustomDictionary();
38 106
107 const chrome::spellcheck_common::WordList& GetWords() const;
108
109 // Adds |word| to the dictionary, schedules a write to disk, and notifies
110 // observers of the change.
111 Change::Result AddWord(const std::string& word);
112
113 // Adds |word| to the dictionary and notifies observers of the change.
114 Change::Result CustomWordAddedLocally(const std::string& word);
groby-ooo-7-16 2013/01/10 22:04:44 Do we need the xxxLocally functions to be part of
please use gerrit instead 2013/01/12 02:50:46 *Locally() methods were used only by tests to avoi
115
116 // Removes |word| from the dictionary, schedules a write to disk, and notifies
117 // observers of the change.
118 Change::Result RemoveWord(const std::string& word);
119
120 // Removes |word| from the dictionary and notifies observers of the change.
121 Change::Result CustomWordRemovedLocally(const std::string& word);
122
123 // Adds |observer| to be notified of dictionary events and changes.
124 void AddObserver(Observer* observer);
125
126 // Removes |observer| to stop notifications of dictionary events and changes.
127 void RemoveObserver(Observer* observer);
128
129 // Whether the dictionary is loaded.
130 bool IsLoaded();
131
132 // Whether the dictionary is being synced.
133 bool IsSyncing();
134
135 // The reply point for PostTaskAndReplyWithResult, called when
136 // custom_dictionary::LoadDictionary finishes reading the dictionary file.
137 void OnLoaded(scoped_ptr<chrome::spellcheck_common::WordList> custom_words);
groby-ooo-7-16 2013/01/10 22:04:44 Does this need to be public?
please use gerrit instead 2013/01/12 02:50:46 Was used by unit-tests. Made private and friended
138
39 // Overridden from SpellcheckDictionary: 139 // Overridden from SpellcheckDictionary:
40 virtual void Load() OVERRIDE; 140 virtual void Load() OVERRIDE;
41 141
42 const chrome::spellcheck_common::WordList& GetWords() const; 142 // Overridden from syncer::SyncableService:
43 143 virtual syncer::SyncMergeResult MergeDataAndStartSyncing(
44 // Populates the |custom_words| with the list of words in the custom 144 syncer::ModelType type,
45 // dictionary file. Makes sure that the custom dictionary file is sorted, does 145 const syncer::SyncDataList& initial_sync_data,
46 // not have duplicates, and contains only valid words. 146 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
47 void LoadDictionaryIntoCustomWordList( 147 scoped_ptr<syncer::SyncErrorFactory> sync_error_handler) OVERRIDE;
48 chrome::spellcheck_common::WordList* custom_words); 148 virtual void StopSyncing(syncer::ModelType type) OVERRIDE;
49 149 virtual syncer::SyncDataList GetAllSyncData(
50 // Moves the words from the |custom_words| argument into its own private 150 syncer::ModelType type) const OVERRIDE;
51 // member variable. Does not delete the memory at |custom_words|. 151 virtual syncer::SyncError ProcessSyncChanges(
52 void SetCustomWordList(chrome::spellcheck_common::WordList* custom_words); 152 const tracked_objects::Location& from_here,
53 153 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 154
76 private: 155 private:
77 // Returns a newly allocated list of words read from custom dictionary file. 156 // Applies the |dictionary_change| to the in-memory copy of the dictionary and
78 // The caller owns this new list. 157 // cleans up |dictionary_change| to be without duplicates, invalid words, and
79 chrome::spellcheck_common::WordList* LoadDictionary(); 158 // missing words. Does not take ownership of |dictionary_change|.
159 void Apply(Change* dictionary_change);
80 160
81 // The reply point for PostTaskAndReplyWithResult. Called when LoadDictionary 161 // Schedules a write of |dictionary_change| to disk.
82 // is finished reading words from custom dictionary file. Moves the strings 162 void Save(scoped_ptr<Change> dictionary_change);
83 // from the |custom_words| argument into the private member variable |words_|
84 // and deletes the memory at |custom_words|.
85 void SetCustomWordListAndDelete(
86 chrome::spellcheck_common::WordList* custom_words);
87 163
88 // Loads the dictionary file into |custom_words|. If the dictionary checksum 164 // Notifies the sync service of the |dictionary_change|. Syncs up to the
89 // is not valid, but backup checksum is valid, then restores the backup and 165 // maximum syncable words on the server. Disables syncing of this dictionary
90 // loads that into |custom_words| instead. If the backup is invalid too, then 166 // if the server contains the maximum number of syncable words. Does not take
91 // clears |custom_words|. 167 // ownership of |dictionary_change|.
92 void LoadDictionaryFileReliably( 168 syncer::SyncError Sync(const Change* dictionary_change);
93 chrome::spellcheck_common::WordList* custom_words);
94 169
95 // Backs up the original dictionary, saves |custom_words| and its checksum 170 // Notifies observers of the dictionary change if the dictionary has been
96 // into the dictionary file. 171 // loaded. Does not take ownership of |dictionary_change|.
97 void SaveDictionaryFileReliably( 172 void Notify(const Change* dictionary_change);
98 const chrome::spellcheck_common::WordList& custom_words);
99 173
100 // In-memory cache of the custom words file. 174 // In-memory cache of the custom words file.
101 chrome::spellcheck_common::WordList words_; 175 chrome::spellcheck_common::WordList words_;
102 176
103 // A path for custom dictionary per profile. 177 // A path for custom dictionary per profile.
104 FilePath custom_dictionary_path_; 178 FilePath custom_dictionary_path_;
105 179
180 // Used to create weak pointers for an instance of this class.
106 base::WeakPtrFactory<SpellcheckCustomDictionary> weak_ptr_factory_; 181 base::WeakPtrFactory<SpellcheckCustomDictionary> weak_ptr_factory_;
107 182
183 // Observers for changes in dictionary load status and content changes.
108 ObserverList<Observer> observers_; 184 ObserverList<Observer> observers_;
109 185
186 // Used to send local changes to the sync infrastructure.
187 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_;
188
189 // Used to send sync-related errors to the sync infrastructure.
190 scoped_ptr<syncer::SyncErrorFactory> sync_error_handler_;
191
192 // Whether the dictionary has been loaded.
193 bool is_loaded_;
194
110 DISALLOW_COPY_AND_ASSIGN(SpellcheckCustomDictionary); 195 DISALLOW_COPY_AND_ASSIGN(SpellcheckCustomDictionary);
111 }; 196 };
112 197
113 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_CUSTOM_DICTIONARY_H_ 198 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_CUSTOM_DICTIONARY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698