summaryrefslogtreecommitdiffstats
path: root/components/test_runner/spell_check_client.cc
blob: 9ef7caf356ad6bdfe8cff11ce1d42ffdcc7ba74c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/test_runner/spell_check_client.h"

#include "components/test_runner/mock_grammar_check.h"
#include "components/test_runner/web_test_delegate.h"
#include "components/test_runner/web_test_proxy.h"
#include "third_party/WebKit/public/web/WebTextCheckingCompletion.h"
#include "third_party/WebKit/public/web/WebTextCheckingResult.h"

namespace test_runner {

namespace {

class HostMethodTask : public WebMethodTask<SpellCheckClient> {
 public:
  typedef void (SpellCheckClient::*CallbackMethodType)();
  HostMethodTask(SpellCheckClient* object, CallbackMethodType callback)
      : WebMethodTask<SpellCheckClient>(object), callback_(callback) {}

  ~HostMethodTask() override {}

  void RunIfValid() override { (object_->*callback_)(); }

 private:
  CallbackMethodType callback_;

  DISALLOW_COPY_AND_ASSIGN(HostMethodTask);
};

}  // namespace

SpellCheckClient::SpellCheckClient(WebTestProxyBase* web_test_proxy)
    : last_requested_text_checking_completion_(0),
      web_test_proxy_(web_test_proxy) {
}

SpellCheckClient::~SpellCheckClient() {
}

void SpellCheckClient::SetDelegate(WebTestDelegate* delegate) {
  delegate_ = delegate;
}

// blink::WebSpellCheckClient
void SpellCheckClient::spellCheck(
    const blink::WebString& text,
    int& misspelled_offset,
    int& misspelled_length,
    blink::WebVector<blink::WebString>* optional_suggestions) {
  // Check the spelling of the given text.
  spell_check_.SpellCheckWord(text, &misspelled_offset, &misspelled_length);
}

void SpellCheckClient::checkTextOfParagraph(
    const blink::WebString& text,
    blink::WebTextCheckingTypeMask mask,
    blink::WebVector<blink::WebTextCheckingResult>* web_results) {
  std::vector<blink::WebTextCheckingResult> results;
  if (mask & blink::WebTextCheckingTypeSpelling) {
    size_t offset = 0;
    base::string16 data = text;
    while (offset < data.length()) {
      int misspelled_position = 0;
      int misspelled_length = 0;
      spell_check_.SpellCheckWord(
          data.substr(offset), &misspelled_position, &misspelled_length);
      if (!misspelled_length)
        break;
      blink::WebTextCheckingResult result;
      result.decoration = blink::WebTextDecorationTypeSpelling;
      result.location = offset + misspelled_position;
      result.length = misspelled_length;
      results.push_back(result);
      offset += misspelled_position + misspelled_length;
    }
  }
  if (mask & blink::WebTextCheckingTypeGrammar)
    MockGrammarCheck::CheckGrammarOfString(text, &results);
  web_results->assign(results);
}

void SpellCheckClient::requestCheckingOfText(
    const blink::WebString& text,
    const blink::WebVector<uint32_t>& markers,
    const blink::WebVector<unsigned>& marker_offsets,
    blink::WebTextCheckingCompletion* completion) {
  if (text.isEmpty()) {
    if (completion)
      completion->didCancelCheckingText();
    return;
  }

  if (last_requested_text_checking_completion_)
    last_requested_text_checking_completion_->didCancelCheckingText();

  last_requested_text_checking_completion_ = completion;
  last_requested_text_check_string_ = text;
  if (spell_check_.HasInCache(text))
    FinishLastTextCheck();
  else
    delegate_->PostDelayedTask(
        new HostMethodTask(this, &SpellCheckClient::FinishLastTextCheck), 0);
}

void SpellCheckClient::FinishLastTextCheck() {
  if (!last_requested_text_checking_completion_)
    return;
  std::vector<blink::WebTextCheckingResult> results;
  int offset = 0;
  base::string16 text = last_requested_text_check_string_;
  if (!spell_check_.IsMultiWordMisspelling(blink::WebString(text), &results)) {
    while (text.length()) {
      int misspelled_position = 0;
      int misspelled_length = 0;
      spell_check_.SpellCheckWord(
          blink::WebString(text), &misspelled_position, &misspelled_length);
      if (!misspelled_length)
        break;
      blink::WebVector<blink::WebString> suggestions;
      spell_check_.FillSuggestionList(
          blink::WebString(text.substr(misspelled_position, misspelled_length)),
          &suggestions);
      results.push_back(blink::WebTextCheckingResult(
          blink::WebTextDecorationTypeSpelling,
          offset + misspelled_position,
          misspelled_length,
          suggestions.isEmpty() ? blink::WebString() : suggestions[0]));
      text = text.substr(misspelled_position + misspelled_length);
      offset += misspelled_position + misspelled_length;
    }
    MockGrammarCheck::CheckGrammarOfString(last_requested_text_check_string_,
                                           &results);
  }
  last_requested_text_checking_completion_->didFinishCheckingText(results);
  last_requested_text_checking_completion_ = 0;

  web_test_proxy_->PostSpellCheckEvent(blink::WebString("FinishLastTextCheck"));
}

}  // namespace test_runner