OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/browser/spellchecker.h" | 5 #include "chrome/browser/spellchecker.h" |
6 #include "base/basictypes.h" | 6 #include "base/basictypes.h" |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/histogram.h" | 9 #include "base/histogram.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 return false; | 468 return false; |
469 } | 469 } |
470 | 470 |
471 // Control has come so far - both files probably exist. | 471 // Control has come so far - both files probably exist. |
472 TimeTicks begin_time = TimeTicks::Now(); | 472 TimeTicks begin_time = TimeTicks::Now(); |
473 bdict_file_.reset(new file_util::MemoryMappedFile()); | 473 bdict_file_.reset(new file_util::MemoryMappedFile()); |
474 if (bdict_file_->Initialize(FilePath::FromWStringHack(bdict_file_name_))) { | 474 if (bdict_file_->Initialize(FilePath::FromWStringHack(bdict_file_name_))) { |
475 hunspell_.reset(new Hunspell(bdict_file_->data(), bdict_file_->length())); | 475 hunspell_.reset(new Hunspell(bdict_file_->data(), bdict_file_->length())); |
476 AddCustomWordsToHunspell(); | 476 AddCustomWordsToHunspell(); |
477 } | 477 } |
478 DHISTOGRAM_TIMES(L"Spellcheck.InitTime", TimeTicks::Now() - begin_time); | 478 DHISTOGRAM_TIMES("Spellcheck.InitTime", TimeTicks::Now() - begin_time); |
479 | 479 |
480 tried_to_init_ = true; | 480 tried_to_init_ = true; |
481 return false; | 481 return false; |
482 } | 482 } |
483 | 483 |
484 void SpellChecker::AddCustomWordsToHunspell() { | 484 void SpellChecker::AddCustomWordsToHunspell() { |
485 // Add custom words to Hunspell. | 485 // Add custom words to Hunspell. |
486 // This should be done in File Loop, but since Hunspell is in this IO Loop, | 486 // This should be done in File Loop, but since Hunspell is in this IO Loop, |
487 // this too has to be initialized here. | 487 // this too has to be initialized here. |
488 // TODO (sidchat): Work out a way to initialize Hunspell in the File Loop. | 488 // TODO (sidchat): Work out a way to initialize Hunspell in the File Loop. |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
554 int word_length; | 554 int word_length; |
555 word_iterator.Initialize(&character_attributes_, in_word_utf16.c_str(), | 555 word_iterator.Initialize(&character_attributes_, in_word_utf16.c_str(), |
556 in_word_len, true); | 556 in_word_len, true); |
557 while (word_iterator.GetNextWord(&word, &word_start, &word_length)) { | 557 while (word_iterator.GetNextWord(&word, &word_start, &word_length)) { |
558 // Found a word (or a contraction) that hunspell can check its spelling. | 558 // Found a word (or a contraction) that hunspell can check its spelling. |
559 std::string encoded_word = UTF16ToUTF8(word); | 559 std::string encoded_word = UTF16ToUTF8(word); |
560 | 560 |
561 { | 561 { |
562 TimeTicks begin_time = TimeTicks::Now(); | 562 TimeTicks begin_time = TimeTicks::Now(); |
563 bool word_ok = !!hunspell_->spell(encoded_word.c_str()); | 563 bool word_ok = !!hunspell_->spell(encoded_word.c_str()); |
564 DHISTOGRAM_TIMES(L"Spellcheck.CheckTime", TimeTicks::Now() - begin_time); | 564 DHISTOGRAM_TIMES("Spellcheck.CheckTime", TimeTicks::Now() - begin_time); |
565 if (word_ok) | 565 if (word_ok) |
566 continue; | 566 continue; |
567 } | 567 } |
568 | 568 |
569 // If the given word is a concatenated word of two or more valid words | 569 // If the given word is a concatenated word of two or more valid words |
570 // (e.g. "hello:hello"), we should treat it as a valid word. | 570 // (e.g. "hello:hello"), we should treat it as a valid word. |
571 if (IsValidContraction(word)) | 571 if (IsValidContraction(word)) |
572 continue; | 572 continue; |
573 | 573 |
574 *misspelling_start = word_start; | 574 *misspelling_start = word_start; |
575 *misspelling_len = word_length; | 575 *misspelling_len = word_length; |
576 | 576 |
577 // Get the list of suggested words. | 577 // Get the list of suggested words. |
578 if (optional_suggestions) { | 578 if (optional_suggestions) { |
579 char** suggestions; | 579 char** suggestions; |
580 TimeTicks begin_time = TimeTicks::Now(); | 580 TimeTicks begin_time = TimeTicks::Now(); |
581 int number_of_suggestions = hunspell_->suggest(&suggestions, | 581 int number_of_suggestions = hunspell_->suggest(&suggestions, |
582 encoded_word.c_str()); | 582 encoded_word.c_str()); |
583 DHISTOGRAM_TIMES(L"Spellcheck.SuggestTime", | 583 DHISTOGRAM_TIMES("Spellcheck.SuggestTime", |
584 TimeTicks::Now() - begin_time); | 584 TimeTicks::Now() - begin_time); |
585 | 585 |
586 // Populate the vector of WideStrings. | 586 // Populate the vector of WideStrings. |
587 for (int i = 0; i < number_of_suggestions; i++) { | 587 for (int i = 0; i < number_of_suggestions; i++) { |
588 if (i < kMaxSuggestions) | 588 if (i < kMaxSuggestions) |
589 optional_suggestions->push_back(UTF8ToWide(suggestions[i])); | 589 optional_suggestions->push_back(UTF8ToWide(suggestions[i])); |
590 free(suggestions[i]); | 590 free(suggestions[i]); |
591 } | 591 } |
592 free(suggestions); | 592 free(suggestions); |
593 } | 593 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
634 hunspell_->put_word(word_to_add.c_str()); | 634 hunspell_->put_word(word_to_add.c_str()); |
635 | 635 |
636 // Now add the word to the custom dictionary file. | 636 // Now add the word to the custom dictionary file. |
637 Task* write_word_task = | 637 Task* write_word_task = |
638 new AddWordToCustomDictionaryTask(custom_dictionary_file_name_, word); | 638 new AddWordToCustomDictionaryTask(custom_dictionary_file_name_, word); |
639 if (file_loop_) | 639 if (file_loop_) |
640 file_loop_->PostTask(FROM_HERE, write_word_task); | 640 file_loop_->PostTask(FROM_HERE, write_word_task); |
641 else | 641 else |
642 write_word_task->Run(); | 642 write_word_task->Run(); |
643 } | 643 } |
OLD | NEW |