electron_api_spell_check_client.h 4.3 KB

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