Update mojo sdk to rev c02a28868825edfa57ab77947b8cb15e741c5598
[chromium-blink-merge.git] / components / test_runner / spell_check_client.cc
blob3798426d35af2af2d50fe8f2198d35ae4c040e78
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
3 // found in the LICENSE file.
5 #include "components/test_runner/spell_check_client.h"
7 #include "components/test_runner/mock_grammar_check.h"
8 #include "components/test_runner/web_test_delegate.h"
9 #include "components/test_runner/web_test_proxy.h"
10 #include "third_party/WebKit/public/web/WebTextCheckingCompletion.h"
11 #include "third_party/WebKit/public/web/WebTextCheckingResult.h"
13 namespace test_runner {
15 namespace {
17 class HostMethodTask : public WebMethodTask<SpellCheckClient> {
18 public:
19 typedef void (SpellCheckClient::*CallbackMethodType)();
20 HostMethodTask(SpellCheckClient* object, CallbackMethodType callback)
21 : WebMethodTask<SpellCheckClient>(object), callback_(callback) {}
23 ~HostMethodTask() override {}
25 void RunIfValid() override { (object_->*callback_)(); }
27 private:
28 CallbackMethodType callback_;
30 DISALLOW_COPY_AND_ASSIGN(HostMethodTask);
33 } // namespace
35 SpellCheckClient::SpellCheckClient(WebTestProxyBase* web_test_proxy)
36 : last_requested_text_checking_completion_(0),
37 web_test_proxy_(web_test_proxy) {
40 SpellCheckClient::~SpellCheckClient() {
43 void SpellCheckClient::SetDelegate(WebTestDelegate* delegate) {
44 delegate_ = delegate;
47 // blink::WebSpellCheckClient
48 void SpellCheckClient::spellCheck(
49 const blink::WebString& text,
50 int& misspelled_offset,
51 int& misspelled_length,
52 blink::WebVector<blink::WebString>* optional_suggestions) {
53 // Check the spelling of the given text.
54 spell_check_.SpellCheckWord(text, &misspelled_offset, &misspelled_length);
57 void SpellCheckClient::checkTextOfParagraph(
58 const blink::WebString& text,
59 blink::WebTextCheckingTypeMask mask,
60 blink::WebVector<blink::WebTextCheckingResult>* web_results) {
61 std::vector<blink::WebTextCheckingResult> results;
62 if (mask & blink::WebTextCheckingTypeSpelling) {
63 size_t offset = 0;
64 base::string16 data = text;
65 while (offset < data.length()) {
66 int misspelled_position = 0;
67 int misspelled_length = 0;
68 spell_check_.SpellCheckWord(
69 data.substr(offset), &misspelled_position, &misspelled_length);
70 if (!misspelled_length)
71 break;
72 blink::WebTextCheckingResult result;
73 result.decoration = blink::WebTextDecorationTypeSpelling;
74 result.location = offset + misspelled_position;
75 result.length = misspelled_length;
76 results.push_back(result);
77 offset += misspelled_position + misspelled_length;
80 if (mask & blink::WebTextCheckingTypeGrammar)
81 MockGrammarCheck::CheckGrammarOfString(text, &results);
82 web_results->assign(results);
85 void SpellCheckClient::requestCheckingOfText(
86 const blink::WebString& text,
87 const blink::WebVector<uint32_t>& markers,
88 const blink::WebVector<unsigned>& marker_offsets,
89 blink::WebTextCheckingCompletion* completion) {
90 if (text.isEmpty()) {
91 if (completion)
92 completion->didCancelCheckingText();
93 return;
96 if (last_requested_text_checking_completion_)
97 last_requested_text_checking_completion_->didCancelCheckingText();
99 last_requested_text_checking_completion_ = completion;
100 last_requested_text_check_string_ = text;
101 if (spell_check_.HasInCache(text))
102 FinishLastTextCheck();
103 else
104 delegate_->PostDelayedTask(
105 new HostMethodTask(this, &SpellCheckClient::FinishLastTextCheck), 0);
108 void SpellCheckClient::FinishLastTextCheck() {
109 if (!last_requested_text_checking_completion_)
110 return;
111 std::vector<blink::WebTextCheckingResult> results;
112 int offset = 0;
113 base::string16 text = last_requested_text_check_string_;
114 if (!spell_check_.IsMultiWordMisspelling(blink::WebString(text), &results)) {
115 while (text.length()) {
116 int misspelled_position = 0;
117 int misspelled_length = 0;
118 spell_check_.SpellCheckWord(
119 blink::WebString(text), &misspelled_position, &misspelled_length);
120 if (!misspelled_length)
121 break;
122 blink::WebVector<blink::WebString> suggestions;
123 spell_check_.FillSuggestionList(
124 blink::WebString(text.substr(misspelled_position, misspelled_length)),
125 &suggestions);
126 results.push_back(blink::WebTextCheckingResult(
127 blink::WebTextDecorationTypeSpelling,
128 offset + misspelled_position,
129 misspelled_length,
130 suggestions.isEmpty() ? blink::WebString() : suggestions[0]));
131 text = text.substr(misspelled_position + misspelled_length);
132 offset += misspelled_position + misspelled_length;
134 MockGrammarCheck::CheckGrammarOfString(last_requested_text_check_string_,
135 &results);
137 last_requested_text_checking_completion_->didFinishCheckingText(results);
138 last_requested_text_checking_completion_ = 0;
140 web_test_proxy_->PostSpellCheckEvent(blink::WebString("FinishLastTextCheck"));
143 blink::WebString SpellCheckClient::autoCorrectWord(
144 const blink::WebString& word) {
145 // Returns an empty string as Mac WebKit ('WebKitSupport/WebEditorClient.mm')
146 // does. (If this function returns a non-empty string, WebKit replaces the
147 // given misspelled string with the result one. This process executes some
148 // editor commands and causes layout-test failures.)
149 return blink::WebString();
152 } // namespace test_runner