electron_api_spell_check_client.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #ifndef ELECTRON_SHELL_RENDERER_API_ELECTRON_API_SPELL_CHECK_CLIENT_H_
  5. #define ELECTRON_SHELL_RENDERER_API_ELECTRON_API_SPELL_CHECK_CLIENT_H_
  6. #include <memory>
  7. #include <set>
  8. #include <string>
  9. #include <vector>
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "components/spellcheck/renderer/spellcheck_worditerator.h"
  13. #include "third_party/blink/public/platform/web_spell_check_panel_host_client.h"
  14. #include "third_party/blink/public/web/web_text_check_client.h"
  15. #include "v8/include/v8-context.h"
  16. #include "v8/include/v8-forward.h"
  17. #include "v8/include/v8-local-handle.h"
  18. namespace blink {
  19. struct WebTextCheckingResult;
  20. class WebTextCheckingCompletion;
  21. } // namespace blink
  22. namespace electron::api {
  23. class SpellCheckClient : public blink::WebSpellCheckPanelHostClient,
  24. public blink::WebTextCheckClient {
  25. public:
  26. SpellCheckClient(const std::string& language,
  27. v8::Isolate* isolate,
  28. v8::Local<v8::Object> provider);
  29. ~SpellCheckClient() override;
  30. // disable copy
  31. SpellCheckClient(const SpellCheckClient&) = delete;
  32. SpellCheckClient& operator=(const SpellCheckClient&) = delete;
  33. private:
  34. class SpellcheckRequest;
  35. // blink::WebTextCheckClient:
  36. void RequestCheckingOfText(const blink::WebString& textToCheck,
  37. std::unique_ptr<blink::WebTextCheckingCompletion>
  38. completionCallback) override;
  39. bool IsSpellCheckingEnabled() const override;
  40. // blink::WebSpellCheckPanelHostClient:
  41. void ShowSpellingUI(bool show) override {}
  42. bool IsShowingSpellingUI() override;
  43. void UpdateSpellingUIWithMisspelledWord(
  44. const blink::WebString& word) override {}
  45. struct SpellCheckScope {
  46. v8::HandleScope handle_scope_;
  47. v8::Context::Scope context_scope_;
  48. v8::Local<v8::Object> provider_;
  49. v8::Local<v8::Function> spell_check_;
  50. explicit SpellCheckScope(const SpellCheckClient& client);
  51. ~SpellCheckScope();
  52. };
  53. // Run through the word iterator and send out requests
  54. // to the JS API for checking spellings of words in the current
  55. // request.
  56. void SpellCheckText();
  57. // Call JavaScript to check spelling a word.
  58. // The javascript function will callback OnSpellCheckDone
  59. // with the results of all the misspelled words.
  60. void SpellCheckWords(const SpellCheckScope& scope,
  61. const std::set<std::u16string>& words);
  62. // Returns whether or not the given word is a contraction of valid words
  63. // (e.g. "word:word").
  64. // Output variable contraction_words will contain individual
  65. // words in the contraction.
  66. bool IsContraction(const SpellCheckScope& scope,
  67. const std::u16string& contraction,
  68. std::vector<std::u16string>* contraction_words);
  69. // Callback for the JS API which returns the list of misspelled words.
  70. void OnSpellCheckDone(const std::vector<std::u16string>& misspelled_words);
  71. // Represents character attributes used for filtering out characters which
  72. // are not supported by this SpellCheck object.
  73. SpellcheckCharAttribute character_attributes_;
  74. // Represents word iterators used in this spellchecker. The |text_iterator_|
  75. // splits text provided by Blink into words, contractions, or concatenated
  76. // words. The |contraction_iterator_| splits a concatenated word extracted by
  77. // |text_iterator_| into word components so we can treat a concatenated word
  78. // consisting only of correct words as a correct word.
  79. SpellcheckWordIterator text_iterator_;
  80. SpellcheckWordIterator contraction_iterator_;
  81. // The parameters of a pending background-spellchecking request.
  82. // (When Blink sends two or more requests, we cancel the previous
  83. // requests so we do not have to use vectors.)
  84. std::unique_ptr<SpellcheckRequest> pending_request_param_;
  85. raw_ptr<v8::Isolate> isolate_;
  86. v8::Global<v8::Context> context_;
  87. v8::Global<v8::Object> provider_;
  88. v8::Global<v8::Function> spell_check_;
  89. base::WeakPtrFactory<SpellCheckClient> weak_factory_{this};
  90. };
  91. } // namespace electron::api
  92. #endif // ELECTRON_SHELL_RENDERER_API_ELECTRON_API_SPELL_CHECK_CLIENT_H_