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" |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 if (LoadFile(path, custom_words) == VALID_CHECKSUM) | 95 if (LoadFile(path, custom_words) == VALID_CHECKSUM) |
96 return; | 96 return; |
97 // Checksum is not valid. See if there's a backup. | 97 // Checksum is not valid. See if there's a backup. |
98 base::FilePath backup = path.AddExtension(BACKUP_EXTENSION); | 98 base::FilePath backup = path.AddExtension(BACKUP_EXTENSION); |
99 if (!file_util::PathExists(backup)) | 99 if (!file_util::PathExists(backup)) |
100 return; | 100 return; |
101 // Load the backup and verify its checksum. | 101 // Load the backup and verify its checksum. |
102 if (LoadFile(backup, custom_words) != VALID_CHECKSUM) | 102 if (LoadFile(backup, custom_words) != VALID_CHECKSUM) |
103 return; | 103 return; |
104 // Backup checksum is valid. Restore the backup. | 104 // Backup checksum is valid. Restore the backup. |
105 file_util::CopyFile(backup, path); | 105 base::CopyFile(backup, path); |
106 } | 106 } |
107 | 107 |
108 // Backs up the original dictionary, saves |custom_words| and its checksum into | 108 // Backs up the original dictionary, saves |custom_words| and its checksum into |
109 // the custom spellcheck dictionary at |path|. | 109 // the custom spellcheck dictionary at |path|. |
110 void SaveDictionaryFileReliably( | 110 void SaveDictionaryFileReliably( |
111 const WordList& custom_words, | 111 const WordList& custom_words, |
112 const base::FilePath& path) { | 112 const base::FilePath& path) { |
113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
114 std::stringstream content; | 114 std::stringstream content; |
115 for (WordList::const_iterator it = custom_words.begin(); | 115 for (WordList::const_iterator it = custom_words.begin(); |
116 it != custom_words.end(); | 116 it != custom_words.end(); |
117 ++it) { | 117 ++it) { |
118 content << *it << '\n'; | 118 content << *it << '\n'; |
119 } | 119 } |
120 std::string checksum = base::MD5String(content.str()); | 120 std::string checksum = base::MD5String(content.str()); |
121 content << CHECKSUM_PREFIX << checksum; | 121 content << CHECKSUM_PREFIX << checksum; |
122 file_util::CopyFile(path, path.AddExtension(BACKUP_EXTENSION)); | 122 base::CopyFile(path, path.AddExtension(BACKUP_EXTENSION)); |
123 base::ImportantFileWriter::WriteFileAtomically(path, content.str()); | 123 base::ImportantFileWriter::WriteFileAtomically(path, content.str()); |
124 } | 124 } |
125 | 125 |
126 // Removes duplicate and invalid words from |to_add| word list and sorts it. | 126 // Removes duplicate and invalid words from |to_add| word list and sorts it. |
127 // Looks for duplicates in both |to_add| and |existing| word lists. Returns a | 127 // Looks for duplicates in both |to_add| and |existing| word lists. Returns a |
128 // bitmap of |ChangeSanitationResult| values. | 128 // bitmap of |ChangeSanitationResult| values. |
129 int SanitizeWordsToAdd(const WordSet& existing, WordList& to_add) { | 129 int SanitizeWordsToAdd(const WordSet& existing, WordList& to_add) { |
130 // Do not add duplicate words. | 130 // Do not add duplicate words. |
131 std::sort(to_add.begin(), to_add.end()); | 131 std::sort(to_add.begin(), to_add.end()); |
132 WordList new_words; | 132 WordList new_words; |
(...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
542 | 542 |
543 void SpellcheckCustomDictionary::Notify( | 543 void SpellcheckCustomDictionary::Notify( |
544 const SpellcheckCustomDictionary::Change& dictionary_change) { | 544 const SpellcheckCustomDictionary::Change& dictionary_change) { |
545 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 545 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
546 if (!IsLoaded() || dictionary_change.empty()) | 546 if (!IsLoaded() || dictionary_change.empty()) |
547 return; | 547 return; |
548 FOR_EACH_OBSERVER(Observer, | 548 FOR_EACH_OBSERVER(Observer, |
549 observers_, | 549 observers_, |
550 OnCustomDictionaryChanged(dictionary_change)); | 550 OnCustomDictionaryChanged(dictionary_change)); |
551 } | 551 } |
OLD | NEW |