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

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

Issue 11414282: Improve reliability of custom spelling dictionary file. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Address comments 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
« no previous file with comments | « no previous file | chrome/browser/spellchecker/spellcheck_custom_dictionary.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "chrome/browser/spellchecker/spellcheck_dictionary.h" 15 #include "chrome/browser/spellchecker/spellcheck_dictionary.h"
15 #include "chrome/common/spellcheck_common.h" 16 #include "chrome/common/spellcheck_common.h"
16 17
17 // Defines a custom dictionary which users can add their own words to. 18 // 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 // The dictionary contains its own checksum when saved on disk. Example
21 // dictionary file contents:
22 //
23 // bar
24 // foo
25 // checksum_v1 = ec3df4034567e59e119fcf87f2d9bad4
26 //
18 class SpellcheckCustomDictionary : public SpellcheckDictionary { 27 class SpellcheckCustomDictionary : public SpellcheckDictionary {
19 public: 28 public:
20 class Observer { 29 class Observer {
21 public: 30 public:
22 virtual void OnCustomDictionaryLoaded() = 0; 31 virtual void OnCustomDictionaryLoaded() = 0;
23 virtual void OnCustomDictionaryWordAdded(const std::string& word) = 0; 32 virtual void OnCustomDictionaryWordAdded(const std::string& word) = 0;
24 virtual void OnCustomDictionaryWordRemoved(const std::string& word) = 0; 33 virtual void OnCustomDictionaryWordRemoved(const std::string& word) = 0;
25 }; 34 };
26 35
27 explicit SpellcheckCustomDictionary(Profile* profile); 36 explicit SpellcheckCustomDictionary(Profile* profile);
28 virtual ~SpellcheckCustomDictionary(); 37 virtual ~SpellcheckCustomDictionary();
29 38
30 // Overridden from SpellcheckDictionary: 39 // Overridden from SpellcheckDictionary:
31 virtual void Load() OVERRIDE; 40 virtual void Load() OVERRIDE;
32 41
33 const chrome::spellcheck_common::WordList& GetWords() const; 42 const chrome::spellcheck_common::WordList& GetWords() const;
34 43
35 // Populates the |custom_words| with the list of words in the custom 44 // Populates the |custom_words| with the list of words in the custom
36 // dictionary file. Makes sure that the custom dictionary file is sorted and 45 // dictionary file. Makes sure that the custom dictionary file is sorted, does
37 // does not have duplicates. 46 // not have duplicates, and contains only valid words.
38 void LoadDictionaryIntoCustomWordList( 47 void LoadDictionaryIntoCustomWordList(
39 chrome::spellcheck_common::WordList* custom_words); 48 chrome::spellcheck_common::WordList* custom_words);
40 49
41 // Moves the words from the |custom_words| argument into its own private 50 // Moves the words from the |custom_words| argument into its own private
42 // member variable. Does not delete the memory at |custom_words|. 51 // member variable. Does not delete the memory at |custom_words|.
43 void SetCustomWordList(chrome::spellcheck_common::WordList* custom_words); 52 void SetCustomWordList(chrome::spellcheck_common::WordList* custom_words);
44 53
45 // Adds the given word to the custom words list and inform renderer of the 54 // Adds the given word to the custom words list and informs renderers of the
46 // update. Returns false for duplicate words. 55 // update. Returns false for duplicate and invalid words.
47 bool AddWord(const std::string& word); 56 bool AddWord(const std::string& word);
48 57
49 // Returns false for duplicate words. 58 // Returns false for duplicate words.
50 bool CustomWordAddedLocally(const std::string& word); 59 bool CustomWordAddedLocally(const std::string& word);
51 60
52 void WriteWordToCustomDictionary(const std::string& word); 61 void WriteWordToCustomDictionary(const std::string& word);
53 62
54 // Removes the given word from the custom words list and informs renderer of 63 // Removes the given word from the custom words list and informs renderers of
55 // the update. Returns false for words that are not in the dictionary. 64 // the update. Returns false for words that are not in the dictionary and
65 // invalid words.
56 bool RemoveWord(const std::string& word); 66 bool RemoveWord(const std::string& word);
57 67
58 // Returns false for words that are not in the dictionary. 68 // Returns false for words that are not in the dictionary.
59 bool CustomWordRemovedLocally(const std::string& word); 69 bool CustomWordRemovedLocally(const std::string& word);
60 70
61 void EraseWordFromCustomDictionary(const std::string& word); 71 void EraseWordFromCustomDictionary(const std::string& word);
62 72
63 void AddObserver(Observer* observer); 73 void AddObserver(Observer* observer);
64 void RemoveObserver(Observer* observer); 74 void RemoveObserver(Observer* observer);
65 75
66 private: 76 private:
67 // Returns a newly allocated list of words read from custom dictionary file. 77 // Returns a newly allocated list of words read from custom dictionary file.
68 // The caller owns this new list. 78 // The caller owns this new list.
69 chrome::spellcheck_common::WordList* LoadDictionary(); 79 chrome::spellcheck_common::WordList* LoadDictionary();
70 80
71 // The reply point for PostTaskAndReplyWithResult. Called when LoadDictionary 81 // The reply point for PostTaskAndReplyWithResult. Called when LoadDictionary
72 // is finished reading words from custom dictionary file. Moves the strings 82 // is finished reading words from custom dictionary file. Moves the strings
73 // from the |custom_words| argument into the private member variable |words_| 83 // from the |custom_words| argument into the private member variable |words_|
74 // and deletes the memory at |custom_words|. 84 // and deletes the memory at |custom_words|.
75 void SetCustomWordListAndDelete( 85 void SetCustomWordListAndDelete(
76 chrome::spellcheck_common::WordList* custom_words); 86 chrome::spellcheck_common::WordList* custom_words);
77 87
88 // 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
90 // loads that into |custom_words| instead. If the backup is invalid too, then
91 // clears |custom_words|.
92 void LoadDictionaryFileReliably(
93 chrome::spellcheck_common::WordList* custom_words);
94
95 // Backs up the original dictionary, saves |custom_words| and its checksum
96 // into the dictionary file.
97 void SaveDictionaryFileReliably(
98 const chrome::spellcheck_common::WordList& custom_words);
99
78 // In-memory cache of the custom words file. 100 // In-memory cache of the custom words file.
79 chrome::spellcheck_common::WordList words_; 101 chrome::spellcheck_common::WordList words_;
80 102
81 // A path for custom dictionary per profile. 103 // A path for custom dictionary per profile.
82 FilePath custom_dictionary_path_; 104 FilePath custom_dictionary_path_;
83 105
84 base::WeakPtrFactory<SpellcheckCustomDictionary> weak_ptr_factory_; 106 base::WeakPtrFactory<SpellcheckCustomDictionary> weak_ptr_factory_;
85 107
86 std::vector<Observer*> observers_; 108 ObserverList<Observer> observers_;
87 109
88 DISALLOW_COPY_AND_ASSIGN(SpellcheckCustomDictionary); 110 DISALLOW_COPY_AND_ASSIGN(SpellcheckCustomDictionary);
89 }; 111 };
90 112
91 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_CUSTOM_DICTIONARY_H_ 113 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_CUSTOM_DICTIONARY_H_
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/spellchecker/spellcheck_custom_dictionary.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698