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" |
11 #include "chrome/renderer/chrome_content_renderer_client.h" | 11 #include "chrome/renderer/chrome_content_renderer_client.h" |
12 #include "chrome/renderer/spellchecker/spellcheck.h" | 12 #include "chrome/renderer/spellchecker/spellcheck.h" |
13 #include "content/public/renderer/render_view.h" | 13 #include "content/public/renderer/render_view.h" |
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCheckingComple
tion.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCheckingComple
tion.h" |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCheckingResult
.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCheckingResult
.h" |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCheckingType.h
" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCheckingType.h
" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" |
19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
20 | 20 |
| 21 using spellcheck::ToWebResultList; |
21 using WebKit::WebFrame; | 22 using WebKit::WebFrame; |
22 using WebKit::WebString; | 23 using WebKit::WebString; |
23 using WebKit::WebTextCheckingCompletion; | 24 using WebKit::WebTextCheckingCompletion; |
24 using WebKit::WebTextCheckingResult; | 25 using WebKit::WebTextCheckingResult; |
25 using WebKit::WebTextCheckingType; | 26 using WebKit::WebTextCheckingType; |
26 using WebKit::WebVector; | 27 using WebKit::WebVector; |
27 | 28 |
28 COMPILE_ASSERT(int(WebKit::WebTextCheckingTypeSpelling) == | 29 COMPILE_ASSERT(int(WebKit::WebTextCheckingTypeSpelling) == |
29 int(SpellCheckResult::SPELLING), mismatching_enums); | 30 int(SpellCheckResult::SPELLING), mismatching_enums); |
30 COMPILE_ASSERT(int(WebKit::WebTextCheckingTypeGrammar) == | 31 COMPILE_ASSERT(int(WebKit::WebTextCheckingTypeGrammar) == |
31 int(SpellCheckResult::GRAMMAR), mismatching_enums); | 32 int(SpellCheckResult::GRAMMAR), mismatching_enums); |
32 COMPILE_ASSERT(int(WebKit::WebTextCheckingTypeLink) == | 33 COMPILE_ASSERT(int(WebKit::WebTextCheckingTypeLink) == |
33 int(SpellCheckResult::LINK), mismatching_enums); | 34 int(SpellCheckResult::LINK), mismatching_enums); |
34 COMPILE_ASSERT(int(WebKit::WebTextCheckingTypeQuote) == | 35 COMPILE_ASSERT(int(WebKit::WebTextCheckingTypeQuote) == |
35 int(SpellCheckResult::QUOTE), mismatching_enums); | 36 int(SpellCheckResult::QUOTE), mismatching_enums); |
36 COMPILE_ASSERT(int(WebKit::WebTextCheckingTypeDash) == | 37 COMPILE_ASSERT(int(WebKit::WebTextCheckingTypeDash) == |
37 int(SpellCheckResult::DASH), mismatching_enums); | 38 int(SpellCheckResult::DASH), mismatching_enums); |
38 COMPILE_ASSERT(int(WebKit::WebTextCheckingTypeReplacement) == | 39 COMPILE_ASSERT(int(WebKit::WebTextCheckingTypeReplacement) == |
39 int(SpellCheckResult::REPLACEMENT), mismatching_enums); | 40 int(SpellCheckResult::REPLACEMENT), mismatching_enums); |
40 COMPILE_ASSERT(int(WebKit::WebTextCheckingTypeCorrection) == | 41 COMPILE_ASSERT(int(WebKit::WebTextCheckingTypeCorrection) == |
41 int(SpellCheckResult::CORRECTION), mismatching_enums); | 42 int(SpellCheckResult::CORRECTION), mismatching_enums); |
42 COMPILE_ASSERT(int(WebKit::WebTextCheckingTypeShowCorrectionPanel) == | 43 COMPILE_ASSERT(int(WebKit::WebTextCheckingTypeShowCorrectionPanel) == |
43 int(SpellCheckResult::SHOWCORRECTIONPANEL), mismatching_enums); | 44 int(SpellCheckResult::SHOWCORRECTIONPANEL), mismatching_enums); |
44 | 45 |
45 namespace { | |
46 | |
47 void ToWebResultList( | |
48 int offset, | |
49 const std::vector<SpellCheckResult>& results, | |
50 WebVector<WebTextCheckingResult>* web_results) { | |
51 WebVector<WebTextCheckingResult> list(results.size()); | |
52 for (size_t i = 0; i < results.size(); ++i) { | |
53 list[i] = WebTextCheckingResult( | |
54 static_cast<WebTextCheckingType>(results[i].type), | |
55 results[i].location + offset, | |
56 results[i].length, | |
57 results[i].replacement); | |
58 } | |
59 | |
60 list.swap(*web_results); | |
61 } | |
62 | |
63 WebVector<WebTextCheckingResult> ToWebResultList( | |
64 int offset, | |
65 const std::vector<SpellCheckResult>& results) { | |
66 WebVector<WebTextCheckingResult> web_results; | |
67 ToWebResultList(offset, results, &web_results); | |
68 return web_results; | |
69 } | |
70 | |
71 } // namespace | |
72 | |
73 SpellCheckProvider::SpellCheckProvider( | 46 SpellCheckProvider::SpellCheckProvider( |
74 content::RenderView* render_view, | 47 content::RenderView* render_view, |
75 chrome::ChromeContentRendererClient* renderer_client) | 48 chrome::ChromeContentRendererClient* renderer_client) |
76 : content::RenderViewObserver(render_view), | 49 : content::RenderViewObserver(render_view), |
77 #if defined(OS_MACOSX) | 50 #if defined(OS_MACOSX) |
78 has_document_tag_(false), | 51 has_document_tag_(false), |
79 #endif | 52 #endif |
80 document_tag_(0), | 53 document_tag_(0), |
81 spelling_panel_visible_(false), | 54 spelling_panel_visible_(false), |
82 chrome_content_renderer_client_(renderer_client) { | 55 chrome_content_renderer_client_(renderer_client) { |
(...skipping 28 matching lines...) Expand all Loading... |
111 #else | 84 #else |
112 // Send this text to a browser. A browser checks the user profile and send | 85 // Send this text to a browser. A browser checks the user profile and send |
113 // this text to the Spelling service only if a user enables this feature. | 86 // this text to the Spelling service only if a user enables this feature. |
114 // TODO(hbono) Implement a cache to avoid sending IPC messages. | 87 // TODO(hbono) Implement a cache to avoid sending IPC messages. |
115 string16 line; | 88 string16 line; |
116 int offset = -1; | 89 int offset = -1; |
117 if (!GetRequestLine(text, &line, &offset)) { | 90 if (!GetRequestLine(text, &line, &offset)) { |
118 completion->didFinishCheckingText(std::vector<WebTextCheckingResult>()); | 91 completion->didFinishCheckingText(std::vector<WebTextCheckingResult>()); |
119 return; | 92 return; |
120 } | 93 } |
| 94 |
121 last_line_ = line; | 95 last_line_ = line; |
122 Send(new SpellCheckHostMsg_CallSpellingService( | 96 Send(new SpellCheckHostMsg_CallSpellingService( |
123 routing_id(), | 97 routing_id(), |
124 text_check_completions_.Add(completion), | 98 text_check_completions_.Add(completion), |
125 offset, | 99 offset, |
126 line)); | 100 line)); |
127 #endif // !OS_MACOSX | 101 #endif // !OS_MACOSX |
128 } | 102 } |
129 | 103 |
130 bool SpellCheckProvider::OnMessageReceived(const IPC::Message& message) { | 104 bool SpellCheckProvider::OnMessageReceived(const IPC::Message& message) { |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 | 177 |
204 EnsureDocumentTag(); | 178 EnsureDocumentTag(); |
205 | 179 |
206 // Will be NULL during unit tets. | 180 // Will be NULL during unit tets. |
207 if (!chrome_content_renderer_client_) | 181 if (!chrome_content_renderer_client_) |
208 return; | 182 return; |
209 | 183 |
210 std::vector<SpellCheckResult> tmp_results; | 184 std::vector<SpellCheckResult> tmp_results; |
211 chrome_content_renderer_client_->spellcheck()->SpellCheckParagraph( | 185 chrome_content_renderer_client_->spellcheck()->SpellCheckParagraph( |
212 string16(text), | 186 string16(text), |
213 document_tag_, | |
214 &tmp_results); | 187 &tmp_results); |
215 ToWebResultList(0, tmp_results, results); | 188 ToWebResultList(0, tmp_results, results); |
216 #endif | 189 #endif |
217 } | 190 } |
218 | 191 |
219 void SpellCheckProvider::requestCheckingOfText( | 192 void SpellCheckProvider::requestCheckingOfText( |
220 const WebString& text, | 193 const WebString& text, |
221 WebTextCheckingCompletion* completion) { | 194 WebTextCheckingCompletion* completion) { |
222 RequestTextChecking(text, document_tag_, completion); | 195 RequestTextChecking(text, document_tag_, completion); |
223 } | 196 } |
(...skipping 26 matching lines...) Expand all Loading... |
250 #if defined(OS_MACOSX) | 223 #if defined(OS_MACOSX) |
251 Send(new SpellCheckHostMsg_UpdateSpellingPanelWithMisspelledWord(routing_id(), | 224 Send(new SpellCheckHostMsg_UpdateSpellingPanelWithMisspelledWord(routing_id(), |
252 word)); | 225 word)); |
253 #endif | 226 #endif |
254 } | 227 } |
255 | 228 |
256 #if !defined(OS_MACOSX) | 229 #if !defined(OS_MACOSX) |
257 void SpellCheckProvider::OnRespondSpellingService( | 230 void SpellCheckProvider::OnRespondSpellingService( |
258 int identifier, | 231 int identifier, |
259 int offset, | 232 int offset, |
| 233 bool succeeded, |
| 234 const string16& text, |
260 const std::vector<SpellCheckResult>& results) { | 235 const std::vector<SpellCheckResult>& results) { |
261 WebTextCheckingCompletion* completion = | 236 WebTextCheckingCompletion* completion = |
262 text_check_completions_.Lookup(identifier); | 237 text_check_completions_.Lookup(identifier); |
263 if (!completion) | 238 if (!completion) |
264 return; | 239 return; |
265 text_check_completions_.Remove(identifier); | 240 text_check_completions_.Remove(identifier); |
| 241 |
| 242 // If |succeeded| is false, we use local spellcheck as a fallback. |
| 243 if (!succeeded) { |
| 244 // |chrome_content_renderer_client| may be NULL in unit tests. |
| 245 if (chrome_content_renderer_client_) { |
| 246 chrome_content_renderer_client_->spellcheck()->RequestTextChecking( |
| 247 text, offset, completion); |
| 248 return; |
| 249 } |
| 250 } |
| 251 |
266 completion->didFinishCheckingText(ToWebResultList(offset, results)); | 252 completion->didFinishCheckingText(ToWebResultList(offset, results)); |
267 } | 253 } |
268 | 254 |
269 bool SpellCheckProvider::HasWordCharacters(const string16& text, | 255 bool SpellCheckProvider::HasWordCharacters(const string16& text, |
270 int index) const { | 256 int index) const { |
271 const char16* data = text.c_str(); | 257 const char16* data = text.c_str(); |
272 int length = text.length(); | 258 int length = text.length(); |
273 while (index < length) { | 259 while (index < length) { |
274 uint32 code = 0; | 260 uint32 code = 0; |
275 U16_NEXT(data, index, length, code); | 261 U16_NEXT(data, index, length, code); |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 // TODO(darin): There's actually no reason for this to be here. We should | 349 // TODO(darin): There's actually no reason for this to be here. We should |
364 // have the browser side manage the document tag. | 350 // have the browser side manage the document tag. |
365 #if defined(OS_MACOSX) | 351 #if defined(OS_MACOSX) |
366 if (!has_document_tag_) { | 352 if (!has_document_tag_) { |
367 // Make the call to get the tag. | 353 // Make the call to get the tag. |
368 Send(new SpellCheckHostMsg_GetDocumentTag(routing_id(), &document_tag_)); | 354 Send(new SpellCheckHostMsg_GetDocumentTag(routing_id(), &document_tag_)); |
369 has_document_tag_ = true; | 355 has_document_tag_ = true; |
370 } | 356 } |
371 #endif | 357 #endif |
372 } | 358 } |
OLD | NEW |