| 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 void Initialize(); | 31 void Initialize(); |
| 32 | 32 |
| 33 // Clear |observer_|. Used to prevent calling back to a deleted object. | 33 // Clear |observer_|. Used to prevent calling back to a deleted object. |
| 34 void UnsetObserver(); | 34 void UnsetObserver(); |
| 35 | 35 |
| 36 // Add the given word to the custom words list and inform renderer of the | 36 // Add the given word to the custom words list and inform renderer of the |
| 37 // update. | 37 // update. |
| 38 void AddWord(const std::string& word); | 38 void AddWord(const std::string& word); |
| 39 | 39 |
| 40 const base::FileDescriptor& bdict_fd() const { return fd_; }; | 40 const base::PlatformFile& bdict_file() const { return file_; } |
| 41 | 41 |
| 42 const std::vector<std::string>& custom_words() const { return custom_words_; } | 42 const std::vector<std::string>& custom_words() const { return custom_words_; } |
| 43 | 43 |
| 44 const std::string& last_added_word() const { return custom_words_.back(); } | 44 const std::string& last_added_word() const { return custom_words_.back(); } |
| 45 | 45 |
| 46 const std::string& language() const { return language_; } | 46 const std::string& language() const { return language_; } |
| 47 | 47 |
| 48 private: | 48 private: |
| 49 // These two classes can destruct us. | 49 // These two classes can destruct us. |
| 50 friend class ChromeThread; | 50 friend class ChromeThread; |
| 51 friend class DeleteTask<SpellCheckHost>; | 51 friend class DeleteTask<SpellCheckHost>; |
| 52 | 52 |
| 53 virtual ~SpellCheckHost(); | 53 virtual ~SpellCheckHost(); |
| 54 | 54 |
| 55 // Figure out the location for the dictionary. This is only non-trivial for |
| 56 // Windows: |
| 57 // The default place whether the spellcheck dictionary can reside is |
| 58 // chrome::DIR_APP_DICTIONARIES. However, for systemwide installations, |
| 59 // this directory may not have permissions for download. In that case, the |
| 60 // alternate directory for download is chrome::DIR_USER_DATA. |
| 61 void InitializeDictionaryLocation(); |
| 62 |
| 55 // Load and parse the custom words dictionary and open the bdic file. | 63 // Load and parse the custom words dictionary and open the bdic file. |
| 56 // Executed on the file thread. | 64 // Executed on the file thread. |
| 57 void InitializeInternal(); | 65 void InitializeInternal(); |
| 58 | 66 |
| 59 // Inform |observer_| that initialization has finished. | 67 // Inform |observer_| that initialization has finished. |
| 60 void InformObserverOfInitialization(); | 68 void InformObserverOfInitialization(); |
| 61 | 69 |
| 62 // If |bdict_file_| is missing, we attempt to download it. | 70 // If |bdict_file_| is missing, we attempt to download it. |
| 63 void DownloadDictionary(); | 71 void DownloadDictionary(); |
| 64 | 72 |
| 65 // Write a custom dictionary addition to disk. | 73 // Write a custom dictionary addition to disk. |
| 66 void WriteWordToCustomDictionary(const std::string& word); | 74 void WriteWordToCustomDictionary(const std::string& word); |
| 67 | 75 |
| 68 // URLFetcher::Delegate implementation. Called when we finish downloading the | 76 // URLFetcher::Delegate implementation. Called when we finish downloading the |
| 69 // spellcheck dictionary; saves the dictionary to disk. | 77 // spellcheck dictionary; saves the dictionary to |data_|. |
| 70 virtual void OnURLFetchComplete(const URLFetcher* source, | 78 virtual void OnURLFetchComplete(const URLFetcher* source, |
| 71 const GURL& url, | 79 const GURL& url, |
| 72 const URLRequestStatus& status, | 80 const URLRequestStatus& status, |
| 73 int response_code, | 81 int response_code, |
| 74 const ResponseCookies& cookies, | 82 const ResponseCookies& cookies, |
| 75 const std::string& data); | 83 const std::string& data); |
| 76 | 84 |
| 77 // May be NULL. | 85 // May be NULL. |
| 78 Observer* observer_; | 86 Observer* observer_; |
| 79 | 87 |
| 80 // The desired location of the dictionary file (whether or not it exists yet). | 88 // The desired location of the dictionary file (whether or not t exists yet). |
| 81 FilePath bdict_file_; | 89 FilePath bdict_file_path_; |
| 82 | 90 |
| 83 // The location of the custom words file. | 91 // The location of the custom words file. |
| 84 FilePath custom_dictionary_file_; | 92 FilePath custom_dictionary_file_; |
| 85 | 93 |
| 86 // The language of the dictionary file. | 94 // The language of the dictionary file. |
| 87 std::string language_; | 95 std::string language_; |
| 88 | 96 |
| 89 // On POSIX, the file descriptor for the dictionary file. | 97 // The file descriptor/handle for the dictionary file. |
| 90 base::FileDescriptor fd_; | 98 base::PlatformFile file_; |
| 91 | 99 |
| 92 // In-memory cache of the custom words file. | 100 // In-memory cache of the custom words file. |
| 93 std::vector<std::string> custom_words_; | 101 std::vector<std::string> custom_words_; |
| 94 | 102 |
| 95 // We don't want to attempt to download a missing dictionary file more than | 103 // We don't want to attempt to download a missing dictionary file more than |
| 96 // once. | 104 // once. |
| 97 bool tried_to_download_; | 105 bool tried_to_download_; |
| 98 | 106 |
| 99 // Used for downloading the dictionary file. | 107 // Used for downloading the dictionary file. |
| 100 scoped_refptr<URLRequestContextGetter> request_context_getter_; | 108 scoped_refptr<URLRequestContextGetter> request_context_getter_; |
| 101 | 109 |
| 102 // Used for downloading the dictionary file. | 110 // Used for downloading the dictionary file. |
| 103 scoped_ptr<URLFetcher> fetcher_; | 111 scoped_ptr<URLFetcher> fetcher_; |
| 104 }; | 112 }; |
| 105 | 113 |
| 106 #endif // CHROME_BROWSER_SPELLCHECK_HOST_H_ | 114 #endif // CHROME_BROWSER_SPELLCHECK_HOST_H_ |
| OLD | NEW |