Chromium Code Reviews| 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 #include "chrome/browser/spellchecker/spellcheck_custom_dictionary.h" | 5 #include "chrome/browser/spellchecker/spellcheck_custom_dictionary.h" |
| 6 | 6 |
| 7 #include <functional> | 7 #include <functional> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/files/important_file_writer.h" | 10 #include "base/files/important_file_writer.h" |
| 11 #include "base/md5.h" | 11 #include "base/md5.h" |
| 12 #include "base/string_number_conversions.h" | |
| 12 #include "base/string_split.h" | 13 #include "base/string_split.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/common/chrome_constants.h" | 15 #include "chrome/common/chrome_constants.h" |
| 15 #include "chrome/common/spellcheck_messages.h" | 16 #include "chrome/common/spellcheck_messages.h" |
| 16 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/browser/render_process_host.h" | 18 #include "sync/api/sync_change.h" |
| 19 #include "sync/api/sync_data.h" | |
| 20 #include "sync/api/sync_error_factory.h" | |
| 21 #include "sync/protocol/sync.pb.h" | |
|
Nicolas Zea
2013/01/02 23:12:15
Do you need this, or can you get away with just in
please use gerrit instead
2013/01/04 23:30:50
We need sync.pb.h for sync_pb::EntitySpecifics.
| |
| 22 #include "third_party/hunspell/src/hunspell/hunspell.hxx" | |
| 18 | 23 |
| 19 using content::BrowserThread; | 24 using content::BrowserThread; |
| 20 using chrome::spellcheck_common::WordList; | 25 using chrome::spellcheck_common::WordList; |
| 21 | 26 |
| 27 namespace custom_dictionary { | |
| 22 namespace { | 28 namespace { |
| 23 | 29 |
| 30 // Filename extension for backup dictionary file. | |
| 24 const FilePath::CharType BACKUP_EXTENSION[] = FILE_PATH_LITERAL("backup"); | 31 const FilePath::CharType BACKUP_EXTENSION[] = FILE_PATH_LITERAL("backup"); |
| 32 | |
| 33 // Prefix for the checksum in the dictionary file. | |
| 25 const char CHECKSUM_PREFIX[] = "checksum_v1 = "; | 34 const char CHECKSUM_PREFIX[] = "checksum_v1 = "; |
| 26 | 35 |
| 36 // The status of the checksum in a custom spellcheck dictionary. | |
| 37 enum ChecksumStatus { | |
| 38 VALID_CHECKSUM, | |
| 39 INVALID_CHECKSUM, | |
| 40 }; | |
| 41 | |
| 27 // Loads the lines from the file at |file_path| into the |lines| container. If | 42 // Loads the lines from the file at |file_path| into the |lines| container. If |
| 28 // the file has a valid checksum, then returns |true|. If the file has an | 43 // the file has a valid checksum, then returns ChecksumStatus::VALID. If the |
| 29 // invalid checksum, then returns |false| and clears |lines|. | 44 // file has an invalid checksum, then returns ChecksumStatus::INVALID and clears |
| 30 bool LoadFile(FilePath file_path, std::vector<std::string>* lines) { | 45 // |lines|. Must be called on the file thread. |
| 46 ChecksumStatus LoadFile(FilePath file_path, std::vector<std::string>* lines) { | |
| 47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 31 lines->clear(); | 48 lines->clear(); |
| 32 std::string contents; | 49 std::string contents; |
| 33 file_util::ReadFileToString(file_path, &contents); | 50 file_util::ReadFileToString(file_path, &contents); |
| 34 size_t pos = contents.rfind(CHECKSUM_PREFIX); | 51 size_t pos = contents.rfind(CHECKSUM_PREFIX); |
| 35 if (pos != std::string::npos) { | 52 if (pos != std::string::npos) { |
| 36 std::string checksum = contents.substr(pos + strlen(CHECKSUM_PREFIX)); | 53 std::string checksum = contents.substr(pos + strlen(CHECKSUM_PREFIX)); |
| 37 contents = contents.substr(0, pos); | 54 contents = contents.substr(0, pos); |
| 38 if (checksum != base::MD5String(contents)) | 55 if (checksum != base::MD5String(contents)) |
| 39 return false; | 56 return INVALID_CHECKSUM; |
| 40 } | 57 } |
| 41 TrimWhitespaceASCII(contents, TRIM_ALL, &contents); | 58 TrimWhitespaceASCII(contents, TRIM_ALL, &contents); |
| 42 base::SplitString(contents, '\n', lines); | 59 base::SplitString(contents, '\n', lines); |
| 43 return true; | 60 return VALID_CHECKSUM; |
| 44 } | 61 } |
| 45 | 62 |
| 46 bool IsValidWord(const std::string& word) { | 63 // Returns true for invalid words and false for valid words. Useful for |
| 47 return IsStringUTF8(word) && word.length() <= 128 && word.length() > 0 && | 64 // std::remove_if() calls. |
| 48 std::string::npos == word.find_first_of(kWhitespaceASCII); | 65 bool IsInvalidWord(const std::string& word) { |
| 66 return !IsStringUTF8(word) || word.length() >= MAXWORDLEN || word.empty() || | |
| 67 word.find_first_of(kWhitespaceASCII) != std::string::npos; | |
| 49 } | 68 } |
| 50 | 69 |
| 51 bool IsInvalidWord(const std::string& word) { | 70 // Loads the custom spellcheck dictionary from |path| into |custom_words|. If |
| 52 return !IsValidWord(word); | 71 // the dictionary checksum is not valid, but backup checksum is valid, then |
| 72 // restores the backup and loads that into |custom_words| instead. If the backup | |
| 73 // is invalid too, then clears |custom_words|. Must be called on the file | |
| 74 // thread. | |
| 75 void LoadDictionaryFileReliably(WordList* custom_words, const FilePath& path) { | |
| 76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 77 | |
| 78 // Load the contents and verify the checksum. | |
| 79 if (LoadFile(path, custom_words) == VALID_CHECKSUM) | |
| 80 return; | |
| 81 | |
| 82 // Checksum is not valid. See if there's a backup. | |
| 83 FilePath backup = path.AddExtension(BACKUP_EXTENSION); | |
| 84 if (!file_util::PathExists(backup)) | |
| 85 return; | |
| 86 | |
| 87 // Load the backup and verify its checksum. | |
| 88 if (LoadFile(backup, custom_words) != VALID_CHECKSUM) | |
| 89 return; | |
| 90 | |
| 91 // Backup checksum is valid. Restore the backup. | |
| 92 file_util::CopyFile(backup, path); | |
| 93 } | |
| 94 | |
| 95 // Backs up the original dictionary, saves |custom_words| and its checksum into | |
| 96 // the custom spellcheck dictionary at |path|. Does not take ownership of | |
| 97 // |custom_words|. Must be called on the file thread. | |
| 98 void SaveDictionaryFileReliably( | |
| 99 const WordList* custom_words, | |
| 100 const FilePath& path) { | |
| 101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 102 DCHECK(custom_words); | |
| 103 | |
| 104 std::stringstream content; | |
| 105 for (WordList::const_iterator it = custom_words->begin(); | |
| 106 it != custom_words->end(); | |
| 107 ++it) { | |
| 108 content << *it << '\n'; | |
| 109 } | |
| 110 std::string checksum = base::MD5String(content.str()); | |
| 111 content << CHECKSUM_PREFIX << checksum; | |
| 112 | |
| 113 file_util::CopyFile(path, path.AddExtension(BACKUP_EXTENSION)); | |
| 114 base::ImportantFileWriter::WriteFileAtomically(path, content.str()); | |
| 115 } | |
| 116 | |
| 117 // Applies the change in |dictionary_change| to the custom spellcheck dictionary | |
| 118 // at |path|. Assumes that |dictionary_change| has already been processed to | |
| 119 // clean the words that cannot be added or removed. Deletes |dictionary_change| | |
| 120 // when done. Must be called on the file thread. | |
| 121 void WriteAndEraseWordsInCustomDictionary( | |
| 122 scoped_ptr<SpellcheckCustomDictionary::Change> dictionary_change, | |
| 123 const FilePath& path) { | |
| 124 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 125 DCHECK(dictionary_change.get()); | |
| 126 | |
| 127 WordList custom_words; | |
| 128 LoadDictionaryFileReliably(&custom_words, path); | |
| 129 | |
| 130 // Add words. | |
| 131 custom_words.insert(custom_words.end(), | |
| 132 dictionary_change->to_add().begin(), | |
| 133 dictionary_change->to_add().end()); | |
| 134 | |
| 135 // Remove words. | |
| 136 std::sort(custom_words.begin(), custom_words.end()); | |
| 137 std::sort(dictionary_change->to_remove().begin(), | |
| 138 dictionary_change->to_remove().end()); | |
| 139 WordList remaining; | |
| 140 std::set_difference(custom_words.begin(), | |
| 141 custom_words.end(), | |
| 142 dictionary_change->to_remove().begin(), | |
| 143 dictionary_change->to_remove().end(), | |
| 144 std::back_inserter(remaining)); | |
| 145 std::swap(custom_words, remaining); | |
| 146 | |
| 147 SaveDictionaryFileReliably(&custom_words, path); | |
| 148 } | |
| 149 | |
| 150 // Converts |dictionary_change| into a sync change list. Does not take ownership | |
| 151 // of the |dictionary_change|. | |
| 152 syncer::SyncChangeList GetSyncChangeList( | |
| 153 const SpellcheckCustomDictionary::Change* dictionary_change) { | |
| 154 DCHECK(dictionary_change); | |
| 155 syncer::SyncChangeList sync_change_list; | |
| 156 std::string word; | |
| 157 for (WordList::const_iterator it = dictionary_change->to_add().begin(); | |
| 158 it != dictionary_change->to_add().end(); | |
| 159 ++it) { | |
| 160 word = *it; | |
| 161 sync_pb::EntitySpecifics specifics; | |
| 162 specifics.mutable_dictionary()->set_word(word); | |
| 163 sync_change_list.push_back(syncer::SyncChange( | |
| 164 FROM_HERE, | |
| 165 syncer::SyncChange::ACTION_ADD, | |
| 166 syncer::SyncData::CreateLocalData(word, word, specifics))); | |
| 167 } | |
| 168 for (WordList::const_iterator it = dictionary_change->to_remove().begin(); | |
| 169 it != dictionary_change->to_remove().end(); | |
| 170 ++it) { | |
| 171 word = *it; | |
| 172 sync_pb::EntitySpecifics specifics; | |
| 173 specifics.mutable_dictionary()->set_word(word); | |
| 174 sync_change_list.push_back(syncer::SyncChange( | |
| 175 FROM_HERE, | |
| 176 syncer::SyncChange::ACTION_DELETE, | |
| 177 syncer::SyncData::CreateLocalData(word, word, specifics))); | |
| 178 } | |
| 179 return sync_change_list; | |
| 53 } | 180 } |
| 54 | 181 |
| 55 } // namespace | 182 } // namespace |
| 56 | 183 |
| 57 SpellcheckCustomDictionary::SpellcheckCustomDictionary(Profile* profile) | 184 scoped_ptr<WordList> LoadDictionary(const FilePath& path) { |
| 58 : SpellcheckDictionary(profile), | |
| 59 custom_dictionary_path_(), | |
| 60 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
| 61 DCHECK(profile); | |
| 62 custom_dictionary_path_ = | |
| 63 profile_->GetPath().Append(chrome::kCustomDictionaryFileName); | |
| 64 } | |
| 65 | |
| 66 SpellcheckCustomDictionary::~SpellcheckCustomDictionary() { | |
| 67 } | |
| 68 | |
| 69 void SpellcheckCustomDictionary::Load() { | |
| 70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 71 | |
| 72 BrowserThread::PostTaskAndReplyWithResult<WordList*>( | |
| 73 BrowserThread::FILE, | |
| 74 FROM_HERE, | |
| 75 base::Bind(&SpellcheckCustomDictionary::LoadDictionary, | |
| 76 base::Unretained(this)), | |
| 77 base::Bind(&SpellcheckCustomDictionary::SetCustomWordListAndDelete, | |
| 78 weak_ptr_factory_.GetWeakPtr())); | |
| 79 } | |
| 80 | |
| 81 const WordList& SpellcheckCustomDictionary::GetWords() const { | |
| 82 return words_; | |
| 83 } | |
| 84 | |
| 85 void SpellcheckCustomDictionary::LoadDictionaryIntoCustomWordList( | |
| 86 WordList* custom_words) { | |
| 87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 88 | 186 |
| 89 LoadDictionaryFileReliably(custom_words); | 187 scoped_ptr<WordList> custom_words(new WordList); |
| 188 LoadDictionaryFileReliably(custom_words.get(), path); | |
| 90 if (custom_words->empty()) | 189 if (custom_words->empty()) |
| 91 return; | 190 return custom_words.Pass(); |
| 92 | 191 |
| 93 // Clean up the dictionary file contents by removing duplicates and invalid | 192 // Clean up the dictionary file contents by removing duplicates and invalid |
| 94 // words. | 193 // words. |
| 95 std::sort(custom_words->begin(), custom_words->end()); | 194 std::sort(custom_words->begin(), custom_words->end()); |
| 96 custom_words->erase(std::unique(custom_words->begin(), custom_words->end()), | 195 custom_words->erase(std::unique(custom_words->begin(), custom_words->end()), |
| 97 custom_words->end()); | 196 custom_words->end()); |
| 98 custom_words->erase(std::remove_if(custom_words->begin(), | 197 custom_words->erase(std::remove_if(custom_words->begin(), |
| 99 custom_words->end(), | 198 custom_words->end(), |
| 100 IsInvalidWord), | 199 custom_dictionary::IsInvalidWord), |
| 101 custom_words->end()); | 200 custom_words->end()); |
| 102 | 201 |
| 103 SaveDictionaryFileReliably(*custom_words); | 202 SaveDictionaryFileReliably(custom_words.get(), path); |
| 203 | |
| 204 return custom_words.Pass(); | |
| 104 } | 205 } |
| 105 | 206 |
| 106 void SpellcheckCustomDictionary::SetCustomWordList(WordList* custom_words) { | 207 void WriteWordToCustomDictionary( |
| 208 const std::string& word, | |
| 209 const FilePath& path) { | |
| 210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 211 scoped_ptr<SpellcheckCustomDictionary::Change> dictionary_change( | |
| 212 new SpellcheckCustomDictionary::Change); | |
| 213 dictionary_change->AddWord(word); | |
| 214 WriteAndEraseWordsInCustomDictionary(dictionary_change.Pass(), path); | |
| 215 } | |
| 216 | |
| 217 void EraseWordFromCustomDictionary( | |
| 218 const std::string& word, | |
| 219 const FilePath& path) { | |
| 220 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 221 scoped_ptr<SpellcheckCustomDictionary::Change> dictionary_change( | |
| 222 new SpellcheckCustomDictionary::Change); | |
| 223 dictionary_change->RemoveWord(word); | |
| 224 WriteAndEraseWordsInCustomDictionary(dictionary_change.Pass(), path); | |
| 225 } | |
| 226 | |
| 227 } // namespace custom_dictionary | |
| 228 | |
| 229 SpellcheckCustomDictionary::Change::Result::Result() | |
| 230 : invalid_(false), | |
| 231 duplicate_(false), | |
| 232 missing_(false) { | |
| 233 } | |
| 234 | |
| 235 SpellcheckCustomDictionary::Change::Result::Result( | |
| 236 const SpellcheckCustomDictionary::Change::Result& other) | |
| 237 : invalid_(other.detected_invalid_words()), | |
| 238 duplicate_(other.detected_duplicate_words()), | |
| 239 missing_(other.detected_missing_words()) { | |
| 240 } | |
| 241 | |
| 242 SpellcheckCustomDictionary::Change::Result::~Result() { | |
| 243 } | |
| 244 | |
| 245 SpellcheckCustomDictionary::Change::Change() { | |
| 246 } | |
| 247 | |
| 248 SpellcheckCustomDictionary::Change::Change( | |
| 249 const SpellcheckCustomDictionary::Change* other) { | |
| 250 DCHECK(other); | |
| 251 | |
| 252 AddWords(&other->to_add()); | |
| 253 RemoveWords(&other->to_remove()); | |
| 254 } | |
| 255 | |
| 256 SpellcheckCustomDictionary::Change::~Change() { | |
| 257 } | |
| 258 | |
| 259 void SpellcheckCustomDictionary::Change::AddWord(const std::string& word) { | |
| 260 to_add_.push_back(word); | |
| 261 } | |
| 262 | |
| 263 void SpellcheckCustomDictionary::Change::RemoveWord(const std::string& word) { | |
| 264 to_remove_.push_back(word); | |
| 265 } | |
| 266 | |
| 267 void SpellcheckCustomDictionary::Change::AddWords(const WordList* words) { | |
| 268 to_add_.insert(to_add_.end(), words->begin(), words->end()); | |
| 269 } | |
| 270 | |
| 271 void SpellcheckCustomDictionary::Change::RemoveWords(const WordList* words) { | |
| 272 to_remove_.insert(to_remove_.end(), words->begin(), words->end()); | |
| 273 } | |
| 274 | |
| 275 SpellcheckCustomDictionary::SpellcheckCustomDictionary(Profile* profile) | |
| 276 : SpellcheckDictionary(profile), | |
| 277 custom_dictionary_path_(), | |
| 278 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | |
| 279 is_loaded_(false) { | |
| 280 DCHECK(profile); | |
| 281 | |
| 282 custom_dictionary_path_ = | |
| 283 profile_->GetPath().Append(chrome::kCustomDictionaryFileName); | |
| 284 } | |
| 285 | |
| 286 SpellcheckCustomDictionary::~SpellcheckCustomDictionary() { | |
| 287 } | |
| 288 | |
| 289 const WordList& SpellcheckCustomDictionary::GetWords() const { | |
| 107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 290 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 108 | 291 |
| 109 words_.clear(); | 292 return words_; |
| 110 if (custom_words) | |
| 111 std::swap(words_, *custom_words); | |
| 112 | |
| 113 FOR_EACH_OBSERVER(Observer, observers_, OnCustomDictionaryLoaded()); | |
| 114 } | 293 } |
| 115 | 294 |
| 116 bool SpellcheckCustomDictionary::AddWord(const std::string& word) { | 295 SpellcheckCustomDictionary::Change::Result SpellcheckCustomDictionary::AddWord( |
| 296 const std::string& word) { | |
| 117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 297 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 118 if (!IsValidWord(word)) | |
| 119 return false; | |
| 120 | 298 |
| 121 if (!CustomWordAddedLocally(word)) | 299 scoped_ptr<Change> dictionary_change(new Change); |
| 122 return false; | 300 dictionary_change->AddWord(word); |
| 123 | 301 dictionary_change = Apply(dictionary_change.Pass()); |
| 124 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 302 Change::Result result(dictionary_change->result()); |
| 125 base::Bind(&SpellcheckCustomDictionary::WriteWordToCustomDictionary, | 303 Notify(dictionary_change.get()); |
| 126 base::Unretained(this), word)); | 304 Sync(scoped_ptr<Change>(new Change(dictionary_change.get()))); |
| 127 | 305 Save(dictionary_change.Pass()); |
| 128 for (content::RenderProcessHost::iterator i( | 306 return result; |
| 129 content::RenderProcessHost::AllHostsIterator()); | |
| 130 !i.IsAtEnd(); i.Advance()) { | |
| 131 i.GetCurrentValue()->Send(new SpellCheckMsg_WordAdded(word)); | |
| 132 } | |
| 133 | |
| 134 FOR_EACH_OBSERVER(Observer, observers_, OnCustomDictionaryWordAdded(word)); | |
| 135 | |
| 136 return true; | |
| 137 } | 307 } |
| 138 | 308 |
| 139 bool SpellcheckCustomDictionary::CustomWordAddedLocally( | 309 SpellcheckCustomDictionary::Change::Result |
| 140 const std::string& word) { | 310 SpellcheckCustomDictionary::CustomWordAddedLocally( |
| 141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 311 const std::string& word) { |
| 142 DCHECK(IsValidWord(word)); | 312 scoped_ptr<Change> dictionary_change(new Change); |
| 143 | 313 dictionary_change->AddWord(word); |
| 144 WordList::iterator it = std::find(words_.begin(), words_.end(), word); | 314 dictionary_change = Apply(dictionary_change.Pass()); |
| 145 if (it == words_.end()) { | 315 Notify(dictionary_change.get()); |
| 146 words_.push_back(word); | 316 Sync(scoped_ptr<Change>(new Change(dictionary_change.get()))); |
| 147 return true; | 317 return dictionary_change->result(); |
| 148 } | |
| 149 return false; | |
| 150 // TODO(rlp): record metrics on custom word size | |
| 151 } | 318 } |
| 152 | 319 |
| 153 void SpellcheckCustomDictionary::WriteWordToCustomDictionary( | 320 SpellcheckCustomDictionary::Change::Result |
| 154 const std::string& word) { | 321 SpellcheckCustomDictionary::RemoveWord(const std::string& word) { |
| 155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 322 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 156 DCHECK(IsValidWord(word)); | |
| 157 | 323 |
| 158 WordList custom_words; | 324 scoped_ptr<Change> dictionary_change(new Change); |
| 159 LoadDictionaryFileReliably(&custom_words); | 325 dictionary_change->RemoveWord(word); |
| 160 custom_words.push_back(word); | 326 dictionary_change = Apply(dictionary_change.Pass()); |
| 161 SaveDictionaryFileReliably(custom_words); | 327 Change::Result result(dictionary_change->result()); |
| 328 Notify(dictionary_change.get()); | |
| 329 Sync(scoped_ptr<Change>(new Change(dictionary_change.get()))); | |
| 330 Save(dictionary_change.Pass()); | |
| 331 return result; | |
| 162 } | 332 } |
| 163 | 333 |
| 164 bool SpellcheckCustomDictionary::RemoveWord(const std::string& word) { | 334 SpellcheckCustomDictionary::Change::Result |
| 335 SpellcheckCustomDictionary::CustomWordRemovedLocally( | |
| 336 const std::string& word) { | |
| 165 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 337 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 166 if (!IsValidWord(word)) | |
| 167 return false; | |
| 168 | 338 |
| 169 if (!CustomWordRemovedLocally(word)) | 339 scoped_ptr<Change> dictionary_change(new Change); |
| 170 return false; | 340 dictionary_change->RemoveWord(word); |
| 171 | 341 dictionary_change = Apply(dictionary_change.Pass()); |
| 172 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 342 Notify(dictionary_change.get()); |
| 173 base::Bind(&SpellcheckCustomDictionary::EraseWordFromCustomDictionary, | 343 Sync(scoped_ptr<Change>(new Change(dictionary_change.get()))); |
| 174 base::Unretained(this), word)); | 344 return dictionary_change->result(); |
| 175 | |
| 176 for (content::RenderProcessHost::iterator i( | |
| 177 content::RenderProcessHost::AllHostsIterator()); | |
| 178 !i.IsAtEnd(); i.Advance()) { | |
| 179 i.GetCurrentValue()->Send(new SpellCheckMsg_WordRemoved(word)); | |
| 180 } | |
| 181 | |
| 182 FOR_EACH_OBSERVER(Observer, observers_, OnCustomDictionaryWordRemoved(word)); | |
| 183 | |
| 184 return true; | |
| 185 } | |
| 186 | |
| 187 bool SpellcheckCustomDictionary::CustomWordRemovedLocally( | |
| 188 const std::string& word) { | |
| 189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 190 DCHECK(IsValidWord(word)); | |
| 191 | |
| 192 WordList::iterator it = std::find(words_.begin(), words_.end(), word); | |
| 193 if (it != words_.end()) { | |
| 194 words_.erase(it); | |
| 195 return true; | |
| 196 } | |
| 197 return false; | |
| 198 } | |
| 199 | |
| 200 void SpellcheckCustomDictionary::EraseWordFromCustomDictionary( | |
| 201 const std::string& word) { | |
| 202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 203 DCHECK(IsValidWord(word)); | |
| 204 | |
| 205 WordList custom_words; | |
| 206 LoadDictionaryFileReliably(&custom_words); | |
| 207 if (custom_words.empty()) | |
| 208 return; | |
| 209 | |
| 210 WordList::iterator it = std::find(custom_words.begin(), | |
| 211 custom_words.end(), | |
| 212 word); | |
| 213 if (it != custom_words.end()) | |
| 214 custom_words.erase(it); | |
| 215 | |
| 216 SaveDictionaryFileReliably(custom_words); | |
| 217 } | 345 } |
| 218 | 346 |
| 219 void SpellcheckCustomDictionary::AddObserver(Observer* observer) { | 347 void SpellcheckCustomDictionary::AddObserver(Observer* observer) { |
| 220 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 348 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 221 | 349 |
| 222 observers_.AddObserver(observer); | 350 observers_.AddObserver(observer); |
| 223 } | 351 } |
| 224 | 352 |
| 225 void SpellcheckCustomDictionary::RemoveObserver(Observer* observer) { | 353 void SpellcheckCustomDictionary::RemoveObserver(Observer* observer) { |
| 226 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 354 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 227 | 355 |
| 228 observers_.RemoveObserver(observer); | 356 observers_.RemoveObserver(observer); |
| 229 } | 357 } |
| 230 | 358 |
| 231 WordList* SpellcheckCustomDictionary::LoadDictionary() { | 359 bool SpellcheckCustomDictionary::IsLoaded() { |
| 232 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 360 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 233 | 361 |
| 234 WordList* custom_words = new WordList; | 362 return is_loaded_; |
| 235 LoadDictionaryIntoCustomWordList(custom_words); | 363 } |
| 236 return custom_words; | 364 |
| 237 } | 365 bool SpellcheckCustomDictionary::IsSyncing() { |
| 238 | 366 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 239 void SpellcheckCustomDictionary::SetCustomWordListAndDelete( | 367 |
| 240 WordList* custom_words) { | 368 return !!sync_processor_.get(); |
| 241 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 369 } |
| 242 | 370 |
| 243 SetCustomWordList(custom_words); | 371 void SpellcheckCustomDictionary::OnLoaded(scoped_ptr<WordList> custom_words) { |
| 244 delete custom_words; | 372 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 245 } | 373 DCHECK(custom_words.get()); |
| 246 | 374 |
| 247 void SpellcheckCustomDictionary::LoadDictionaryFileReliably( | 375 scoped_ptr<Change> dictionary_change(new Change); |
| 248 WordList* custom_words) { | 376 dictionary_change->AddWords(custom_words.get()); |
| 249 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 377 dictionary_change = Apply(dictionary_change.Pass()); |
| 250 | 378 Sync(dictionary_change.Pass()); |
| 251 // Load the contents and verify the checksum. | 379 |
| 252 if (LoadFile(custom_dictionary_path_, custom_words)) | 380 is_loaded_ = true; |
| 381 FOR_EACH_OBSERVER(Observer, observers_, OnCustomDictionaryLoaded()); | |
| 382 } | |
| 383 | |
| 384 void SpellcheckCustomDictionary::Load() { | |
| 385 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 386 | |
| 387 BrowserThread::PostTaskAndReplyWithResult<scoped_ptr<WordList> >( | |
| 388 BrowserThread::FILE, | |
| 389 FROM_HERE, | |
| 390 base::Bind(&custom_dictionary::LoadDictionary, | |
| 391 custom_dictionary_path_), | |
| 392 base::Bind(&SpellcheckCustomDictionary::OnLoaded, | |
| 393 weak_ptr_factory_.GetWeakPtr())); | |
| 394 } | |
| 395 | |
| 396 syncer::SyncMergeResult SpellcheckCustomDictionary::MergeDataAndStartSyncing( | |
| 397 syncer::ModelType type, | |
| 398 const syncer::SyncDataList& initial_sync_data, | |
| 399 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, | |
| 400 scoped_ptr<syncer::SyncErrorFactory> sync_error_handler) { | |
| 401 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 402 DCHECK(!sync_processor_.get()); | |
| 403 DCHECK(!sync_error_handler_.get()); | |
| 404 DCHECK(sync_processor.get()); | |
| 405 DCHECK(sync_error_handler.get()); | |
| 406 DCHECK_EQ(syncer::DICTIONARY, type); | |
| 407 | |
| 408 sync_processor_ = sync_processor.Pass(); | |
| 409 sync_error_handler_ = sync_error_handler.Pass(); | |
| 410 | |
| 411 // Add remote words locally. | |
| 412 WordList sync_words; | |
| 413 std::string word; | |
| 414 for (syncer::SyncDataList::const_iterator it = initial_sync_data.begin(); | |
| 415 it != initial_sync_data.end(); | |
| 416 ++it) { | |
| 417 DCHECK_EQ(syncer::DICTIONARY, it->GetDataType()); | |
| 418 sync_words.push_back(it->GetSpecifics().dictionary().word()); | |
| 419 } | |
| 420 scoped_ptr<Change> to_change_locally(new Change); | |
| 421 to_change_locally->AddWords(&sync_words); | |
| 422 to_change_locally = Apply(to_change_locally.Pass()); | |
|
Nicolas Zea
2013/01/02 23:12:15
It seems odd that you pass ownership, then use the
please use gerrit instead
2013/01/04 23:30:50
I don't think use after free or double free could
| |
| 423 Notify(to_change_locally.get()); | |
| 424 Save(to_change_locally.Pass()); | |
| 425 | |
| 426 // Add as many as possible local words remotely. | |
| 427 std::sort(words_.begin(), words_.end()); | |
| 428 std::sort(sync_words.begin(), sync_words.end()); | |
| 429 scoped_ptr<Change> to_change_remotely(new Change); | |
| 430 std::set_difference(words_.begin(), | |
| 431 words_.end(), | |
| 432 sync_words.begin(), | |
| 433 sync_words.end(), | |
| 434 std::back_inserter(to_change_remotely->to_add())); | |
| 435 syncer::SyncMergeResult result(type); | |
| 436 result.set_error(Sync(to_change_remotely.Pass())); | |
| 437 return result; | |
| 438 } | |
| 439 | |
| 440 void SpellcheckCustomDictionary::StopSyncing(syncer::ModelType type) { | |
| 441 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 442 DCHECK_EQ(syncer::DICTIONARY, type); | |
| 443 | |
| 444 sync_processor_.reset(); | |
| 445 sync_error_handler_.reset(); | |
| 446 } | |
| 447 | |
| 448 syncer::SyncDataList SpellcheckCustomDictionary::GetAllSyncData( | |
| 449 syncer::ModelType type) const { | |
| 450 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 451 DCHECK_EQ(syncer::DICTIONARY, type); | |
| 452 | |
| 453 syncer::SyncDataList data; | |
| 454 std::string word; | |
| 455 size_t i = 0; | |
| 456 for (WordList::const_iterator it = words_.begin(); | |
| 457 it != words_.end() && | |
| 458 i < chrome::spellcheck_common::MAX_SYNCABLE_DICTIONARY_SIZE_IN_WORDS; | |
| 459 ++it, ++i) { | |
| 460 word = *it; | |
| 461 sync_pb::EntitySpecifics specifics; | |
| 462 specifics.mutable_dictionary()->set_word(word); | |
| 463 data.push_back(syncer::SyncData::CreateLocalData(word, word, specifics)); | |
| 464 } | |
| 465 return data; | |
| 466 } | |
| 467 | |
| 468 syncer::SyncError SpellcheckCustomDictionary::ProcessSyncChanges( | |
| 469 const tracked_objects::Location& from_here, | |
| 470 const syncer::SyncChangeList& change_list) { | |
| 471 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 472 | |
| 473 scoped_ptr<Change> dictionary_change(new Change); | |
| 474 for (syncer::SyncChangeList::const_iterator it = change_list.begin(); | |
| 475 it != change_list.end(); | |
| 476 ++it) { | |
| 477 DCHECK(it->IsValid()); | |
| 478 std::string word = it->sync_data().GetSpecifics().dictionary().word(); | |
| 479 switch (it->change_type()) { | |
| 480 case syncer::SyncChange::ACTION_ADD: | |
| 481 dictionary_change->AddWord(word); | |
|
Nicolas Zea
2013/01/02 23:12:15
I think this (and the RemoveWord below) is going t
please use gerrit instead
2013/01/04 23:30:50
Apologies for the confusing code :-). This will no
| |
| 482 break; | |
| 483 case syncer::SyncChange::ACTION_DELETE: | |
| 484 dictionary_change->RemoveWord(word); | |
| 485 break; | |
| 486 default: | |
| 487 return sync_error_handler_->CreateAndUploadError( | |
| 488 FROM_HERE, | |
| 489 "Processing sync changes failed on change type " + | |
| 490 syncer::SyncChange::ChangeTypeToString(it->change_type())); | |
| 491 } | |
| 492 } | |
| 493 | |
| 494 dictionary_change = Apply(dictionary_change.Pass()); | |
| 495 Notify(dictionary_change.get()); | |
| 496 Save(dictionary_change.Pass()); | |
| 497 | |
| 498 return syncer::SyncError(); | |
| 499 } | |
| 500 | |
| 501 // TODO(rlp): record metrics on custom word size | |
| 502 scoped_ptr<SpellcheckCustomDictionary::Change> | |
| 503 SpellcheckCustomDictionary::Apply( | |
| 504 scoped_ptr<SpellcheckCustomDictionary::Change> dictionary_change) { | |
| 505 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 506 DCHECK(dictionary_change.get()); | |
| 507 | |
| 508 // Do not add duplicate words. | |
|
Nicolas Zea
2013/01/02 23:12:15
consider skipping all this if to_add is empty (and
please use gerrit instead
2013/01/04 23:30:50
Great idea! Skipping sections on empty as you sugg
| |
| 509 std::sort(dictionary_change->to_add().begin(), | |
| 510 dictionary_change->to_add().end()); | |
| 511 std::sort(words_.begin(), words_.end()); | |
| 512 WordList to_add; | |
| 513 std::set_difference(dictionary_change->to_add().begin(), | |
| 514 dictionary_change->to_add().end(), | |
| 515 words_.begin(), | |
| 516 words_.end(), | |
| 517 std::back_inserter(to_add)); | |
| 518 to_add.erase(std::unique(to_add.begin(), to_add.end()), | |
| 519 to_add.end()); | |
| 520 if (dictionary_change->to_add().size() != to_add.size()) | |
| 521 dictionary_change->result().set_detected_duplicate_words(); | |
| 522 | |
| 523 // Do not add invalid words. | |
| 524 size_t size = to_add.size(); | |
| 525 to_add.erase(std::remove_if(to_add.begin(), | |
| 526 to_add.end(), | |
| 527 custom_dictionary::IsInvalidWord), | |
| 528 to_add.end()); | |
| 529 if (size != to_add.size()) | |
| 530 dictionary_change->result().set_detected_invalid_words(); | |
| 531 | |
| 532 // Add the words to the dictionary. | |
| 533 words_.insert(words_.end(), to_add.begin(), to_add.end()); | |
| 534 | |
| 535 // Clean up the dictionary change by removing duplicates and invalid words. | |
| 536 std::swap(dictionary_change->to_add(), to_add); | |
| 537 | |
| 538 // Do not remove words that are missing from the dictionary. | |
| 539 std::sort(dictionary_change->to_remove().begin(), | |
| 540 dictionary_change->to_remove().end()); | |
| 541 std::sort(words_.begin(), words_.end()); | |
| 542 WordList to_remove; | |
| 543 std::set_intersection(words_.begin(), | |
| 544 words_.end(), | |
| 545 dictionary_change->to_remove().begin(), | |
| 546 dictionary_change->to_remove().end(), | |
| 547 std::back_inserter(to_remove)); | |
| 548 if (dictionary_change->to_remove().size() > to_remove.size()) | |
| 549 dictionary_change->result().set_detected_missing_words(); | |
| 550 | |
| 551 // Remove the words from the dictionary. | |
| 552 WordList updated_words; | |
| 553 std::set_difference(words_.begin(), | |
| 554 words_.end(), | |
| 555 to_remove.begin(), | |
| 556 to_remove.end(), | |
| 557 std::back_inserter(updated_words)); | |
| 558 std::swap(words_, updated_words); | |
| 559 | |
| 560 // Clean up the dictionary change by removing missing words. | |
| 561 std::swap(dictionary_change->to_remove(), to_remove); | |
| 562 | |
| 563 return dictionary_change.Pass(); | |
| 564 } | |
| 565 | |
| 566 void SpellcheckCustomDictionary::Save( | |
| 567 scoped_ptr<SpellcheckCustomDictionary::Change> dictionary_change) { | |
| 568 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 569 DCHECK(dictionary_change.get()); | |
| 570 | |
| 571 BrowserThread::PostTask( | |
| 572 BrowserThread::FILE, | |
| 573 FROM_HERE, | |
| 574 base::Bind( | |
| 575 &custom_dictionary::WriteAndEraseWordsInCustomDictionary, | |
| 576 base::Passed(dictionary_change.Pass()), | |
| 577 custom_dictionary_path_)); | |
| 578 } | |
| 579 | |
| 580 syncer::SyncError SpellcheckCustomDictionary::Sync( | |
| 581 scoped_ptr<SpellcheckCustomDictionary::Change> dictionary_change) { | |
| 582 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 583 DCHECK(dictionary_change.get()); | |
| 584 | |
| 585 syncer::SyncError error; | |
| 586 if (!IsSyncing()) | |
|
Nicolas Zea
2013/01/02 23:12:15
return early if no changes?
please use gerrit instead
2013/01/04 23:30:50
Good idea. Returning early if there are no changes
| |
| 587 return error; | |
| 588 | |
| 589 // Truncate remote dictionary change to avoid syncing more than maximum | |
| 590 // syncable words. | |
| 591 if (words_.size() > | |
| 592 chrome::spellcheck_common::MAX_SYNCABLE_DICTIONARY_SIZE_IN_WORDS) { | |
| 593 size_t trim_size = dictionary_change->to_add().size() + | |
| 594 chrome::spellcheck_common::MAX_SYNCABLE_DICTIONARY_SIZE_IN_WORDS - | |
| 595 words_.size(); | |
| 596 dictionary_change->to_add().erase( | |
| 597 dictionary_change->to_add().begin() + trim_size, | |
| 598 dictionary_change->to_add().end()); | |
| 599 } | |
| 600 | |
| 601 // Send the changes to the sync processor. | |
| 602 error = sync_processor_->ProcessSyncChanges( | |
| 603 FROM_HERE, | |
| 604 custom_dictionary::GetSyncChangeList(dictionary_change.get())); | |
| 605 | |
| 606 if (error.IsSet()) | |
| 607 return error; | |
| 608 | |
| 609 // Turn off sync if total number of words is more than maximum syncable words. | |
|
Nicolas Zea
2013/01/02 23:12:15
Shouldn't this happen before pushing the change to
please use gerrit instead
2013/01/04 23:30:50
The reason why I prevent further syncing at the en
| |
| 610 if (words_.size() > | |
| 611 chrome::spellcheck_common::MAX_SYNCABLE_DICTIONARY_SIZE_IN_WORDS) { | |
| 612 std::stringstream ss; | |
| 613 ss << "Turning off sync for custom spelling dictionary with " | |
| 614 << words_.size() | |
| 615 << " words. Sync limit is " | |
| 616 << chrome::spellcheck_common::MAX_SYNCABLE_DICTIONARY_SIZE_IN_WORDS | |
| 617 << " words."; | |
| 618 error = sync_error_handler_->CreateAndUploadError(FROM_HERE, ss.str()); | |
| 619 StopSyncing(syncer::DICTIONARY); | |
| 620 } | |
| 621 | |
| 622 return error; | |
| 623 } | |
| 624 | |
| 625 void SpellcheckCustomDictionary::Notify( | |
| 626 const SpellcheckCustomDictionary::Change* dictionary_change) { | |
| 627 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 628 DCHECK(dictionary_change); | |
| 629 | |
| 630 if (!IsLoaded() || dictionary_change->empty()) | |
| 253 return; | 631 return; |
| 254 | 632 |
| 255 // Checksum is not valid. See if there's a backup. | 633 FOR_EACH_OBSERVER(Observer, |
| 256 FilePath backup = custom_dictionary_path_.AddExtension(BACKUP_EXTENSION); | 634 observers_, |
| 257 if (!file_util::PathExists(backup)) | 635 OnCustomDictionaryChanged(dictionary_change)); |
| 258 return; | 636 } |
| 259 | |
| 260 // Load the backup and verify its checksum. | |
| 261 if (!LoadFile(backup, custom_words)) | |
| 262 return; | |
| 263 | |
| 264 // Backup checksum is valid. Restore the backup. | |
| 265 file_util::CopyFile(backup, custom_dictionary_path_); | |
| 266 } | |
| 267 | |
| 268 void SpellcheckCustomDictionary::SaveDictionaryFileReliably( | |
| 269 const WordList& custom_words) { | |
| 270 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 271 | |
| 272 std::stringstream content; | |
| 273 for (WordList::const_iterator it = custom_words.begin(); | |
| 274 it != custom_words.end(); | |
| 275 ++it) { | |
| 276 content << *it << '\n'; | |
| 277 } | |
| 278 std::string checksum = base::MD5String(content.str()); | |
| 279 content << CHECKSUM_PREFIX << checksum; | |
| 280 | |
| 281 file_util::CopyFile(custom_dictionary_path_, | |
| 282 custom_dictionary_path_.AddExtension(BACKUP_EXTENSION)); | |
| 283 base::ImportantFileWriter::WriteFileAtomically(custom_dictionary_path_, | |
| 284 content.str()); | |
| 285 } | |
| OLD | NEW |