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_provider.h" | 5 #include "chrome/renderer/spellchecker/spellcheck_provider.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "chrome/common/chrome_switches.h" | 8 #include "chrome/common/chrome_switches.h" |
9 #include "chrome/common/spellcheck_messages.h" | 9 #include "chrome/common/spellcheck_messages.h" |
10 #include "chrome/common/spellcheck_result.h" | 10 #include "chrome/common/spellcheck_result.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 int(SpellCheckResult::QUOTE), mismatching_enums); | 34 int(SpellCheckResult::QUOTE), mismatching_enums); |
35 COMPILE_ASSERT(int(WebKit::WebTextCheckingTypeDash) == | 35 COMPILE_ASSERT(int(WebKit::WebTextCheckingTypeDash) == |
36 int(SpellCheckResult::DASH), mismatching_enums); | 36 int(SpellCheckResult::DASH), mismatching_enums); |
37 COMPILE_ASSERT(int(WebKit::WebTextCheckingTypeReplacement) == | 37 COMPILE_ASSERT(int(WebKit::WebTextCheckingTypeReplacement) == |
38 int(SpellCheckResult::REPLACEMENT), mismatching_enums); | 38 int(SpellCheckResult::REPLACEMENT), mismatching_enums); |
39 COMPILE_ASSERT(int(WebKit::WebTextCheckingTypeCorrection) == | 39 COMPILE_ASSERT(int(WebKit::WebTextCheckingTypeCorrection) == |
40 int(SpellCheckResult::CORRECTION), mismatching_enums); | 40 int(SpellCheckResult::CORRECTION), mismatching_enums); |
41 COMPILE_ASSERT(int(WebKit::WebTextCheckingTypeShowCorrectionPanel) == | 41 COMPILE_ASSERT(int(WebKit::WebTextCheckingTypeShowCorrectionPanel) == |
42 int(SpellCheckResult::SHOWCORRECTIONPANEL), mismatching_enums); | 42 int(SpellCheckResult::SHOWCORRECTIONPANEL), mismatching_enums); |
43 | 43 |
44 namespace { | |
45 | |
46 // Converts a vector of SpellCheckResult objects (used by Chrome) to a vector of | |
47 // WebTextCheckingResult objects (used by WebKit). | |
48 void CreateTextCheckingResults( | |
49 int offset, | |
50 const std::vector<SpellCheckResult>& spellcheck_results, | |
51 WebKit::WebVector<WebKit::WebTextCheckingResult>* textcheck_results) { | |
52 size_t result_size = spellcheck_results.size(); | |
53 WebKit::WebVector<WebKit::WebTextCheckingResult> list(result_size); | |
54 for (size_t i = 0; i < result_size; ++i) { | |
55 list[i] = WebTextCheckingResult( | |
56 static_cast<WebTextCheckingType>(spellcheck_results[i].type), | |
57 spellcheck_results[i].location + offset, | |
58 spellcheck_results[i].length, | |
59 spellcheck_results[i].replacement); | |
60 } | |
61 textcheck_results->swap(list); | |
62 } | |
63 | |
64 } // namespace | |
65 | |
66 SpellCheckProvider::SpellCheckProvider( | 44 SpellCheckProvider::SpellCheckProvider( |
67 content::RenderView* render_view, | 45 content::RenderView* render_view, |
68 SpellCheck* spellcheck) | 46 SpellCheck* spellcheck) |
69 : content::RenderViewObserver(render_view), | 47 : content::RenderViewObserver(render_view), |
70 content::RenderViewObserverTracker<SpellCheckProvider>(render_view), | 48 content::RenderViewObserverTracker<SpellCheckProvider>(render_view), |
71 spelling_panel_visible_(false), | 49 spelling_panel_visible_(false), |
72 spellcheck_(spellcheck) { | 50 spellcheck_(spellcheck) { |
| 51 DCHECK(spellcheck_); |
73 if (render_view) // NULL in unit tests. | 52 if (render_view) // NULL in unit tests. |
74 render_view->GetWebView()->setSpellCheckClient(this); | 53 render_view->GetWebView()->setSpellCheckClient(this); |
75 } | 54 } |
76 | 55 |
77 SpellCheckProvider::~SpellCheckProvider() { | 56 SpellCheckProvider::~SpellCheckProvider() { |
78 #if defined(OS_MACOSX) | 57 #if defined(OS_MACOSX) |
79 Send(new SpellCheckHostMsg_DocumentClosed( | 58 Send(new SpellCheckHostMsg_DocumentClosed(routing_id(), routing_id())); |
80 routing_id(), routing_id())); | |
81 #endif | 59 #endif |
82 } | 60 } |
83 | 61 |
84 void SpellCheckProvider::RequestTextChecking( | 62 void SpellCheckProvider::RequestTextChecking( |
85 const WebString& text, | 63 const WebString& text, |
86 WebTextCheckingCompletion* completion) { | 64 WebTextCheckingCompletion* completion) { |
87 #if defined(OS_MACOSX) | 65 #if defined(OS_MACOSX) |
88 // Text check (unified request for grammar and spell check) is only | 66 // Text check (unified request for grammar and spell check) is only |
89 // available for browser process, so we ask the system spellchecker | 67 // available for browser process, so we ask the system spellchecker |
90 // over IPC or return an empty result if the checker is not | 68 // over IPC or return an empty result if the checker is not |
(...skipping 13 matching lines...) Expand all Loading... |
104 // Try to satisfy check from cache. | 82 // Try to satisfy check from cache. |
105 // TODO(groby): Should that be applied to OSX results, too? | 83 // TODO(groby): Should that be applied to OSX results, too? |
106 if (SatisfyRequestFromCache(text, completion)) | 84 if (SatisfyRequestFromCache(text, completion)) |
107 return; | 85 return; |
108 | 86 |
109 // Send this text to a browser. A browser checks the user profile and send | 87 // Send this text to a browser. A browser checks the user profile and send |
110 // this text to the Spelling service only if a user enables this feature. | 88 // this text to the Spelling service only if a user enables this feature. |
111 last_request_.clear(); | 89 last_request_.clear(); |
112 last_results_.assign(WebKit::WebVector<WebKit::WebTextCheckingResult>()); | 90 last_results_.assign(WebKit::WebVector<WebKit::WebTextCheckingResult>()); |
113 | 91 |
114 | |
115 Send(new SpellCheckHostMsg_CallSpellingService( | 92 Send(new SpellCheckHostMsg_CallSpellingService( |
116 routing_id(), | 93 routing_id(), |
117 text_check_completions_.Add(completion), | 94 text_check_completions_.Add(completion), |
118 0, | 95 0, |
119 string16(text))); | 96 string16(text))); |
120 #endif // !OS_MACOSX | 97 #endif // !OS_MACOSX |
121 } | 98 } |
122 | 99 |
123 bool SpellCheckProvider::OnMessageReceived(const IPC::Message& message) { | 100 bool SpellCheckProvider::OnMessageReceived(const IPC::Message& message) { |
124 bool handled = true; | 101 bool handled = true; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 Send(new SpellCheckHostMsg_ToggleSpellCheck(routing_id(), enabled, checked)); | 133 Send(new SpellCheckHostMsg_ToggleSpellCheck(routing_id(), enabled, checked)); |
157 #endif // OS_MACOSX | 134 #endif // OS_MACOSX |
158 } | 135 } |
159 | 136 |
160 void SpellCheckProvider::spellCheck( | 137 void SpellCheckProvider::spellCheck( |
161 const WebString& text, | 138 const WebString& text, |
162 int& offset, | 139 int& offset, |
163 int& length, | 140 int& length, |
164 WebVector<WebString>* optional_suggestions) { | 141 WebVector<WebString>* optional_suggestions) { |
165 string16 word(text); | 142 string16 word(text); |
166 // Will be NULL during unit tests. | 143 std::vector<string16> suggestions; |
167 if (spellcheck_) { | 144 spellcheck_->SpellCheckWord( |
168 std::vector<string16> suggestions; | 145 word.c_str(), word.size(), routing_id(), |
169 spellcheck_->SpellCheckWord( | 146 &offset, &length, optional_suggestions ? & suggestions : NULL); |
170 word.c_str(), word.size(), routing_id(), | 147 if (optional_suggestions) |
171 &offset, &length, optional_suggestions ? & suggestions : NULL); | 148 *optional_suggestions = suggestions; |
172 if (optional_suggestions) | 149 if (!optional_suggestions) { |
173 *optional_suggestions = suggestions; | 150 // If optional_suggestions is not requested, the API is called |
174 if (!optional_suggestions) { | 151 // for marking. So we use this for counting markable words. |
175 // If optional_suggestions is not requested, the API is called | 152 Send(new SpellCheckHostMsg_NotifyChecked(routing_id(), word, 0 < length)); |
176 // for marking. So we use this for counting markable words. | |
177 Send(new SpellCheckHostMsg_NotifyChecked(routing_id(), word, 0 < length)); | |
178 } | |
179 } | 153 } |
180 } | 154 } |
181 | 155 |
182 void SpellCheckProvider::checkTextOfParagraph( | 156 void SpellCheckProvider::checkTextOfParagraph( |
183 const WebKit::WebString& text, | 157 const WebKit::WebString& text, |
184 WebKit::WebTextCheckingTypeMask mask, | 158 WebKit::WebTextCheckingTypeMask mask, |
185 WebKit::WebVector<WebKit::WebTextCheckingResult>* results) { | 159 WebKit::WebVector<WebKit::WebTextCheckingResult>* results) { |
186 #if !defined(OS_MACOSX) | 160 #if !defined(OS_MACOSX) |
187 // Since Mac has its own spell checker, this method will not be used on Mac. | 161 // Since Mac has its own spell checker, this method will not be used on Mac. |
188 | 162 |
189 if (!results) | 163 if (!results) |
190 return; | 164 return; |
191 | 165 |
192 if (!(mask & WebKit::WebTextCheckingTypeSpelling)) | 166 if (!(mask & WebKit::WebTextCheckingTypeSpelling)) |
193 return; | 167 return; |
194 | 168 |
195 // Will be NULL during unit tets. | |
196 if (!spellcheck_) | |
197 return; | |
198 | |
199 spellcheck_->SpellCheckParagraph(string16(text), results); | 169 spellcheck_->SpellCheckParagraph(string16(text), results); |
200 #endif | 170 #endif |
201 } | 171 } |
202 | 172 |
203 void SpellCheckProvider::requestCheckingOfText( | 173 void SpellCheckProvider::requestCheckingOfText( |
204 const WebString& text, | 174 const WebString& text, |
205 WebTextCheckingCompletion* completion) { | 175 WebTextCheckingCompletion* completion) { |
206 RequestTextChecking(text, completion); | 176 RequestTextChecking(text, completion); |
207 } | 177 } |
208 | 178 |
209 WebString SpellCheckProvider::autoCorrectWord(const WebString& word) { | 179 WebString SpellCheckProvider::autoCorrectWord(const WebString& word) { |
210 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 180 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
211 if (command_line.HasSwitch(switches::kEnableSpellingAutoCorrect)) { | 181 if (command_line.HasSwitch(switches::kEnableSpellingAutoCorrect)) |
212 // Will be NULL during unit tests. | 182 return spellcheck_->GetAutoCorrectionWord(word, routing_id()); |
213 if (spellcheck_) { | |
214 return spellcheck_->GetAutoCorrectionWord(word, routing_id()); | |
215 } | |
216 } | |
217 return string16(); | 183 return string16(); |
218 } | 184 } |
219 | 185 |
220 void SpellCheckProvider::showSpellingUI(bool show) { | 186 void SpellCheckProvider::showSpellingUI(bool show) { |
221 #if defined(OS_MACOSX) | 187 #if defined(OS_MACOSX) |
222 Send(new SpellCheckHostMsg_ShowSpellingPanel(routing_id(), show)); | 188 Send(new SpellCheckHostMsg_ShowSpellingPanel(routing_id(), show)); |
223 #endif | 189 #endif |
224 } | 190 } |
225 | 191 |
226 bool SpellCheckProvider::isShowingSpellingUI() { | 192 bool SpellCheckProvider::isShowingSpellingUI() { |
(...skipping 16 matching lines...) Expand all Loading... |
243 const string16& line, | 209 const string16& line, |
244 const std::vector<SpellCheckResult>& results) { | 210 const std::vector<SpellCheckResult>& results) { |
245 WebTextCheckingCompletion* completion = | 211 WebTextCheckingCompletion* completion = |
246 text_check_completions_.Lookup(identifier); | 212 text_check_completions_.Lookup(identifier); |
247 if (!completion) | 213 if (!completion) |
248 return; | 214 return; |
249 text_check_completions_.Remove(identifier); | 215 text_check_completions_.Remove(identifier); |
250 | 216 |
251 // If |succeeded| is false, we use local spellcheck as a fallback. | 217 // If |succeeded| is false, we use local spellcheck as a fallback. |
252 if (!succeeded) { | 218 if (!succeeded) { |
253 // |spellcheck_| may be NULL in unit tests. | 219 spellcheck_->RequestTextChecking(line, offset, completion); |
254 if (spellcheck_) { | 220 return; |
255 spellcheck_->RequestTextChecking(line, offset, completion); | |
256 return; | |
257 } | |
258 } | 221 } |
259 | 222 |
260 // Double-check the returned spellchecking results with our spellchecker to | 223 // Double-check the returned spellchecking results with our spellchecker to |
261 // visualize the differences between ours and the on-line spellchecker. | 224 // visualize the differences between ours and the on-line spellchecker. |
262 WebKit::WebVector<WebKit::WebTextCheckingResult> textcheck_results; | 225 WebKit::WebVector<WebKit::WebTextCheckingResult> textcheck_results; |
263 if (spellcheck_) { | 226 spellcheck_->CreateTextCheckingResults(SpellCheck::USE_NATIVE_CHECKER, |
264 spellcheck_->CreateTextCheckingResults( | 227 offset, |
265 offset, line, results, &textcheck_results); | 228 line, |
266 } else { | 229 results, |
267 CreateTextCheckingResults(offset, results, &textcheck_results); | 230 &textcheck_results); |
268 } | |
269 completion->didFinishCheckingText(textcheck_results); | 231 completion->didFinishCheckingText(textcheck_results); |
270 | 232 |
271 // Cache the request and the converted results. | 233 // Cache the request and the converted results. |
272 last_request_ = line; | 234 last_request_ = line; |
273 last_results_.swap(textcheck_results); | 235 last_results_.swap(textcheck_results); |
274 } | 236 } |
275 | 237 |
276 bool SpellCheckProvider::HasWordCharacters( | 238 bool SpellCheckProvider::HasWordCharacters( |
277 const WebKit::WebString& text, | 239 const WebKit::WebString& text, |
278 int index) const { | 240 int index) const { |
(...skipping 14 matching lines...) Expand all Loading... |
293 void SpellCheckProvider::OnAdvanceToNextMisspelling() { | 255 void SpellCheckProvider::OnAdvanceToNextMisspelling() { |
294 if (!render_view()->GetWebView()) | 256 if (!render_view()->GetWebView()) |
295 return; | 257 return; |
296 render_view()->GetWebView()->focusedFrame()->executeCommand( | 258 render_view()->GetWebView()->focusedFrame()->executeCommand( |
297 WebString::fromUTF8("AdvanceToNextMisspelling")); | 259 WebString::fromUTF8("AdvanceToNextMisspelling")); |
298 } | 260 } |
299 | 261 |
300 void SpellCheckProvider::OnRespondTextCheck( | 262 void SpellCheckProvider::OnRespondTextCheck( |
301 int identifier, | 263 int identifier, |
302 const std::vector<SpellCheckResult>& results) { | 264 const std::vector<SpellCheckResult>& results) { |
| 265 DCHECK(spellcheck_); |
303 WebTextCheckingCompletion* completion = | 266 WebTextCheckingCompletion* completion = |
304 text_check_completions_.Lookup(identifier); | 267 text_check_completions_.Lookup(identifier); |
305 if (!completion) | 268 if (!completion) |
306 return; | 269 return; |
307 text_check_completions_.Remove(identifier); | 270 text_check_completions_.Remove(identifier); |
308 WebKit::WebVector<WebKit::WebTextCheckingResult> textcheck_results; | 271 WebKit::WebVector<WebKit::WebTextCheckingResult> textcheck_results; |
309 CreateTextCheckingResults(0, results, &textcheck_results); | 272 spellcheck_->CreateTextCheckingResults(SpellCheck::DO_NOT_MODIFY, |
| 273 0, |
| 274 string16(), |
| 275 results, |
| 276 &textcheck_results); |
310 completion->didFinishCheckingText(textcheck_results); | 277 completion->didFinishCheckingText(textcheck_results); |
311 } | 278 } |
312 | 279 |
313 void SpellCheckProvider::OnToggleSpellPanel(bool is_currently_visible) { | 280 void SpellCheckProvider::OnToggleSpellPanel(bool is_currently_visible) { |
314 if (!render_view()->GetWebView()) | 281 if (!render_view()->GetWebView()) |
315 return; | 282 return; |
316 // We need to tell the webView whether the spelling panel is visible or not so | 283 // We need to tell the webView whether the spelling panel is visible or not so |
317 // that it won't need to make ipc calls later. | 284 // that it won't need to make ipc calls later. |
318 spelling_panel_visible_ = is_currently_visible; | 285 spelling_panel_visible_ = is_currently_visible; |
319 render_view()->GetWebView()->focusedFrame()->executeCommand( | 286 render_view()->GetWebView()->focusedFrame()->executeCommand( |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 results[i].replacement = last_results_[i].replacement; | 341 results[i].replacement = last_results_[i].replacement; |
375 } | 342 } |
376 completion->didFinishCheckingText(results); | 343 completion->didFinishCheckingText(results); |
377 return true; | 344 return true; |
378 } | 345 } |
379 } | 346 } |
380 | 347 |
381 return false; | 348 return false; |
382 } | 349 } |
383 #endif | 350 #endif |
OLD | NEW |