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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_SERVICE_H_
6 #define CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_SERVICE_H_
8 #include "base/compiler_specific.h"
9 #include "base/gtest_prod_util.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/prefs/pref_change_registrar.h"
13 #include "chrome/browser/spellchecker/feedback_sender.h"
14 #include "chrome/browser/spellchecker/spellcheck_custom_dictionary.h"
15 #include "chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h"
16 #include "chrome/common/spellcheck_common.h"
17 #include "components/keyed_service/core/keyed_service.h"
18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h"
22 class SpellCheckHostMetrics
;
29 class RenderProcessHost
;
33 // Encapsulates the browser side spellcheck service. There is one of these per
34 // profile and each is created by the SpellCheckServiceFactory. The
35 // SpellcheckService maintains any per-profile information about spellcheck.
36 class SpellcheckService
: public KeyedService
,
37 public content::NotificationObserver
,
38 public SpellcheckCustomDictionary::Observer
,
39 public SpellcheckHunspellDictionary::Observer
{
41 // Event types used for reporting the status of this class and its derived
42 // classes to browser tests.
48 // Dictionary format used for loading an external dictionary.
49 enum DictionaryFormat
{
55 explicit SpellcheckService(content::BrowserContext
* context
);
56 virtual ~SpellcheckService();
58 // This function computes a vector of strings which are to be displayed in
59 // the context menu over a text area for changing spell check languages. It
60 // returns the index of the current spell check language in the vector.
61 // TODO(port): this should take a vector of base::string16, but the
62 // implementation has some dependencies in l10n util that need porting first.
63 static int GetSpellCheckLanguages(content::BrowserContext
* context
,
64 std::vector
<std::string
>* languages
);
66 // Computes a vector of strings which are to be displayed in the context
67 // menu from |accept_languages| and |dictionary_language|.
68 static void GetSpellCheckLanguagesFromAcceptLanguages(
69 const std::vector
<std::string
>& accept_languages
,
70 const std::string
& dictionary_language
,
71 std::vector
<std::string
>* languages
);
73 // Signals the event attached by AttachTestEvent() to report the specified
74 // event to browser tests. This function is called by this class and its
75 // derived classes to report their status. This function does not do anything
76 // when we do not set an event to |status_event_|.
77 static bool SignalStatusEvent(EventType type
);
79 // Instantiates SpellCheckHostMetrics object and makes it ready for recording
80 // metrics. This should be called only if the metrics recording is active.
81 void StartRecordingMetrics(bool spellcheck_enabled
);
83 // Pass the renderer some basic initialization information. Note that the
84 // renderer will not load Hunspell until it needs to.
85 void InitForRenderer(content::RenderProcessHost
* process
);
87 // Returns a metrics counter associated with this object,
88 // or null when metrics recording is disabled.
89 SpellCheckHostMetrics
* GetMetrics() const;
91 // Returns the instance of the custom dictionary.
92 SpellcheckCustomDictionary
* GetCustomDictionary();
94 // Returns the instance of the Hunspell dictionary.
95 SpellcheckHunspellDictionary
* GetHunspellDictionary();
97 // Returns the instance of the spelling service feedback sender.
98 spellcheck::FeedbackSender
* GetFeedbackSender();
100 // Load a dictionary from a given path. Format specifies how the dictionary
101 // is stored. Return value is true if successful.
102 bool LoadExternalDictionary(std::string language
,
105 DictionaryFormat format
);
107 // Unload a dictionary. The path is given to identify the dictionary.
108 // Return value is true if successful.
109 bool UnloadExternalDictionary(std::string path
);
111 // NotificationProfile implementation.
112 virtual void Observe(int type
,
113 const content::NotificationSource
& source
,
114 const content::NotificationDetails
& details
) OVERRIDE
;
116 // SpellcheckCustomDictionary::Observer implementation.
117 virtual void OnCustomDictionaryLoaded() OVERRIDE
;
118 virtual void OnCustomDictionaryChanged(
119 const SpellcheckCustomDictionary::Change
& dictionary_change
) OVERRIDE
;
121 // SpellcheckHunspellDictionary::Observer implementation.
122 virtual void OnHunspellDictionaryInitialized() OVERRIDE
;
123 virtual void OnHunspellDictionaryDownloadBegin() OVERRIDE
;
124 virtual void OnHunspellDictionaryDownloadSuccess() OVERRIDE
;
125 virtual void OnHunspellDictionaryDownloadFailure() OVERRIDE
;
128 FRIEND_TEST_ALL_PREFIXES(SpellcheckServiceBrowserTest
, DeleteCorruptedBDICT
);
130 // Attaches an event so browser tests can listen the status events.
131 static void AttachStatusEvent(base::WaitableEvent
* status_event
);
133 // Returns the status event type.
134 static EventType
GetStatusEvent();
136 // Pass all renderers some basic initialization information.
137 void InitForAllRenderers();
139 // Reacts to a change in user preferences on whether auto-spell-correct should
141 void OnEnableAutoSpellCorrectChanged();
143 // Reacts to a change in user preference on which language should be used for
145 void OnSpellCheckDictionaryChanged();
147 // Notification handler for changes to prefs::kSpellCheckUseSpellingService.
148 void OnUseSpellingServiceChanged();
150 // Enables the feedback sender if spelling server is available and enabled.
151 // Otherwise disables the feedback sender.
152 void UpdateFeedbackSenderState();
154 PrefChangeRegistrar pref_change_registrar_
;
155 content::NotificationRegistrar registrar_
;
157 // A pointer to the BrowserContext which this service refers to.
158 content::BrowserContext
* context_
;
160 scoped_ptr
<SpellCheckHostMetrics
> metrics_
;
162 scoped_ptr
<SpellcheckCustomDictionary
> custom_dictionary_
;
164 scoped_ptr
<SpellcheckHunspellDictionary
> hunspell_dictionary_
;
166 scoped_ptr
<spellcheck::FeedbackSender
> feedback_sender_
;
168 base::WeakPtrFactory
<SpellcheckService
> weak_ptr_factory_
;
170 DISALLOW_COPY_AND_ASSIGN(SpellcheckService
);
173 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_SERVICE_H_