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::WebTextDecorationTypeSpelling) == | 30 static_assert(int(blink::WebTextDecorationTypeSpelling) == |
33 int(SpellCheckResult::SPELLING), "mismatching enums"); | 31 int(SpellCheckResult::SPELLING), "mismatching enums"); |
34 static_assert(int(blink::WebTextDecorationTypeGrammar) == | 32 static_assert(int(blink::WebTextDecorationTypeGrammar) == |
35 int(SpellCheckResult::GRAMMAR), "mismatching enums"); | 33 int(SpellCheckResult::GRAMMAR), "mismatching enums"); |
36 | 34 |
37 SpellCheckProvider::SpellCheckProvider( | 35 SpellCheckProvider::SpellCheckProvider(content::RenderFrame* render_frame, |
38 content::RenderView* render_view, | 36 SpellCheck* spellcheck) |
39 SpellCheck* spellcheck) | 37 : content::RenderFrameObserver(render_frame), |
40 : content::RenderViewObserver(render_view), | 38 content::RenderFrameObserverTracker<SpellCheckProvider>(render_frame), |
41 content::RenderViewObserverTracker<SpellCheckProvider>(render_view), | |
42 spellcheck_(spellcheck) { | 39 spellcheck_(spellcheck) { |
43 DCHECK(spellcheck_); | 40 DCHECK(spellcheck_); |
44 if (render_view) { // NULL in unit tests. | 41 if (render_frame) { // NULL in unit tests. |
42 render_frame->GetWebFrame()->setTextCheckClient(this); | |
43 // TODO(xiaochengh): It is possible that the LocalFrame has not been set up, | |
44 // even though the WebLocalFrame must have been set up. Investigate why. | |
45 EnableSpellcheck(spellcheck_->IsSpellcheckEnabled()); | 45 EnableSpellcheck(spellcheck_->IsSpellcheckEnabled()); |
Xiaocheng
2017/04/07 01:43:45
Crashes here, because EnableSpellcheck() visits bl
| |
46 } | 46 } |
47 } | 47 } |
48 | 48 |
49 SpellCheckProvider::~SpellCheckProvider() { | 49 SpellCheckProvider::~SpellCheckProvider() { |
50 } | 50 } |
51 | 51 |
52 void SpellCheckProvider::RequestTextChecking( | 52 void SpellCheckProvider::RequestTextChecking( |
53 const base::string16& text, | 53 const base::string16& text, |
54 WebTextCheckingCompletion* completion) { | 54 WebTextCheckingCompletion* completion) { |
55 // Ignore invalid requests. | 55 // Ignore invalid requests. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
91 #if BUILDFLAG(USE_BROWSER_SPELLCHECKER) | 91 #if BUILDFLAG(USE_BROWSER_SPELLCHECKER) |
92 IPC_MESSAGE_HANDLER(SpellCheckMsg_RespondTextCheck, OnRespondTextCheck) | 92 IPC_MESSAGE_HANDLER(SpellCheckMsg_RespondTextCheck, OnRespondTextCheck) |
93 #endif | 93 #endif |
94 IPC_MESSAGE_UNHANDLED(handled = false) | 94 IPC_MESSAGE_UNHANDLED(handled = false) |
95 IPC_END_MESSAGE_MAP() | 95 IPC_END_MESSAGE_MAP() |
96 return handled; | 96 return handled; |
97 } | 97 } |
98 | 98 |
99 void SpellCheckProvider::FocusedNodeChanged(const blink::WebNode& unused) { | 99 void SpellCheckProvider::FocusedNodeChanged(const blink::WebNode& unused) { |
100 #if BUILDFLAG(USE_BROWSER_SPELLCHECKER) | 100 #if BUILDFLAG(USE_BROWSER_SPELLCHECKER) |
101 WebLocalFrame* frame = render_view()->GetWebView()->focusedFrame(); | 101 WebLocalFrame* frame = render_frame()->GetWebFrame(); |
102 WebElement element = frame->document().isNull() ? WebElement() : | 102 WebElement element = frame->document().isNull() ? WebElement() : |
103 frame->document().focusedElement(); | 103 frame->document().focusedElement(); |
104 bool enabled = !element.isNull() && element.isEditable(); | 104 bool enabled = !element.isNull() && element.isEditable(); |
105 bool checked = enabled && frame->isSpellCheckingEnabled(); | 105 bool checked = enabled && frame->isSpellCheckingEnabled(); |
106 | 106 |
107 Send(new SpellCheckHostMsg_ToggleSpellCheck(routing_id(), enabled, checked)); | 107 Send(new SpellCheckHostMsg_ToggleSpellCheck(routing_id(), enabled, checked)); |
108 #endif // USE_BROWSER_SPELLCHECKER | 108 #endif // USE_BROWSER_SPELLCHECKER |
109 } | 109 } |
110 | 110 |
111 void SpellCheckProvider::checkSpelling( | 111 void SpellCheckProvider::checkSpelling( |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
218 &textcheck_results); | 218 &textcheck_results); |
219 completion->didFinishCheckingText(textcheck_results); | 219 completion->didFinishCheckingText(textcheck_results); |
220 | 220 |
221 // Cache the request and the converted results. | 221 // Cache the request and the converted results. |
222 last_request_ = line; | 222 last_request_ = line; |
223 last_results_.swap(textcheck_results); | 223 last_results_.swap(textcheck_results); |
224 } | 224 } |
225 #endif | 225 #endif |
226 | 226 |
227 void SpellCheckProvider::EnableSpellcheck(bool enable) { | 227 void SpellCheckProvider::EnableSpellcheck(bool enable) { |
228 if (!render_view()->GetWebView()) | 228 WebLocalFrame* frame = render_frame()->GetWebFrame(); |
229 return; | |
230 | |
231 WebLocalFrame* frame = render_view()->GetWebView()->focusedFrame(); | |
232 // TODO(yabinh): The null check should be unnecessary. | |
233 // See crbug.com/625068 | |
234 if (!frame) | |
235 return; | |
236 | |
237 frame->enableSpellChecking(enable); | 229 frame->enableSpellChecking(enable); |
238 if (!enable) | 230 if (!enable) |
239 frame->removeSpellingMarkers(); | 231 frame->removeSpellingMarkers(); |
240 } | 232 } |
241 | 233 |
242 bool SpellCheckProvider::SatisfyRequestFromCache( | 234 bool SpellCheckProvider::SatisfyRequestFromCache( |
243 const base::string16& text, | 235 const base::string16& text, |
244 WebTextCheckingCompletion* completion) { | 236 WebTextCheckingCompletion* completion) { |
245 size_t last_length = last_request_.length(); | 237 size_t last_length = last_request_.length(); |
246 if (!last_length) | 238 if (!last_length) |
(...skipping 30 matching lines...) Expand all Loading... | |
277 completion->didFinishCheckingText(results); | 269 completion->didFinishCheckingText(results); |
278 return true; | 270 return true; |
279 } | 271 } |
280 | 272 |
281 return false; | 273 return false; |
282 } | 274 } |
283 | 275 |
284 void SpellCheckProvider::OnDestruct() { | 276 void SpellCheckProvider::OnDestruct() { |
285 delete this; | 277 delete this; |
286 } | 278 } |
OLD | NEW |