Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / spellchecker / spellcheck_hunspell_dictionary.h
blob2a37a55f92b20635989871e90a82d249fa159074
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_HUNSPELL_DICTIONARY_H_
6 #define CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_HUNSPELL_DICTIONARY_H_
8 #include "base/files/file.h"
9 #include "base/files/file_path.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/move.h"
13 #include "base/observer_list.h"
14 #include "chrome/browser/spellchecker/spellcheck_dictionary.h"
15 #include "net/url_request/url_fetcher_delegate.h"
17 class GURL;
18 class SpellcheckService;
20 namespace net {
21 class URLFetcher;
22 class URLRequestContextGetter;
23 } // namespace net
25 // Defines the browser-side hunspell dictionary and provides access to it.
26 class SpellcheckHunspellDictionary
27 : public SpellcheckDictionary,
28 public net::URLFetcherDelegate,
29 public base::SupportsWeakPtr<SpellcheckHunspellDictionary> {
30 public:
31 // Interface to implement for observers of the Hunspell dictionary.
32 class Observer {
33 public:
34 // The dictionary has been initialized.
35 virtual void OnHunspellDictionaryInitialized() = 0;
37 // Dictionary download began.
38 virtual void OnHunspellDictionaryDownloadBegin() = 0;
40 // Dictionary download succeeded.
41 virtual void OnHunspellDictionaryDownloadSuccess() = 0;
43 // Dictionary download failed.
44 virtual void OnHunspellDictionaryDownloadFailure() = 0;
47 SpellcheckHunspellDictionary(
48 const std::string& language,
49 net::URLRequestContextGetter* request_context_getter,
50 SpellcheckService* spellcheck_service);
51 ~SpellcheckHunspellDictionary() override;
53 // SpellcheckDictionary implementation:
54 void Load() override;
56 // Retry downloading |dictionary_file_|.
57 void RetryDownloadDictionary(
58 net::URLRequestContextGetter* request_context_getter);
60 // Returns true if the dictionary is ready to use.
61 virtual bool IsReady() const;
63 const base::File& GetDictionaryFile() const;
64 const std::string& GetLanguage() const;
65 bool IsUsingPlatformChecker() const;
67 // Add an observer for Hunspell dictionary events.
68 void AddObserver(Observer* observer);
70 // Remove an observer for Hunspell dictionary events.
71 void RemoveObserver(Observer* observer);
73 // Whether dictionary is being downloaded.
74 bool IsDownloadInProgress();
76 // Whether dictionary download failed.
77 bool IsDownloadFailure();
79 private:
80 // Dictionary download status.
81 enum DownloadStatus {
82 DOWNLOAD_NONE,
83 DOWNLOAD_IN_PROGRESS,
84 DOWNLOAD_FAILED,
87 // Dictionary file information to be passed between the FILE and UI threads.
88 struct DictionaryFile {
89 MOVE_ONLY_TYPE_FOR_CPP_03(DictionaryFile, RValue)
90 public:
91 DictionaryFile();
92 ~DictionaryFile();
94 // C++03 move emulation of this type.
95 DictionaryFile(RValue other);
96 DictionaryFile& operator=(RValue other);
98 // The desired location of the dictionary file, whether or not it exists.
99 base::FilePath path;
101 // The dictionary file.
102 base::File file;
105 // net::URLFetcherDelegate implementation. Called when dictionary download
106 // finishes.
107 void OnURLFetchComplete(const net::URLFetcher* source) override;
109 // Determine the correct url to download the dictionary.
110 GURL GetDictionaryURL();
112 // Attempt to download the dictionary.
113 void DownloadDictionary(GURL url);
115 // Figures out the location for the dictionary, verifies its contents, and
116 // opens it.
117 static DictionaryFile OpenDictionaryFile(const base::FilePath& path);
119 // Gets the default location for the dictionary file.
120 static DictionaryFile InitializeDictionaryLocation(
121 const std::string& language);
123 // The reply point for PostTaskAndReplyWithResult, called after the dictionary
124 // file has been initialized.
125 void InitializeDictionaryLocationComplete(DictionaryFile file);
127 // The reply point for PostTaskAndReplyWithResult, called after the dictionary
128 // file has been saved.
129 void SaveDictionaryDataComplete(bool dictionary_saved);
131 // Notify listeners that the dictionary has been initialized.
132 void InformListenersOfInitialization();
134 // Notify listeners that the dictionary download failed.
135 void InformListenersOfDownloadFailure();
137 // The language of the dictionary file.
138 std::string language_;
140 // Whether to use the platform spellchecker instead of Hunspell.
141 bool use_browser_spellchecker_;
143 // Used for downloading the dictionary file. SpellcheckHunspellDictionary does
144 // not hold a reference, and it is only valid to use it on the UI thread.
145 net::URLRequestContextGetter* request_context_getter_;
147 // Used for downloading the dictionary file.
148 scoped_ptr<net::URLFetcher> fetcher_;
150 SpellcheckService* spellcheck_service_;
152 // Observers of Hunspell dictionary events.
153 base::ObserverList<Observer> observers_;
155 // Status of the dictionary download.
156 DownloadStatus download_status_;
158 // Dictionary file path and descriptor.
159 DictionaryFile dictionary_file_;
161 base::WeakPtrFactory<SpellcheckHunspellDictionary> weak_ptr_factory_;
163 DISALLOW_COPY_AND_ASSIGN(SpellcheckHunspellDictionary);
166 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_HUNSPELL_DICTIONARY_H_