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