Home | History | Annotate | Download | only in spellchecker
      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 Profile;
     22 class SpellCheckHostMetrics;
     23 
     24 namespace base {
     25 class WaitableEvent;
     26 }
     27 
     28 namespace content {
     29 class RenderProcessHost;
     30 }
     31 
     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 BrowserContextKeyedService,
     36                           public content::NotificationObserver,
     37                           public SpellcheckCustomDictionary::Observer,
     38                           public SpellcheckHunspellDictionary::Observer {
     39  public:
     40   // Event types used for reporting the status of this class and its derived
     41   // classes to browser tests.
     42   enum EventType {
     43     BDICT_NOTINITIALIZED,
     44     BDICT_CORRUPTED,
     45   };
     46 
     47   // Dictionary format used for loading an external dictionary.
     48   enum DictionaryFormat {
     49     DICT_HUNSPELL,
     50     DICT_TEXT,
     51     DICT_UNKNOWN,
     52   };
     53 
     54   explicit SpellcheckService(Profile* profile);
     55   virtual ~SpellcheckService();
     56 
     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 string16, but the implementation
     61   // has some dependencies in l10n util that need porting first.
     62   static int GetSpellCheckLanguages(Profile* profile,
     63                                     std::vector<std::string>* languages);
     64 
     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);
     71 
     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);
     77 
     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);
     81 
     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);
     85 
     86   // Returns a metrics counter associated with this object,
     87   // or null when metrics recording is disabled.
     88   SpellCheckHostMetrics* GetMetrics() const;
     89 
     90   // Returns the instance of the custom dictionary.
     91   SpellcheckCustomDictionary* GetCustomDictionary();
     92 
     93   // Returns the instance of the Hunspell dictionary.
     94   SpellcheckHunspellDictionary* GetHunspellDictionary();
     95 
     96   // Returns the instance of the spelling service feedback sender.
     97   spellcheck::FeedbackSender* GetFeedbackSender();
     98 
     99   // Load a dictionary from a given path. Format specifies how the dictionary
    100   // is stored. Return value is true if successful.
    101   bool LoadExternalDictionary(std::string language,
    102                               std::string locale,
    103                               std::string path,
    104                               DictionaryFormat format);
    105 
    106   // Unload a dictionary. The path is given to identify the dictionary.
    107   // Return value is true if successful.
    108   bool UnloadExternalDictionary(std::string path);
    109 
    110   // NotificationProfile implementation.
    111   virtual void Observe(int type,
    112                        const content::NotificationSource& source,
    113                        const content::NotificationDetails& details) OVERRIDE;
    114 
    115   // SpellcheckCustomDictionary::Observer implementation.
    116   virtual void OnCustomDictionaryLoaded() OVERRIDE;
    117   virtual void OnCustomDictionaryChanged(
    118       const SpellcheckCustomDictionary::Change& dictionary_change) OVERRIDE;
    119 
    120   // SpellcheckHunspellDictionary::Observer implementation.
    121   virtual void OnHunspellDictionaryInitialized() OVERRIDE;
    122   virtual void OnHunspellDictionaryDownloadBegin() OVERRIDE;
    123   virtual void OnHunspellDictionaryDownloadSuccess() OVERRIDE;
    124   virtual void OnHunspellDictionaryDownloadFailure() OVERRIDE;
    125 
    126  private:
    127   FRIEND_TEST_ALL_PREFIXES(SpellcheckServiceBrowserTest, DeleteCorruptedBDICT);
    128 
    129   // Attaches an event so browser tests can listen the status events.
    130   static void AttachStatusEvent(base::WaitableEvent* status_event);
    131 
    132   // Returns the status event type.
    133   static EventType GetStatusEvent();
    134 
    135   // Pass all renderers some basic initialization information.
    136   void InitForAllRenderers();
    137 
    138   // Reacts to a change in user preferences on whether auto-spell-correct should
    139   // be enabled.
    140   void OnEnableAutoSpellCorrectChanged();
    141 
    142   // Reacts to a change in user preference on which language should be used for
    143   // spellchecking.
    144   void OnSpellCheckDictionaryChanged();
    145 
    146   // Notification handler for changes to prefs::kSpellCheckUseSpellingService.
    147   void OnUseSpellingServiceChanged();
    148 
    149   PrefChangeRegistrar pref_change_registrar_;
    150   content::NotificationRegistrar registrar_;
    151 
    152   // A pointer to the profile which this service refers to.
    153   Profile* profile_;
    154 
    155   scoped_ptr<SpellCheckHostMetrics> metrics_;
    156 
    157   scoped_ptr<SpellcheckCustomDictionary> custom_dictionary_;
    158 
    159   scoped_ptr<SpellcheckHunspellDictionary> hunspell_dictionary_;
    160 
    161   scoped_ptr<spellcheck::FeedbackSender> feedback_sender_;
    162 
    163   base::WeakPtrFactory<SpellcheckService> weak_ptr_factory_;
    164 
    165   DISALLOW_COPY_AND_ASSIGN(SpellcheckService);
    166 };
    167 
    168 #endif  // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_SERVICE_H_
    169