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_HUNSPELL_DICTIONARY_H_
      6 #define CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_HUNSPELL_DICTIONARY_H_
      7 
      8 #include "chrome/browser/spellchecker/spellcheck_dictionary.h"
      9 
     10 #include "base/files/file_path.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "base/memory/weak_ptr.h"
     13 #include "base/observer_list.h"
     14 #include "base/platform_file.h"
     15 #include "net/url_request/url_fetcher_delegate.h"
     16 
     17 class GURL;
     18 class Profile;
     19 class SpellcheckService;
     20 struct DictionaryFile;
     21 
     22 namespace net {
     23 class URLFetcher;
     24 class URLRequestContextGetter;
     25 }  // namespace net
     26 
     27 // Defines the browser-side hunspell dictionary and provides access to it.
     28 class SpellcheckHunspellDictionary
     29     : public SpellcheckDictionary,
     30       public net::URLFetcherDelegate,
     31       public base::SupportsWeakPtr<SpellcheckHunspellDictionary> {
     32  public:
     33   // Interface to implement for observers of the Hunspell dictionary.
     34   class Observer {
     35    public:
     36     // The dictionary has been initialized.
     37     virtual void OnHunspellDictionaryInitialized() = 0;
     38 
     39     // Dictionary download began.
     40     virtual void OnHunspellDictionaryDownloadBegin() = 0;
     41 
     42     // Dictionary download succeeded.
     43     virtual void OnHunspellDictionaryDownloadSuccess() = 0;
     44 
     45     // Dictionary download failed.
     46     virtual void OnHunspellDictionaryDownloadFailure() = 0;
     47   };
     48 
     49   SpellcheckHunspellDictionary(
     50       const std::string& language,
     51       net::URLRequestContextGetter* request_context_getter,
     52       SpellcheckService* spellcheck_service);
     53   virtual ~SpellcheckHunspellDictionary();
     54 
     55   // SpellcheckDictionary implementation:
     56   virtual void Load() OVERRIDE;
     57 
     58   // Retry downloading |dictionary_file_|.
     59   void RetryDownloadDictionary(
     60       net::URLRequestContextGetter* request_context_getter);
     61 
     62   // Returns true if the dictionary is ready to use.
     63   virtual bool IsReady() const;
     64 
     65   // TODO(rlp): Return by value.
     66   const base::PlatformFile& GetDictionaryFile() const;
     67   const std::string& GetLanguage() const;
     68   bool IsUsingPlatformChecker() const;
     69 
     70   // Add an observer for Hunspell dictionary events.
     71   void AddObserver(Observer* observer);
     72 
     73   // Remove an observer for Hunspell dictionary events.
     74   void RemoveObserver(Observer* observer);
     75 
     76   // Whether dictionary is being downloaded.
     77   bool IsDownloadInProgress();
     78 
     79   // Whether dictionary download failed.
     80   bool IsDownloadFailure();
     81 
     82  private:
     83   // Dictionary download status.
     84   enum DownloadStatus {
     85     DOWNLOAD_NONE,
     86     DOWNLOAD_IN_PROGRESS,
     87     DOWNLOAD_FAILED,
     88   };
     89 
     90   // net::URLFetcherDelegate implementation. Called when dictionary download
     91   // finishes.
     92   virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
     93 
     94   // Determine the correct url to download the dictionary.
     95   GURL GetDictionaryURL();
     96 
     97   // Attempt to download the dictionary.
     98   void DownloadDictionary(GURL url);
     99 
    100   // The reply point for PostTaskAndReplyWithResult, called after the dictionary
    101   // file has been initialized.
    102   void InitializeDictionaryLocationComplete(scoped_ptr<DictionaryFile> file);
    103 
    104   // The reply point for PostTaskAndReplyWithResult, called after the dictionary
    105   // file has been saved.
    106   void SaveDictionaryDataComplete(bool dictionary_saved);
    107 
    108   // Notify listeners that the dictionary has been initialized.
    109   void InformListenersOfInitialization();
    110 
    111   // Notify listeners that the dictionary download failed.
    112   void InformListenersOfDownloadFailure();
    113 
    114   // The language of the dictionary file.
    115   std::string language_;
    116 
    117   // Whether to use the platform spellchecker instead of Hunspell.
    118   bool use_platform_spellchecker_;
    119 
    120   // Used for downloading the dictionary file. SpellcheckHunspellDictionary does
    121   // not hold a reference, and it is only valid to use it on the UI thread.
    122   net::URLRequestContextGetter* request_context_getter_;
    123 
    124   // Used for downloading the dictionary file.
    125   scoped_ptr<net::URLFetcher> fetcher_;
    126 
    127   base::WeakPtrFactory<SpellcheckHunspellDictionary> weak_ptr_factory_;
    128 
    129   SpellcheckService* spellcheck_service_;
    130 
    131   // Observers of Hunspell dictionary events.
    132   ObserverList<Observer> observers_;
    133 
    134   // Status of the dictionary download.
    135   DownloadStatus download_status_;
    136 
    137   // Dictionary file path and descriptor.
    138   scoped_ptr<DictionaryFile> dictionary_file_;
    139 
    140   DISALLOW_COPY_AND_ASSIGN(SpellcheckHunspellDictionary);
    141 };
    142 
    143 #endif  // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_HUNSPELL_DICTIONARY_H_
    144