Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / spellchecker / spellcheck_service.h
blob86805ce644c862e599d42ad329513d3bf0c42d46
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() override;
128 void OnHunspellDictionaryDownloadBegin() override;
129 void OnHunspellDictionaryDownloadSuccess() override;
130 void OnHunspellDictionaryDownloadFailure() override;
132 private:
133 FRIEND_TEST_ALL_PREFIXES(SpellcheckServiceBrowserTest, DeleteCorruptedBDICT);
135 // Attaches an event so browser tests can listen the status events.
136 static void AttachStatusEvent(base::WaitableEvent* status_event);
138 // Returns the status event type.
139 static EventType GetStatusEvent();
141 // Pass all renderers some basic initialization information.
142 void InitForAllRenderers();
144 // Reacts to a change in user preferences on whether auto-spell-correct should
145 // be enabled.
146 void OnEnableAutoSpellCorrectChanged();
148 // Reacts to a change in user preference on which languages should be used for
149 // spellchecking.
150 void OnSpellCheckDictionariesChanged();
152 // Notification handler for changes to prefs::kSpellCheckUseSpellingService.
153 void OnUseSpellingServiceChanged();
155 // Enables the feedback sender if spelling server is available and enabled.
156 // Otherwise disables the feedback sender.
157 void UpdateFeedbackSenderState();
159 PrefChangeRegistrar pref_change_registrar_;
160 content::NotificationRegistrar registrar_;
162 // A pointer to the BrowserContext which this service refers to.
163 content::BrowserContext* context_;
165 scoped_ptr<SpellCheckHostMetrics> metrics_;
167 scoped_ptr<SpellcheckCustomDictionary> custom_dictionary_;
169 ScopedVector<SpellcheckHunspellDictionary> hunspell_dictionaries_;
171 scoped_ptr<spellcheck::FeedbackSender> feedback_sender_;
173 base::WeakPtrFactory<SpellcheckService> weak_ptr_factory_;
175 DISALLOW_COPY_AND_ASSIGN(SpellcheckService);
178 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_SERVICE_H_