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/metrics/histogram_macros.h" | 7 #include "base/metrics/histogram_macros.h" |
8 #include "components/spellcheck/common/spellcheck_messages.h" | 8 #include "components/spellcheck/common/spellcheck_messages.h" |
9 #include "components/spellcheck/common/spellcheck_result.h" | 9 #include "components/spellcheck/common/spellcheck_result.h" |
10 #include "components/spellcheck/renderer/spellcheck.h" | 10 #include "components/spellcheck/renderer/spellcheck.h" |
11 #include "components/spellcheck/renderer/spellcheck_language.h" | 11 #include "components/spellcheck/renderer/spellcheck_language.h" |
12 #include "components/spellcheck/spellcheck_build_features.h" | 12 #include "components/spellcheck/spellcheck_build_features.h" |
13 #include "content/public/renderer/render_frame.h" | 13 #include "content/public/renderer/render_frame.h" |
14 #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" |
18 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 17 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
19 #include "third_party/WebKit/public/web/WebTextCheckingCompletion.h" | 18 #include "third_party/WebKit/public/web/WebTextCheckingCompletion.h" |
20 #include "third_party/WebKit/public/web/WebTextCheckingResult.h" | 19 #include "third_party/WebKit/public/web/WebTextCheckingResult.h" |
21 #include "third_party/WebKit/public/web/WebTextDecorationType.h" | 20 #include "third_party/WebKit/public/web/WebTextDecorationType.h" |
22 #include "third_party/WebKit/public/web/WebView.h" | |
23 | 21 |
24 using blink::WebElement; | 22 using blink::WebElement; |
25 using blink::WebLocalFrame; | 23 using blink::WebLocalFrame; |
26 using blink::WebString; | 24 using blink::WebString; |
27 using blink::WebTextCheckingCompletion; | 25 using blink::WebTextCheckingCompletion; |
28 using blink::WebTextCheckingResult; | 26 using blink::WebTextCheckingResult; |
29 using blink::WebTextDecorationType; | 27 using blink::WebTextDecorationType; |
30 using blink::WebVector; | 28 using blink::WebVector; |
31 | 29 |
32 static_assert(int(blink::kWebTextDecorationTypeSpelling) == | 30 static_assert(int(blink::kWebTextDecorationTypeSpelling) == |
33 int(SpellCheckResult::SPELLING), | 31 int(SpellCheckResult::SPELLING), |
34 "mismatching enums"); | 32 "mismatching enums"); |
35 static_assert(int(blink::kWebTextDecorationTypeGrammar) == | 33 static_assert(int(blink::kWebTextDecorationTypeGrammar) == |
36 int(SpellCheckResult::GRAMMAR), | 34 int(SpellCheckResult::GRAMMAR), |
37 "mismatching enums"); | 35 "mismatching enums"); |
38 | 36 |
39 SpellCheckProvider::SpellCheckProvider( | 37 SpellCheckProvider::SpellCheckProvider(content::RenderFrame* render_frame, |
40 content::RenderView* render_view, | 38 SpellCheck* spellcheck) |
41 SpellCheck* spellcheck) | 39 : content::RenderFrameObserver(render_frame), |
42 : content::RenderViewObserver(render_view), | 40 content::RenderFrameObserverTracker<SpellCheckProvider>(render_frame), |
43 content::RenderViewObserverTracker<SpellCheckProvider>(render_view), | |
44 spellcheck_(spellcheck) { | 41 spellcheck_(spellcheck) { |
45 DCHECK(spellcheck_); | 42 DCHECK(spellcheck_); |
46 if (render_view) { // NULL in unit tests. | 43 if (render_frame) // NULL in unit tests. |
47 EnableSpellcheck(spellcheck_->IsSpellcheckEnabled()); | 44 render_frame->GetWebFrame()->SetTextCheckClient(this); |
48 } | |
49 } | 45 } |
50 | 46 |
51 SpellCheckProvider::~SpellCheckProvider() { | 47 SpellCheckProvider::~SpellCheckProvider() { |
52 } | 48 } |
53 | 49 |
54 void SpellCheckProvider::RequestTextChecking( | 50 void SpellCheckProvider::RequestTextChecking( |
55 const base::string16& text, | 51 const base::string16& text, |
56 WebTextCheckingCompletion* completion) { | 52 WebTextCheckingCompletion* completion) { |
57 // Ignore invalid requests. | 53 // Ignore invalid requests. |
58 if (text.empty() || !HasWordCharacters(text, 0)) { | 54 if (text.empty() || !HasWordCharacters(text, 0)) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 #if BUILDFLAG(USE_BROWSER_SPELLCHECKER) | 89 #if BUILDFLAG(USE_BROWSER_SPELLCHECKER) |
94 IPC_MESSAGE_HANDLER(SpellCheckMsg_RespondTextCheck, OnRespondTextCheck) | 90 IPC_MESSAGE_HANDLER(SpellCheckMsg_RespondTextCheck, OnRespondTextCheck) |
95 #endif | 91 #endif |
96 IPC_MESSAGE_UNHANDLED(handled = false) | 92 IPC_MESSAGE_UNHANDLED(handled = false) |
97 IPC_END_MESSAGE_MAP() | 93 IPC_END_MESSAGE_MAP() |
98 return handled; | 94 return handled; |
99 } | 95 } |
100 | 96 |
101 void SpellCheckProvider::FocusedNodeChanged(const blink::WebNode& unused) { | 97 void SpellCheckProvider::FocusedNodeChanged(const blink::WebNode& unused) { |
102 #if BUILDFLAG(USE_BROWSER_SPELLCHECKER) | 98 #if BUILDFLAG(USE_BROWSER_SPELLCHECKER) |
103 WebLocalFrame* frame = render_view()->GetWebView()->FocusedFrame(); | 99 WebLocalFrame* frame = render_frame()->GetWebFrame(); |
104 WebElement element = frame->GetDocument().IsNull() | 100 WebElement element = frame->GetDocument().IsNull() |
105 ? WebElement() | 101 ? WebElement() |
106 : frame->GetDocument().FocusedElement(); | 102 : frame->GetDocument().FocusedElement(); |
107 bool enabled = !element.IsNull() && element.IsEditable(); | 103 bool enabled = !element.IsNull() && element.IsEditable(); |
108 bool checked = enabled && frame->IsSpellCheckingEnabled(); | 104 bool checked = enabled && frame->IsSpellCheckingEnabled(); |
109 | 105 |
110 Send(new SpellCheckHostMsg_ToggleSpellCheck(routing_id(), enabled, checked)); | 106 Send(new SpellCheckHostMsg_ToggleSpellCheck(routing_id(), enabled, checked)); |
111 #endif // USE_BROWSER_SPELLCHECKER | 107 #endif // USE_BROWSER_SPELLCHECKER |
112 } | 108 } |
113 | 109 |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 &textcheck_results); | 217 &textcheck_results); |
222 completion->DidFinishCheckingText(textcheck_results); | 218 completion->DidFinishCheckingText(textcheck_results); |
223 | 219 |
224 // Cache the request and the converted results. | 220 // Cache the request and the converted results. |
225 last_request_ = line; | 221 last_request_ = line; |
226 last_results_.Swap(textcheck_results); | 222 last_results_.Swap(textcheck_results); |
227 } | 223 } |
228 #endif | 224 #endif |
229 | 225 |
230 void SpellCheckProvider::EnableSpellcheck(bool enable) { | 226 void SpellCheckProvider::EnableSpellcheck(bool enable) { |
231 if (!render_view()->GetWebView()) | 227 WebLocalFrame* frame = render_frame()->GetWebFrame(); |
232 return; | |
233 | |
234 WebLocalFrame* frame = render_view()->GetWebView()->FocusedFrame(); | |
235 // TODO(yabinh): The null check should be unnecessary. | |
236 // See crbug.com/625068 | |
237 if (!frame) | |
238 return; | |
239 | |
240 frame->EnableSpellChecking(enable); | 228 frame->EnableSpellChecking(enable); |
241 if (!enable) | 229 if (!enable) |
242 frame->RemoveSpellingMarkers(); | 230 frame->RemoveSpellingMarkers(); |
243 } | 231 } |
244 | 232 |
245 bool SpellCheckProvider::SatisfyRequestFromCache( | 233 bool SpellCheckProvider::SatisfyRequestFromCache( |
246 const base::string16& text, | 234 const base::string16& text, |
247 WebTextCheckingCompletion* completion) { | 235 WebTextCheckingCompletion* completion) { |
248 size_t last_length = last_request_.length(); | 236 size_t last_length = last_request_.length(); |
249 if (!last_length) | 237 if (!last_length) |
(...skipping 30 matching lines...) Expand all Loading... |
280 completion->DidFinishCheckingText(results); | 268 completion->DidFinishCheckingText(results); |
281 return true; | 269 return true; |
282 } | 270 } |
283 | 271 |
284 return false; | 272 return false; |
285 } | 273 } |
286 | 274 |
287 void SpellCheckProvider::OnDestruct() { | 275 void SpellCheckProvider::OnDestruct() { |
288 delete this; | 276 delete this; |
289 } | 277 } |
OLD | NEW |