Chromium Code Reviews| 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 <vector> | 8 #include <vector> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "app/l10n_util.h" | 11 #include "app/l10n_util.h" |
| 12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/net/url_fetcher.h" | |
| 14 #include "chrome/browser/profile.h" | 15 #include "chrome/browser/profile.h" |
| 15 #include "chrome/browser/spellcheck_worditerator.h" | 16 #include "chrome/browser/spellcheck_worditerator.h" |
| 16 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 17 #include "chrome/common/pref_member.h" | 18 #include "chrome/common/pref_member.h" |
| 18 | 19 |
| 19 #include "base/task.h" | 20 #include "base/task.h" |
| 20 #include "unicode/uscript.h" | 21 #include "unicode/uscript.h" |
| 21 | 22 |
| 22 class FilePath; | 23 class FilePath; |
| 23 class Hunspell; | 24 class Hunspell; |
| 24 class PrefService; | 25 class PrefService; |
| 25 class Profile; | 26 class Profile; |
| 26 class MessageLoop; | 27 class MessageLoop; |
| 27 class URLRequestContext; | 28 class URLRequestContext; |
| 29 class URLFetcher; | |
| 28 | 30 |
| 29 namespace file_util { | 31 namespace file_util { |
| 30 class MemoryMappedFile; | 32 class MemoryMappedFile; |
| 31 } | 33 } |
| 32 | 34 |
| 33 // The Browser's Spell Checker. It checks and suggests corrections. | 35 // The Browser's Spell Checker. It checks and suggests corrections. |
| 34 // | 36 // |
| 35 // This object is not threadsafe. In normal usage (not unit tests) it lives on | 37 // This object is not threadsafe. In normal usage (not unit tests) it lives on |
| 36 // the I/O thread of the browser. It is threadsafe refcounted so that I/O thread | 38 // the I/O thread of the browser. It is threadsafe refcounted so that I/O thread |
| 37 // and the profile on the main thread (which gives out references to it) can | 39 // and the profile on the main thread (which gives out references to it) can |
| 38 // keep it. However, all usage of this must be on the I/O thread. | 40 // keep it. However, all usage of this must be on the I/O thread. |
| 39 // | 41 // |
| 40 // This object should also be deleted on the I/O thread only. It owns a | 42 // This object should also be deleted on the I/O thread only. It owns a |
| 41 // reference to URLRequestContext which in turn owns the cache, etc. and must be | 43 // reference to URLRequestContext which in turn owns the cache, etc. and must be |
| 42 // deleted on the I/O thread itself. | 44 // deleted on the I/O thread itself. |
| 43 class SpellChecker : public base::RefCountedThreadSafe<SpellChecker> { | 45 class SpellChecker : public base::RefCountedThreadSafe<SpellChecker>, |
| 46 public URLFetcher::Delegate { | |
| 44 public: | 47 public: |
| 45 // Creates the spellchecker by reading dictionaries from the given directory, | 48 // Creates the spellchecker by reading dictionaries from the given directory, |
| 46 // and defaulting to the given language. Both strings must be provided. | 49 // and defaulting to the given language. Both strings must be provided. |
| 47 // | 50 // |
| 48 // The request context is used to download dictionaries if they do not exist. | 51 // The request context is used to download dictionaries if they do not exist. |
| 49 // This can be NULL if you don't want this (like in tests). | 52 // This can be NULL if you don't want this (like in tests). |
| 50 // The |custom_dictionary_file_name| should be left blank so that Spellchecker | 53 // The |custom_dictionary_file_name| should be left blank so that Spellchecker |
| 51 // can figure out the custom dictionary file. It is non empty only for unit | 54 // can figure out the custom dictionary file. It is non empty only for unit |
| 52 // testing. | 55 // testing. |
| 53 SpellChecker(const FilePath& dict_dir, | 56 SpellChecker(const FilePath& dict_dir, |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 102 // This function returns the corresponding language-region code for the | 105 // This function returns the corresponding language-region code for the |
| 103 // spell check language. For example, for hi, it returns hi-IN. | 106 // spell check language. For example, for hi, it returns hi-IN. |
| 104 static std::string GetSpellCheckLanguageRegion(std::string input_language); | 107 static std::string GetSpellCheckLanguageRegion(std::string input_language); |
| 105 | 108 |
| 106 // This function returns ll (language code) from ll-RR where 'RR' (region | 109 // This function returns ll (language code) from ll-RR where 'RR' (region |
| 107 // code) is redundant. However, if the region code matters, it's preserved. | 110 // code) is redundant. However, if the region code matters, it's preserved. |
| 108 // That is, it returns 'hi' and 'en-GB' for 'hi-IN' and 'en-GB' respectively. | 111 // That is, it returns 'hi' and 'en-GB' for 'hi-IN' and 'en-GB' respectively. |
| 109 static std::string GetLanguageFromLanguageRegion(std::string input_language); | 112 static std::string GetLanguageFromLanguageRegion(std::string input_language); |
| 110 | 113 |
| 111 private: | 114 private: |
| 115 // URL Fetcher delegate implementation. | |
|
Peter Kasting
2009/08/10 22:25:44
Nit: I suggest changing this to:
// URLFetcher::D
| |
| 116 virtual void OnURLFetchComplete(const URLFetcher* source, | |
| 117 const GURL& url, | |
| 118 const URLRequestStatus& status, | |
| 119 int response_code, | |
| 120 const ResponseCookies& cookies, | |
| 121 const std::string& data); | |
| 112 | 122 |
| 113 // When called, relays the request to check the spelling to the proper | 123 // When called, relays the request to check the spelling to the proper |
| 114 // backend, either hunspell or a platform-specific backend. | 124 // backend, either hunspell or a platform-specific backend. |
| 115 bool CheckSpelling(const std::string& word_to_check); | 125 bool CheckSpelling(const std::string& word_to_check); |
| 116 | 126 |
| 117 // When called, relays the request to fill the list with suggestions to | 127 // When called, relays the request to fill the list with suggestions to |
| 118 // the proper backend, either hunspell or a platform-specific backend. | 128 // the proper backend, either hunspell or a platform-specific backend. |
| 119 void FillSuggestionList(const std::string& wrong_word, | 129 void FillSuggestionList(const std::string& wrong_word, |
| 120 std::vector<std::wstring>* optional_suggestions); | 130 std::vector<std::wstring>* optional_suggestions); |
| 121 | 131 |
| 122 // Download dictionary files when required. | |
| 123 class DictionaryDownloadController; | |
| 124 | |
| 125 // Initializes the Hunspell Dictionary. | 132 // Initializes the Hunspell Dictionary. |
| 126 bool Initialize(); | 133 bool Initialize(); |
| 127 | 134 |
| 128 // After |hunspell_| is initialized, this function is called to add custom | 135 // After |hunspell_| is initialized, this function is called to add custom |
| 129 // words from the custom dictionary to the |hunspell_|. | 136 // words from the custom dictionary to the |hunspell_|. |
| 130 void AddCustomWordsToHunspell(); | 137 void AddCustomWordsToHunspell(); |
| 131 | 138 |
| 132 void set_file_is_downloading(bool value); | |
| 133 | |
| 134 // Memory maps the given .bdic file. On success, it will return true and will | 139 // Memory maps the given .bdic file. On success, it will return true and will |
| 135 // place the data and length into the given out parameters. | 140 // place the data and length into the given out parameters. |
| 136 bool MapBdictFile(const unsigned char** data, size_t* length); | 141 bool MapBdictFile(const unsigned char** data, size_t* length); |
| 137 | 142 |
| 138 // Returns whether or not the given word is a contraction of valid words | 143 // Returns whether or not the given word is a contraction of valid words |
| 139 // (e.g. "word:word"). | 144 // (e.g. "word:word"). |
| 140 bool IsValidContraction(const string16& word); | 145 bool IsValidContraction(const string16& word); |
| 141 | 146 |
| 142 // Return the file name of the dictionary, including the path and the version | 147 // Return the file name of the dictionary, including the path and the version |
| 143 // numbers. | 148 // numbers. |
| 144 FilePath GetVersionedFileName(const std::string& language, | 149 FilePath GetVersionedFileName(const std::string& language, |
| 145 const FilePath& dict_dir); | 150 const FilePath& dict_dir); |
| 146 | 151 |
| 147 static std::string GetCorrespondingSpellCheckLanguage( | 152 static std::string GetCorrespondingSpellCheckLanguage( |
| 148 const std::string& language); | 153 const std::string& language); |
| 149 | 154 |
| 150 // Start the dictionary download process in the file thread. On completion, | 155 // Start the dictionary download process in the file thread. The UrlFetcher |
|
Peter Kasting
2009/08/10 22:25:44
Nit: This comment is no longer true because the do
| |
| 151 // this function calls on set_file_is_downloading() in the IO thread to notify | 156 // lives in the IO thread while downloading the file to memory. |
| 152 // that download has completed. This function has to be called in the IO | 157 // TODO(sidchat): Do disk IO from memory to disc in the file thread. |
| 153 // thread. | 158 void StartDictionaryDownload(const FilePath& file_name); |
| 154 void StartDictionaryDownloadInFileThread(const FilePath& file_name); | |
| 155 | 159 |
| 156 // The given path to the directory whether SpellChecker first tries to | 160 // The given path to the directory whether SpellChecker first tries to |
| 157 // download the spellcheck bdic dictionary file. | 161 // download the spellcheck bdic dictionary file. |
| 158 FilePath given_dictionary_directory_; | 162 FilePath given_dictionary_directory_; |
| 159 | 163 |
| 160 // Path to the custom dictionary file. | 164 // Path to the custom dictionary file. |
| 161 FilePath custom_dictionary_file_name_; | 165 FilePath custom_dictionary_file_name_; |
| 162 | 166 |
| 167 // BDIC file name (e.g. en-US_1_1.bdic). | |
| 168 FilePath bdic_file_name_; | |
| 169 | |
| 163 // We memory-map the BDict file. | 170 // We memory-map the BDict file. |
| 164 scoped_ptr<file_util::MemoryMappedFile> bdict_file_; | 171 scoped_ptr<file_util::MemoryMappedFile> bdict_file_; |
| 165 | 172 |
| 166 // The hunspell dictionary in use. | 173 // The hunspell dictionary in use. |
| 167 scoped_ptr<Hunspell> hunspell_; | 174 scoped_ptr<Hunspell> hunspell_; |
| 168 | 175 |
| 169 // Represents character attributes used for filtering out characters which | 176 // Represents character attributes used for filtering out characters which |
| 170 // are not supported by this SpellChecker object. | 177 // are not supported by this SpellChecker object. |
| 171 SpellcheckCharAttribute character_attributes_; | 178 SpellcheckCharAttribute character_attributes_; |
| 172 | 179 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 186 // constructor since that's on a different thread. | 193 // constructor since that's on a different thread. |
| 187 MessageLoop* worker_loop_; | 194 MessageLoop* worker_loop_; |
| 188 #endif | 195 #endif |
| 189 | 196 |
| 190 // Flag indicating whether we tried to download the dictionary file. | 197 // Flag indicating whether we tried to download the dictionary file. |
| 191 bool tried_to_download_dictionary_file_; | 198 bool tried_to_download_dictionary_file_; |
| 192 | 199 |
| 193 // File Thread Message Loop. | 200 // File Thread Message Loop. |
| 194 MessageLoop* file_loop_; | 201 MessageLoop* file_loop_; |
| 195 | 202 |
| 196 // UI Thread Message Loop - this will be used as a proxy to access io loop. | |
| 197 MessageLoop* ui_loop_; | |
| 198 | |
| 199 // Used for requests. MAY BE NULL which means don't try to download. | 203 // Used for requests. MAY BE NULL which means don't try to download. |
| 200 URLRequestContext* url_request_context_; | 204 URLRequestContext* url_request_context_; |
| 201 | 205 |
| 202 // DictionaryDownloadController object to download dictionary if required. | |
| 203 scoped_refptr<DictionaryDownloadController> ddc_dic_; | |
| 204 | |
| 205 // Set when the dictionary file is currently downloading. | 206 // Set when the dictionary file is currently downloading. |
| 206 bool dic_is_downloading_; | 207 bool dic_is_downloading_; |
| 207 | 208 |
| 208 // Remember state for auto spell correct. | 209 // Remember state for auto spell correct. |
| 209 bool auto_spell_correct_turned_on_; | 210 bool auto_spell_correct_turned_on_; |
| 210 | 211 |
| 211 // True if a platform-specific spellchecking engine is being used, | 212 // True if a platform-specific spellchecking engine is being used, |
| 212 // and False if hunspell is being used. | 213 // and False if hunspell is being used. |
| 213 bool is_using_platform_spelling_engine_; | 214 bool is_using_platform_spelling_engine_; |
| 214 | 215 |
| 215 // Used for generating callbacks to spellchecker, since spellchecker is a | 216 // URLFetcher to download a file in memory. |
| 216 // non-reference counted object. The callback is generated by generating tasks | 217 scoped_ptr<URLFetcher> fetcher_; |
| 217 // using NewRunableMethod on these objects. | |
| 218 ScopedRunnableMethodFactory<SpellChecker> dic_download_state_changer_factory_; | |
| 219 | 218 |
| 220 DISALLOW_COPY_AND_ASSIGN(SpellChecker); | 219 DISALLOW_COPY_AND_ASSIGN(SpellChecker); |
| 221 }; | 220 }; |
| 222 | 221 |
| 223 #endif // CHROME_BROWSER_SPELLCHECKER_H_ | 222 #endif // CHROME_BROWSER_SPELLCHECKER_H_ |
| OLD | NEW |