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