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