OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/histogram.h" | 8 #include "base/histogram.h" |
9 #include "base/time.h" | 9 #include "base/time.h" |
10 #include "chrome/renderer/render_thread.h" | 10 #include "chrome/renderer/render_thread.h" |
11 #include "third_party/hunspell/src/hunspell/hunspell.hxx" | 11 #include "third_party/hunspell/src/hunspell/hunspell.hxx" |
12 | 12 |
13 static const int kMaxAutoCorrectWordSize = 8; | 13 static const int kMaxAutoCorrectWordSize = 8; |
14 static const int kMaxSuggestions = 5; | 14 static const int kMaxSuggestions = 5; |
15 | 15 |
16 using base::TimeTicks; | 16 using base::TimeTicks; |
17 | 17 |
18 SpellCheck::SpellCheck() | 18 SpellCheck::SpellCheck() |
19 : auto_spell_correct_turned_on_(false), | 19 : file_(base::kInvalidPlatformFileValue), |
| 20 auto_spell_correct_turned_on_(false), |
20 // TODO(estade): initialize this properly. | 21 // TODO(estade): initialize this properly. |
21 is_using_platform_spelling_engine_(false), | 22 is_using_platform_spelling_engine_(false), |
22 initialized_(false) { | 23 initialized_(false) { |
23 // Wait till we check the first word before doing any initializing. | 24 // Wait till we check the first word before doing any initializing. |
24 } | 25 } |
25 | 26 |
26 SpellCheck::~SpellCheck() { | 27 SpellCheck::~SpellCheck() { |
27 } | 28 } |
28 | 29 |
29 void SpellCheck::Init(const base::FileDescriptor& fd, | 30 void SpellCheck::Init(base::PlatformFile file, |
30 const std::vector<std::string>& custom_words, | 31 const std::vector<std::string>& custom_words, |
31 const std::string language) { | 32 const std::string language) { |
32 initialized_ = true; | 33 initialized_ = true; |
33 hunspell_.reset(); | 34 hunspell_.reset(); |
34 bdict_file_.reset(); | 35 bdict_file_.reset(); |
35 fd_ = fd; | 36 file_ = file; |
36 character_attributes_.SetDefaultLanguage(language); | 37 character_attributes_.SetDefaultLanguage(language); |
37 | 38 |
38 custom_words_.insert(custom_words_.end(), | 39 custom_words_.insert(custom_words_.end(), |
39 custom_words.begin(), custom_words.end()); | 40 custom_words.begin(), custom_words.end()); |
40 | 41 |
41 // We delay the actual initialization of hunspell until it is needed. | 42 // We delay the actual initialization of hunspell until it is needed. |
42 } | 43 } |
43 | 44 |
44 bool SpellCheck::SpellCheckWord( | 45 bool SpellCheck::SpellCheckWord( |
45 const char16* in_word, | 46 const char16* in_word, |
46 int in_word_len, | 47 int in_word_len, |
47 int tag, | 48 int tag, |
48 int* misspelling_start, | 49 int* misspelling_start, |
49 int* misspelling_len, | 50 int* misspelling_len, |
50 std::vector<string16>* optional_suggestions) { | 51 std::vector<string16>* optional_suggestions) { |
51 DCHECK(in_word_len >= 0); | 52 DCHECK(in_word_len >= 0); |
52 DCHECK(misspelling_start && misspelling_len) << "Out vars must be given."; | 53 DCHECK(misspelling_start && misspelling_len) << "Out vars must be given."; |
53 | 54 |
54 // Do nothing if we need to delay initialization. (Rather than blocking, | 55 // Do nothing if we need to delay initialization. (Rather than blocking, |
55 // report the word as correctly spelled.) | 56 // report the word as correctly spelled.) |
56 if (InitializeIfNeeded()) | 57 if (InitializeIfNeeded()) |
57 return true; | 58 return true; |
58 | 59 |
59 // Do nothing if spell checking is disabled. | 60 // Do nothing if spell checking is disabled. |
60 if (initialized_ && fd_.fd == -1) | 61 if (initialized_ && file_ == base::kInvalidPlatformFileValue) |
61 return true; | 62 return true; |
62 | 63 |
63 *misspelling_start = 0; | 64 *misspelling_start = 0; |
64 *misspelling_len = 0; | 65 *misspelling_len = 0; |
65 if (in_word_len == 0) | 66 if (in_word_len == 0) |
66 return true; // No input means always spelled correctly. | 67 return true; // No input means always spelled correctly. |
67 | 68 |
68 SpellcheckWordIterator word_iterator; | 69 SpellcheckWordIterator word_iterator; |
69 string16 word; | 70 string16 word; |
70 int word_start; | 71 int word_start; |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 AddWordToHunspell(word); | 158 AddWordToHunspell(word); |
158 } | 159 } |
159 } | 160 } |
160 | 161 |
161 void SpellCheck::InitializeHunspell() { | 162 void SpellCheck::InitializeHunspell() { |
162 if (hunspell_.get()) | 163 if (hunspell_.get()) |
163 return; | 164 return; |
164 | 165 |
165 bdict_file_.reset(new file_util::MemoryMappedFile); | 166 bdict_file_.reset(new file_util::MemoryMappedFile); |
166 | 167 |
167 if (bdict_file_->Initialize(fd_)) { | 168 if (bdict_file_->Initialize(file_)) { |
168 TimeTicks start_time = TimeTicks::Now(); | 169 TimeTicks start_time = TimeTicks::Now(); |
169 | 170 |
170 hunspell_.reset( | 171 hunspell_.reset( |
171 new Hunspell(bdict_file_->data(), bdict_file_->length())); | 172 new Hunspell(bdict_file_->data(), bdict_file_->length())); |
172 | 173 |
173 // Add custom words to Hunspell. | 174 // Add custom words to Hunspell. |
174 for (std::vector<std::string>::iterator it = custom_words_.begin(); | 175 for (std::vector<std::string>::iterator it = custom_words_.begin(); |
175 it != custom_words_.end(); ++it) { | 176 it != custom_words_.end(); ++it) { |
176 AddWordToHunspell(*it); | 177 AddWordToHunspell(*it); |
177 } | 178 } |
178 | 179 |
179 DHISTOGRAM_TIMES("Spellcheck.InitTime", | 180 DHISTOGRAM_TIMES("Spellcheck.InitTime", |
180 TimeTicks::Now() - start_time); | 181 TimeTicks::Now() - start_time); |
181 } | 182 } |
182 } | 183 } |
183 | 184 |
184 void SpellCheck::AddWordToHunspell(const std::string& word) { | 185 void SpellCheck::AddWordToHunspell(const std::string& word) { |
185 if (!word.empty() && word.length() < MAXWORDUTF8LEN) | 186 if (!word.empty() && word.length() < MAXWORDUTF8LEN) |
186 hunspell_->add(word.c_str()); | 187 hunspell_->add(word.c_str()); |
187 } | 188 } |
188 | 189 |
189 bool SpellCheck::InitializeIfNeeded() { | 190 bool SpellCheck::InitializeIfNeeded() { |
190 if (!initialized_) { | 191 if (!initialized_) { |
191 RenderThread::current()->RequestSpellCheckDictionary(); | 192 RenderThread::current()->RequestSpellCheckDictionary(); |
192 initialized_ = true; | 193 initialized_ = true; |
193 return true; | 194 return true; |
194 } | 195 } |
195 | 196 |
196 // Check if the platform spellchecker is being used. | 197 // Check if the platform spellchecker is being used. |
197 if (!is_using_platform_spelling_engine_ && fd_.fd != -1) { | 198 if (!is_using_platform_spelling_engine_ && |
| 199 file_ != base::kInvalidPlatformFileValue) { |
198 // If it isn't, init hunspell. | 200 // If it isn't, init hunspell. |
199 InitializeHunspell(); | 201 InitializeHunspell(); |
200 } | 202 } |
201 | 203 |
202 return false; | 204 return false; |
203 } | 205 } |
204 | 206 |
205 // When called, relays the request to check the spelling to the proper | 207 // When called, relays the request to check the spelling to the proper |
206 // backend, either hunspell or a platform-specific backend. | 208 // backend, either hunspell or a platform-specific backend. |
207 bool SpellCheck::CheckSpelling(const string16& word_to_check, int tag) { | 209 bool SpellCheck::CheckSpelling(const string16& word_to_check, int tag) { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 | 257 |
256 string16 word; | 258 string16 word; |
257 int word_start; | 259 int word_start; |
258 int word_length; | 260 int word_length; |
259 while (word_iterator.GetNextWord(&word, &word_start, &word_length)) { | 261 while (word_iterator.GetNextWord(&word, &word_start, &word_length)) { |
260 if (!CheckSpelling(word, tag)) | 262 if (!CheckSpelling(word, tag)) |
261 return false; | 263 return false; |
262 } | 264 } |
263 return true; | 265 return true; |
264 } | 266 } |
OLD | NEW |