OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_SPELLCHECK_HOST_H_ | 5 #ifndef CHROME_BROWSER_SPELLCHECK_HOST_H_ |
6 #define CHROME_BROWSER_SPELLCHECK_HOST_H_ | 6 #define CHROME_BROWSER_SPELLCHECK_HOST_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
| 11 #include "base/file_descriptor_posix.h" |
11 #include "base/file_path.h" | 12 #include "base/file_path.h" |
12 #include "base/platform_file.h" | |
13 #include "base/ref_counted.h" | 13 #include "base/ref_counted.h" |
14 #include "chrome/browser/chrome_thread.h" | 14 #include "chrome/browser/chrome_thread.h" |
15 #include "chrome/browser/net/url_fetcher.h" | 15 #include "chrome/browser/net/url_fetcher.h" |
16 #include "chrome/browser/net/url_request_context_getter.h" | 16 #include "chrome/browser/net/url_request_context_getter.h" |
17 | 17 |
18 class SpellCheckHost : public base::RefCountedThreadSafe<SpellCheckHost, | 18 class SpellCheckHost : public base::RefCountedThreadSafe<SpellCheckHost, |
19 ChromeThread::DeleteOnFileThread>, | 19 ChromeThread::DeleteOnFileThread>, |
20 public URLFetcher::Delegate { | 20 public URLFetcher::Delegate { |
21 public: | 21 public: |
22 class Observer { | 22 class Observer { |
23 public: | 23 public: |
24 virtual void SpellCheckHostInitialized() = 0; | 24 virtual void SpellCheckHostInitialized() = 0; |
25 }; | 25 }; |
26 | 26 |
27 SpellCheckHost(Observer* observer, | 27 SpellCheckHost(Observer* observer, |
28 const std::string& language, | 28 const std::string& language, |
29 URLRequestContextGetter* request_context_getter); | 29 URLRequestContextGetter* request_context_getter); |
30 | 30 |
31 // Clear |observer_|. Used to prevent calling back to a deleted object. | 31 // Clear |observer_|. Used to prevent calling back to a deleted object. |
32 void UnsetObserver(); | 32 void UnsetObserver(); |
33 | 33 |
34 // Add the given word to the custom words list and inform renderer of the | 34 // Add the given word to the custom words list and inform renderer of the |
35 // update. | 35 // update. |
36 void AddWord(const std::string& word); | 36 void AddWord(const std::string& word); |
37 | 37 |
38 const base::PlatformFile& bdict_file() const { return file_; } | 38 const base::FileDescriptor& bdict_fd() const { return fd_; }; |
39 | 39 |
40 const std::vector<std::string>& custom_words() const { return custom_words_; } | 40 const std::vector<std::string>& custom_words() const { return custom_words_; } |
41 | 41 |
42 const std::string& last_added_word() const { return custom_words_.back(); } | 42 const std::string& last_added_word() const { return custom_words_.back(); } |
43 | 43 |
44 const std::string& language() const { return language_; } | 44 const std::string& language() const { return language_; } |
45 | 45 |
46 private: | 46 private: |
47 // These two classes can destruct us. | 47 // These two classes can destruct us. |
48 friend class ChromeThread; | 48 friend class ChromeThread; |
49 friend class DeleteTask<SpellCheckHost>; | 49 friend class DeleteTask<SpellCheckHost>; |
50 | 50 |
51 virtual ~SpellCheckHost(); | 51 virtual ~SpellCheckHost(); |
52 | 52 |
53 // Figure out the location for the dictionary. This is only non-trivial for | |
54 // Windows: | |
55 // The default place whether the spellcheck dictionary can reside is | |
56 // chrome::DIR_APP_DICTIONARIES. However, for systemwide installations, | |
57 // this directory may not have permissions for download. In that case, the | |
58 // alternate directory for download is chrome::DIR_USER_DATA. | |
59 void InitializeDictionaryLocation(); | |
60 | |
61 // Load and parse the custom words dictionary and open the bdic file. | 53 // Load and parse the custom words dictionary and open the bdic file. |
62 // Executed on the file thread. | 54 // Executed on the file thread. |
63 void Initialize(); | 55 void Initialize(); |
64 | 56 |
65 // Inform |observer_| that initialization has finished. | 57 // Inform |observer_| that initialization has finished. |
66 void InformObserverOfInitialization(); | 58 void InformObserverOfInitialization(); |
67 | 59 |
68 // If |bdict_file_| is missing, we attempt to download it. | 60 // If |bdict_file_| is missing, we attempt to download it. |
69 void DownloadDictionary(); | 61 void DownloadDictionary(); |
70 | 62 |
71 // Write a custom dictionary addition to disk. | 63 // Write a custom dictionary addition to disk. |
72 void WriteWordToCustomDictionary(const std::string& word); | 64 void WriteWordToCustomDictionary(const std::string& word); |
73 | 65 |
74 // URLFetcher::Delegate implementation. Called when we finish downloading the | 66 // URLFetcher::Delegate implementation. Called when we finish downloading the |
75 // spellcheck dictionary; saves the dictionary to disk. | 67 // spellcheck dictionary; saves the dictionary to disk. |
76 virtual void OnURLFetchComplete(const URLFetcher* source, | 68 virtual void OnURLFetchComplete(const URLFetcher* source, |
77 const GURL& url, | 69 const GURL& url, |
78 const URLRequestStatus& status, | 70 const URLRequestStatus& status, |
79 int response_code, | 71 int response_code, |
80 const ResponseCookies& cookies, | 72 const ResponseCookies& cookies, |
81 const std::string& data); | 73 const std::string& data); |
82 | 74 |
83 // May be NULL. | 75 // May be NULL. |
84 Observer* observer_; | 76 Observer* observer_; |
85 | 77 |
86 // The desired location of the dictionary file (whether or not it exists yet). | 78 // The desired location of the dictionary file (whether or not it exists yet). |
87 FilePath bdict_file_path_; | 79 FilePath bdict_file_; |
88 | 80 |
89 // The location of the custom words file. | 81 // The location of the custom words file. |
90 FilePath custom_dictionary_file_; | 82 FilePath custom_dictionary_file_; |
91 | 83 |
92 // The language of the dictionary file. | 84 // The language of the dictionary file. |
93 std::string language_; | 85 std::string language_; |
94 | 86 |
95 // The file descriptor/handle for the dictionary file. | 87 // On POSIX, the file descriptor for the dictionary file. |
96 base::PlatformFile file_; | 88 base::FileDescriptor fd_; |
97 | 89 |
98 // In-memory cache of the custom words file. | 90 // In-memory cache of the custom words file. |
99 std::vector<std::string> custom_words_; | 91 std::vector<std::string> custom_words_; |
100 | 92 |
101 // We don't want to attempt to download a missing dictionary file more than | 93 // We don't want to attempt to download a missing dictionary file more than |
102 // once. | 94 // once. |
103 bool tried_to_download_; | 95 bool tried_to_download_; |
104 | 96 |
105 // Used for downloading the dictionary file. | 97 // Used for downloading the dictionary file. |
106 scoped_refptr<URLRequestContextGetter> request_context_getter_; | 98 scoped_refptr<URLRequestContextGetter> request_context_getter_; |
107 | 99 |
108 // Used for downloading the dictionary file. | 100 // Used for downloading the dictionary file. |
109 scoped_ptr<URLFetcher> fetcher_; | 101 scoped_ptr<URLFetcher> fetcher_; |
110 }; | 102 }; |
111 | 103 |
112 #endif // CHROME_BROWSER_SPELLCHECK_HOST_H_ | 104 #endif // CHROME_BROWSER_SPELLCHECK_HOST_H_ |
OLD | NEW |