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/spellcheck_custom_dictionary.h"
14 #include "chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h"
15 #include "chrome/common/spellcheck_common.h"
16 #include "components/keyed_service/core/keyed_service.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
21 class SpellCheckHostMetrics
;
28 class RenderProcessHost
;
32 // Encapsulates the browser side spellcheck service. There is one of these per
33 // profile and each is created by the SpellCheckServiceFactory. The
34 // SpellcheckService maintains any per-profile information about spellcheck.
35 class SpellcheckService
: public KeyedService
,
36 public content::NotificationObserver
,
37 public SpellcheckCustomDictionary::Observer
,
38 public SpellcheckHunspellDictionary::Observer
{
40 // Event types used for reporting the status of this class and its derived
41 // classes to browser tests.
47 // Dictionary format used for loading an external dictionary.
48 enum DictionaryFormat
{
54 explicit SpellcheckService(content::BrowserContext
* context
);
55 ~SpellcheckService() override
;
57 // This function computes a vector of strings which are to be displayed in
58 // the context menu over a text area for changing spell check languages. It
59 // returns the index of the current spell check language in the vector.
60 // TODO(port): this should take a vector of base::string16, but the
61 // implementation has some dependencies in l10n util that need porting first.
62 static int GetSpellCheckLanguages(content::BrowserContext
* context
,
63 std::vector
<std::string
>* languages
);
65 // Computes a vector of strings which are to be displayed in the context
66 // menu from |accept_languages| and |dictionary_language|.
67 static void GetSpellCheckLanguagesFromAcceptLanguages(
68 const std::vector
<std::string
>& accept_languages
,
69 const std::string
& dictionary_language
,
70 std::vector
<std::string
>* languages
);
72 // Signals the event attached by AttachTestEvent() to report the specified
73 // event to browser tests. This function is called by this class and its
74 // derived classes to report their status. This function does not do anything
75 // when we do not set an event to |status_event_|.
76 static bool SignalStatusEvent(EventType type
);
78 // Instantiates SpellCheckHostMetrics object and makes it ready for recording
79 // metrics. This should be called only if the metrics recording is active.
80 void StartRecordingMetrics(bool spellcheck_enabled
);
82 // Pass the renderer some basic initialization information. Note that the
83 // renderer will not load Hunspell until it needs to.
84 void InitForRenderer(content::RenderProcessHost
* process
);
86 // Returns a metrics counter associated with this object,
87 // or null when metrics recording is disabled.
88 SpellCheckHostMetrics
* GetMetrics() const;
90 // Returns the instance of the custom dictionary.
91 SpellcheckCustomDictionary
* GetCustomDictionary();
93 // Returns the instance of the Hunspell dictionary.
94 SpellcheckHunspellDictionary
* GetHunspellDictionary();
96 // Load a dictionary from a given path. Format specifies how the dictionary
97 // is stored. Return value is true if successful.
98 bool LoadExternalDictionary(std::string language
,
101 DictionaryFormat format
);
103 // Unload a dictionary. The path is given to identify the dictionary.
104 // Return value is true if successful.
105 bool UnloadExternalDictionary(std::string path
);
107 // NotificationProfile implementation.
108 void Observe(int type
,
109 const content::NotificationSource
& source
,
110 const content::NotificationDetails
& details
) override
;
112 // SpellcheckCustomDictionary::Observer implementation.
113 void OnCustomDictionaryLoaded() override
;
114 void OnCustomDictionaryChanged(
115 const SpellcheckCustomDictionary::Change
& dictionary_change
) override
;
117 // SpellcheckHunspellDictionary::Observer implementation.
118 void OnHunspellDictionaryInitialized() override
;
119 void OnHunspellDictionaryDownloadBegin() override
;
120 void OnHunspellDictionaryDownloadSuccess() override
;
121 void OnHunspellDictionaryDownloadFailure() override
;
124 FRIEND_TEST_ALL_PREFIXES(SpellcheckServiceBrowserTest
, DeleteCorruptedBDICT
);
126 // Attaches an event so browser tests can listen the status events.
127 static void AttachStatusEvent(base::WaitableEvent
* status_event
);
129 // Returns the status event type.
130 static EventType
GetStatusEvent();
132 // Pass all renderers some basic initialization information.
133 void InitForAllRenderers();
135 // Reacts to a change in user preferences on whether auto-spell-correct should
137 void OnEnableAutoSpellCorrectChanged();
139 // Reacts to a change in user preference on which language should be used for
141 void OnSpellCheckDictionaryChanged();
143 // Notification handler for changes to prefs::kSpellCheckUseSpellingService.
144 void OnUseSpellingServiceChanged();
146 PrefChangeRegistrar pref_change_registrar_
;
147 content::NotificationRegistrar registrar_
;
149 // A pointer to the BrowserContext which this service refers to.
150 content::BrowserContext
* context_
;
152 scoped_ptr
<SpellCheckHostMetrics
> metrics_
;
154 scoped_ptr
<SpellcheckCustomDictionary
> custom_dictionary_
;
156 scoped_ptr
<SpellcheckHunspellDictionary
> hunspell_dictionary_
;
158 base::WeakPtrFactory
<SpellcheckService
> weak_ptr_factory_
;
160 DISALLOW_COPY_AND_ASSIGN(SpellcheckService
);
163 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_SERVICE_H_