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

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: Created 8 years 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> 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 "base/observer_list.h" 14 #include "base/observer_list.h"
15 #include "chrome/browser/spellchecker/spellcheck_dictionary.h" 15 #include "chrome/browser/spellchecker/spellcheck_dictionary.h"
16 #include "chrome/common/spellcheck_common.h" 16 #include "chrome/common/spellcheck_common.h"
17 #include "sync/api/syncable_service.h"
17 18
18 // Defines a custom dictionary where users can add their own words. All words 19 // 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. 20 // must be UTF8, between 1 and 128 bytes long, and without ASCII whitespace.
20 // The dictionary contains its own checksum when saved on disk. Example 21 // The dictionary contains its own checksum when saved on disk. Example
21 // dictionary file contents: 22 // dictionary file contents:
22 // 23 //
23 // bar 24 // bar
24 // foo 25 // foo
25 // checksum_v1 = ec3df4034567e59e119fcf87f2d9bad4 26 // checksum_v1 = ec3df4034567e59e119fcf87f2d9bad4
26 // 27 //
27 class SpellcheckCustomDictionary : public SpellcheckDictionary { 28 class SpellcheckCustomDictionary : public SpellcheckDictionary,
29 public syncer::SyncableService {
28 public: 30 public:
29 class Observer { 31 class Observer {
30 public: 32 public:
31 virtual void OnCustomDictionaryLoaded() = 0; 33 virtual void OnCustomDictionaryLoaded() = 0;
32 virtual void OnCustomDictionaryWordAdded(const std::string& word) = 0; 34 virtual void OnCustomDictionaryWordsAdded(
33 virtual void OnCustomDictionaryWordRemoved(const std::string& word) = 0; 35 const chrome::spellcheck_common::WordList& words) = 0;
36 virtual void OnCustomDictionaryWordsRemoved(
37 const chrome::spellcheck_common::WordList& words) = 0;
34 }; 38 };
35 39
36 explicit SpellcheckCustomDictionary(Profile* profile); 40 explicit SpellcheckCustomDictionary(Profile* profile);
37 virtual ~SpellcheckCustomDictionary(); 41 virtual ~SpellcheckCustomDictionary();
38 42
39 // Overridden from SpellcheckDictionary: 43 // Overridden from SpellcheckDictionary:
40 virtual void Load() OVERRIDE; 44 virtual void Load() OVERRIDE;
41 45
42 const chrome::spellcheck_common::WordList& GetWords() const; 46 const chrome::spellcheck_common::WordList& GetWords() const;
43 47
44 // Populates the |custom_words| with the list of words in the custom 48 // Populates the |custom_words| with the list of words in the custom
45 // dictionary file. Makes sure that the custom dictionary file is sorted, does 49 // dictionary file. Makes sure that the custom dictionary file is sorted, does
46 // not have duplicates, and contains only valid words. 50 // not have duplicates, and contains only valid words.
47 void LoadDictionaryIntoCustomWordList( 51 static void LoadDictionaryIntoCustomWordList(
groby-ooo-7-16 2012/12/19 22:16:00 Personal preference: I like to make functions stan
please use gerrit instead 2012/12/22 03:20:19 Done.
48 chrome::spellcheck_common::WordList* custom_words); 52 chrome::spellcheck_common::WordList* custom_words,
53 const FilePath& path);
49 54
50 // Moves the words from the |custom_words| argument into its own private 55 // Moves the words from the |custom_words| argument into its own private
51 // member variable. Does not delete the memory at |custom_words|. 56 // member variable. Does not delete the memory at |custom_words|.
52 void SetCustomWordList(chrome::spellcheck_common::WordList* custom_words); 57 void SetCustomWordList(chrome::spellcheck_common::WordList* custom_words);
53 58
54 // Adds the given word to the custom words list and informs renderers of the 59 // Adds the given word to the custom words list and informs renderers of the
55 // update. Returns false for duplicate and invalid words. 60 // update. Returns false for duplicate and invalid words.
56 bool AddWord(const std::string& word); 61 bool AddWord(const std::string& word);
57 62
58 // Returns false for duplicate words. 63 // Returns false for duplicate words.
59 bool CustomWordAddedLocally(const std::string& word); 64 bool CustomWordAddedLocally(const std::string& word);
60 65
61 void WriteWordToCustomDictionary(const std::string& word); 66 static void WriteWordToCustomDictionary(
67 const std::string& word,
68 const FilePath& path);
62 69
63 // Removes the given word from the custom words list and informs renderers of 70 // 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 71 // the update. Returns false for words that are not in the dictionary and
65 // invalid words. 72 // invalid words.
66 bool RemoveWord(const std::string& word); 73 bool RemoveWord(const std::string& word);
67 74
68 // Returns false for words that are not in the dictionary. 75 // Returns false for words that are not in the dictionary.
69 bool CustomWordRemovedLocally(const std::string& word); 76 bool CustomWordRemovedLocally(const std::string& word);
70 77
71 void EraseWordFromCustomDictionary(const std::string& word); 78 static void EraseWordFromCustomDictionary(
79 const std::string& word,
80 const FilePath& path);
72 81
73 void AddObserver(Observer* observer); 82 void AddObserver(Observer* observer);
74 void RemoveObserver(Observer* observer); 83 void RemoveObserver(Observer* observer);
75 84
85 // Overridden from syncer::SyncableService:
groby-ooo-7-16 2012/12/19 22:16:00 Personal preference: I like overridden interfaces
please use gerrit instead 2012/12/22 03:20:19 Done.
86 virtual syncer::SyncMergeResult MergeDataAndStartSyncing(
87 syncer::ModelType type,
88 const syncer::SyncDataList& initial_sync_data,
89 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
90 scoped_ptr<syncer::SyncErrorFactory> sync_error_handler) OVERRIDE;
91 virtual void StopSyncing(syncer::ModelType type) OVERRIDE;
92 virtual syncer::SyncDataList GetAllSyncData(
93 syncer::ModelType type) const OVERRIDE;
94 virtual syncer::SyncError ProcessSyncChanges(
95 const tracked_objects::Location& from_here,
96 const syncer::SyncChangeList& change_list) OVERRIDE;
97
98 // Relinquishes the management of |sync_processor_|, because it is managed by
99 // |SpellcheckService| in testing.
100 void StopSyncingForTesting();
101
102 // Whether the dictionary is loaded.
103 bool IsLoaded();
104
76 private: 105 private:
77 // Returns a newly allocated list of words read from custom dictionary file. 106 // Returns a newly allocated list of words read from custom dictionary file.
78 // The caller owns this new list. 107 // The caller owns this new list.
79 chrome::spellcheck_common::WordList* LoadDictionary(); 108 static chrome::spellcheck_common::WordList* LoadDictionary(
109 const FilePath& path);
80 110
81 // The reply point for PostTaskAndReplyWithResult. Called when LoadDictionary 111 // The reply point for PostTaskAndReplyWithResult. Called when LoadDictionary
82 // is finished reading words from custom dictionary file. Moves the strings 112 // is finished reading words from custom dictionary file. Moves the strings
83 // from the |custom_words| argument into the private member variable |words_| 113 // from the |custom_words| argument into the private member variable |words_|
84 // and deletes the memory at |custom_words|. 114 // and deletes the memory at |custom_words|.
85 void SetCustomWordListAndDelete( 115 void SetCustomWordListAndDelete(
86 chrome::spellcheck_common::WordList* custom_words); 116 chrome::spellcheck_common::WordList* custom_words);
87 117
88 // Loads the dictionary file into |custom_words|. If the dictionary checksum 118 // Loads the dictionary file into |custom_words|. If the dictionary checksum
89 // is not valid, but backup checksum is valid, then restores the backup and 119 // is not valid, but backup checksum is valid, then restores the backup and
90 // loads that into |custom_words| instead. If the backup is invalid too, then 120 // loads that into |custom_words| instead. If the backup is invalid too, then
91 // clears |custom_words|. 121 // clears |custom_words|.
92 void LoadDictionaryFileReliably( 122 static void LoadDictionaryFileReliably(
93 chrome::spellcheck_common::WordList* custom_words); 123 chrome::spellcheck_common::WordList* custom_words,
124 const FilePath& path);
94 125
95 // Backs up the original dictionary, saves |custom_words| and its checksum 126 // Backs up the original dictionary, saves |custom_words| and its checksum
96 // into the dictionary file. 127 // into the dictionary file.
97 void SaveDictionaryFileReliably( 128 static void SaveDictionaryFileReliably(
98 const chrome::spellcheck_common::WordList& custom_words); 129 const chrome::spellcheck_common::WordList& custom_words,
130 const FilePath& path);
131
132 // Adds words |to_add| and removes words |to_remove| in the custom words list
133 // and informs renderers of the updates. Returns true on success, false on
134 // duplicate words that were not added, missing words that could not be
groby-ooo-7-16 2012/12/19 22:16:00 That's a confusing API - usually boolean returns i
please use gerrit instead 2012/12/22 03:20:19 Done.
135 // removed, and invalid words. Removes duplicates and invalid words from
136 // |to_add| and missing words from |to_remove|. The optional parameter
137 // |is_sync_update| indicates that this change comes from a sync update, which
138 // means that we should not notify the sync process of these changes to avoid
139 // an infinite loop. |is_sync_update| is false by default.
140 bool UpdateWords(chrome::spellcheck_common::WordList* to_add,
141 chrome::spellcheck_common::WordList* to_remove);
142 bool UpdateWords(chrome::spellcheck_common::WordList* to_add,
143 chrome::spellcheck_common::WordList* to_remove,
144 bool is_sync_update);
145
146 // Adds words |to_add| and removes words |to_remove| in the custom words list.
147 // Returns true on success, false on duplicate words that were not added,
148 // missing words that could not be removed, and invalid words. Removes
149 // duplicates and invalid words from |to_add| and missing words from
150 // |to_remove|. The optional parameter |is_sync_update| indicates that this
151 // change comes from a sync update, which means that we should not notify the
152 // sync process of these changes to avoid an infinite loop. |is_sync_update|
153 // is false by default.
154 bool CustomWordsUpdatedLocally(
155 chrome::spellcheck_common::WordList* to_add,
156 chrome::spellcheck_common::WordList* to_remove);
157 bool CustomWordsUpdatedLocally(
158 chrome::spellcheck_common::WordList* to_add,
159 chrome::spellcheck_common::WordList* to_remove,
160 bool is_sync_update);
161
162 // Writes words |to_write| and erases words |to_erase| in the dictionary file.
163 static void WriteAndEraseWordsInCustomDictionary(
164 const chrome::spellcheck_common::WordList* to_write,
165 const chrome::spellcheck_common::WordList* to_erase,
166 const FilePath& path);
167
168 // Deletes the memory at |written| and |erased|.
169 void OnWriteAndEraseFinished(
170 const chrome::spellcheck_common::WordList* written,
171 const chrome::spellcheck_common::WordList* erased);
99 172
100 // In-memory cache of the custom words file. 173 // In-memory cache of the custom words file.
101 chrome::spellcheck_common::WordList words_; 174 chrome::spellcheck_common::WordList words_;
102 175
103 // A path for custom dictionary per profile. 176 // A path for custom dictionary per profile.
104 FilePath custom_dictionary_path_; 177 FilePath custom_dictionary_path_;
105 178
106 base::WeakPtrFactory<SpellcheckCustomDictionary> weak_ptr_factory_; 179 base::WeakPtrFactory<SpellcheckCustomDictionary> weak_ptr_factory_;
107 180
108 ObserverList<Observer> observers_; 181 ObserverList<Observer> observers_;
182 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_;
groby-ooo-7-16 2012/12/19 22:16:00 Please document member vars.
please use gerrit instead 2012/12/22 03:20:19 Done.
183 scoped_ptr<syncer::SyncErrorFactory> sync_error_handler_;
184 bool is_loaded_;
109 185
110 DISALLOW_COPY_AND_ASSIGN(SpellcheckCustomDictionary); 186 DISALLOW_COPY_AND_ASSIGN(SpellcheckCustomDictionary);
111 }; 187 };
112 188
113 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_CUSTOM_DICTIONARY_H_ 189 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_CUSTOM_DICTIONARY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698