| OLD | NEW |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-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_SPELLCHECKER_H_ | 5 #ifndef CHROME_BROWSER_SPELLCHECKER_H_ |
| 6 #define CHROME_BROWSER_SPELLCHECKER_H_ | 6 #define CHROME_BROWSER_SPELLCHECKER_H_ |
| 7 | 7 |
| 8 #include <queue> |
| 9 #include <string> |
| 8 #include <vector> | 10 #include <vector> |
| 9 #include <string> | |
| 10 | 11 |
| 11 #include "app/l10n_util.h" | 12 #include "app/l10n_util.h" |
| 12 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 14 #include "base/task.h" |
| 15 #include "base/time.h" |
| 13 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/net/url_fetcher.h" | 17 #include "chrome/browser/net/url_fetcher.h" |
| 15 #include "chrome/browser/profile.h" | 18 #include "chrome/browser/profile.h" |
| 16 #include "chrome/browser/spellcheck_worditerator.h" | 19 #include "chrome/browser/spellcheck_worditerator.h" |
| 17 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
| 18 #include "chrome/common/pref_member.h" | 21 #include "chrome/common/pref_member.h" |
| 19 | |
| 20 #include "base/task.h" | |
| 21 #include "unicode/uscript.h" | 22 #include "unicode/uscript.h" |
| 22 | 23 |
| 23 class FilePath; | 24 class FilePath; |
| 24 class Hunspell; | 25 class Hunspell; |
| 25 class PrefService; | 26 class PrefService; |
| 26 class Profile; | 27 class Profile; |
| 27 class MessageLoop; | 28 class MessageLoop; |
| 28 class URLRequestContext; | 29 class URLRequestContext; |
| 29 class URLFetcher; | 30 class URLFetcher; |
| 30 | 31 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 // This function returns the corresponding language-region code for the | 109 // This function returns the corresponding language-region code for the |
| 109 // spell check language. For example, for hi, it returns hi-IN. | 110 // spell check language. For example, for hi, it returns hi-IN. |
| 110 static std::string GetSpellCheckLanguageRegion(std::string input_language); | 111 static std::string GetSpellCheckLanguageRegion(std::string input_language); |
| 111 | 112 |
| 112 // This function returns ll (language code) from ll-RR where 'RR' (region | 113 // This function returns ll (language code) from ll-RR where 'RR' (region |
| 113 // code) is redundant. However, if the region code matters, it's preserved. | 114 // code) is redundant. However, if the region code matters, it's preserved. |
| 114 // That is, it returns 'hi' and 'en-GB' for 'hi-IN' and 'en-GB' respectively. | 115 // That is, it returns 'hi' and 'en-GB' for 'hi-IN' and 'en-GB' respectively. |
| 115 static std::string GetLanguageFromLanguageRegion(std::string input_language); | 116 static std::string GetLanguageFromLanguageRegion(std::string input_language); |
| 116 | 117 |
| 117 private: | 118 private: |
| 119 friend class ReadDictionaryTask; |
| 120 |
| 118 // URLFetcher::Delegate implementation. Called when we finish downloading the | 121 // URLFetcher::Delegate implementation. Called when we finish downloading the |
| 119 // spellcheck dictionary; saves the dictionary to disk. | 122 // spellcheck dictionary; saves the dictionary to disk. |
| 120 // TODO(sidchat): Save to disk in the file thread instead of the IO thread. | |
| 121 virtual void OnURLFetchComplete(const URLFetcher* source, | 123 virtual void OnURLFetchComplete(const URLFetcher* source, |
| 122 const GURL& url, | 124 const GURL& url, |
| 123 const URLRequestStatus& status, | 125 const URLRequestStatus& status, |
| 124 int response_code, | 126 int response_code, |
| 125 const ResponseCookies& cookies, | 127 const ResponseCookies& cookies, |
| 126 const std::string& data); | 128 const std::string& data); |
| 127 | 129 |
| 128 // When called, relays the request to check the spelling to the proper | 130 // When called, relays the request to check the spelling to the proper |
| 129 // backend, either hunspell or a platform-specific backend. | 131 // backend, either hunspell or a platform-specific backend. |
| 130 bool CheckSpelling(const std::string& word_to_check, int tag); | 132 bool CheckSpelling(const std::string& word_to_check, int tag); |
| 131 | 133 |
| 132 // When called, relays the request to fill the list with suggestions to | 134 // When called, relays the request to fill the list with suggestions to |
| 133 // the proper backend, either hunspell or a platform-specific backend. | 135 // the proper backend, either hunspell or a platform-specific backend. |
| 134 void FillSuggestionList(const std::string& wrong_word, | 136 void FillSuggestionList(const std::string& wrong_word, |
| 135 std::vector<std::wstring>* optional_suggestions); | 137 std::vector<std::wstring>* optional_suggestions); |
| 136 | 138 |
| 137 // Initializes the Hunspell Dictionary. | 139 // Initializes the Hunspell Dictionary. |
| 138 bool Initialize(); | 140 bool Initialize(); |
| 139 | 141 |
| 140 // After |hunspell_| is initialized, this function is called to add custom | 142 // Called when |hunspell| is done loading, succesfully or not. If |hunspell| |
| 141 // words from the custom dictionary to the |hunspell_|. | 143 // and |bdict_file| are non-NULL, assume ownership. |
| 142 void AddCustomWordsToHunspell(); | 144 void HunspellInited(Hunspell* hunspell, |
| 145 file_util::MemoryMappedFile* bdict_file, |
| 146 bool file_existed); |
| 143 | 147 |
| 144 // Memory maps the given .bdic file. On success, it will return true and will | 148 // Either start downloading a dictionary if we have not already, or do nothing |
| 145 // place the data and length into the given out parameters. | 149 // if we have already tried to download one. |
| 146 bool MapBdictFile(const unsigned char** data, size_t* length); | 150 void DoDictionaryDownload(); |
| 147 | 151 |
| 148 // Returns whether or not the given word is a contraction of valid words | 152 // Returns whether or not the given word is a contraction of valid words |
| 149 // (e.g. "word:word"). | 153 // (e.g. "word:word"). |
| 150 bool IsValidContraction(const string16& word, int tag); | 154 bool IsValidContraction(const string16& word, int tag); |
| 151 | 155 |
| 152 // Return the file name of the dictionary, including the path and the version | 156 // Return the file name of the dictionary, including the path and the version |
| 153 // numbers. | 157 // numbers. |
| 154 FilePath GetVersionedFileName(const std::string& language, | 158 FilePath GetVersionedFileName(const std::string& language, |
| 155 const FilePath& dict_dir); | 159 const FilePath& dict_dir); |
| 156 | 160 |
| 157 static std::string GetCorrespondingSpellCheckLanguage( | 161 static std::string GetCorrespondingSpellCheckLanguage( |
| 158 const std::string& language); | 162 const std::string& language); |
| 159 | 163 |
| 160 // Start downloading a dictionary from the server. On completion, the | 164 // Start downloading a dictionary from the server. On completion, the |
| 161 // OnURLFetchComplete() function is invoked. | 165 // OnURLFetchComplete() function is invoked. |
| 162 void StartDictionaryDownload(const FilePath& file_name); | 166 void StartDictionaryDownload(const FilePath& file_name); |
| 163 | 167 |
| 164 // This method is called in the IO thread after dictionary download has | 168 // This method is called in the IO thread after dictionary download has |
| 165 // completed in FILE thread. | 169 // completed in FILE thread. |
| 166 void OnDictionarySaveComplete(){ obtaining_dictionary_ = false; } | 170 void OnDictionarySaveComplete(); |
| 167 | 171 |
| 168 // The given path to the directory whether SpellChecker first tries to | 172 // The given path to the directory whether SpellChecker first tries to |
| 169 // download the spellcheck bdic dictionary file. | 173 // download the spellcheck bdic dictionary file. |
| 170 FilePath given_dictionary_directory_; | 174 FilePath given_dictionary_directory_; |
| 171 | 175 |
| 172 // Path to the custom dictionary file. | 176 // Path to the custom dictionary file. |
| 173 FilePath custom_dictionary_file_name_; | 177 FilePath custom_dictionary_file_name_; |
| 174 | 178 |
| 175 // BDIC file name (e.g. en-US_1_1.bdic). | 179 // BDIC file name (e.g. en-US_1_1.bdic). |
| 176 FilePath bdic_file_name_; | 180 FilePath bdic_file_name_; |
| 177 | 181 |
| 178 // We memory-map the BDict file. | 182 // We memory-map the BDict file. |
| 179 scoped_ptr<file_util::MemoryMappedFile> bdict_file_; | 183 scoped_ptr<file_util::MemoryMappedFile> bdict_file_; |
| 180 | 184 |
| 181 // The hunspell dictionary in use. | 185 // The hunspell dictionary in use. |
| 182 scoped_ptr<Hunspell> hunspell_; | 186 scoped_ptr<Hunspell> hunspell_; |
| 183 | 187 |
| 184 // Represents character attributes used for filtering out characters which | 188 // Represents character attributes used for filtering out characters which |
| 185 // are not supported by this SpellChecker object. | 189 // are not supported by this SpellChecker object. |
| 186 SpellcheckCharAttribute character_attributes_; | 190 SpellcheckCharAttribute character_attributes_; |
| 187 | 191 |
| 188 // Flag indicating whether we've tried to initialize. If we've already | 192 // Flag indicating whether we've tried to initialize. If we've already |
| 189 // attempted initialiation, we won't retry to avoid failure loops. | 193 // attempted initialiation, we won't retry to avoid failure loops. |
| 190 bool tried_to_init_; | 194 bool tried_to_init_; |
| 191 | 195 |
| 192 // The language that this spellchecker works in. | 196 // The language that this spellchecker works in. |
| 193 std::string language_; | 197 std::string language_; |
| 194 | 198 |
| 195 #ifndef NDEBUG | |
| 196 // This object must only be used on the same thread. However, it is normally | 199 // This object must only be used on the same thread. However, it is normally |
| 197 // created on the UI thread. This checks calls to SpellCheckWord and the | 200 // created on the UI thread. This checks calls to SpellCheckWord and the |
| 198 // destructor to make sure we're only ever running on the same thread. | 201 // destructor to make sure we're only ever running on the same thread. |
| 199 // | 202 // |
| 200 // This will be NULL if it is not initialized yet (not initialized in the | 203 // This will be NULL if it is not initialized yet (not initialized in the |
| 201 // constructor since that's on a different thread. | 204 // constructor since that's on a different thread). |
| 202 MessageLoop* worker_loop_; | 205 MessageLoop* worker_loop_; |
| 203 #endif | |
| 204 | 206 |
| 205 // Flag indicating whether we tried to download the dictionary file. | 207 // Flag indicating whether we tried to download the dictionary file. |
| 206 bool tried_to_download_dictionary_file_; | 208 bool tried_to_download_dictionary_file_; |
| 207 | 209 |
| 208 // File Thread Message Loop. | 210 // File Thread Message Loop. |
| 209 MessageLoop* file_loop_; | 211 MessageLoop* file_loop_; |
| 210 | 212 |
| 211 // UI Thread Message Loop. | |
| 212 MessageLoop* ui_loop_; | |
| 213 | |
| 214 // Used for requests. MAY BE NULL which means don't try to download. | 213 // Used for requests. MAY BE NULL which means don't try to download. |
| 215 URLRequestContext* url_request_context_; | 214 URLRequestContext* url_request_context_; |
| 216 | 215 |
| 217 // True when we're downloading or saving a dictionary. | 216 // True when we're downloading or saving a dictionary. |
| 218 bool obtaining_dictionary_; | 217 bool obtaining_dictionary_; |
| 219 | 218 |
| 220 // Remember state for auto spell correct. | 219 // Remember state for auto spell correct. |
| 221 bool auto_spell_correct_turned_on_; | 220 bool auto_spell_correct_turned_on_; |
| 222 | 221 |
| 223 // True if a platform-specific spellchecking engine is being used, | 222 // True if a platform-specific spellchecking engine is being used, |
| 224 // and False if hunspell is being used. | 223 // and False if hunspell is being used. |
| 225 bool is_using_platform_spelling_engine_; | 224 bool is_using_platform_spelling_engine_; |
| 226 | 225 |
| 227 // URLFetcher to download a file in memory. | 226 // URLFetcher to download a file in memory. |
| 228 scoped_ptr<URLFetcher> fetcher_; | 227 scoped_ptr<URLFetcher> fetcher_; |
| 229 | 228 |
| 229 // While Hunspell is loading, we add any new custom words to this queue. |
| 230 // We will add them to |hunspell_| when it is done loading. |
| 231 std::queue<std::string> custom_words_; |
| 232 |
| 230 // Used for generating callbacks to spellchecker, since spellchecker is a | 233 // Used for generating callbacks to spellchecker, since spellchecker is a |
| 231 // non-reference counted object. | 234 // non-reference counted object. |
| 232 ScopedRunnableMethodFactory<SpellChecker> | 235 ScopedRunnableMethodFactory<SpellChecker> method_factory_; |
| 233 on_dictionary_save_complete_callback_factory_; | |
| 234 | 236 |
| 235 DISALLOW_COPY_AND_ASSIGN(SpellChecker); | 237 DISALLOW_COPY_AND_ASSIGN(SpellChecker); |
| 236 }; | 238 }; |
| 237 | 239 |
| 238 #endif // CHROME_BROWSER_SPELLCHECKER_H_ | 240 #endif // CHROME_BROWSER_SPELLCHECKER_H_ |
| OLD | NEW |