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 ~SpellcheckService() override
;
58 base::WeakPtr
<SpellcheckService
> GetWeakPtr();
60 // This function computes a vector of strings which are to be displayed in
61 // the context menu over a text area for changing spell check languages. It
62 // returns the index of the current spell check language in the vector.
63 // TODO(port): this should take a vector of base::string16, but the
64 // implementation has some dependencies in l10n util that need porting first.
65 static int GetSpellCheckLanguages(content::BrowserContext
* context
,
66 std::vector
<std::string
>* languages
);
68 // Computes a vector of strings which are to be displayed in the context
69 // menu from |accept_languages| and |dictionary_language|.
70 static void GetSpellCheckLanguagesFromAcceptLanguages(
71 const std::vector
<std::string
>& accept_languages
,
72 const std::string
& dictionary_language
,
73 std::vector
<std::string
>* languages
);
75 // Signals the event attached by AttachTestEvent() to report the specified
76 // event to browser tests. This function is called by this class and its
77 // derived classes to report their status. This function does not do anything
78 // when we do not set an event to |status_event_|.
79 static bool SignalStatusEvent(EventType type
);
81 // Instantiates SpellCheckHostMetrics object and makes it ready for recording
82 // metrics. This should be called only if the metrics recording is active.
83 void StartRecordingMetrics(bool spellcheck_enabled
);
85 // Pass the renderer some basic initialization information. Note that the
86 // renderer will not load Hunspell until it needs to.
87 void InitForRenderer(content::RenderProcessHost
* process
);
89 // Returns a metrics counter associated with this object,
90 // or null when metrics recording is disabled.
91 SpellCheckHostMetrics
* GetMetrics() const;
93 // Returns the instance of the custom dictionary.
94 SpellcheckCustomDictionary
* GetCustomDictionary();
96 // Returns the instance of the Hunspell dictionary.
97 SpellcheckHunspellDictionary
* GetHunspellDictionary();
99 // Returns the instance of the spelling service feedback sender.
100 spellcheck::FeedbackSender
* GetFeedbackSender();
102 // Load a dictionary from a given path. Format specifies how the dictionary
103 // is stored. Return value is true if successful.
104 bool LoadExternalDictionary(std::string language
,
107 DictionaryFormat format
);
109 // Unload a dictionary. The path is given to identify the dictionary.
110 // Return value is true if successful.
111 bool UnloadExternalDictionary(std::string path
);
113 // NotificationProfile implementation.
114 void Observe(int type
,
115 const content::NotificationSource
& source
,
116 const content::NotificationDetails
& details
) override
;
118 // SpellcheckCustomDictionary::Observer implementation.
119 void OnCustomDictionaryLoaded() override
;
120 void OnCustomDictionaryChanged(
121 const SpellcheckCustomDictionary::Change
& dictionary_change
) override
;
123 // SpellcheckHunspellDictionary::Observer implementation.
124 void OnHunspellDictionaryInitialized() override
;
125 void OnHunspellDictionaryDownloadBegin() override
;
126 void OnHunspellDictionaryDownloadSuccess() override
;
127 void OnHunspellDictionaryDownloadFailure() override
;
130 FRIEND_TEST_ALL_PREFIXES(SpellcheckServiceBrowserTest
, DeleteCorruptedBDICT
);
132 // Attaches an event so browser tests can listen the status events.
133 static void AttachStatusEvent(base::WaitableEvent
* status_event
);
135 // Returns the status event type.
136 static EventType
GetStatusEvent();
138 // Pass all renderers some basic initialization information.
139 void InitForAllRenderers();
141 // Reacts to a change in user preferences on whether auto-spell-correct should
143 void OnEnableAutoSpellCorrectChanged();
145 // Reacts to a change in user preference on which language should be used for
147 void OnSpellCheckDictionaryChanged();
149 // Notification handler for changes to prefs::kSpellCheckUseSpellingService.
150 void OnUseSpellingServiceChanged();
152 // Enables the feedback sender if spelling server is available and enabled.
153 // Otherwise disables the feedback sender.
154 void UpdateFeedbackSenderState();
156 PrefChangeRegistrar pref_change_registrar_
;
157 content::NotificationRegistrar registrar_
;
159 // A pointer to the BrowserContext which this service refers to.
160 content::BrowserContext
* context_
;
162 scoped_ptr
<SpellCheckHostMetrics
> metrics_
;
164 scoped_ptr
<SpellcheckCustomDictionary
> custom_dictionary_
;
166 scoped_ptr
<SpellcheckHunspellDictionary
> hunspell_dictionary_
;
168 scoped_ptr
<spellcheck::FeedbackSender
> feedback_sender_
;
170 base::WeakPtrFactory
<SpellcheckService
> weak_ptr_factory_
;
172 DISALLOW_COPY_AND_ASSIGN(SpellcheckService
);
175 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_SERVICE_H_