atom_api_spell_check_client.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Copyright (c) 2014 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/renderer/api/atom_api_spell_check_client.h"
  5. #include <algorithm>
  6. #include <vector>
  7. #include "atom/common/native_mate_converters/string16_converter.h"
  8. #include "base/logging.h"
  9. #include "base/threading/thread_task_runner_handle.h"
  10. #include "chrome/renderer/spellchecker/spellcheck_worditerator.h"
  11. #include "native_mate/converter.h"
  12. #include "native_mate/dictionary.h"
  13. #include "third_party/WebKit/public/web/WebTextCheckingCompletion.h"
  14. #include "third_party/WebKit/public/web/WebTextCheckingResult.h"
  15. #include "third_party/icu/source/common/unicode/uscript.h"
  16. namespace atom {
  17. namespace api {
  18. namespace {
  19. bool HasWordCharacters(const base::string16& text, int index) {
  20. const base::char16* data = text.data();
  21. int length = text.length();
  22. while (index < length) {
  23. uint32_t code = 0;
  24. U16_NEXT(data, index, length, code);
  25. UErrorCode error = U_ZERO_ERROR;
  26. if (uscript_getScript(code, &error) != USCRIPT_COMMON)
  27. return true;
  28. }
  29. return false;
  30. }
  31. } // namespace
  32. class SpellCheckClient::SpellcheckRequest {
  33. public:
  34. SpellcheckRequest(const base::string16& text,
  35. blink::WebTextCheckingCompletion* completion)
  36. : text_(text), completion_(completion) {
  37. DCHECK(completion);
  38. }
  39. ~SpellcheckRequest() {}
  40. base::string16 text() { return text_; }
  41. blink::WebTextCheckingCompletion* completion() { return completion_; }
  42. private:
  43. base::string16 text_; // Text to be checked in this task.
  44. // The interface to send the misspelled ranges to WebKit.
  45. blink::WebTextCheckingCompletion* completion_;
  46. DISALLOW_COPY_AND_ASSIGN(SpellcheckRequest);
  47. };
  48. SpellCheckClient::SpellCheckClient(const std::string& language,
  49. bool auto_spell_correct_turned_on,
  50. v8::Isolate* isolate,
  51. v8::Local<v8::Object> provider)
  52. : isolate_(isolate),
  53. context_(isolate, isolate->GetCurrentContext()),
  54. provider_(isolate, provider) {
  55. DCHECK(!context_.IsEmpty());
  56. character_attributes_.SetDefaultLanguage(language);
  57. // Persistent the method.
  58. mate::Dictionary dict(isolate, provider);
  59. dict.Get("spellCheck", &spell_check_);
  60. }
  61. SpellCheckClient::~SpellCheckClient() {
  62. context_.Reset();
  63. }
  64. void SpellCheckClient::CheckSpelling(
  65. const blink::WebString& text,
  66. int& misspelling_start,
  67. int& misspelling_len,
  68. blink::WebVector<blink::WebString>* optional_suggestions) {
  69. std::vector<blink::WebTextCheckingResult> results;
  70. SpellCheckText(text.Utf16(), true, &results);
  71. if (results.size() == 1) {
  72. misspelling_start = results[0].location;
  73. misspelling_len = results[0].length;
  74. }
  75. }
  76. void SpellCheckClient::RequestCheckingOfText(
  77. const blink::WebString& textToCheck,
  78. blink::WebTextCheckingCompletion* completionCallback) {
  79. base::string16 text(textToCheck.Utf16());
  80. // Ignore invalid requests.
  81. if (text.empty() || !HasWordCharacters(text, 0)) {
  82. completionCallback->DidCancelCheckingText();
  83. return;
  84. }
  85. // Clean up the previous request before starting a new request.
  86. if (pending_request_param_.get()) {
  87. pending_request_param_->completion()->DidCancelCheckingText();
  88. }
  89. pending_request_param_.reset(new SpellcheckRequest(text, completionCallback));
  90. base::ThreadTaskRunnerHandle::Get()->PostTask(
  91. FROM_HERE,
  92. base::BindOnce(&SpellCheckClient::PerformSpellCheck, AsWeakPtr(),
  93. base::Owned(pending_request_param_.release())));
  94. }
  95. void SpellCheckClient::ShowSpellingUI(bool show) {}
  96. bool SpellCheckClient::IsShowingSpellingUI() {
  97. return false;
  98. }
  99. void SpellCheckClient::UpdateSpellingUIWithMisspelledWord(
  100. const blink::WebString& word) {}
  101. void SpellCheckClient::SpellCheckText(
  102. const base::string16& text,
  103. bool stop_at_first_result,
  104. std::vector<blink::WebTextCheckingResult>* results) {
  105. if (text.empty() || spell_check_.IsEmpty())
  106. return;
  107. if (!text_iterator_.IsInitialized() &&
  108. !text_iterator_.Initialize(&character_attributes_, true)) {
  109. // We failed to initialize text_iterator_, return as spelled correctly.
  110. VLOG(1) << "Failed to initialize SpellcheckWordIterator";
  111. return;
  112. }
  113. if (!contraction_iterator_.IsInitialized() &&
  114. !contraction_iterator_.Initialize(&character_attributes_, false)) {
  115. // We failed to initialize the word iterator, return as spelled correctly.
  116. VLOG(1) << "Failed to initialize contraction_iterator_";
  117. return;
  118. }
  119. text_iterator_.SetText(text.c_str(), text.size());
  120. SpellCheckScope scope(*this);
  121. base::string16 word;
  122. int word_start;
  123. int word_length;
  124. for (auto status =
  125. text_iterator_.GetNextWord(&word, &word_start, &word_length);
  126. status != SpellcheckWordIterator::IS_END_OF_TEXT;
  127. status = text_iterator_.GetNextWord(&word, &word_start, &word_length)) {
  128. if (status == SpellcheckWordIterator::IS_SKIPPABLE)
  129. continue;
  130. // Found a word (or a contraction) that the spellchecker can check the
  131. // spelling of.
  132. if (SpellCheckWord(scope, word))
  133. continue;
  134. // If the given word is a concatenated word of two or more valid words
  135. // (e.g. "hello:hello"), we should treat it as a valid word.
  136. if (IsValidContraction(scope, word))
  137. continue;
  138. blink::WebTextCheckingResult result;
  139. result.location = word_start;
  140. result.length = word_length;
  141. results->push_back(result);
  142. if (stop_at_first_result)
  143. return;
  144. }
  145. }
  146. bool SpellCheckClient::SpellCheckWord(
  147. const SpellCheckScope& scope,
  148. const base::string16& word_to_check) const {
  149. DCHECK(!scope.spell_check_.IsEmpty());
  150. v8::Local<v8::Value> word = mate::ConvertToV8(isolate_, word_to_check);
  151. v8::Local<v8::Value> result =
  152. scope.spell_check_->Call(scope.provider_, 1, &word);
  153. if (!result.IsEmpty() && result->IsBoolean())
  154. return result->BooleanValue();
  155. else
  156. return true;
  157. }
  158. // Returns whether or not the given string is a valid contraction.
  159. // This function is a fall-back when the SpellcheckWordIterator class
  160. // returns a concatenated word which is not in the selected dictionary
  161. // (e.g. "in'n'out") but each word is valid.
  162. bool SpellCheckClient::IsValidContraction(const SpellCheckScope& scope,
  163. const base::string16& contraction) {
  164. DCHECK(contraction_iterator_.IsInitialized());
  165. contraction_iterator_.SetText(contraction.c_str(), contraction.length());
  166. base::string16 word;
  167. int word_start;
  168. int word_length;
  169. for (auto status =
  170. contraction_iterator_.GetNextWord(&word, &word_start, &word_length);
  171. status != SpellcheckWordIterator::IS_END_OF_TEXT;
  172. status = contraction_iterator_.GetNextWord(&word, &word_start,
  173. &word_length)) {
  174. if (status == SpellcheckWordIterator::IS_SKIPPABLE)
  175. continue;
  176. if (!SpellCheckWord(scope, word))
  177. return false;
  178. }
  179. return true;
  180. }
  181. void SpellCheckClient::PerformSpellCheck(SpellcheckRequest* param) {
  182. DCHECK(param);
  183. std::vector<blink::WebTextCheckingResult> results;
  184. SpellCheckText(param->text(), false, &results);
  185. param->completion()->DidFinishCheckingText(results);
  186. }
  187. SpellCheckClient::SpellCheckScope::SpellCheckScope(
  188. const SpellCheckClient& client)
  189. : handle_scope_(client.isolate_),
  190. context_scope_(
  191. v8::Local<v8::Context>::New(client.isolate_, client.context_)),
  192. provider_(client.provider_.NewHandle()),
  193. spell_check_(client.spell_check_.NewHandle()) {}
  194. SpellCheckClient::SpellCheckScope::~SpellCheckScope() = default;
  195. } // namespace api
  196. } // namespace atom