Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(976)

Side by Side Diff: components/test_runner/spell_check_client.cc

Issue 2587823003: Allow test runner to run a callback when finishing a spellcheck request (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
20 #include "third_party/WebKit/public/web/WebView.h"
18 21
19 namespace test_runner { 22 namespace test_runner {
20 23
21 SpellCheckClient::SpellCheckClient(TestRunner* test_runner) 24 SpellCheckClient::SpellCheckClient(TestRunner* test_runner)
22 : last_requested_text_checking_completion_(nullptr), 25 : last_requested_text_checking_completion_(nullptr),
23 test_runner_(test_runner), 26 test_runner_(test_runner),
24 weak_factory_(this) { 27 weak_factory_(this) {
25 DCHECK(test_runner); 28 DCHECK(test_runner);
26 } 29 }
27 30
28 SpellCheckClient::~SpellCheckClient() { 31 SpellCheckClient::~SpellCheckClient() {
29 } 32 }
30 33
31 void SpellCheckClient::SetDelegate(WebTestDelegate* delegate) { 34 void SpellCheckClient::SetDelegate(WebTestDelegate* delegate) {
32 delegate_ = delegate; 35 delegate_ = delegate;
33 } 36 }
34 37
35 void SpellCheckClient::SetEnabled(bool enabled) { 38 void SpellCheckClient::SetEnabled(bool enabled) {
36 enabled_ = enabled; 39 enabled_ = enabled;
37 } 40 }
38 41
42 void SpellCheckClient::Reset() {
43 enabled_ = false;
44 resolved_callback_.Reset();
45 }
46
39 // blink::WebSpellCheckClient 47 // blink::WebSpellCheckClient
40 void SpellCheckClient::spellCheck( 48 void SpellCheckClient::spellCheck(
41 const blink::WebString& text, 49 const blink::WebString& text,
42 int& misspelled_offset, 50 int& misspelled_offset,
43 int& misspelled_length, 51 int& misspelled_length,
44 blink::WebVector<blink::WebString>* optional_suggestions) { 52 blink::WebVector<blink::WebString>* optional_suggestions) {
45 if (!enabled_) { 53 if (!enabled_) {
46 misspelled_offset = 0; 54 misspelled_offset = 0;
47 misspelled_length = 0; 55 misspelled_length = 0;
48 return; 56 return;
49 } 57 }
50 58
51 // Check the spelling of the given text. 59 // Check the spelling of the given text.
52 spell_check_.SpellCheckWord(text, &misspelled_offset, &misspelled_length); 60 spell_check_.SpellCheckWord(text, &misspelled_offset, &misspelled_length);
53 } 61 }
54 62
55 void SpellCheckClient::requestCheckingOfText( 63 void SpellCheckClient::requestCheckingOfText(
56 const blink::WebString& text, 64 const blink::WebString& text,
57 const blink::WebVector<uint32_t>& markers, 65 const blink::WebVector<uint32_t>& markers,
58 const blink::WebVector<unsigned>& marker_offsets, 66 const blink::WebVector<unsigned>& marker_offsets,
59 blink::WebTextCheckingCompletion* completion) { 67 blink::WebTextCheckingCompletion* completion) {
60 if (!enabled_ || text.isEmpty()) { 68 if (!enabled_ || text.isEmpty()) {
61 if (completion) 69 if (completion) {
62 completion->didCancelCheckingText(); 70 completion->didCancelCheckingText();
71 RequestResolved();
72 }
63 return; 73 return;
64 } 74 }
65 75
66 if (last_requested_text_checking_completion_) 76 if (last_requested_text_checking_completion_) {
67 last_requested_text_checking_completion_->didCancelCheckingText(); 77 last_requested_text_checking_completion_->didCancelCheckingText();
78 RequestResolved();
79 }
68 80
69 last_requested_text_checking_completion_ = completion; 81 last_requested_text_checking_completion_ = completion;
70 last_requested_text_check_string_ = text; 82 last_requested_text_check_string_ = text;
71 if (spell_check_.HasInCache(text)) 83 if (spell_check_.HasInCache(text))
72 FinishLastTextCheck(); 84 FinishLastTextCheck();
73 else 85 else
74 delegate_->PostDelayedTask( 86 delegate_->PostDelayedTask(
75 base::Bind(&SpellCheckClient::FinishLastTextCheck, 87 base::Bind(&SpellCheckClient::FinishLastTextCheck,
76 weak_factory_.GetWeakPtr()), 88 weak_factory_.GetWeakPtr()),
77 0); 89 0);
(...skipping 30 matching lines...) Expand all
108 misspelled_length, 120 misspelled_length,
109 suggestions.isEmpty() ? blink::WebString() : suggestions[0])); 121 suggestions.isEmpty() ? blink::WebString() : suggestions[0]));
110 text = text.substr(misspelled_position + misspelled_length); 122 text = text.substr(misspelled_position + misspelled_length);
111 offset += misspelled_position + misspelled_length; 123 offset += misspelled_position + misspelled_length;
112 } 124 }
113 MockGrammarCheck::CheckGrammarOfString(last_requested_text_check_string_, 125 MockGrammarCheck::CheckGrammarOfString(last_requested_text_check_string_,
114 &results); 126 &results);
115 } 127 }
116 last_requested_text_checking_completion_->didFinishCheckingText(results); 128 last_requested_text_checking_completion_->didFinishCheckingText(results);
117 last_requested_text_checking_completion_ = 0; 129 last_requested_text_checking_completion_ = 0;
130 RequestResolved();
118 131
119 if (test_runner_->shouldDumpSpellCheckCallbacks()) 132 if (test_runner_->shouldDumpSpellCheckCallbacks())
120 delegate_->PrintMessage("SpellCheckEvent: FinishLastTextCheck\n"); 133 delegate_->PrintMessage("SpellCheckEvent: FinishLastTextCheck\n");
121 } 134 }
122 135
136 void SpellCheckClient::SetSpellCheckResolvedCallback(
137 v8::Local<v8::Function> callback) {
138 resolved_callback_.Reset(blink::mainThreadIsolate(), callback);
139 }
140
141 void SpellCheckClient::RequestResolved() {
142 if (resolved_callback_.IsEmpty())
143 return;
144
145 v8::Isolate* isolate = blink::mainThreadIsolate();
146 v8::HandleScope handle_scope(isolate);
147
148 blink::WebFrame* frame = test_runner_->main_view()->mainFrame();
149 if (!frame || frame->isWebRemoteFrame())
150 return;
151
152 v8::Local<v8::Context> context = frame->mainWorldScriptContext();
153 if (context.IsEmpty())
154 return;
155
156 v8::Context::Scope context_scope(context);
157
158 frame->callFunctionEvenIfScriptDisabled(
haraken 2016/12/20 02:46:21 Is there any reason you want to run scripts when s
Xiaocheng 2016/12/20 03:05:59 No specific reason. I just copied code from access
159 v8::Local<v8::Function>::New(isolate, resolved_callback_),
160 context->Global(), 0, nullptr);
161 }
162
123 } // namespace test_runner 163 } // namespace test_runner
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698