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