| 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 "hunspell_engine.h" | 5 #include "hunspell_engine.h" |
| 6 | 6 |
| 7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
| 8 #include "base/time.h" | 8 #include "base/time.h" |
| 9 #include "chrome/common/spellcheck_common.h" | 9 #include "chrome/common/spellcheck_common.h" |
| 10 #include "chrome/common/spellcheck_messages.h" | 10 #include "chrome/common/spellcheck_messages.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 | 49 |
| 50 bdict_file_.reset(new file_util::MemoryMappedFile); | 50 bdict_file_.reset(new file_util::MemoryMappedFile); |
| 51 | 51 |
| 52 if (bdict_file_->Initialize(file_)) { | 52 if (bdict_file_->Initialize(file_)) { |
| 53 TimeTicks debug_start_time = base::Histogram::DebugNow(); | 53 TimeTicks debug_start_time = base::Histogram::DebugNow(); |
| 54 | 54 |
| 55 hunspell_.reset( | 55 hunspell_.reset( |
| 56 new Hunspell(bdict_file_->data(), bdict_file_->length())); | 56 new Hunspell(bdict_file_->data(), bdict_file_->length())); |
| 57 | 57 |
| 58 // Add custom words to Hunspell. | 58 // Add custom words to Hunspell. |
| 59 for (std::vector<std::string>::iterator it = custom_words_.begin(); | 59 chrome::spellcheck_common::WordList::iterator it; |
| 60 it != custom_words_.end(); ++it) { | 60 for (it = custom_words_.begin(); it != custom_words_.end(); ++it) |
| 61 AddWordToHunspell(*it); | 61 AddWordToHunspell(*it); |
| 62 } | |
| 63 | 62 |
| 64 DHISTOGRAM_TIMES("Spellcheck.InitTime", | 63 DHISTOGRAM_TIMES("Spellcheck.InitTime", |
| 65 base::Histogram::DebugNow() - debug_start_time); | 64 base::Histogram::DebugNow() - debug_start_time); |
| 66 } else { | 65 } else { |
| 67 NOTREACHED() << "Could not mmap spellchecker dictionary."; | 66 NOTREACHED() << "Could not mmap spellchecker dictionary."; |
| 68 } | 67 } |
| 69 } | 68 } |
| 70 | 69 |
| 71 void HunspellEngine::AddWordToHunspell(const std::string& word) { | 70 void HunspellEngine::AddWordToHunspell(const std::string& word) { |
| 72 if (!word.empty() && word.length() < MAXWORDLEN) | 71 if (!word.empty() && word.length() < MAXWORDLEN) |
| 73 hunspell_->add(word.c_str()); | 72 hunspell_->add(word.c_str()); |
| 74 } | 73 } |
| 75 | 74 |
| 75 void HunspellEngine::RemoveWordFromHunspell(const std::string& word) { |
| 76 if (!word.empty() && word.length() < MAXWORDLEN) |
| 77 hunspell_->remove(word.c_str()); |
| 78 } |
| 79 |
| 76 bool HunspellEngine::CheckSpelling(const string16& word_to_check, int tag) { | 80 bool HunspellEngine::CheckSpelling(const string16& word_to_check, int tag) { |
| 77 bool word_correct = false; | 81 bool word_correct = false; |
| 78 std::string word_to_check_utf8(UTF16ToUTF8(word_to_check)); | 82 std::string word_to_check_utf8(UTF16ToUTF8(word_to_check)); |
| 79 // Hunspell shouldn't let us exceed its max, but check just in case | 83 // Hunspell shouldn't let us exceed its max, but check just in case |
| 80 if (word_to_check_utf8.length() < MAXWORDLEN) { | 84 if (word_to_check_utf8.length() < MAXWORDLEN) { |
| 81 if (hunspell_.get()) { | 85 if (hunspell_.get()) { |
| 82 // |hunspell_->spell| returns 0 if the word is spelled correctly and | 86 // |hunspell_->spell| returns 0 if the word is spelled correctly and |
| 83 // non-zero otherwsie. | 87 // non-zero otherwsie. |
| 84 word_correct = (hunspell_->spell(word_to_check_utf8.c_str()) != 0); | 88 word_correct = (hunspell_->spell(word_to_check_utf8.c_str()) != 0); |
| 85 } else { | 89 } else { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 | 121 |
| 118 void HunspellEngine::OnWordAdded(const std::string& word) { | 122 void HunspellEngine::OnWordAdded(const std::string& word) { |
| 119 if (!hunspell_.get()) { | 123 if (!hunspell_.get()) { |
| 120 // Save it for later---add it when hunspell is initialized. | 124 // Save it for later---add it when hunspell is initialized. |
| 121 custom_words_.push_back(word); | 125 custom_words_.push_back(word); |
| 122 } else { | 126 } else { |
| 123 AddWordToHunspell(word); | 127 AddWordToHunspell(word); |
| 124 } | 128 } |
| 125 } | 129 } |
| 126 | 130 |
| 131 void HunspellEngine::OnWordRemoved(const std::string& word) { |
| 132 if (!hunspell_.get()) { |
| 133 chrome::spellcheck_common::WordList::iterator it = std::find( |
| 134 custom_words_.begin(), custom_words_.end(), word); |
| 135 if (it != custom_words_.end()) |
| 136 custom_words_.erase(it); |
| 137 } else { |
| 138 RemoveWordFromHunspell(word); |
| 139 } |
| 140 } |
| 141 |
| 127 bool HunspellEngine::InitializeIfNeeded() { | 142 bool HunspellEngine::InitializeIfNeeded() { |
| 128 if (!initialized_ && !dictionary_requested_) { | 143 if (!initialized_ && !dictionary_requested_) { |
| 129 // RenderThread will not exist in test. | 144 // RenderThread will not exist in test. |
| 130 if (RenderThread::Get()) | 145 if (RenderThread::Get()) |
| 131 RenderThread::Get()->Send(new SpellCheckHostMsg_RequestDictionary); | 146 RenderThread::Get()->Send(new SpellCheckHostMsg_RequestDictionary); |
| 132 dictionary_requested_ = true; | 147 dictionary_requested_ = true; |
| 133 return true; | 148 return true; |
| 134 } | 149 } |
| 135 | 150 |
| 136 // Don't initialize if hunspell is disabled. | 151 // Don't initialize if hunspell is disabled. |
| 137 if (file_ != base::kInvalidPlatformFileValue) | 152 if (file_ != base::kInvalidPlatformFileValue) |
| 138 InitializeHunspell(); | 153 InitializeHunspell(); |
| 139 | 154 |
| 140 return !initialized_; | 155 return !initialized_; |
| 141 } | 156 } |
| 142 | 157 |
| 143 bool HunspellEngine::IsEnabled() { | 158 bool HunspellEngine::IsEnabled() { |
| 144 return file_ != base::kInvalidPlatformFileValue; | 159 return file_ != base::kInvalidPlatformFileValue; |
| 145 } | 160 } |
| OLD | NEW |