Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / spellchecker / spellcheck_service.h
blob81956f8e91160fa38de86c19b976c8558e57f993
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 <string>
9 #include <vector>
11 #include "base/gtest_prod_util.h"
12 #include "base/macros.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/prefs/pref_change_registrar.h"
17 #include "chrome/browser/spellchecker/spellcheck_custom_dictionary.h"
18 #include "chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h"
19 #include "components/keyed_service/core/keyed_service.h"
20 #include "content/public/browser/notification_observer.h"
21 #include "content/public/browser/notification_registrar.h"
23 class SpellCheckHostMetrics;
25 namespace base {
26 class WaitableEvent;
27 class SupportsUserData;
30 namespace content {
31 class RenderProcessHost;
32 class BrowserContext;
33 class NotificationDetails;
34 class NotificationSource;
37 namespace spellcheck {
38 class FeedbackSender;
41 // Encapsulates the browser side spellcheck service. There is one of these per
42 // profile and each is created by the SpellCheckServiceFactory. The
43 // SpellcheckService maintains any per-profile information about spellcheck.
44 class SpellcheckService : public KeyedService,
45 public content::NotificationObserver,
46 public SpellcheckCustomDictionary::Observer,
47 public SpellcheckHunspellDictionary::Observer {
48 public:
49 // Event types used for reporting the status of this class and its derived
50 // classes to browser tests.
51 enum EventType {
52 BDICT_NOTINITIALIZED,
53 BDICT_CORRUPTED,
56 // Dictionary format used for loading an external dictionary.
57 enum DictionaryFormat {
58 DICT_HUNSPELL,
59 DICT_TEXT,
60 DICT_UNKNOWN,
63 explicit SpellcheckService(content::BrowserContext* context);
64 ~SpellcheckService() override;
66 base::WeakPtr<SpellcheckService> GetWeakPtr();
68 #if !defined(OS_MACOSX)
69 // Computes |languages| to display in the context menu over a text area for
70 // changing spellcheck languages. Returns the number of languages that are
71 // enabled, which are always at the beginning of |languages|.
72 // TODO(port): this should take a vector of base::string16, but the
73 // implementation has some dependencies in l10n util that need porting first.
74 static size_t GetSpellCheckLanguages(base::SupportsUserData* context,
75 std::vector<std::string>* languages);
76 #endif // !OS_MACOSX
78 // Signals the event attached by AttachTestEvent() to report the specified
79 // event to browser tests. This function is called by this class and its
80 // derived classes to report their status. This function does not do anything
81 // when we do not set an event to |status_event_|.
82 static bool SignalStatusEvent(EventType type);
84 // Instantiates SpellCheckHostMetrics object and makes it ready for recording
85 // metrics. This should be called only if the metrics recording is active.
86 void StartRecordingMetrics(bool spellcheck_enabled);
88 // Pass the renderer some basic initialization information. Note that the
89 // renderer will not load Hunspell until it needs to.
90 void InitForRenderer(content::RenderProcessHost* process);
92 // Returns a metrics counter associated with this object,
93 // or null when metrics recording is disabled.
94 SpellCheckHostMetrics* GetMetrics() const;
96 // Returns the instance of the custom dictionary.
97 SpellcheckCustomDictionary* GetCustomDictionary();
99 // Returns the instance of the vector of Hunspell dictionaries.
100 const ScopedVector<SpellcheckHunspellDictionary>& GetHunspellDictionaries();
102 // Returns the instance of the spelling service feedback sender.
103 spellcheck::FeedbackSender* GetFeedbackSender();
105 // Load a dictionary from a given path. Format specifies how the dictionary
106 // is stored. Return value is true if successful.
107 bool LoadExternalDictionary(std::string language,
108 std::string locale,
109 std::string path,
110 DictionaryFormat format);
112 // Unload a dictionary. The path is given to identify the dictionary.
113 // Return value is true if successful.
114 bool UnloadExternalDictionary(std::string path);
116 // NotificationProfile implementation.
117 void Observe(int type,
118 const content::NotificationSource& source,
119 const content::NotificationDetails& details) override;
121 // SpellcheckCustomDictionary::Observer implementation.
122 void OnCustomDictionaryLoaded() override;
123 void OnCustomDictionaryChanged(
124 const SpellcheckCustomDictionary::Change& dictionary_change) override;
126 // SpellcheckHunspellDictionary::Observer implementation.
127 void OnHunspellDictionaryInitialized(const std::string& language) override;
128 void OnHunspellDictionaryDownloadBegin(const std::string& language) override;
129 void OnHunspellDictionaryDownloadSuccess(
130 const std::string& language) override;
131 void OnHunspellDictionaryDownloadFailure(
132 const std::string& language) override;
134 private:
135 FRIEND_TEST_ALL_PREFIXES(SpellcheckServiceBrowserTest, DeleteCorruptedBDICT);
137 // Attaches an event so browser tests can listen the status events.
138 static void AttachStatusEvent(base::WaitableEvent* status_event);
140 // Returns the status event type.
141 static EventType GetStatusEvent();
143 // Pass all renderers some basic initialization information.
144 void InitForAllRenderers();
146 // Reacts to a change in user preferences on whether auto-spell-correct should
147 // be enabled.
148 void OnEnableAutoSpellCorrectChanged();
150 // Reacts to a change in user preference on which languages should be used for
151 // spellchecking.
152 void OnSpellCheckDictionariesChanged();
154 // Notification handler for changes to prefs::kSpellCheckUseSpellingService.
155 void OnUseSpellingServiceChanged();
157 // Enables the feedback sender if spelling server is available and enabled.
158 // Otherwise disables the feedback sender.
159 void UpdateFeedbackSenderState();
161 PrefChangeRegistrar pref_change_registrar_;
162 content::NotificationRegistrar registrar_;
164 // A pointer to the BrowserContext which this service refers to.
165 content::BrowserContext* context_;
167 scoped_ptr<SpellCheckHostMetrics> metrics_;
169 scoped_ptr<SpellcheckCustomDictionary> custom_dictionary_;
171 ScopedVector<SpellcheckHunspellDictionary> hunspell_dictionaries_;
173 scoped_ptr<spellcheck::FeedbackSender> feedback_sender_;
175 base::WeakPtrFactory<SpellcheckService> weak_ptr_factory_;
177 DISALLOW_COPY_AND_ASSIGN(SpellcheckService);
180 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_SERVICE_H_