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" | |
12 #include "base/file_path.h" | 11 #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::FileDescriptor& bdict_fd() const { return fd_; }; | 38 const base::PlatformFile& bdict_file() const { return file_; }; |
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 |
53 // Load and parse the custom words dictionary and open the bdic file. | 61 // Load and parse the custom words dictionary and open the bdic file. |
54 // Executed on the file thread. | 62 // Executed on the file thread. |
55 void Initialize(); | 63 void Initialize(); |
56 | 64 |
57 // Inform |observer_| that initialization has finished. | 65 // Inform |observer_| that initialization has finished. |
58 void InformObserverOfInitialization(); | 66 void InformObserverOfInitialization(); |
59 | 67 |
60 // If |bdict_file_| is missing, we attempt to download it. | 68 // If |bdict_file_| is missing, we attempt to download it. |
61 void DownloadDictionary(); | 69 void DownloadDictionary(); |
62 | 70 |
63 // Write a custom dictionary addition to disk. | 71 // Write a custom dictionary addition to disk. |
64 void WriteWordToCustomDictionary(const std::string& word); | 72 void WriteWordToCustomDictionary(const std::string& word); |
65 | 73 |
66 // URLFetcher::Delegate implementation. Called when we finish downloading the | 74 // URLFetcher::Delegate implementation. Called when we finish downloading the |
67 // spellcheck dictionary; saves the dictionary to disk. | 75 // spellcheck dictionary; saves the dictionary to disk. |
68 virtual void OnURLFetchComplete(const URLFetcher* source, | 76 virtual void OnURLFetchComplete(const URLFetcher* source, |
69 const GURL& url, | 77 const GURL& url, |
70 const URLRequestStatus& status, | 78 const URLRequestStatus& status, |
71 int response_code, | 79 int response_code, |
72 const ResponseCookies& cookies, | 80 const ResponseCookies& cookies, |
73 const std::string& data); | 81 const std::string& data); |
74 | 82 |
75 // May be NULL. | 83 // May be NULL. |
76 Observer* observer_; | 84 Observer* observer_; |
77 | 85 |
78 // The desired location of the dictionary file (whether or not it exists yet). | 86 // The desired location of the dictionary file (whether or not it exists yet). |
79 FilePath bdict_file_; | 87 FilePath bdict_file_path_; |
80 | 88 |
81 // The location of the custom words file. | 89 // The location of the custom words file. |
82 FilePath custom_dictionary_file_; | 90 FilePath custom_dictionary_file_; |
83 | 91 |
84 // The language of the dictionary file. | 92 // The language of the dictionary file. |
85 std::string language_; | 93 std::string language_; |
86 | 94 |
87 // On POSIX, the file descriptor for the dictionary file. | 95 // The file descriptor/handle for the dictionary file. |
88 base::FileDescriptor fd_; | 96 base::PlatformFile file_; |
89 | 97 |
90 // In-memory cache of the custom words file. | 98 // In-memory cache of the custom words file. |
91 std::vector<std::string> custom_words_; | 99 std::vector<std::string> custom_words_; |
92 | 100 |
93 // We don't want to attempt to download a missing dictionary file more than | 101 // We don't want to attempt to download a missing dictionary file more than |
94 // once. | 102 // once. |
95 bool tried_to_download_; | 103 bool tried_to_download_; |
96 | 104 |
97 // Used for downloading the dictionary file. | 105 // Used for downloading the dictionary file. |
98 scoped_refptr<URLRequestContextGetter> request_context_getter_; | 106 scoped_refptr<URLRequestContextGetter> request_context_getter_; |
99 | 107 |
100 // Used for downloading the dictionary file. | 108 // Used for downloading the dictionary file. |
101 scoped_ptr<URLFetcher> fetcher_; | 109 scoped_ptr<URLFetcher> fetcher_; |
102 }; | 110 }; |
103 | 111 |
104 #endif // CHROME_BROWSER_SPELLCHECK_HOST_H_ | 112 #endif // CHROME_BROWSER_SPELLCHECK_HOST_H_ |
OLD | NEW |