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 <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 bool auto_spell_correct) { | 191 bool auto_spell_correct) { |
192 languages_.clear(); | 192 languages_.clear(); |
193 for (const auto& bdict_language : bdict_languages) { | 193 for (const auto& bdict_language : bdict_languages) { |
194 AddSpellcheckLanguage( | 194 AddSpellcheckLanguage( |
195 IPC::PlatformFileForTransitToFile(bdict_language.file), | 195 IPC::PlatformFileForTransitToFile(bdict_language.file), |
196 bdict_language.language); | 196 bdict_language.language); |
197 } | 197 } |
198 | 198 |
199 custom_dictionary_.Init(custom_words); | 199 custom_dictionary_.Init(custom_words); |
200 auto_spell_correct_turned_on_ = auto_spell_correct; | 200 auto_spell_correct_turned_on_ = auto_spell_correct; |
201 #if !defined(OS_MACOSX) | 201 #if !defined(USE_BROWSER_SPELLCHECKER) |
202 PostDelayedSpellCheckTask(pending_request_param_.release()); | 202 PostDelayedSpellCheckTask(pending_request_param_.release()); |
203 #endif | 203 #endif |
204 } | 204 } |
205 | 205 |
206 void SpellCheck::OnCustomDictionaryChanged( | 206 void SpellCheck::OnCustomDictionaryChanged( |
207 const std::set<std::string>& words_added, | 207 const std::set<std::string>& words_added, |
208 const std::set<std::string>& words_removed) { | 208 const std::set<std::string>& words_removed) { |
209 custom_dictionary_.OnCustomDictionaryChanged(words_added, words_removed); | 209 custom_dictionary_.OnCustomDictionaryChanged(words_added, words_removed); |
210 if (words_added.empty()) | 210 if (words_added.empty()) |
211 return; | 211 return; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 return true; | 254 return true; |
255 | 255 |
256 return languages_.front()->SpellCheckWord(in_word, in_word_len, tag, | 256 return languages_.front()->SpellCheckWord(in_word, in_word_len, tag, |
257 misspelling_start, misspelling_len, | 257 misspelling_start, misspelling_len, |
258 optional_suggestions); | 258 optional_suggestions); |
259 } | 259 } |
260 | 260 |
261 bool SpellCheck::SpellCheckParagraph( | 261 bool SpellCheck::SpellCheckParagraph( |
262 const base::string16& text, | 262 const base::string16& text, |
263 WebVector<WebTextCheckingResult>* results) { | 263 WebVector<WebTextCheckingResult>* results) { |
264 #if !defined(OS_MACOSX) | 264 #if !defined(USE_BROWSER_SPELLCHECKER) |
265 // Mac has its own spell checker, so this method will not be used. | 265 // Mac and Android have their own spell checkers,so this method won't be used |
266 DCHECK(results); | 266 DCHECK(results); |
267 std::vector<WebTextCheckingResult> textcheck_results; | 267 std::vector<WebTextCheckingResult> textcheck_results; |
268 size_t length = text.length(); | 268 size_t length = text.length(); |
269 size_t offset = 0; | 269 size_t offset = 0; |
270 | 270 |
271 // Spellcheck::SpellCheckWord() automatically breaks text into words and | 271 // Spellcheck::SpellCheckWord() automatically breaks text into words and |
272 // checks the spellings of the extracted words. This function sets the | 272 // checks the spellings of the extracted words. This function sets the |
273 // position and length of the first misspelled word and returns false when | 273 // position and length of the first misspelled word and returns false when |
274 // the text includes misspelled words. Therefore, we just repeat calling the | 274 // the text includes misspelled words. Therefore, we just repeat calling the |
275 // function until it returns true to check the whole text. | 275 // function until it returns true to check the whole text. |
(...skipping 18 matching lines...) Expand all Loading... |
294 misspelling_start + offset, | 294 misspelling_start + offset, |
295 misspelling_length, | 295 misspelling_length, |
296 replacement)); | 296 replacement)); |
297 } | 297 } |
298 offset += misspelling_start + misspelling_length; | 298 offset += misspelling_start + misspelling_length; |
299 } | 299 } |
300 results->assign(textcheck_results); | 300 results->assign(textcheck_results); |
301 return false; | 301 return false; |
302 #else | 302 #else |
303 // This function is only invoked for spell checker functionality that runs | 303 // This function is only invoked for spell checker functionality that runs |
304 // on the render thread. OSX builds don't have that. | 304 // on the render thread. OSX and Android builds don't have that. |
305 NOTREACHED(); | 305 NOTREACHED(); |
306 return true; | 306 return true; |
307 #endif | 307 #endif |
308 } | 308 } |
309 | 309 |
310 base::string16 SpellCheck::GetAutoCorrectionWord(const base::string16& word, | 310 base::string16 SpellCheck::GetAutoCorrectionWord(const base::string16& word, |
311 int tag) { | 311 int tag) { |
312 base::string16 autocorrect_word; | 312 base::string16 autocorrect_word; |
313 if (!auto_spell_correct_turned_on_) | 313 if (!auto_spell_correct_turned_on_) |
314 return autocorrect_word; // Return the empty string. | 314 return autocorrect_word; // Return the empty string. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 break; | 353 break; |
354 } | 354 } |
355 } | 355 } |
356 | 356 |
357 // Restore the swapped characters. | 357 // Restore the swapped characters. |
358 std::swap(misspelled_word[i], misspelled_word[i + 1]); | 358 std::swap(misspelled_word[i], misspelled_word[i + 1]); |
359 } | 359 } |
360 return autocorrect_word; | 360 return autocorrect_word; |
361 } | 361 } |
362 | 362 |
363 #if !defined(OS_MACOSX) // OSX uses its own spell checker | 363 // OSX and Android use their own spell checkers |
| 364 #if !defined(USE_BROWSER_SPELLCHECKER) |
364 void SpellCheck::RequestTextChecking( | 365 void SpellCheck::RequestTextChecking( |
365 const base::string16& text, | 366 const base::string16& text, |
366 blink::WebTextCheckingCompletion* completion) { | 367 blink::WebTextCheckingCompletion* completion) { |
367 // Clean up the previous request before starting a new request. | 368 // Clean up the previous request before starting a new request. |
368 if (pending_request_param_.get()) | 369 if (pending_request_param_.get()) |
369 pending_request_param_->completion()->didCancelCheckingText(); | 370 pending_request_param_->completion()->didCancelCheckingText(); |
370 | 371 |
371 pending_request_param_.reset(new SpellcheckRequest( | 372 pending_request_param_.reset(new SpellcheckRequest( |
372 text, completion)); | 373 text, completion)); |
373 // We will check this text after we finish loading the hunspell dictionary. | 374 // We will check this text after we finish loading the hunspell dictionary. |
374 if (InitializeIfNeeded()) | 375 if (InitializeIfNeeded()) |
375 return; | 376 return; |
376 | 377 |
377 PostDelayedSpellCheckTask(pending_request_param_.release()); | 378 PostDelayedSpellCheckTask(pending_request_param_.release()); |
378 } | 379 } |
379 #endif | 380 #endif |
380 | 381 |
381 bool SpellCheck::InitializeIfNeeded() { | 382 bool SpellCheck::InitializeIfNeeded() { |
382 return languages_.empty() ? true : languages_.front()->InitializeIfNeeded(); | 383 return languages_.empty() ? true : languages_.front()->InitializeIfNeeded(); |
383 } | 384 } |
384 | 385 |
385 #if !defined(OS_MACOSX) // OSX doesn't have |pending_request_param_| | 386 // OSX and Android don't have |pending_request_param_| |
| 387 #if !defined(USE_BROWSER_SPELLCHECKER) |
386 void SpellCheck::PostDelayedSpellCheckTask(SpellcheckRequest* request) { | 388 void SpellCheck::PostDelayedSpellCheckTask(SpellcheckRequest* request) { |
387 if (!request) | 389 if (!request) |
388 return; | 390 return; |
389 | 391 |
390 base::ThreadTaskRunnerHandle::Get()->PostTask( | 392 base::ThreadTaskRunnerHandle::Get()->PostTask( |
391 FROM_HERE, base::Bind(&SpellCheck::PerformSpellCheck, AsWeakPtr(), | 393 FROM_HERE, base::Bind(&SpellCheck::PerformSpellCheck, AsWeakPtr(), |
392 base::Owned(request))); | 394 base::Owned(request))); |
393 } | 395 } |
394 #endif | 396 #endif |
395 | 397 |
396 #if !defined(OS_MACOSX) // Mac uses its platform engine instead. | 398 // Mac and Android use their platform engines instead. |
| 399 #if !defined(USE_BROWSER_SPELLCHECKER) |
397 void SpellCheck::PerformSpellCheck(SpellcheckRequest* param) { | 400 void SpellCheck::PerformSpellCheck(SpellcheckRequest* param) { |
398 DCHECK(param); | 401 DCHECK(param); |
399 | 402 |
400 if (languages_.empty() || !languages_.front()->IsEnabled()) { | 403 if (languages_.empty() || !languages_.front()->IsEnabled()) { |
401 param->completion()->didCancelCheckingText(); | 404 param->completion()->didCancelCheckingText(); |
402 } else { | 405 } else { |
403 WebVector<blink::WebTextCheckingResult> results; | 406 WebVector<blink::WebTextCheckingResult> results; |
404 SpellCheckParagraph(param->text(), &results); | 407 SpellCheckParagraph(param->text(), &results); |
405 param->completion()->didFinishCheckingText(results); | 408 param->completion()->didFinishCheckingText(results); |
406 } | 409 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 } | 459 } |
457 | 460 |
458 results.push_back(WebTextCheckingResult( | 461 results.push_back(WebTextCheckingResult( |
459 static_cast<WebTextDecorationType>(decoration), | 462 static_cast<WebTextDecorationType>(decoration), |
460 line_offset + spellcheck_result.location, spellcheck_result.length, | 463 line_offset + spellcheck_result.location, spellcheck_result.length, |
461 replacement, spellcheck_result.hash)); | 464 replacement, spellcheck_result.hash)); |
462 } | 465 } |
463 | 466 |
464 textcheck_results->assign(results); | 467 textcheck_results->assign(results); |
465 } | 468 } |
OLD | NEW |