Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / spellchecker / spellcheck_hunspell_dictionary.h
bloba2c506845931a433574e811104366571c3e43872
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 <string>
10 #include "base/files/file.h"
11 #include "base/files/file_path.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/move.h"
15 #include "base/observer_list.h"
16 #include "chrome/browser/spellchecker/spellcheck_dictionary.h"
17 #include "net/url_request/url_fetcher_delegate.h"
19 class GURL;
20 class SpellcheckService;
22 namespace net {
23 class URLFetcher;
24 class URLRequestContextGetter;
25 } // namespace net
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(
38 const std::string& language) = 0;
40 // Dictionary download began.
41 virtual void OnHunspellDictionaryDownloadBegin(
42 const std::string& language) = 0;
44 // Dictionary download succeeded.
45 virtual void OnHunspellDictionaryDownloadSuccess(
46 const std::string& language) = 0;
48 // Dictionary download failed.
49 virtual void OnHunspellDictionaryDownloadFailure(
50 const std::string& language) = 0;
53 SpellcheckHunspellDictionary(
54 const std::string& language,
55 net::URLRequestContextGetter* request_context_getter,
56 SpellcheckService* spellcheck_service);
57 ~SpellcheckHunspellDictionary() override;
59 // SpellcheckDictionary implementation:
60 void Load() override;
62 // Retry downloading |dictionary_file_|.
63 void RetryDownloadDictionary(
64 net::URLRequestContextGetter* request_context_getter);
66 // Returns true if the dictionary is ready to use.
67 virtual bool IsReady() const;
69 const base::File& GetDictionaryFile() const;
70 const std::string& GetLanguage() const;
71 bool IsUsingPlatformChecker() const;
73 // Add an observer for Hunspell dictionary events.
74 void AddObserver(Observer* observer);
76 // Remove an observer for Hunspell dictionary events.
77 void RemoveObserver(Observer* observer);
79 // Whether dictionary is being downloaded.
80 bool IsDownloadInProgress();
82 // Whether dictionary download failed.
83 bool IsDownloadFailure();
85 private:
86 // Dictionary download status.
87 enum DownloadStatus {
88 DOWNLOAD_NONE,
89 DOWNLOAD_IN_PROGRESS,
90 DOWNLOAD_FAILED,
93 // Dictionary file information to be passed between the FILE and UI threads.
94 struct DictionaryFile {
95 MOVE_ONLY_TYPE_FOR_CPP_03(DictionaryFile, RValue)
96 public:
97 DictionaryFile();
98 ~DictionaryFile();
100 // C++03 move emulation of this type.
101 DictionaryFile(RValue other);
102 DictionaryFile& operator=(RValue other);
104 // The desired location of the dictionary file, whether or not it exists.
105 base::FilePath path;
107 // The dictionary file.
108 base::File file;
111 // net::URLFetcherDelegate implementation. Called when dictionary download
112 // finishes.
113 void OnURLFetchComplete(const net::URLFetcher* source) override;
115 // Determine the correct url to download the dictionary.
116 GURL GetDictionaryURL();
118 // Attempt to download the dictionary.
119 void DownloadDictionary(GURL url);
121 // Figures out the location for the dictionary, verifies its contents, and
122 // opens it.
123 static DictionaryFile OpenDictionaryFile(const base::FilePath& path);
125 // Gets the default location for the dictionary file.
126 static DictionaryFile InitializeDictionaryLocation(
127 const std::string& language);
129 // The reply point for PostTaskAndReplyWithResult, called after the dictionary
130 // file has been initialized.
131 void InitializeDictionaryLocationComplete(DictionaryFile file);
133 // The reply point for PostTaskAndReplyWithResult, called after the dictionary
134 // file has been saved.
135 void SaveDictionaryDataComplete(bool dictionary_saved);
137 // Notify listeners that the dictionary has been initialized.
138 void InformListenersOfInitialization();
140 // Notify listeners that the dictionary download failed.
141 void InformListenersOfDownloadFailure();
143 // The language of the dictionary file.
144 std::string language_;
146 // Whether to use the platform spellchecker instead of Hunspell.
147 bool use_browser_spellchecker_;
149 // Used for downloading the dictionary file. SpellcheckHunspellDictionary does
150 // not hold a reference, and it is only valid to use it on the UI thread.
151 net::URLRequestContextGetter* request_context_getter_;
153 // Used for downloading the dictionary file.
154 scoped_ptr<net::URLFetcher> fetcher_;
156 SpellcheckService* spellcheck_service_;
158 // Observers of Hunspell dictionary events.
159 base::ObserverList<Observer> observers_;
161 // Status of the dictionary download.
162 DownloadStatus download_status_;
164 // Dictionary file path and descriptor.
165 DictionaryFile dictionary_file_;
167 base::WeakPtrFactory<SpellcheckHunspellDictionary> weak_ptr_factory_;
169 DISALLOW_COPY_AND_ASSIGN(SpellcheckHunspellDictionary);
172 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_HUNSPELL_DICTIONARY_H_