2014-12-18 21:43:51 +00:00
|
|
|
// Copyright (c) 2014 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#ifndef ELECTRON_SHELL_RENDERER_API_ELECTRON_API_SPELL_CHECK_CLIENT_H_
|
|
|
|
#define ELECTRON_SHELL_RENDERER_API_ELECTRON_API_SPELL_CHECK_CLIENT_H_
|
2014-12-18 21:43:51 +00:00
|
|
|
|
2018-09-13 00:25:56 +00:00
|
|
|
#include <memory>
|
2019-05-31 04:19:10 +00:00
|
|
|
#include <set>
|
2014-12-20 04:42:19 +00:00
|
|
|
#include <string>
|
2014-12-20 06:13:07 +00:00
|
|
|
#include <vector>
|
2014-12-20 04:42:19 +00:00
|
|
|
|
|
|
|
#include "base/callback.h"
|
2018-04-09 06:18:50 +00:00
|
|
|
#include "base/memory/weak_ptr.h"
|
2018-10-26 17:34:57 +00:00
|
|
|
#include "components/spellcheck/renderer/spellcheck_worditerator.h"
|
2018-07-20 16:08:18 +00:00
|
|
|
#include "third_party/blink/public/platform/web_spell_check_panel_host_client.h"
|
|
|
|
#include "third_party/blink/public/platform/web_vector.h"
|
|
|
|
#include "third_party/blink/public/web/web_text_check_client.h"
|
2019-10-31 07:56:00 +00:00
|
|
|
#include "v8/include/v8.h"
|
2014-12-18 21:43:51 +00:00
|
|
|
|
2017-04-11 07:18:40 +00:00
|
|
|
namespace blink {
|
|
|
|
struct WebTextCheckingResult;
|
2017-06-21 14:56:05 +00:00
|
|
|
class WebTextCheckingCompletion;
|
2018-04-09 06:18:50 +00:00
|
|
|
} // namespace blink
|
2017-04-11 07:18:40 +00:00
|
|
|
|
2014-12-18 21:43:51 +00:00
|
|
|
namespace electron {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2017-08-25 11:23:13 +00:00
|
|
|
class SpellCheckClient : public blink::WebSpellCheckPanelHostClient,
|
2018-04-09 06:18:50 +00:00
|
|
|
public blink::WebTextCheckClient,
|
|
|
|
public base::SupportsWeakPtr<SpellCheckClient> {
|
2014-12-18 21:43:51 +00:00
|
|
|
public:
|
2014-12-20 05:01:47 +00:00
|
|
|
SpellCheckClient(const std::string& language,
|
|
|
|
v8::Isolate* isolate,
|
2015-05-22 11:11:22 +00:00
|
|
|
v8::Local<v8::Object> provider);
|
2018-04-17 23:03:51 +00:00
|
|
|
~SpellCheckClient() override;
|
2014-12-18 21:43:51 +00:00
|
|
|
|
2021-11-03 11:41:45 +00:00
|
|
|
// disable copy
|
|
|
|
SpellCheckClient(const SpellCheckClient&) = delete;
|
|
|
|
SpellCheckClient& operator=(const SpellCheckClient&) = delete;
|
|
|
|
|
2014-12-18 21:43:51 +00:00
|
|
|
private:
|
2018-04-09 06:18:50 +00:00
|
|
|
class SpellcheckRequest;
|
2017-06-21 14:56:05 +00:00
|
|
|
// blink::WebTextCheckClient:
|
2019-05-17 04:58:03 +00:00
|
|
|
void RequestCheckingOfText(const blink::WebString& textToCheck,
|
|
|
|
std::unique_ptr<blink::WebTextCheckingCompletion>
|
|
|
|
completionCallback) override;
|
2018-07-10 19:43:39 +00:00
|
|
|
bool IsSpellCheckingEnabled() const override;
|
2017-06-21 14:56:05 +00:00
|
|
|
|
2017-08-25 11:23:13 +00:00
|
|
|
// blink::WebSpellCheckPanelHostClient:
|
2017-06-16 20:42:33 +00:00
|
|
|
void ShowSpellingUI(bool show) override;
|
|
|
|
bool IsShowingSpellingUI() override;
|
|
|
|
void UpdateSpellingUIWithMisspelledWord(
|
2014-12-18 21:43:51 +00:00
|
|
|
const blink::WebString& word) override;
|
|
|
|
|
2017-12-06 11:55:28 +00:00
|
|
|
struct SpellCheckScope {
|
|
|
|
v8::HandleScope handle_scope_;
|
|
|
|
v8::Context::Scope context_scope_;
|
|
|
|
v8::Local<v8::Object> provider_;
|
|
|
|
v8::Local<v8::Function> spell_check_;
|
|
|
|
|
|
|
|
explicit SpellCheckScope(const SpellCheckClient& client);
|
2018-04-17 23:37:22 +00:00
|
|
|
~SpellCheckScope();
|
2017-12-06 11:55:28 +00:00
|
|
|
};
|
|
|
|
|
2018-10-18 16:11:53 +00:00
|
|
|
// Run through the word iterator and send out requests
|
|
|
|
// to the JS API for checking spellings of words in the current
|
|
|
|
// request.
|
|
|
|
void SpellCheckText();
|
2014-12-19 00:44:38 +00:00
|
|
|
|
2014-12-20 06:13:07 +00:00
|
|
|
// Call JavaScript to check spelling a word.
|
2018-10-18 16:11:53 +00:00
|
|
|
// The javascript function will callback OnSpellCheckDone
|
|
|
|
// with the results of all the misspelled words.
|
|
|
|
void SpellCheckWords(const SpellCheckScope& scope,
|
2021-03-16 16:18:45 +00:00
|
|
|
const std::set<std::u16string>& words);
|
2014-12-20 04:56:16 +00:00
|
|
|
|
2014-12-20 04:42:19 +00:00
|
|
|
// Returns whether or not the given word is a contraction of valid words
|
|
|
|
// (e.g. "word:word").
|
2018-10-18 16:11:53 +00:00
|
|
|
// Output variable contraction_words will contain individual
|
|
|
|
// words in the contraction.
|
|
|
|
bool IsContraction(const SpellCheckScope& scope,
|
2021-06-01 01:46:25 +00:00
|
|
|
const std::u16string& contraction,
|
2021-03-16 16:18:45 +00:00
|
|
|
std::vector<std::u16string>* contraction_words);
|
2018-10-18 16:11:53 +00:00
|
|
|
|
|
|
|
// Callback for the JS API which returns the list of misspelled words.
|
2021-03-16 16:18:45 +00:00
|
|
|
void OnSpellCheckDone(const std::vector<std::u16string>& misspelled_words);
|
2018-04-09 06:18:50 +00:00
|
|
|
|
2014-12-20 04:42:19 +00:00
|
|
|
// Represents character attributes used for filtering out characters which
|
|
|
|
// are not supported by this SpellCheck object.
|
|
|
|
SpellcheckCharAttribute character_attributes_;
|
|
|
|
|
|
|
|
// Represents word iterators used in this spellchecker. The |text_iterator_|
|
|
|
|
// splits text provided by WebKit into words, contractions, or concatenated
|
|
|
|
// words. The |contraction_iterator_| splits a concatenated word extracted by
|
|
|
|
// |text_iterator_| into word components so we can treat a concatenated word
|
|
|
|
// consisting only of correct words as a correct word.
|
|
|
|
SpellcheckWordIterator text_iterator_;
|
|
|
|
SpellcheckWordIterator contraction_iterator_;
|
|
|
|
|
2018-04-09 06:18:50 +00:00
|
|
|
// The parameters of a pending background-spellchecking request.
|
|
|
|
// (When WebKit sends two or more requests, we cancel the previous
|
|
|
|
// requests so we do not have to use vectors.)
|
|
|
|
std::unique_ptr<SpellcheckRequest> pending_request_param_;
|
|
|
|
|
2014-12-19 00:44:38 +00:00
|
|
|
v8::Isolate* isolate_;
|
2019-10-31 07:56:00 +00:00
|
|
|
v8::Global<v8::Context> context_;
|
|
|
|
v8::Global<v8::Object> provider_;
|
|
|
|
v8::Global<v8::Function> spell_check_;
|
2014-12-18 21:43:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace electron
|
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#endif // ELECTRON_SHELL_RENDERER_API_ELECTRON_API_SPELL_CHECK_CLIENT_H_
|