• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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"
16 
17 class GURL;
18 class SpellcheckService;
19 
20 namespace net {
21 class URLFetcher;
22 class URLRequestContextGetter;
23 }  // namespace net
24 
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;
36 
37     // Dictionary download began.
38     virtual void OnHunspellDictionaryDownloadBegin() = 0;
39 
40     // Dictionary download succeeded.
41     virtual void OnHunspellDictionaryDownloadSuccess() = 0;
42 
43     // Dictionary download failed.
44     virtual void OnHunspellDictionaryDownloadFailure() = 0;
45   };
46 
47   SpellcheckHunspellDictionary(
48       const std::string& language,
49       net::URLRequestContextGetter* request_context_getter,
50       SpellcheckService* spellcheck_service);
51   virtual ~SpellcheckHunspellDictionary();
52 
53   // SpellcheckDictionary implementation:
54   virtual void Load() OVERRIDE;
55 
56   // Retry downloading |dictionary_file_|.
57   void RetryDownloadDictionary(
58       net::URLRequestContextGetter* request_context_getter);
59 
60   // Returns true if the dictionary is ready to use.
61   virtual bool IsReady() const;
62 
63   const base::File& GetDictionaryFile() const;
64   const std::string& GetLanguage() const;
65   bool IsUsingPlatformChecker() const;
66 
67   // Add an observer for Hunspell dictionary events.
68   void AddObserver(Observer* observer);
69 
70   // Remove an observer for Hunspell dictionary events.
71   void RemoveObserver(Observer* observer);
72 
73   // Whether dictionary is being downloaded.
74   bool IsDownloadInProgress();
75 
76   // Whether dictionary download failed.
77   bool IsDownloadFailure();
78 
79  private:
80   // Dictionary download status.
81   enum DownloadStatus {
82     DOWNLOAD_NONE,
83     DOWNLOAD_IN_PROGRESS,
84     DOWNLOAD_FAILED,
85   };
86 
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();
93 
94     // C++03 move emulation of this type.
95     DictionaryFile(RValue other);
96     DictionaryFile& operator=(RValue other);
97 
98     // The desired location of the dictionary file, whether or not it exists.
99     base::FilePath path;
100 
101     // The dictionary file.
102     base::File file;
103   };
104 
105   // net::URLFetcherDelegate implementation. Called when dictionary download
106   // finishes.
107   virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
108 
109   // Determine the correct url to download the dictionary.
110   GURL GetDictionaryURL();
111 
112   // Attempt to download the dictionary.
113   void DownloadDictionary(GURL url);
114 
115   // Figures out the location for the dictionary, verifies its contents, and
116   // opens it.
117   static DictionaryFile OpenDictionaryFile(const base::FilePath& path);
118 
119   // Gets the default location for the dictionary file.
120   static DictionaryFile InitializeDictionaryLocation(
121       const std::string& language);
122 
123   // The reply point for PostTaskAndReplyWithResult, called after the dictionary
124   // file has been initialized.
125   void InitializeDictionaryLocationComplete(DictionaryFile file);
126 
127   // The reply point for PostTaskAndReplyWithResult, called after the dictionary
128   // file has been saved.
129   void SaveDictionaryDataComplete(bool dictionary_saved);
130 
131   // Notify listeners that the dictionary has been initialized.
132   void InformListenersOfInitialization();
133 
134   // Notify listeners that the dictionary download failed.
135   void InformListenersOfDownloadFailure();
136 
137   // The language of the dictionary file.
138   std::string language_;
139 
140   // Whether to use the platform spellchecker instead of Hunspell.
141   bool use_platform_spellchecker_;
142 
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_;
146 
147   // Used for downloading the dictionary file.
148   scoped_ptr<net::URLFetcher> fetcher_;
149 
150   SpellcheckService* spellcheck_service_;
151 
152   // Observers of Hunspell dictionary events.
153   ObserverList<Observer> observers_;
154 
155   // Status of the dictionary download.
156   DownloadStatus download_status_;
157 
158   // Dictionary file path and descriptor.
159   DictionaryFile dictionary_file_;
160 
161   base::WeakPtrFactory<SpellcheckHunspellDictionary> weak_ptr_factory_;
162 
163   DISALLOW_COPY_AND_ASSIGN(SpellcheckHunspellDictionary);
164 };
165 
166 #endif  // CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_HUNSPELL_DICTIONARY_H_
167