- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / spellchecker / spellcheck_service.h
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.
4
5 #ifndef CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_SERVICE_H_
6 #define CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_SERVICE_H_
7
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/browser_context_keyed_service/browser_context_keyed_service.h"
18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h"
20
21 class PrefService;
22 class SpellCheckHostMetrics;
23
24 namespace base {
25 class WaitableEvent;
26 }
27
28 namespace content {
29 class RenderProcessHost;
30 class BrowserContext;
31 }
32
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 BrowserContextKeyedService,
37                           public content::NotificationObserver,
38                           public SpellcheckCustomDictionary::Observer,
39                           public SpellcheckHunspellDictionary::Observer {
40  public:
41   // Event types used for reporting the status of this class and its derived
42   // classes to browser tests.
43   enum EventType {
44     BDICT_NOTINITIALIZED,
45     BDICT_CORRUPTED,
46   };
47
48   // Dictionary format used for loading an external dictionary.
49   enum DictionaryFormat {
50     DICT_HUNSPELL,
51     DICT_TEXT,
52     DICT_UNKNOWN,
53   };
54
55   explicit SpellcheckService(content::BrowserContext* context);
56   virtual ~SpellcheckService();
57
58   // This function computes a vector of strings which are to be displayed in
59   // the context menu over a text area for changing spell check languages. It
60   // returns the index of the current spell check language in the vector.
61   // TODO(port): this should take a vector of string16, but the implementation
62   // has some dependencies in l10n util that need porting first.
63   static int GetSpellCheckLanguages(content::BrowserContext* context,
64                                     std::vector<std::string>* languages);
65
66   // Computes a vector of strings which are to be displayed in the context
67   // menu from |accept_languages| and |dictionary_language|.
68   static void GetSpellCheckLanguagesFromAcceptLanguages(
69       const std::vector<std::string>& accept_languages,
70       const std::string& dictionary_language,
71       std::vector<std::string>* languages);
72
73   // Signals the event attached by AttachTestEvent() to report the specified
74   // event to browser tests. This function is called by this class and its
75   // derived classes to report their status. This function does not do anything
76   // when we do not set an event to |status_event_|.
77   static bool SignalStatusEvent(EventType type);
78
79   // Instantiates SpellCheckHostMetrics object and makes it ready for recording
80   // metrics. This should be called only if the metrics recording is active.
81   void StartRecordingMetrics(bool spellcheck_enabled);
82
83   // Pass the renderer some basic initialization information. Note that the
84   // renderer will not load Hunspell until it needs to.
85   void InitForRenderer(content::RenderProcessHost* process);
86
87   // Returns a metrics counter associated with this object,
88   // or null when metrics recording is disabled.
89   SpellCheckHostMetrics* GetMetrics() const;
90
91   // Returns the instance of the custom dictionary.
92   SpellcheckCustomDictionary* GetCustomDictionary();
93
94   // Returns the instance of the Hunspell dictionary.
95   SpellcheckHunspellDictionary* GetHunspellDictionary();
96
97   // Returns the instance of the spelling service feedback sender.
98   spellcheck::FeedbackSender* GetFeedbackSender();
99
100   // Load a dictionary from a given path. Format specifies how the dictionary
101   // is stored. Return value is true if successful.
102   bool LoadExternalDictionary(std::string language,
103                               std::string locale,
104                               std::string path,
105                               DictionaryFormat format);
106
107   // Unload a dictionary. The path is given to identify the dictionary.
108   // Return value is true if successful.
109   bool UnloadExternalDictionary(std::string path);
110
111   // NotificationProfile implementation.
112   virtual void Observe(int type,
113                        const content::NotificationSource& source,
114                        const content::NotificationDetails& details) OVERRIDE;
115
116   // SpellcheckCustomDictionary::Observer implementation.
117   virtual void OnCustomDictionaryLoaded() OVERRIDE;
118   virtual void OnCustomDictionaryChanged(
119       const SpellcheckCustomDictionary::Change& dictionary_change) OVERRIDE;
120
121   // SpellcheckHunspellDictionary::Observer implementation.
122   virtual void OnHunspellDictionaryInitialized() OVERRIDE;
123   virtual void OnHunspellDictionaryDownloadBegin() OVERRIDE;
124   virtual void OnHunspellDictionaryDownloadSuccess() OVERRIDE;
125   virtual void OnHunspellDictionaryDownloadFailure() OVERRIDE;
126
127  private:
128   FRIEND_TEST_ALL_PREFIXES(SpellcheckServiceBrowserTest, DeleteCorruptedBDICT);
129
130   // Attaches an event so browser tests can listen the status events.
131   static void AttachStatusEvent(base::WaitableEvent* status_event);
132
133   // Returns the status event type.
134   static EventType GetStatusEvent();
135
136   // Pass all renderers some basic initialization information.
137   void InitForAllRenderers();
138
139   // Reacts to a change in user preferences on whether auto-spell-correct should
140   // be enabled.
141   void OnEnableAutoSpellCorrectChanged();
142
143   // Reacts to a change in user preference on which language should be used for
144   // spellchecking.
145   void OnSpellCheckDictionaryChanged();
146
147   // Notification handler for changes to prefs::kSpellCheckUseSpellingService.
148   void OnUseSpellingServiceChanged();
149
150   PrefChangeRegistrar pref_change_registrar_;
151   content::NotificationRegistrar registrar_;
152
153   // A pointer to the BrowserContext which this service refers to.
154   content::BrowserContext* context_;
155
156   scoped_ptr<SpellCheckHostMetrics> metrics_;
157
158   scoped_ptr<SpellcheckCustomDictionary> custom_dictionary_;
159
160   scoped_ptr<SpellcheckHunspellDictionary> hunspell_dictionary_;
161
162   scoped_ptr<spellcheck::FeedbackSender> feedback_sender_;
163
164   base::WeakPtrFactory<SpellcheckService> weak_ptr_factory_;
165
166   DISALLOW_COPY_AND_ASSIGN(SpellcheckService);
167 };
168
169 #endif  // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_SERVICE_H_