Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / shell / renderer / test_runner / spell_check_client.cc
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.
4
5 #include "content/shell/renderer/test_runner/spell_check_client.h"
6
7 #include "content/shell/renderer/test_runner/mock_grammar_check.h"
8 #include "content/shell/renderer/test_runner/web_test_delegate.h"
9 #include "content/shell/renderer/test_runner/web_test_proxy.h"
10 #include "third_party/WebKit/public/web/WebTextCheckingCompletion.h"
11 #include "third_party/WebKit/public/web/WebTextCheckingResult.h"
12
13 namespace content {
14
15 namespace {
16
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) {}
22
23   ~HostMethodTask() override {}
24
25   void RunIfValid() override { (object_->*callback_)(); }
26
27  private:
28   CallbackMethodType callback_;
29
30   DISALLOW_COPY_AND_ASSIGN(HostMethodTask);
31 };
32
33 }  // namespace
34
35 SpellCheckClient::SpellCheckClient(WebTestProxyBase* web_test_proxy)
36     : last_requested_text_checking_completion_(0),
37       web_test_proxy_(web_test_proxy) {
38 }
39
40 SpellCheckClient::~SpellCheckClient() {
41 }
42
43 void SpellCheckClient::SetDelegate(WebTestDelegate* delegate) {
44   delegate_ = delegate;
45 }
46
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);
55 }
56
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;
78     }
79   }
80   if (mask & blink::WebTextCheckingTypeGrammar)
81     MockGrammarCheck::CheckGrammarOfString(text, &results);
82   web_results->assign(results);
83 }
84
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;
94   }
95
96   if (last_requested_text_checking_completion_)
97     last_requested_text_checking_completion_->didCancelCheckingText();
98
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);
106 }
107
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;
133     }
134     MockGrammarCheck::CheckGrammarOfString(last_requested_text_check_string_,
135                                            &results);
136   }
137   last_requested_text_checking_completion_->didFinishCheckingText(results);
138   last_requested_text_checking_completion_ = 0;
139
140   web_test_proxy_->PostSpellCheckEvent(blink::WebString("FinishLastTextCheck"));
141 }
142
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();
150 }
151
152 }  // namespace content