OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop_proxy.h" | 8 #include "base/message_loop/message_loop_proxy.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/common/render_messages.h" | 10 #include "chrome/common/render_messages.h" |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 // TODO(groby): Make sure we always have a spelling engine, even before Init() | 168 // TODO(groby): Make sure we always have a spelling engine, even before Init() |
169 // is called. | 169 // is called. |
170 void SpellCheck::Init(base::PlatformFile file, | 170 void SpellCheck::Init(base::PlatformFile file, |
171 const std::set<std::string>& custom_words, | 171 const std::set<std::string>& custom_words, |
172 const std::string& language) { | 172 const std::string& language) { |
173 spellcheck_.Init(file, language); | 173 spellcheck_.Init(file, language); |
174 custom_dictionary_.Init(custom_words); | 174 custom_dictionary_.Init(custom_words); |
175 } | 175 } |
176 | 176 |
177 bool SpellCheck::SpellCheckWord( | 177 bool SpellCheck::SpellCheckWord( |
178 const char16* in_word, | 178 const base::char16* in_word, |
179 int in_word_len, | 179 int in_word_len, |
180 int tag, | 180 int tag, |
181 int* misspelling_start, | 181 int* misspelling_start, |
182 int* misspelling_len, | 182 int* misspelling_len, |
183 std::vector<base::string16>* optional_suggestions) { | 183 std::vector<base::string16>* optional_suggestions) { |
184 DCHECK(in_word_len >= 0); | 184 DCHECK(in_word_len >= 0); |
185 DCHECK(misspelling_start && misspelling_len) << "Out vars must be given."; | 185 DCHECK(misspelling_start && misspelling_len) << "Out vars must be given."; |
186 | 186 |
187 // Do nothing if we need to delay initialization. (Rather than blocking, | 187 // Do nothing if we need to delay initialization. (Rather than blocking, |
188 // report the word as correctly spelled.) | 188 // report the word as correctly spelled.) |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 return autocorrect_word; // Return the empty string. | 251 return autocorrect_word; // Return the empty string. |
252 | 252 |
253 int word_length = static_cast<int>(word.size()); | 253 int word_length = static_cast<int>(word.size()); |
254 if (word_length < 2 || | 254 if (word_length < 2 || |
255 word_length > chrome::spellcheck_common::kMaxAutoCorrectWordSize) | 255 word_length > chrome::spellcheck_common::kMaxAutoCorrectWordSize) |
256 return autocorrect_word; | 256 return autocorrect_word; |
257 | 257 |
258 if (InitializeIfNeeded()) | 258 if (InitializeIfNeeded()) |
259 return autocorrect_word; | 259 return autocorrect_word; |
260 | 260 |
261 char16 misspelled_word[ | 261 base::char16 misspelled_word[ |
262 chrome::spellcheck_common::kMaxAutoCorrectWordSize + 1]; | 262 chrome::spellcheck_common::kMaxAutoCorrectWordSize + 1]; |
263 const char16* word_char = word.c_str(); | 263 const base::char16* word_char = word.c_str(); |
264 for (int i = 0; i <= chrome::spellcheck_common::kMaxAutoCorrectWordSize; | 264 for (int i = 0; i <= chrome::spellcheck_common::kMaxAutoCorrectWordSize; |
265 ++i) { | 265 ++i) { |
266 if (i >= word_length) | 266 if (i >= word_length) |
267 misspelled_word[i] = 0; | 267 misspelled_word[i] = 0; |
268 else | 268 else |
269 misspelled_word[i] = word_char[i]; | 269 misspelled_word[i] = word_char[i]; |
270 } | 270 } |
271 | 271 |
272 // Swap adjacent characters and spellcheck. | 272 // Swap adjacent characters and spellcheck. |
273 int misspelling_start, misspelling_len; | 273 int misspelling_start, misspelling_len; |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 | 347 |
348 void SpellCheck::CreateTextCheckingResults( | 348 void SpellCheck::CreateTextCheckingResults( |
349 ResultFilter filter, | 349 ResultFilter filter, |
350 int line_offset, | 350 int line_offset, |
351 const base::string16& line_text, | 351 const base::string16& line_text, |
352 const std::vector<SpellCheckResult>& spellcheck_results, | 352 const std::vector<SpellCheckResult>& spellcheck_results, |
353 WebVector<WebTextCheckingResult>* textcheck_results) { | 353 WebVector<WebTextCheckingResult>* textcheck_results) { |
354 // Double-check misspelled words with our spellchecker and attach grammar | 354 // Double-check misspelled words with our spellchecker and attach grammar |
355 // markers to them if our spellchecker tells they are correct words, i.e. they | 355 // markers to them if our spellchecker tells they are correct words, i.e. they |
356 // are probably contextually-misspelled words. | 356 // are probably contextually-misspelled words. |
357 const char16* text = line_text.c_str(); | 357 const base::char16* text = line_text.c_str(); |
358 std::vector<WebTextCheckingResult> list; | 358 std::vector<WebTextCheckingResult> list; |
359 for (size_t i = 0; i < spellcheck_results.size(); ++i) { | 359 for (size_t i = 0; i < spellcheck_results.size(); ++i) { |
360 SpellCheckResult::Decoration decoration = spellcheck_results[i].decoration; | 360 SpellCheckResult::Decoration decoration = spellcheck_results[i].decoration; |
361 int word_location = spellcheck_results[i].location; | 361 int word_location = spellcheck_results[i].location; |
362 int word_length = spellcheck_results[i].length; | 362 int word_length = spellcheck_results[i].length; |
363 int misspelling_start = 0; | 363 int misspelling_start = 0; |
364 int misspelling_length = 0; | 364 int misspelling_length = 0; |
365 if (decoration == SpellCheckResult::SPELLING && | 365 if (decoration == SpellCheckResult::SPELLING && |
366 filter == USE_NATIVE_CHECKER) { | 366 filter == USE_NATIVE_CHECKER) { |
367 if (SpellCheckWord(text + word_location, word_length, 0, | 367 if (SpellCheckWord(text + word_location, word_length, 0, |
368 &misspelling_start, &misspelling_length, NULL)) { | 368 &misspelling_start, &misspelling_length, NULL)) { |
369 decoration = SpellCheckResult::GRAMMAR; | 369 decoration = SpellCheckResult::GRAMMAR; |
370 } | 370 } |
371 } | 371 } |
372 if (!custom_dictionary_.SpellCheckWord( | 372 if (!custom_dictionary_.SpellCheckWord( |
373 line_text, word_location, word_length)) { | 373 line_text, word_location, word_length)) { |
374 list.push_back(WebTextCheckingResult( | 374 list.push_back(WebTextCheckingResult( |
375 static_cast<WebTextDecorationType>(decoration), | 375 static_cast<WebTextDecorationType>(decoration), |
376 word_location + line_offset, | 376 word_location + line_offset, |
377 word_length, | 377 word_length, |
378 spellcheck_results[i].replacement, | 378 spellcheck_results[i].replacement, |
379 spellcheck_results[i].hash)); | 379 spellcheck_results[i].hash)); |
380 } | 380 } |
381 } | 381 } |
382 textcheck_results->assign(list); | 382 textcheck_results->assign(list); |
383 } | 383 } |
OLD | NEW |