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

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

Issue 8233040: SpellCheck refactoring: Moved user custom dictionary for spell check to SpellCheckProfile. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Mentioned lifecycle of word_list Created 9 years, 2 months 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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_PROFILE_H_ 5 #ifndef CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_PROFILE_H_
6 #define CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_PROFILE_H_ 6 #define CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_PROFILE_H_
7 #pragma once 7 #pragma once
8 8
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/file_path.h"
12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
14 #include "chrome/browser/spellchecker/spellcheck_profile_provider.h" 15 #include "chrome/browser/spellchecker/spellcheck_profile_provider.h"
16 #include "chrome/common/chrome_constants.h"
17 #include "chrome/common/chrome_paths.h"
15 18
16 class Profile; 19 class Profile;
17 class SpellCheckHost; 20 class SpellCheckHost;
18 class SpellCheckHostMetrics; 21 class SpellCheckHostMetrics;
19 22
20 namespace net { 23 namespace net {
21 class URLRequestContextGetter; 24 class URLRequestContextGetter;
22 } 25 }
23 26
24 // A facade of spell-check related objects in the browser process. 27 // A facade of spell-check related objects in the browser process.
25 // This class is responsible for managing a life-cycle of: 28 // This class is responsible for managing a life-cycle of:
26 // * SpellCheckHost object (instantiation, deletion, reset) 29 // * SpellCheckHost object (instantiation, deletion, reset)
27 // * SpellCheckHostMetrics object (only initiate when metrics is enabled) 30 // * SpellCheckHostMetrics object (only initiate when metrics is enabled)
28 // 31 //
29 // The following snippet describes how to use this class: 32 // The following snippet describes how to use this class:
30 // 33 //
31 // PrefService* pref = ....; 34 // PrefService* pref = ....;
32 // net::URLRequestContextGetter* context = ...; 35 // net::URLRequestContextGetter* context = ...;
33 // SpellCheckProfile* profile = new SpellCheckProfile(); 36 // SpellCheckProfile* profile = new SpellCheckProfile();
34 // profile->StartRecordingMetrics(true); // to enable recording 37 // profile->StartRecordingMetrics(true); // to enable recording
35 // profile->ReinitializeResult(true, true, pref->GetLanguage(), context); 38 // profile->ReinitializeResult(true, true, pref->GetLanguage(), context);
36 // ... 39 // ...
37 // SpellCheckHost* host = profile->GetHost(); 40 // SpellCheckHost* host = profile->GetHost();
38 // 41 //
39 // The class is intended to be owned and managed by ProfileImpl internally. 42 // The class is intended to be owned and managed by ProfileImpl internally.
40 // Any usable APIs are provided by SpellCheckHost interface, 43 // Any usable APIs are provided by SpellCheckHost interface,
41 // which can be retrieved from Profile::GetSpellCheckHost(). 44 // which can be retrieved from Profile::GetSpellCheckHost().
42 class SpellCheckProfile : public SpellCheckProfileProvider { 45 class SpellCheckProfile
46 : public SpellCheckProfileProvider,
47 public base::RefCountedThreadSafe<SpellCheckProfile> {
43 public: 48 public:
44 // Return value of ReinitializeHost() 49 // Return value of ReinitializeHost()
45 enum ReinitializeResult { 50 enum ReinitializeResult {
46 // SpellCheckProfile created new SpellCheckHost object. We can 51 // SpellCheckProfile created new SpellCheckHost object. We can
47 // retrieve it once the asynchronous initialization task is 52 // retrieve it once the asynchronous initialization task is
48 // finished. 53 // finished.
49 REINITIALIZE_CREATED_HOST, 54 REINITIALIZE_CREATED_HOST,
50 // An existing SpellCheckHost insntace is deleted, that means 55 // An existing SpellCheckHost insntace is deleted, that means
51 // the spell-check feature just tunred from enabled to disabled. 56 // the spell-check feature just tunred from enabled to disabled.
52 // The state should be synced to renderer processes 57 // The state should be synced to renderer processes
53 REINITIALIZE_REMOVED_HOST, 58 REINITIALIZE_REMOVED_HOST,
54 // The API did nothing. SpellCheckHost instance isn't created nor 59 // The API did nothing. SpellCheckHost instance isn't created nor
55 // deleted. 60 // deleted.
56 REINITIALIZE_DID_NOTHING 61 REINITIALIZE_DID_NOTHING
57 }; 62 };
58 63
59 SpellCheckProfile(); 64 SpellCheckProfile();
60 virtual ~SpellCheckProfile();
61 65
62 // Retrieves SpellCheckHost object. 66 // Retrieves SpellCheckHost object.
63 // This can return NULL when the spell-checking is disabled 67 // This can return NULL when the spell-checking is disabled
64 // or the host object is not ready yet. 68 // or the host object is not ready yet.
65 SpellCheckHost* GetHost(); 69 SpellCheckHost* GetHost();
66 70
67 // Initializes or deletes SpellCheckHost object if necessary. 71 // Initializes or deletes SpellCheckHost object if necessary.
68 // The caller should provide URLRequestContextGetter for 72 // The caller should provide URLRequestContextGetter for
69 // possible dictionary download. 73 // possible dictionary download.
70 // 74 //
71 // If |force| is true, the host instance is newly created 75 // If |force| is true, the host instance is newly created
72 // regardless there is existing instance. 76 // regardless there is existing instance.
73 ReinitializeResult ReinitializeHost( 77 ReinitializeResult ReinitializeHost(
74 bool force, 78 bool force,
75 bool enable, 79 bool enable,
76 const std::string& language, 80 const std::string& language,
77 net::URLRequestContextGetter* request_context); 81 net::URLRequestContextGetter* request_context);
78 82
79 // Instantiates SpellCheckHostMetrics object and 83 // Instantiates SpellCheckHostMetrics object and
80 // makes it ready for recording metrics. 84 // makes it ready for recording metrics.
81 // This should be called only if the metrics recording is active. 85 // This should be called only if the metrics recording is active.
82 void StartRecordingMetrics(bool spellcheck_enabled); 86 void StartRecordingMetrics(bool spellcheck_enabled);
83 87
88 // Removes all words form the in-memory dictionary. This is intended to
89 // be used in tests.
90 void ClearCustomWords();
91
84 // SpellCheckProfileProvider implementation. 92 // SpellCheckProfileProvider implementation.
85 virtual void SpellCheckHostInitialized(CustomWordList* custom_words); 93 virtual void LoadCustomDictionary();
94 virtual void SpellCheckHostInitialized();
86 virtual const CustomWordList& GetCustomWords() const; 95 virtual const CustomWordList& GetCustomWords() const;
87 virtual void CustomWordAddedLocally(const std::string& word); 96 virtual void CustomWordAddedLocally(const std::string& word);
88 97
98 // Called when LoadCustomDirectory finished.
99 // word_list is loaded in LoadCustomDictionaryOnFileThread,
100 // and the method transfers its ownership to this method.
101 // After the ownership is transferred, word_list should be
102 // touched only in UI thread, and it will be deleted
103 // when a new word_list is trasferred or when this object is deleted.
104 void CustomDictionaryLoaded(CustomWordList* word_list);
105
89 protected: 106 protected:
107 friend class base::RefCountedThreadSafe<SpellCheckProfile>;
108 virtual ~SpellCheckProfile();
109
90 // Only tests should override this. 110 // Only tests should override this.
91 virtual SpellCheckHost* CreateHost( 111 virtual SpellCheckHost* CreateHost(
92 SpellCheckProfileProvider* provider, 112 SpellCheckProfileProvider* provider,
93 const std::string& language, 113 const std::string& language,
94 net::URLRequestContextGetter* request_context, 114 net::URLRequestContextGetter* request_context,
95 SpellCheckHostMetrics* metrics); 115 SpellCheckHostMetrics* metrics);
96 116
117 virtual void WriteWordToCustomDictionary(const std::string& word);
118
97 virtual bool IsTesting() const; 119 virtual bool IsTesting() const;
98 120
121 // Tests should override this.
122 virtual FilePath GetCustomDictionaryDir();
123
99 private: 124 private:
125 // Load a custom dictionary on file thread.
126 void LoadCustomDictionaryOnFileThread();
127
128 // Write a custom dictionary addition to disk.
129 void WriteWordToCustomDictionaryOnFileThread(const std::string& word);
130
131 const FilePath& GetCustomDictionaryFile();
132
100 scoped_refptr<SpellCheckHost> host_; 133 scoped_refptr<SpellCheckHost> host_;
101 scoped_ptr<SpellCheckHostMetrics> metrics_; 134 scoped_ptr<SpellCheckHostMetrics> metrics_;
102 135
103 // Indicates whether |host_| has told us initialization is 136 // Indicates whether |host_| has told us initialization is
104 // finished. 137 // finished.
105 bool host_ready_; 138 bool host_ready_;
106 139
140 // The location of the custom words file. Empty if not initialized.
141 FilePath custom_dictionary_file_;
142
107 // In-memory cache of the custom words file. 143 // In-memory cache of the custom words file.
108 scoped_ptr<CustomWordList> custom_words_; 144 scoped_ptr<CustomWordList> custom_words_;
109 145
110 DISALLOW_COPY_AND_ASSIGN(SpellCheckProfile); 146 DISALLOW_COPY_AND_ASSIGN(SpellCheckProfile);
111 }; 147 };
112 148
113 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_PROFILE_H_ 149 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_PROFILE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698