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 "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "chrome/common/chrome_switches.h" | 9 #include "chrome/common/chrome_switches.h" |
10 #include "chrome/common/spellcheck_marker.h" | 10 #include "chrome/common/spellcheck_marker.h" |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 | 65 |
66 // Try to satisfy check from cache. | 66 // Try to satisfy check from cache. |
67 if (SatisfyRequestFromCache(text, completion)) | 67 if (SatisfyRequestFromCache(text, completion)) |
68 return; | 68 return; |
69 | 69 |
70 // Send this text to a browser. A browser checks the user profile and send | 70 // Send this text to a browser. A browser checks the user profile and send |
71 // this text to the Spelling service only if a user enables this feature. | 71 // this text to the Spelling service only if a user enables this feature. |
72 last_request_.clear(); | 72 last_request_.clear(); |
73 last_results_.assign(blink::WebVector<blink::WebTextCheckingResult>()); | 73 last_results_.assign(blink::WebVector<blink::WebTextCheckingResult>()); |
74 | 74 |
75 #if defined(OS_MACOSX) | 75 #if defined(USE_PLATFORM_SPELLCHECKER) |
76 // Text check (unified request for grammar and spell check) is only | 76 // Text check (unified request for grammar and spell check) is only |
77 // available for browser process, so we ask the system spellchecker | 77 // available for browser process, so we ask the system spellchecker |
78 // over IPC or return an empty result if the checker is not | 78 // over IPC or return an empty result if the checker is not |
79 // available. | 79 // available. |
80 Send(new SpellCheckHostMsg_RequestTextCheck( | 80 Send(new SpellCheckHostMsg_RequestTextCheck( |
81 routing_id(), | 81 routing_id(), |
82 text_check_completions_.Add(completion), | 82 text_check_completions_.Add(completion), |
83 text, | 83 text, |
84 markers)); | 84 markers)); |
85 #else | 85 #else |
86 Send(new SpellCheckHostMsg_CallSpellingService( | 86 Send(new SpellCheckHostMsg_CallSpellingService( |
87 routing_id(), | 87 routing_id(), |
88 text_check_completions_.Add(completion), | 88 text_check_completions_.Add(completion), |
89 base::string16(text), | 89 base::string16(text), |
90 markers)); | 90 markers)); |
91 #endif // !OS_MACOSX | 91 #endif // !USE_PLATFORM_SPELLCHECKER |
92 } | 92 } |
93 | 93 |
94 bool SpellCheckProvider::OnMessageReceived(const IPC::Message& message) { | 94 bool SpellCheckProvider::OnMessageReceived(const IPC::Message& message) { |
95 bool handled = true; | 95 bool handled = true; |
96 IPC_BEGIN_MESSAGE_MAP(SpellCheckProvider, message) | 96 IPC_BEGIN_MESSAGE_MAP(SpellCheckProvider, message) |
97 #if !defined(OS_MACOSX) | 97 #if !defined(USE_PLATFORM_SPELLCHECKER) |
98 IPC_MESSAGE_HANDLER(SpellCheckMsg_RespondSpellingService, | 98 IPC_MESSAGE_HANDLER(SpellCheckMsg_RespondSpellingService, |
99 OnRespondSpellingService) | 99 OnRespondSpellingService) |
100 #endif | 100 #endif |
101 #if defined(OS_MACOSX) | 101 #if defined(USE_PLATFORM_SPELLCHECKER) |
102 IPC_MESSAGE_HANDLER(SpellCheckMsg_AdvanceToNextMisspelling, | 102 IPC_MESSAGE_HANDLER(SpellCheckMsg_AdvanceToNextMisspelling, |
103 OnAdvanceToNextMisspelling) | 103 OnAdvanceToNextMisspelling) |
104 IPC_MESSAGE_HANDLER(SpellCheckMsg_RespondTextCheck, OnRespondTextCheck) | 104 IPC_MESSAGE_HANDLER(SpellCheckMsg_RespondTextCheck, OnRespondTextCheck) |
105 IPC_MESSAGE_HANDLER(SpellCheckMsg_ToggleSpellPanel, OnToggleSpellPanel) | 105 IPC_MESSAGE_HANDLER(SpellCheckMsg_ToggleSpellPanel, OnToggleSpellPanel) |
106 #endif | 106 #endif |
107 IPC_MESSAGE_UNHANDLED(handled = false) | 107 IPC_MESSAGE_UNHANDLED(handled = false) |
108 IPC_END_MESSAGE_MAP() | 108 IPC_END_MESSAGE_MAP() |
109 return handled; | 109 return handled; |
110 } | 110 } |
111 | 111 |
112 void SpellCheckProvider::FocusedNodeChanged(const blink::WebNode& unused) { | 112 void SpellCheckProvider::FocusedNodeChanged(const blink::WebNode& unused) { |
113 #if defined(OS_MACOSX) | 113 #if defined(USE_PLATFORM_SPELLCHECKER) |
114 WebFrame* frame = render_view()->GetWebView()->focusedFrame(); | 114 WebFrame* frame = render_view()->GetWebView()->focusedFrame(); |
115 WebElement element = frame->document().isNull() ? WebElement() : | 115 WebElement element = frame->document().isNull() ? WebElement() : |
116 frame->document().focusedElement(); | 116 frame->document().focusedElement(); |
117 bool enabled = !element.isNull() && render_view()->IsEditableNode(element); | 117 bool enabled = !element.isNull() && render_view()->IsEditableNode(element); |
118 | 118 |
119 bool checked = enabled && render_view()->GetWebView() && | 119 bool checked = enabled && render_view()->GetWebView() && |
120 frame->isContinuousSpellCheckingEnabled(); | 120 frame->isContinuousSpellCheckingEnabled(); |
121 | 121 |
122 Send(new SpellCheckHostMsg_ToggleSpellCheck(routing_id(), enabled, checked)); | 122 Send(new SpellCheckHostMsg_ToggleSpellCheck(routing_id(), enabled, checked)); |
123 #endif // OS_MACOSX | 123 #endif // USE_PLATFORM_SPELLCHECKER |
124 } | 124 } |
125 | 125 |
126 void SpellCheckProvider::spellCheck( | 126 void SpellCheckProvider::spellCheck( |
127 const WebString& text, | 127 const WebString& text, |
128 int& offset, | 128 int& offset, |
129 int& length, | 129 int& length, |
130 WebVector<WebString>* optional_suggestions) { | 130 WebVector<WebString>* optional_suggestions) { |
131 base::string16 word(text); | 131 base::string16 word(text); |
132 std::vector<base::string16> suggestions; | 132 std::vector<base::string16> suggestions; |
133 spellcheck_->SpellCheckWord( | 133 spellcheck_->SpellCheckWord( |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 const base::CommandLine& command_line = | 179 const base::CommandLine& command_line = |
180 *base::CommandLine::ForCurrentProcess(); | 180 *base::CommandLine::ForCurrentProcess(); |
181 if (command_line.HasSwitch(switches::kEnableSpellingAutoCorrect)) { | 181 if (command_line.HasSwitch(switches::kEnableSpellingAutoCorrect)) { |
182 UMA_HISTOGRAM_COUNTS("SpellCheck.api.autocorrect", word.length()); | 182 UMA_HISTOGRAM_COUNTS("SpellCheck.api.autocorrect", word.length()); |
183 return spellcheck_->GetAutoCorrectionWord(word, routing_id()); | 183 return spellcheck_->GetAutoCorrectionWord(word, routing_id()); |
184 } | 184 } |
185 return base::string16(); | 185 return base::string16(); |
186 } | 186 } |
187 | 187 |
188 void SpellCheckProvider::showSpellingUI(bool show) { | 188 void SpellCheckProvider::showSpellingUI(bool show) { |
189 #if defined(OS_MACOSX) | 189 #if defined(USE_PLATFORM_SPELLCHECKER) |
190 UMA_HISTOGRAM_BOOLEAN("SpellCheck.api.showUI", show); | 190 UMA_HISTOGRAM_BOOLEAN("SpellCheck.api.showUI", show); |
191 Send(new SpellCheckHostMsg_ShowSpellingPanel(routing_id(), show)); | 191 Send(new SpellCheckHostMsg_ShowSpellingPanel(routing_id(), show)); |
192 #endif | 192 #endif |
193 } | 193 } |
194 | 194 |
195 bool SpellCheckProvider::isShowingSpellingUI() { | 195 bool SpellCheckProvider::isShowingSpellingUI() { |
196 return spelling_panel_visible_; | 196 return spelling_panel_visible_; |
197 } | 197 } |
198 | 198 |
199 void SpellCheckProvider::updateSpellingUIWithMisspelledWord( | 199 void SpellCheckProvider::updateSpellingUIWithMisspelledWord( |
200 const WebString& word) { | 200 const WebString& word) { |
201 #if defined(OS_MACOSX) | 201 #if defined(USE_PLATFORM_SPELLCHECKER) |
202 Send(new SpellCheckHostMsg_UpdateSpellingPanelWithMisspelledWord(routing_id(), | 202 Send(new SpellCheckHostMsg_UpdateSpellingPanelWithMisspelledWord(routing_id(), |
203 word)); | 203 word)); |
204 #endif | 204 #endif |
205 } | 205 } |
206 | 206 |
207 #if !defined(OS_MACOSX) | 207 #if !defined(USE_PLATFORM_SPELLCHECKER) |
208 void SpellCheckProvider::OnRespondSpellingService( | 208 void SpellCheckProvider::OnRespondSpellingService( |
209 int identifier, | 209 int identifier, |
210 bool succeeded, | 210 bool succeeded, |
211 const base::string16& line, | 211 const base::string16& line, |
212 const std::vector<SpellCheckResult>& results) { | 212 const std::vector<SpellCheckResult>& results) { |
213 WebTextCheckingCompletion* completion = | 213 WebTextCheckingCompletion* completion = |
214 text_check_completions_.Lookup(identifier); | 214 text_check_completions_.Lookup(identifier); |
215 if (!completion) | 215 if (!completion) |
216 return; | 216 return; |
217 text_check_completions_.Remove(identifier); | 217 text_check_completions_.Remove(identifier); |
(...skipping 28 matching lines...) Expand all Loading... |
246 while (index < length) { | 246 while (index < length) { |
247 uint32 code = 0; | 247 uint32 code = 0; |
248 U16_NEXT(data, index, length, code); | 248 U16_NEXT(data, index, length, code); |
249 UErrorCode error = U_ZERO_ERROR; | 249 UErrorCode error = U_ZERO_ERROR; |
250 if (uscript_getScript(code, &error) != USCRIPT_COMMON) | 250 if (uscript_getScript(code, &error) != USCRIPT_COMMON) |
251 return true; | 251 return true; |
252 } | 252 } |
253 return false; | 253 return false; |
254 } | 254 } |
255 | 255 |
256 #if defined(OS_MACOSX) | 256 #if defined(USE_PLATFORM_SPELLCHECKER) |
257 void SpellCheckProvider::OnAdvanceToNextMisspelling() { | 257 void SpellCheckProvider::OnAdvanceToNextMisspelling() { |
258 if (!render_view()->GetWebView()) | 258 if (!render_view()->GetWebView()) |
259 return; | 259 return; |
260 render_view()->GetWebView()->focusedFrame()->executeCommand( | 260 render_view()->GetWebView()->focusedFrame()->executeCommand( |
261 WebString::fromUTF8("AdvanceToNextMisspelling")); | 261 WebString::fromUTF8("AdvanceToNextMisspelling")); |
262 } | 262 } |
263 | 263 |
264 void SpellCheckProvider::OnRespondTextCheck( | 264 void SpellCheckProvider::OnRespondTextCheck( |
265 int identifier, | 265 int identifier, |
266 const std::vector<SpellCheckResult>& results) { | 266 const std::vector<SpellCheckResult>& results) { |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 results[i].length = last_results_[i].length; | 351 results[i].length = last_results_[i].length; |
352 results[i].replacement = last_results_[i].replacement; | 352 results[i].replacement = last_results_[i].replacement; |
353 } | 353 } |
354 completion->didFinishCheckingText(results); | 354 completion->didFinishCheckingText(results); |
355 return true; | 355 return true; |
356 } | 356 } |
357 } | 357 } |
358 | 358 |
359 return false; | 359 return false; |
360 } | 360 } |
OLD | NEW |