OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/test_runner/spell_check_client.h" | 5 #include "components/test_runner/spell_check_client.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "components/test_runner/mock_grammar_check.h" | 13 #include "components/test_runner/mock_grammar_check.h" |
14 #include "components/test_runner/test_runner.h" | 14 #include "components/test_runner/test_runner.h" |
15 #include "components/test_runner/web_test_delegate.h" | 15 #include "components/test_runner/web_test_delegate.h" |
| 16 #include "third_party/WebKit/public/web/WebFrame.h" |
| 17 #include "third_party/WebKit/public/web/WebKit.h" |
16 #include "third_party/WebKit/public/web/WebTextCheckingCompletion.h" | 18 #include "third_party/WebKit/public/web/WebTextCheckingCompletion.h" |
17 #include "third_party/WebKit/public/web/WebTextCheckingResult.h" | 19 #include "third_party/WebKit/public/web/WebTextCheckingResult.h" |
18 | 20 |
19 namespace test_runner { | 21 namespace test_runner { |
20 | 22 |
21 SpellCheckClient::SpellCheckClient(TestRunner* test_runner) | 23 SpellCheckClient::SpellCheckClient(TestRunner* test_runner) |
22 : last_requested_text_checking_completion_(nullptr), | 24 : last_requested_text_checking_completion_(nullptr), |
23 test_runner_(test_runner), | 25 test_runner_(test_runner), |
24 weak_factory_(this) { | 26 weak_factory_(this) { |
25 DCHECK(test_runner); | 27 DCHECK(test_runner); |
26 } | 28 } |
27 | 29 |
28 SpellCheckClient::~SpellCheckClient() { | 30 SpellCheckClient::~SpellCheckClient() { |
29 } | 31 } |
30 | 32 |
31 void SpellCheckClient::SetDelegate(WebTestDelegate* delegate) { | 33 void SpellCheckClient::SetDelegate(WebTestDelegate* delegate) { |
32 delegate_ = delegate; | 34 delegate_ = delegate; |
33 } | 35 } |
34 | 36 |
35 void SpellCheckClient::SetEnabled(bool enabled) { | 37 void SpellCheckClient::SetEnabled(bool enabled) { |
36 enabled_ = enabled; | 38 enabled_ = enabled; |
37 } | 39 } |
38 | 40 |
| 41 void SpellCheckClient::Reset() { |
| 42 enabled_ = false; |
| 43 resolved_callback_.Reset(); |
| 44 } |
| 45 |
39 // blink::WebSpellCheckClient | 46 // blink::WebSpellCheckClient |
40 void SpellCheckClient::spellCheck( | 47 void SpellCheckClient::spellCheck( |
41 const blink::WebString& text, | 48 const blink::WebString& text, |
42 int& misspelled_offset, | 49 int& misspelled_offset, |
43 int& misspelled_length, | 50 int& misspelled_length, |
44 blink::WebVector<blink::WebString>* optional_suggestions) { | 51 blink::WebVector<blink::WebString>* optional_suggestions) { |
45 if (!enabled_) { | 52 if (!enabled_) { |
46 misspelled_offset = 0; | 53 misspelled_offset = 0; |
47 misspelled_length = 0; | 54 misspelled_length = 0; |
48 return; | 55 return; |
49 } | 56 } |
50 | 57 |
51 // Check the spelling of the given text. | 58 // Check the spelling of the given text. |
52 spell_check_.SpellCheckWord(text, &misspelled_offset, &misspelled_length); | 59 spell_check_.SpellCheckWord(text, &misspelled_offset, &misspelled_length); |
53 } | 60 } |
54 | 61 |
55 void SpellCheckClient::requestCheckingOfText( | 62 void SpellCheckClient::requestCheckingOfText( |
56 const blink::WebString& text, | 63 const blink::WebString& text, |
57 const blink::WebVector<uint32_t>& markers, | 64 const blink::WebVector<uint32_t>& markers, |
58 const blink::WebVector<unsigned>& marker_offsets, | 65 const blink::WebVector<unsigned>& marker_offsets, |
59 blink::WebTextCheckingCompletion* completion) { | 66 blink::WebTextCheckingCompletion* completion) { |
60 if (!enabled_ || text.isEmpty()) { | 67 if (!enabled_ || text.isEmpty()) { |
61 if (completion) | 68 if (completion) { |
62 completion->didCancelCheckingText(); | 69 completion->didCancelCheckingText(); |
| 70 RequestResolved(); |
| 71 } |
63 return; | 72 return; |
64 } | 73 } |
65 | 74 |
66 if (last_requested_text_checking_completion_) | 75 if (last_requested_text_checking_completion_) { |
67 last_requested_text_checking_completion_->didCancelCheckingText(); | 76 last_requested_text_checking_completion_->didCancelCheckingText(); |
| 77 RequestResolved(); |
| 78 } |
68 | 79 |
69 last_requested_text_checking_completion_ = completion; | 80 last_requested_text_checking_completion_ = completion; |
70 last_requested_text_check_string_ = text; | 81 last_requested_text_check_string_ = text; |
71 if (spell_check_.HasInCache(text)) | 82 if (spell_check_.HasInCache(text)) |
72 FinishLastTextCheck(); | 83 FinishLastTextCheck(); |
73 else | 84 else |
74 delegate_->PostDelayedTask( | 85 delegate_->PostDelayedTask( |
75 base::Bind(&SpellCheckClient::FinishLastTextCheck, | 86 base::Bind(&SpellCheckClient::FinishLastTextCheck, |
76 weak_factory_.GetWeakPtr()), | 87 weak_factory_.GetWeakPtr()), |
77 0); | 88 0); |
(...skipping 30 matching lines...) Expand all Loading... |
108 misspelled_length, | 119 misspelled_length, |
109 suggestions.isEmpty() ? blink::WebString() : suggestions[0])); | 120 suggestions.isEmpty() ? blink::WebString() : suggestions[0])); |
110 text = text.substr(misspelled_position + misspelled_length); | 121 text = text.substr(misspelled_position + misspelled_length); |
111 offset += misspelled_position + misspelled_length; | 122 offset += misspelled_position + misspelled_length; |
112 } | 123 } |
113 MockGrammarCheck::CheckGrammarOfString(last_requested_text_check_string_, | 124 MockGrammarCheck::CheckGrammarOfString(last_requested_text_check_string_, |
114 &results); | 125 &results); |
115 } | 126 } |
116 last_requested_text_checking_completion_->didFinishCheckingText(results); | 127 last_requested_text_checking_completion_->didFinishCheckingText(results); |
117 last_requested_text_checking_completion_ = 0; | 128 last_requested_text_checking_completion_ = 0; |
| 129 RequestResolved(); |
118 | 130 |
119 if (test_runner_->shouldDumpSpellCheckCallbacks()) | 131 if (test_runner_->shouldDumpSpellCheckCallbacks()) |
120 delegate_->PrintMessage("SpellCheckEvent: FinishLastTextCheck\n"); | 132 delegate_->PrintMessage("SpellCheckEvent: FinishLastTextCheck\n"); |
121 } | 133 } |
122 | 134 |
| 135 void SpellCheckClient::SetSpellCheckResolvedCallback( |
| 136 v8::Local<v8::Function> callback) { |
| 137 resolved_callback_.Reset(blink::mainThreadIsolate(), callback); |
| 138 } |
| 139 |
| 140 void SpellCheckClient::RemoveSpellCheckResolvedCallback() { |
| 141 resolved_callback_.Reset(); |
| 142 } |
| 143 |
| 144 void SpellCheckClient::RequestResolved() { |
| 145 if (resolved_callback_.IsEmpty()) |
| 146 return; |
| 147 |
| 148 v8::Isolate* isolate = blink::mainThreadIsolate(); |
| 149 v8::HandleScope handle_scope(isolate); |
| 150 |
| 151 blink::WebFrame* frame = test_runner_->mainFrame(); |
| 152 if (!frame || frame->isWebRemoteFrame()) |
| 153 return; |
| 154 |
| 155 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); |
| 156 if (context.IsEmpty()) |
| 157 return; |
| 158 |
| 159 v8::Context::Scope context_scope(context); |
| 160 |
| 161 frame->callFunctionEvenIfScriptDisabled( |
| 162 v8::Local<v8::Function>::New(isolate, resolved_callback_), |
| 163 context->Global(), 0, nullptr); |
| 164 } |
| 165 |
123 } // namespace test_runner | 166 } // namespace test_runner |
OLD | NEW |