| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "chrome/renderer/spellchecker/spellcheck.h" | 5 #include "chrome/renderer/spellchecker/spellcheck.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "base/time.h" | 9 #include "base/time.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "chrome/common/render_messages.h" | 11 #include "chrome/common/render_messages.h" |
| 12 #include "chrome/common/spellcheck_common.h" | 12 #include "chrome/common/spellcheck_common.h" |
| 13 #include "chrome/common/spellcheck_messages.h" | 13 #include "chrome/common/spellcheck_messages.h" |
| 14 #include "content/renderer/render_thread.h" |
| 14 #include "third_party/hunspell/src/hunspell/hunspell.hxx" | 15 #include "third_party/hunspell/src/hunspell/hunspell.hxx" |
| 15 | 16 |
| 16 using base::TimeTicks; | 17 using base::TimeTicks; |
| 17 | 18 |
| 18 SpellCheck::SpellCheck() | 19 SpellCheck::SpellCheck() |
| 19 : file_(base::kInvalidPlatformFileValue), | 20 : file_(base::kInvalidPlatformFileValue), |
| 20 auto_spell_correct_turned_on_(false), | 21 auto_spell_correct_turned_on_(false), |
| 21 is_using_platform_spelling_engine_(false), | 22 is_using_platform_spelling_engine_(false), |
| 22 initialized_(false) { | 23 initialized_(false) { |
| 23 // Wait till we check the first word before doing any initializing. | 24 // Wait till we check the first word before doing any initializing. |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 void SpellCheck::AddWordToHunspell(const std::string& word) { | 215 void SpellCheck::AddWordToHunspell(const std::string& word) { |
| 215 if (!word.empty() && word.length() < MAXWORDUTF8LEN) | 216 if (!word.empty() && word.length() < MAXWORDUTF8LEN) |
| 216 hunspell_->add(word.c_str()); | 217 hunspell_->add(word.c_str()); |
| 217 } | 218 } |
| 218 | 219 |
| 219 bool SpellCheck::InitializeIfNeeded() { | 220 bool SpellCheck::InitializeIfNeeded() { |
| 220 if (is_using_platform_spelling_engine_) | 221 if (is_using_platform_spelling_engine_) |
| 221 return false; | 222 return false; |
| 222 | 223 |
| 223 if (!initialized_) { | 224 if (!initialized_) { |
| 224 Send(new SpellCheckHostMsg_RequestDictionary); | 225 RenderThread::current()->Send(new SpellCheckHostMsg_RequestDictionary); |
| 225 initialized_ = true; | 226 initialized_ = true; |
| 226 return true; | 227 return true; |
| 227 } | 228 } |
| 228 | 229 |
| 229 // Don't initialize if hunspell is disabled. | 230 // Don't initialize if hunspell is disabled. |
| 230 if (file_ != base::kInvalidPlatformFileValue) | 231 if (file_ != base::kInvalidPlatformFileValue) |
| 231 InitializeHunspell(); | 232 InitializeHunspell(); |
| 232 | 233 |
| 233 return false; | 234 return false; |
| 234 } | 235 } |
| 235 | 236 |
| 236 // When called, relays the request to check the spelling to the proper | 237 // When called, relays the request to check the spelling to the proper |
| 237 // backend, either hunspell or a platform-specific backend. | 238 // backend, either hunspell or a platform-specific backend. |
| 238 bool SpellCheck::CheckSpelling(const string16& word_to_check, int tag) { | 239 bool SpellCheck::CheckSpelling(const string16& word_to_check, int tag) { |
| 239 bool word_correct = false; | 240 bool word_correct = false; |
| 240 | 241 |
| 241 if (is_using_platform_spelling_engine_) { | 242 if (is_using_platform_spelling_engine_) { |
| 242 Send(new SpellCheckHostMsg_PlatformCheckSpelling(word_to_check, tag, | 243 RenderThread::current()->Send(new SpellCheckHostMsg_PlatformCheckSpelling( |
| 243 &word_correct)); | 244 word_to_check, tag, &word_correct)); |
| 244 } else { | 245 } else { |
| 245 std::string word_to_check_utf8(UTF16ToUTF8(word_to_check)); | 246 std::string word_to_check_utf8(UTF16ToUTF8(word_to_check)); |
| 246 // Hunspell shouldn't let us exceed its max, but check just in case | 247 // Hunspell shouldn't let us exceed its max, but check just in case |
| 247 if (word_to_check_utf8.length() < MAXWORDUTF8LEN) { | 248 if (word_to_check_utf8.length() < MAXWORDUTF8LEN) { |
| 248 if (hunspell_.get()) { | 249 if (hunspell_.get()) { |
| 249 // |hunspell_->spell| returns 0 if the word is spelled correctly and | 250 // |hunspell_->spell| returns 0 if the word is spelled correctly and |
| 250 // non-zero otherwsie. | 251 // non-zero otherwsie. |
| 251 word_correct = (hunspell_->spell(word_to_check_utf8.c_str()) != 0); | 252 word_correct = (hunspell_->spell(word_to_check_utf8.c_str()) != 0); |
| 252 } else { | 253 } else { |
| 253 // If |hunspell_| is NULL here, an error has occurred, but it's better | 254 // If |hunspell_| is NULL here, an error has occurred, but it's better |
| 254 // to check rather than crash. | 255 // to check rather than crash. |
| 255 word_correct = true; | 256 word_correct = true; |
| 256 } | 257 } |
| 257 } | 258 } |
| 258 } | 259 } |
| 259 | 260 |
| 260 return word_correct; | 261 return word_correct; |
| 261 } | 262 } |
| 262 | 263 |
| 263 void SpellCheck::FillSuggestionList( | 264 void SpellCheck::FillSuggestionList( |
| 264 const string16& wrong_word, | 265 const string16& wrong_word, |
| 265 std::vector<string16>* optional_suggestions) { | 266 std::vector<string16>* optional_suggestions) { |
| 266 if (is_using_platform_spelling_engine_) { | 267 if (is_using_platform_spelling_engine_) { |
| 267 Send(new SpellCheckHostMsg_PlatformFillSuggestionList( | 268 RenderThread::current()->Send( |
| 268 wrong_word, optional_suggestions)); | 269 new SpellCheckHostMsg_PlatformFillSuggestionList( |
| 270 wrong_word, optional_suggestions)); |
| 269 return; | 271 return; |
| 270 } | 272 } |
| 271 | 273 |
| 272 // If |hunspell_| is NULL here, an error has occurred, but it's better | 274 // If |hunspell_| is NULL here, an error has occurred, but it's better |
| 273 // to check rather than crash. | 275 // to check rather than crash. |
| 274 if (!hunspell_.get()) | 276 if (!hunspell_.get()) |
| 275 return; | 277 return; |
| 276 | 278 |
| 277 char** suggestions; | 279 char** suggestions; |
| 278 int number_of_suggestions = | 280 int number_of_suggestions = |
| (...skipping 20 matching lines...) Expand all Loading... |
| 299 | 301 |
| 300 string16 word; | 302 string16 word; |
| 301 int word_start; | 303 int word_start; |
| 302 int word_length; | 304 int word_length; |
| 303 while (contraction_iterator_.GetNextWord(&word, &word_start, &word_length)) { | 305 while (contraction_iterator_.GetNextWord(&word, &word_start, &word_length)) { |
| 304 if (!CheckSpelling(word, tag)) | 306 if (!CheckSpelling(word, tag)) |
| 305 return false; | 307 return false; |
| 306 } | 308 } |
| 307 return true; | 309 return true; |
| 308 } | 310 } |
| OLD | NEW |