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 "chrome/browser/spellchecker/spellcheck_dictionary.h"
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"
18 class SpellcheckService
;
19 struct DictionaryFile
;
23 class URLRequestContextGetter
;
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
> {
32 // Interface to implement for observers of the Hunspell dictionary.
35 // The dictionary has been initialized.
36 virtual void OnHunspellDictionaryInitialized() = 0;
38 // Dictionary download began.
39 virtual void OnHunspellDictionaryDownloadBegin() = 0;
41 // Dictionary download succeeded.
42 virtual void OnHunspellDictionaryDownloadSuccess() = 0;
44 // Dictionary download failed.
45 virtual void OnHunspellDictionaryDownloadFailure() = 0;
48 SpellcheckHunspellDictionary(
49 const std::string
& language
,
50 net::URLRequestContextGetter
* request_context_getter
,
51 SpellcheckService
* spellcheck_service
);
52 virtual ~SpellcheckHunspellDictionary();
54 // SpellcheckDictionary implementation:
55 virtual void Load() OVERRIDE
;
57 // Retry downloading |dictionary_file_|.
58 void RetryDownloadDictionary(
59 net::URLRequestContextGetter
* request_context_getter
);
61 // Returns true if the dictionary is ready to use.
62 virtual bool IsReady() const;
64 // TODO(rlp): Return by value.
65 const base::PlatformFile
& GetDictionaryFile() const;
66 const std::string
& GetLanguage() const;
67 bool IsUsingPlatformChecker() const;
69 // Add an observer for Hunspell dictionary events.
70 void AddObserver(Observer
* observer
);
72 // Remove an observer for Hunspell dictionary events.
73 void RemoveObserver(Observer
* observer
);
75 // Whether dictionary is being downloaded.
76 bool IsDownloadInProgress();
78 // Whether dictionary download failed.
79 bool IsDownloadFailure();
82 // Dictionary download status.
89 // net::URLFetcherDelegate implementation. Called when dictionary download
91 virtual void OnURLFetchComplete(const net::URLFetcher
* source
) OVERRIDE
;
93 // Determine the correct url to download the dictionary.
94 GURL
GetDictionaryURL();
96 // Attempt to download the dictionary.
97 void DownloadDictionary(GURL url
);
99 // The reply point for PostTaskAndReplyWithResult, called after the dictionary
100 // file has been initialized.
101 void InitializeDictionaryLocationComplete(scoped_ptr
<DictionaryFile
> file
);
103 // The reply point for PostTaskAndReplyWithResult, called after the dictionary
104 // file has been saved.
105 void SaveDictionaryDataComplete(bool dictionary_saved
);
107 // Notify listeners that the dictionary has been initialized.
108 void InformListenersOfInitialization();
110 // Notify listeners that the dictionary download failed.
111 void InformListenersOfDownloadFailure();
113 // The language of the dictionary file.
114 std::string language_
;
116 // Whether to use the platform spellchecker instead of Hunspell.
117 bool use_platform_spellchecker_
;
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_
;
123 // Used for downloading the dictionary file.
124 scoped_ptr
<net::URLFetcher
> fetcher_
;
126 SpellcheckService
* spellcheck_service_
;
128 // Observers of Hunspell dictionary events.
129 ObserverList
<Observer
> observers_
;
131 // Status of the dictionary download.
132 DownloadStatus download_status_
;
134 // Dictionary file path and descriptor.
135 scoped_ptr
<DictionaryFile
> dictionary_file_
;
137 base::WeakPtrFactory
<SpellcheckHunspellDictionary
> weak_ptr_factory_
;
139 DISALLOW_COPY_AND_ASSIGN(SpellcheckHunspellDictionary
);
142 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_HUNSPELL_DICTIONARY_H_