Make castv2 performance test work.
[chromium-blink-merge.git] / chrome / browser / spellchecker / spellcheck_custom_dictionary.h
blobb8b329e765e543a5b17e72eb897081b1bf0ebcd9
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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_CUSTOM_DICTIONARY_H_
6 #define CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_CUSTOM_DICTIONARY_H_
8 #include <string>
10 #include "base/files/file_path.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/observer_list.h"
14 #include "chrome/browser/spellchecker/spellcheck_dictionary.h"
15 #include "chrome/common/spellcheck_common.h"
16 #include "sync/api/syncable_service.h"
18 // Defines a custom dictionary where users can add their own words. All words
19 // must be UTF8, between 1 and 99 bytes long, and without leading or trailing
20 // ASCII whitespace. The dictionary contains its own checksum when saved on
21 // disk. Example dictionary file contents:
23 // bar
24 // foo
25 // checksum_v1 = ec3df4034567e59e119fcf87f2d9bad4
27 class SpellcheckCustomDictionary : public SpellcheckDictionary,
28 public syncer::SyncableService {
29 public:
30 // A change to the dictionary.
31 class Change {
32 public:
33 Change();
34 Change(const Change& other);
35 explicit Change(const chrome::spellcheck_common::WordList& to_add);
36 ~Change();
38 // Adds |word| in this change.
39 void AddWord(const std::string& word);
41 // Removes |word| in this change.
42 void RemoveWord(const std::string& word);
44 // Prepares this change to be applied to |words| by removing duplicate and
45 // invalid words from words to be added, removing missing words from words
46 // to be removed, and sorting both lists of words. Assumes that |words| is
47 // sorted. Returns a bitmap of |ChangeSanitationResult| values.
48 int Sanitize(const chrome::spellcheck_common::WordSet& words);
50 // Returns the words to be added in this change.
51 const chrome::spellcheck_common::WordList& to_add() const;
53 // Returns the words to be removed in this change.
54 const chrome::spellcheck_common::WordList& to_remove() const;
56 // Returns true if there are no changes to be made. Otherwise returns false.
57 bool empty() const;
59 private:
60 // The words to be added.
61 chrome::spellcheck_common::WordList to_add_;
63 // The words to be removed.
64 chrome::spellcheck_common::WordList to_remove_;
67 // Interface to implement for dictionary load and change observers.
68 class Observer {
69 public:
70 // Called when the custom dictionary has been loaded.
71 virtual void OnCustomDictionaryLoaded() = 0;
73 // Called when the custom dictionary has been changed.
74 virtual void OnCustomDictionaryChanged(const Change& dictionary_change) = 0;
77 explicit SpellcheckCustomDictionary(const base::FilePath& path);
78 ~SpellcheckCustomDictionary() override;
80 // Returns the in-memory cache of words in the custom dictionary.
81 const chrome::spellcheck_common::WordSet& GetWords() const;
83 // Adds |word| to the dictionary, schedules a write to disk, and notifies
84 // observers of the change. Returns true if |word| is valid and not a
85 // duplicate. Otherwise returns false.
86 bool AddWord(const std::string& word);
88 // Removes |word| from the dictionary, schedules a write to disk, and notifies
89 // observers of the change. Returns true if |word| was found. Otherwise
90 // returns false.
91 bool RemoveWord(const std::string& word);
93 // Returns true if the dictionary contains |word|. Otherwise returns false.
94 bool HasWord(const std::string& word) const;
96 // Adds |observer| to be notified of dictionary events and changes.
97 void AddObserver(Observer* observer);
99 // Removes |observer| to stop notifications of dictionary events and changes.
100 void RemoveObserver(Observer* observer);
102 // Returns true if the dictionary has been loaded. Otherwise returns false.
103 bool IsLoaded();
105 // Returns true if the dictionary is being synced. Otherwise returns false.
106 bool IsSyncing();
108 // Overridden from SpellcheckDictionary:
109 void Load() override;
111 // Overridden from syncer::SyncableService:
112 syncer::SyncMergeResult MergeDataAndStartSyncing(
113 syncer::ModelType type,
114 const syncer::SyncDataList& initial_sync_data,
115 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
116 scoped_ptr<syncer::SyncErrorFactory> sync_error_handler) override;
117 void StopSyncing(syncer::ModelType type) override;
118 syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const override;
119 syncer::SyncError ProcessSyncChanges(
120 const tracked_objects::Location& from_here,
121 const syncer::SyncChangeList& change_list) override;
123 private:
124 friend class DictionarySyncIntegrationTestHelper;
125 friend class SpellcheckCustomDictionaryTest;
127 // Returns the list of words in the custom spellcheck dictionary at |path|.
128 // Makes sure that the custom dictionary file does not have duplicates and
129 // contains only valid words.
130 static chrome::spellcheck_common::WordList LoadDictionaryFile(
131 const base::FilePath& path);
133 // Applies the change in |dictionary_change| to the custom spellcheck
134 // dictionary. Assumes that |dictionary_change| has been sanitized.
135 static void UpdateDictionaryFile(
136 const Change& dictionary_change,
137 const base::FilePath& path);
139 // The reply point for PostTaskAndReplyWithResult, called when
140 // LoadDictionaryFile finishes reading the dictionary file. Does not modify
141 // |custom_words|, but cannot be a const-ref due to the signature of
142 // PostTaskAndReplyWithResult.
143 void OnLoaded(chrome::spellcheck_common::WordList custom_words);
145 // Applies the |dictionary_change| to the in-memory copy of the dictionary.
146 // Assumes that words in |dictionary_change| are sorted.
147 void Apply(const Change& dictionary_change);
149 // Schedules a write of |dictionary_change| to disk. Assumes that words in
150 // |dictionary_change| are sorted.
151 void Save(const Change& dictionary_change);
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.
156 syncer::SyncError Sync(const Change& dictionary_change);
158 // Notifies observers of the dictionary change if the dictionary has been
159 // changed.
160 void Notify(const Change& dictionary_change);
162 // In-memory cache of the custom words file.
163 chrome::spellcheck_common::WordSet words_;
165 // A path for custom dictionary.
166 base::FilePath custom_dictionary_path_;
168 // Observers for changes in dictionary load status and content changes.
169 ObserverList<Observer> observers_;
171 // Used to send local changes to the sync infrastructure.
172 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_;
174 // Used to send sync-related errors to the sync infrastructure.
175 scoped_ptr<syncer::SyncErrorFactory> sync_error_handler_;
177 // True if the dictionary has been loaded. Otherwise false.
178 bool is_loaded_;
180 // Used to create weak pointers for an instance of this class.
181 base::WeakPtrFactory<SpellcheckCustomDictionary> weak_ptr_factory_;
183 DISALLOW_COPY_AND_ASSIGN(SpellcheckCustomDictionary);
186 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_CUSTOM_DICTIONARY_H_