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