OLD | NEW |
---|---|
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. | |
gmorrita
2011/10/12 11:03:36
Could you mention about a lifetime/ownership of wo
| |
99 void CustomDictionaryLoaded(CustomWordList* word_list); | |
100 | |
89 protected: | 101 protected: |
102 friend class base::RefCountedThreadSafe<SpellCheckProfile>; | |
103 virtual ~SpellCheckProfile(); | |
104 | |
90 // Only tests should override this. | 105 // Only tests should override this. |
91 virtual SpellCheckHost* CreateHost( | 106 virtual SpellCheckHost* CreateHost( |
92 SpellCheckProfileProvider* provider, | 107 SpellCheckProfileProvider* provider, |
93 const std::string& language, | 108 const std::string& language, |
94 net::URLRequestContextGetter* request_context, | 109 net::URLRequestContextGetter* request_context, |
95 SpellCheckHostMetrics* metrics); | 110 SpellCheckHostMetrics* metrics); |
96 | 111 |
112 virtual void WriteWordToCustomDictionary(const std::string& word); | |
113 | |
97 virtual bool IsTesting() const; | 114 virtual bool IsTesting() const; |
98 | 115 |
116 // Tests should override this. | |
117 virtual FilePath GetCustomDictionaryDir(); | |
118 | |
99 private: | 119 private: |
120 // Load a custom dictionary on file thread. | |
121 void LoadCustomDictionaryOnFileThread(); | |
122 | |
123 // Write a custom dictionary addition to disk. | |
124 void WriteWordToCustomDictionaryOnFileThread(const std::string& word); | |
125 | |
126 const FilePath& GetCustomDictionaryFile(); | |
127 | |
100 scoped_refptr<SpellCheckHost> host_; | 128 scoped_refptr<SpellCheckHost> host_; |
101 scoped_ptr<SpellCheckHostMetrics> metrics_; | 129 scoped_ptr<SpellCheckHostMetrics> metrics_; |
102 | 130 |
103 // Indicates whether |host_| has told us initialization is | 131 // Indicates whether |host_| has told us initialization is |
104 // finished. | 132 // finished. |
105 bool host_ready_; | 133 bool host_ready_; |
106 | 134 |
135 // The location of the custom words file. Empty if not initialized. | |
136 FilePath custom_dictionary_file_; | |
137 | |
107 // In-memory cache of the custom words file. | 138 // In-memory cache of the custom words file. |
108 scoped_ptr<CustomWordList> custom_words_; | 139 scoped_ptr<CustomWordList> custom_words_; |
109 | 140 |
110 DISALLOW_COPY_AND_ASSIGN(SpellCheckProfile); | 141 DISALLOW_COPY_AND_ASSIGN(SpellCheckProfile); |
111 }; | 142 }; |
112 | 143 |
113 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_PROFILE_H_ | 144 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_PROFILE_H_ |
OLD | NEW |