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 "components/spellcheck/renderer/spellcheck_provider.h" | 5 #include "components/spellcheck/renderer/spellcheck_provider.h" |
6 | 6 |
7 #include "base/command_line.h" | |
8 #include "base/metrics/histogram_macros.h" | 7 #include "base/metrics/histogram_macros.h" |
9 #include "components/spellcheck/common/spellcheck_messages.h" | 8 #include "components/spellcheck/common/spellcheck_messages.h" |
10 #include "components/spellcheck/common/spellcheck_result.h" | 9 #include "components/spellcheck/common/spellcheck_result.h" |
11 #include "components/spellcheck/renderer/spellcheck.h" | 10 #include "components/spellcheck/renderer/spellcheck.h" |
12 #include "components/spellcheck/renderer/spellcheck_language.h" | 11 #include "components/spellcheck/renderer/spellcheck_language.h" |
13 #include "components/spellcheck/spellcheck_build_features.h" | 12 #include "components/spellcheck/spellcheck_build_features.h" |
14 #include "content/public/renderer/render_view.h" | 13 #include "content/public/renderer/render_view.h" |
15 #include "third_party/WebKit/public/platform/WebVector.h" | 14 #include "third_party/WebKit/public/platform/WebVector.h" |
16 #include "third_party/WebKit/public/web/WebDocument.h" | 15 #include "third_party/WebKit/public/web/WebDocument.h" |
17 #include "third_party/WebKit/public/web/WebElement.h" | 16 #include "third_party/WebKit/public/web/WebElement.h" |
(...skipping 14 matching lines...) Expand all Loading... |
32 static_assert(int(blink::WebTextDecorationTypeSpelling) == | 31 static_assert(int(blink::WebTextDecorationTypeSpelling) == |
33 int(SpellCheckResult::SPELLING), "mismatching enums"); | 32 int(SpellCheckResult::SPELLING), "mismatching enums"); |
34 static_assert(int(blink::WebTextDecorationTypeGrammar) == | 33 static_assert(int(blink::WebTextDecorationTypeGrammar) == |
35 int(SpellCheckResult::GRAMMAR), "mismatching enums"); | 34 int(SpellCheckResult::GRAMMAR), "mismatching enums"); |
36 | 35 |
37 SpellCheckProvider::SpellCheckProvider( | 36 SpellCheckProvider::SpellCheckProvider( |
38 content::RenderView* render_view, | 37 content::RenderView* render_view, |
39 SpellCheck* spellcheck) | 38 SpellCheck* spellcheck) |
40 : content::RenderViewObserver(render_view), | 39 : content::RenderViewObserver(render_view), |
41 content::RenderViewObserverTracker<SpellCheckProvider>(render_view), | 40 content::RenderViewObserverTracker<SpellCheckProvider>(render_view), |
42 spelling_panel_visible_(false), | |
43 spellcheck_(spellcheck) { | 41 spellcheck_(spellcheck) { |
44 DCHECK(spellcheck_); | 42 DCHECK(spellcheck_); |
45 if (render_view) { // NULL in unit tests. | 43 if (render_view) { // NULL in unit tests. |
46 render_view->GetWebView()->setSpellCheckClient(this); | |
47 render_view->GetWebView()->setTextCheckClient(this); | 44 render_view->GetWebView()->setTextCheckClient(this); |
48 EnableSpellcheck(spellcheck_->IsSpellcheckEnabled()); | 45 EnableSpellcheck(spellcheck_->IsSpellcheckEnabled()); |
49 } | 46 } |
50 } | 47 } |
51 | 48 |
52 SpellCheckProvider::~SpellCheckProvider() { | 49 SpellCheckProvider::~SpellCheckProvider() { |
53 } | 50 } |
54 | 51 |
55 void SpellCheckProvider::RequestTextChecking( | 52 void SpellCheckProvider::RequestTextChecking( |
56 const base::string16& text, | 53 const base::string16& text, |
(...skipping 28 matching lines...) Expand all Loading... |
85 } | 82 } |
86 | 83 |
87 bool SpellCheckProvider::OnMessageReceived(const IPC::Message& message) { | 84 bool SpellCheckProvider::OnMessageReceived(const IPC::Message& message) { |
88 bool handled = true; | 85 bool handled = true; |
89 IPC_BEGIN_MESSAGE_MAP(SpellCheckProvider, message) | 86 IPC_BEGIN_MESSAGE_MAP(SpellCheckProvider, message) |
90 #if !BUILDFLAG(USE_BROWSER_SPELLCHECKER) | 87 #if !BUILDFLAG(USE_BROWSER_SPELLCHECKER) |
91 IPC_MESSAGE_HANDLER(SpellCheckMsg_RespondSpellingService, | 88 IPC_MESSAGE_HANDLER(SpellCheckMsg_RespondSpellingService, |
92 OnRespondSpellingService) | 89 OnRespondSpellingService) |
93 #endif | 90 #endif |
94 #if BUILDFLAG(USE_BROWSER_SPELLCHECKER) | 91 #if BUILDFLAG(USE_BROWSER_SPELLCHECKER) |
95 IPC_MESSAGE_HANDLER(SpellCheckMsg_AdvanceToNextMisspelling, | |
96 OnAdvanceToNextMisspelling) | |
97 IPC_MESSAGE_HANDLER(SpellCheckMsg_RespondTextCheck, OnRespondTextCheck) | 92 IPC_MESSAGE_HANDLER(SpellCheckMsg_RespondTextCheck, OnRespondTextCheck) |
98 IPC_MESSAGE_HANDLER(SpellCheckMsg_ToggleSpellPanel, OnToggleSpellPanel) | |
99 #endif | 93 #endif |
100 IPC_MESSAGE_UNHANDLED(handled = false) | 94 IPC_MESSAGE_UNHANDLED(handled = false) |
101 IPC_END_MESSAGE_MAP() | 95 IPC_END_MESSAGE_MAP() |
102 return handled; | 96 return handled; |
103 } | 97 } |
104 | 98 |
105 void SpellCheckProvider::FocusedNodeChanged(const blink::WebNode& unused) { | 99 void SpellCheckProvider::FocusedNodeChanged(const blink::WebNode& unused) { |
106 #if BUILDFLAG(USE_BROWSER_SPELLCHECKER) | 100 #if BUILDFLAG(USE_BROWSER_SPELLCHECKER) |
107 WebLocalFrame* frame = render_view()->GetWebView()->focusedFrame(); | 101 WebLocalFrame* frame = render_view()->GetWebView()->focusedFrame(); |
108 WebElement element = frame->document().isNull() ? WebElement() : | 102 WebElement element = frame->document().isNull() ? WebElement() : |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 } | 142 } |
149 | 143 |
150 void SpellCheckProvider::cancelAllPendingRequests() { | 144 void SpellCheckProvider::cancelAllPendingRequests() { |
151 for (WebTextCheckCompletions::iterator iter(&text_check_completions_); | 145 for (WebTextCheckCompletions::iterator iter(&text_check_completions_); |
152 !iter.IsAtEnd(); iter.Advance()) { | 146 !iter.IsAtEnd(); iter.Advance()) { |
153 iter.GetCurrentValue()->didCancelCheckingText(); | 147 iter.GetCurrentValue()->didCancelCheckingText(); |
154 } | 148 } |
155 text_check_completions_.Clear(); | 149 text_check_completions_.Clear(); |
156 } | 150 } |
157 | 151 |
158 void SpellCheckProvider::showSpellingUI(bool show) { | |
159 #if BUILDFLAG(USE_BROWSER_SPELLCHECKER) | |
160 UMA_HISTOGRAM_BOOLEAN("SpellCheck.api.showUI", show); | |
161 Send(new SpellCheckHostMsg_ShowSpellingPanel(routing_id(), show)); | |
162 #endif | |
163 } | |
164 | |
165 bool SpellCheckProvider::isShowingSpellingUI() { | |
166 return spelling_panel_visible_; | |
167 } | |
168 | |
169 void SpellCheckProvider::updateSpellingUIWithMisspelledWord( | |
170 const WebString& word) { | |
171 #if BUILDFLAG(USE_BROWSER_SPELLCHECKER) | |
172 Send(new SpellCheckHostMsg_UpdateSpellingPanelWithMisspelledWord( | |
173 routing_id(), word.utf16())); | |
174 #endif | |
175 } | |
176 | |
177 #if !BUILDFLAG(USE_BROWSER_SPELLCHECKER) | 152 #if !BUILDFLAG(USE_BROWSER_SPELLCHECKER) |
178 void SpellCheckProvider::OnRespondSpellingService( | 153 void SpellCheckProvider::OnRespondSpellingService( |
179 int identifier, | 154 int identifier, |
180 bool succeeded, | 155 bool succeeded, |
181 const base::string16& line, | 156 const base::string16& line, |
182 const std::vector<SpellCheckResult>& results) { | 157 const std::vector<SpellCheckResult>& results) { |
183 WebTextCheckingCompletion* completion = | 158 WebTextCheckingCompletion* completion = |
184 text_check_completions_.Lookup(identifier); | 159 text_check_completions_.Lookup(identifier); |
185 if (!completion) | 160 if (!completion) |
186 return; | 161 return; |
(...skipping 30 matching lines...) Expand all Loading... |
217 uint32_t code = 0; | 192 uint32_t code = 0; |
218 U16_NEXT(data, index, length, code); | 193 U16_NEXT(data, index, length, code); |
219 UErrorCode error = U_ZERO_ERROR; | 194 UErrorCode error = U_ZERO_ERROR; |
220 if (uscript_getScript(code, &error) != USCRIPT_COMMON) | 195 if (uscript_getScript(code, &error) != USCRIPT_COMMON) |
221 return true; | 196 return true; |
222 } | 197 } |
223 return false; | 198 return false; |
224 } | 199 } |
225 | 200 |
226 #if BUILDFLAG(USE_BROWSER_SPELLCHECKER) | 201 #if BUILDFLAG(USE_BROWSER_SPELLCHECKER) |
227 void SpellCheckProvider::OnAdvanceToNextMisspelling() { | |
228 if (!render_view()->GetWebView()) | |
229 return; | |
230 render_view()->GetWebView()->focusedFrame()->executeCommand( | |
231 WebString::fromUTF8("AdvanceToNextMisspelling")); | |
232 } | |
233 | |
234 void SpellCheckProvider::OnRespondTextCheck( | 202 void SpellCheckProvider::OnRespondTextCheck( |
235 int identifier, | 203 int identifier, |
236 const base::string16& line, | 204 const base::string16& line, |
237 const std::vector<SpellCheckResult>& results) { | 205 const std::vector<SpellCheckResult>& results) { |
238 // TODO(groby): Unify with SpellCheckProvider::OnRespondSpellingService | 206 // TODO(groby): Unify with SpellCheckProvider::OnRespondSpellingService |
239 DCHECK(spellcheck_); | 207 DCHECK(spellcheck_); |
240 WebTextCheckingCompletion* completion = | 208 WebTextCheckingCompletion* completion = |
241 text_check_completions_.Lookup(identifier); | 209 text_check_completions_.Lookup(identifier); |
242 if (!completion) | 210 if (!completion) |
243 return; | 211 return; |
244 text_check_completions_.Remove(identifier); | 212 text_check_completions_.Remove(identifier); |
245 blink::WebVector<blink::WebTextCheckingResult> textcheck_results; | 213 blink::WebVector<blink::WebTextCheckingResult> textcheck_results; |
246 spellcheck_->CreateTextCheckingResults(SpellCheck::DO_NOT_MODIFY, | 214 spellcheck_->CreateTextCheckingResults(SpellCheck::DO_NOT_MODIFY, |
247 0, | 215 0, |
248 line, | 216 line, |
249 results, | 217 results, |
250 &textcheck_results); | 218 &textcheck_results); |
251 completion->didFinishCheckingText(textcheck_results); | 219 completion->didFinishCheckingText(textcheck_results); |
252 | 220 |
253 // Cache the request and the converted results. | 221 // Cache the request and the converted results. |
254 last_request_ = line; | 222 last_request_ = line; |
255 last_results_.swap(textcheck_results); | 223 last_results_.swap(textcheck_results); |
256 } | 224 } |
257 | |
258 void SpellCheckProvider::OnToggleSpellPanel(bool is_currently_visible) { | |
259 if (!render_view()->GetWebView()) | |
260 return; | |
261 // We need to tell the webView whether the spelling panel is visible or not so | |
262 // that it won't need to make ipc calls later. | |
263 spelling_panel_visible_ = is_currently_visible; | |
264 render_view()->GetWebView()->focusedFrame()->executeCommand( | |
265 WebString::fromUTF8("ToggleSpellPanel")); | |
266 } | |
267 #endif | 225 #endif |
268 | 226 |
269 void SpellCheckProvider::EnableSpellcheck(bool enable) { | 227 void SpellCheckProvider::EnableSpellcheck(bool enable) { |
270 if (!render_view()->GetWebView()) | 228 if (!render_view()->GetWebView()) |
271 return; | 229 return; |
272 | 230 |
273 WebLocalFrame* frame = render_view()->GetWebView()->focusedFrame(); | 231 WebLocalFrame* frame = render_view()->GetWebView()->focusedFrame(); |
274 // TODO(yabinh): The null check should be unnecessary. | 232 // TODO(yabinh): The null check should be unnecessary. |
275 // See crbug.com/625068 | 233 // See crbug.com/625068 |
276 if (!frame) | 234 if (!frame) |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 completion->didFinishCheckingText(results); | 277 completion->didFinishCheckingText(results); |
320 return true; | 278 return true; |
321 } | 279 } |
322 | 280 |
323 return false; | 281 return false; |
324 } | 282 } |
325 | 283 |
326 void SpellCheckProvider::OnDestruct() { | 284 void SpellCheckProvider::OnDestruct() { |
327 delete this; | 285 delete this; |
328 } | 286 } |
OLD | NEW |