| OLD | NEW |
| 1 // Copyright (c) 2011 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_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/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/file_path.h" | 13 #include "base/file_path.h" |
| 14 #include "base/gtest_prod_util.h" |
| 15 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/ref_counted.h" | 17 #include "chrome/browser/prefs/pref_change_registrar.h" |
| 18 #include "chrome/browser/profiles/profile_keyed_service.h" |
| 16 #include "chrome/browser/spellchecker/spellcheck_profile_provider.h" | 19 #include "chrome/browser/spellchecker/spellcheck_profile_provider.h" |
| 20 #include "content/public/browser/notification_observer.h" |
| 17 | 21 |
| 18 class Profile; | 22 class Profile; |
| 19 class SpellCheckHost; | 23 class SpellCheckHost; |
| 20 class SpellCheckHostMetrics; | 24 class SpellCheckHostMetrics; |
| 21 | 25 |
| 22 namespace net { | 26 namespace net { |
| 23 class URLRequestContextGetter; | 27 class URLRequestContextGetter; |
| 24 } | 28 } |
| 25 | 29 |
| 26 // A facade of spell-check related objects in the browser process. | 30 // A facade of spell-check related objects in the browser process. |
| 27 // This class is responsible for managing a life-cycle of: | 31 // This class is responsible for managing a life-cycle of: |
| 28 // * SpellCheckHost object (instantiation, deletion, reset) | 32 // * SpellCheckHost object (instantiation, deletion, reset) |
| 29 // * SpellCheckHostMetrics object (only initiate when metrics is enabled) | 33 // * SpellCheckHostMetrics object (only initiate when metrics is enabled) |
| 30 // | 34 // |
| 31 // The following snippet describes how to use this class: | 35 // The class is intended to be owned and managed by SpellCheckFactory |
| 32 // | 36 // internally. Any usable APIs are provided by SpellCheckHost interface, which |
| 33 // PrefService* pref = ....; | 37 // can be retrieved from SpellCheckFactory::GetHostForProfile(); |
| 34 // net::URLRequestContextGetter* context = ...; | 38 class SpellCheckProfile : public SpellCheckProfileProvider, |
| 35 // SpellCheckProfile* profile = new SpellCheckProfile(); | 39 public ProfileKeyedService, |
| 36 // profile->StartRecordingMetrics(true); // to enable recording | 40 public content::NotificationObserver { |
| 37 // profile->ReinitializeResult(true, true, pref->GetLanguage(), context); | |
| 38 // ... | |
| 39 // SpellCheckHost* host = profile->GetHost(); | |
| 40 // | |
| 41 // The class is intended to be owned and managed by ProfileImpl internally. | |
| 42 // Any usable APIs are provided by SpellCheckHost interface, | |
| 43 // which can be retrieved from Profile::GetSpellCheckHost(). | |
| 44 class SpellCheckProfile : public SpellCheckProfileProvider { | |
| 45 public: | 41 public: |
| 46 // Return value of ReinitializeHost() | 42 explicit SpellCheckProfile(Profile* profile); |
| 47 enum ReinitializeResult { | |
| 48 // SpellCheckProfile created new SpellCheckHost object. We can | |
| 49 // retrieve it once the asynchronous initialization task is | |
| 50 // finished. | |
| 51 REINITIALIZE_CREATED_HOST, | |
| 52 // An existing SpellCheckHost insntace is deleted, that means | |
| 53 // the spell-check feature just tunred from enabled to disabled. | |
| 54 // The state should be synced to renderer processes | |
| 55 REINITIALIZE_REMOVED_HOST, | |
| 56 // The API did nothing. SpellCheckHost instance isn't created nor | |
| 57 // deleted. | |
| 58 REINITIALIZE_DID_NOTHING | |
| 59 }; | |
| 60 | |
| 61 explicit SpellCheckProfile(const FilePath& profile_dir); | |
| 62 virtual ~SpellCheckProfile(); | 43 virtual ~SpellCheckProfile(); |
| 63 | 44 |
| 64 // Retrieves SpellCheckHost object. | 45 // Retrieves SpellCheckHost object. |
| 65 // This can return NULL when the spell-checking is disabled | 46 // This can return NULL when the spell-checking is disabled |
| 66 // or the host object is not ready yet. | 47 // or the host object is not ready yet. |
| 67 SpellCheckHost* GetHost(); | 48 SpellCheckHost* GetHost(); |
| 68 | 49 |
| 69 // Initializes or deletes SpellCheckHost object if necessary. | 50 // If |force| is false, and the spellchecker is already initialized (or is in |
| 70 // The caller should provide URLRequestContextGetter for | 51 // the process of initializing), then do nothing. Otherwise clobber the |
| 71 // possible dictionary download. | 52 // current spellchecker and replace it with a new one. |
| 72 // | 53 void ReinitializeSpellCheckHost(bool force); |
| 73 // If |force| is true, the host instance is newly created | |
| 74 // regardless there is existing instance. | |
| 75 ReinitializeResult ReinitializeHost( | |
| 76 bool force, | |
| 77 bool enable, | |
| 78 const std::string& language, | |
| 79 net::URLRequestContextGetter* request_context); | |
| 80 | 54 |
| 81 // Instantiates SpellCheckHostMetrics object and | 55 // Instantiates SpellCheckHostMetrics object and |
| 82 // makes it ready for recording metrics. | 56 // makes it ready for recording metrics. |
| 83 // This should be called only if the metrics recording is active. | 57 // This should be called only if the metrics recording is active. |
| 84 void StartRecordingMetrics(bool spellcheck_enabled); | 58 void StartRecordingMetrics(bool spellcheck_enabled); |
| 85 | 59 |
| 86 // SpellCheckProfileProvider implementation. | 60 // SpellCheckProfileProvider implementation. |
| 87 virtual void SpellCheckHostInitialized(CustomWordList* custom_words) OVERRIDE; | 61 virtual void SpellCheckHostInitialized(CustomWordList* custom_words) OVERRIDE; |
| 88 virtual const CustomWordList& GetCustomWords() const OVERRIDE; | 62 virtual const CustomWordList& GetCustomWords() const OVERRIDE; |
| 89 virtual void CustomWordAddedLocally(const std::string& word) OVERRIDE; | 63 virtual void CustomWordAddedLocally(const std::string& word) OVERRIDE; |
| 90 virtual void LoadCustomDictionary(CustomWordList* custom_words) OVERRIDE; | 64 virtual void LoadCustomDictionary(CustomWordList* custom_words) OVERRIDE; |
| 91 virtual void WriteWordToCustomDictionary(const std::string& word) OVERRIDE; | 65 virtual void WriteWordToCustomDictionary(const std::string& word) OVERRIDE; |
| 92 | 66 |
| 67 // ProfileKeyedService implementation. |
| 68 virtual void Shutdown() OVERRIDE; |
| 69 |
| 70 // content::NotificationObserver implementation. |
| 71 virtual void Observe(int type, |
| 72 const content::NotificationSource& source, |
| 73 const content::NotificationDetails& details) OVERRIDE; |
| 74 |
| 93 protected: | 75 protected: |
| 94 // Only tests should override this. | 76 // Only tests should override this. |
| 95 virtual SpellCheckHost* CreateHost( | 77 virtual SpellCheckHost* CreateHost( |
| 96 SpellCheckProfileProvider* provider, | 78 SpellCheckProfileProvider* provider, |
| 97 const std::string& language, | 79 const std::string& language, |
| 98 net::URLRequestContextGetter* request_context, | 80 net::URLRequestContextGetter* request_context, |
| 99 SpellCheckHostMetrics* metrics); | 81 SpellCheckHostMetrics* metrics); |
| 100 | 82 |
| 101 virtual bool IsTesting() const; | 83 virtual bool IsTesting() const; |
| 102 | 84 |
| 103 private: | 85 private: |
| 86 FRIEND_TEST_ALL_PREFIXES(SpellCheckProfileTest, ReinitializeEnabled); |
| 87 FRIEND_TEST_ALL_PREFIXES(SpellCheckProfileTest, ReinitializeDisabled); |
| 88 FRIEND_TEST_ALL_PREFIXES(SpellCheckProfileTest, ReinitializeRemove); |
| 89 FRIEND_TEST_ALL_PREFIXES(SpellCheckProfileTest, ReinitializeRecreate); |
| 90 FRIEND_TEST_ALL_PREFIXES(SpellCheckProfileTest, |
| 91 SpellCheckHostInitializedWithCustomWords); |
| 92 FRIEND_TEST_ALL_PREFIXES(SpellCheckProfileTest, CustomWordAddedLocally); |
| 93 FRIEND_TEST_ALL_PREFIXES(SpellCheckProfileTest, SaveAndLoad); |
| 94 FRIEND_TEST_ALL_PREFIXES(SpellCheckProfileTest, MultiProfile); |
| 95 |
| 96 // Return value of ReinitializeHost() |
| 97 enum ReinitializeResult { |
| 98 // SpellCheckProfile created new SpellCheckHost object. We can |
| 99 // retrieve it once the asynchronous initialization task is |
| 100 // finished. |
| 101 REINITIALIZE_CREATED_HOST, |
| 102 // An existing SpellCheckHost insntace is deleted, that means |
| 103 // the spell-check feature just tunred from enabled to disabled. |
| 104 // The state should be synced to renderer processes |
| 105 REINITIALIZE_REMOVED_HOST, |
| 106 // The API did nothing. SpellCheckHost instance isn't created nor |
| 107 // deleted. |
| 108 REINITIALIZE_DID_NOTHING |
| 109 }; |
| 110 |
| 111 // Initializes or deletes SpellCheckHost object if necessary. |
| 112 // The caller should provide URLRequestContextGetter for |
| 113 // possible dictionary download. |
| 114 // |
| 115 // If |force| is true, the host instance is newly created |
| 116 // regardless there is existing instance. |
| 117 ReinitializeResult ReinitializeHostImpl( |
| 118 bool force, |
| 119 bool enable, |
| 120 const std::string& language, |
| 121 net::URLRequestContextGetter* request_context); |
| 122 |
| 104 const FilePath& GetCustomDictionaryPath(); | 123 const FilePath& GetCustomDictionaryPath(); |
| 105 | 124 |
| 125 PrefChangeRegistrar pref_change_registrar_; |
| 126 |
| 127 Profile* profile_; |
| 128 |
| 106 scoped_ptr<SpellCheckHost> host_; | 129 scoped_ptr<SpellCheckHost> host_; |
| 107 scoped_ptr<SpellCheckHostMetrics> metrics_; | 130 scoped_ptr<SpellCheckHostMetrics> metrics_; |
| 108 | 131 |
| 109 // Indicates whether |host_| has told us initialization is | 132 // Indicates whether |host_| has told us initialization is |
| 110 // finished. | 133 // finished. |
| 111 bool host_ready_; | 134 bool host_ready_; |
| 112 | 135 |
| 113 // In-memory cache of the custom words file. | 136 // In-memory cache of the custom words file. |
| 114 scoped_ptr<CustomWordList> custom_words_; | 137 scoped_ptr<CustomWordList> custom_words_; |
| 115 | 138 |
| 116 // A directory path of profile. | 139 // A directory path of profile. |
| 117 FilePath profile_dir_; | 140 FilePath profile_dir_; |
| 118 | 141 |
| 119 // A path for custom dictionary per profile. | 142 // A path for custom dictionary per profile. |
| 120 scoped_ptr<FilePath> custom_dictionary_path_; | 143 scoped_ptr<FilePath> custom_dictionary_path_; |
| 121 | 144 |
| 122 DISALLOW_COPY_AND_ASSIGN(SpellCheckProfile); | 145 DISALLOW_COPY_AND_ASSIGN(SpellCheckProfile); |
| 123 }; | 146 }; |
| 124 | 147 |
| 125 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_PROFILE_H_ | 148 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_PROFILE_H_ |
| OLD | NEW |