atom_api_spell_check_client.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 ATOM_RENDERER_API_ATOM_API_SPELL_CHECK_CLIENT_H_
  5. #define ATOM_RENDERER_API_ATOM_API_SPELL_CHECK_CLIENT_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/callback.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "chrome/renderer/spellchecker/spellcheck_worditerator.h"
  11. #include "native_mate/scoped_persistent.h"
  12. #include "third_party/WebKit/public/platform/WebSpellCheckPanelHostClient.h"
  13. #include "third_party/WebKit/public/platform/WebVector.h"
  14. #include "third_party/WebKit/public/web/WebTextCheckClient.h"
  15. namespace blink {
  16. struct WebTextCheckingResult;
  17. class WebTextCheckingCompletion;
  18. } // namespace blink
  19. namespace atom {
  20. namespace api {
  21. class SpellCheckClient : public blink::WebSpellCheckPanelHostClient,
  22. public blink::WebTextCheckClient,
  23. public base::SupportsWeakPtr<SpellCheckClient> {
  24. public:
  25. SpellCheckClient(const std::string& language,
  26. bool auto_spell_correct_turned_on,
  27. v8::Isolate* isolate,
  28. v8::Local<v8::Object> provider);
  29. ~SpellCheckClient() override;
  30. private:
  31. class SpellcheckRequest;
  32. // blink::WebTextCheckClient:
  33. void CheckSpelling(
  34. const blink::WebString& text,
  35. int& misspelledOffset,
  36. int& misspelledLength,
  37. blink::WebVector<blink::WebString>* optionalSuggestions) override;
  38. void RequestCheckingOfText(
  39. const blink::WebString& textToCheck,
  40. blink::WebTextCheckingCompletion* completionCallback) 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. // Check the spelling of text.
  55. void SpellCheckText(const base::string16& text,
  56. bool stop_at_first_result,
  57. std::vector<blink::WebTextCheckingResult>* results);
  58. // Call JavaScript to check spelling a word.
  59. bool SpellCheckWord(const SpellCheckScope& scope,
  60. const base::string16& word_to_check) const;
  61. // Returns whether or not the given word is a contraction of valid words
  62. // (e.g. "word:word").
  63. bool IsValidContraction(const SpellCheckScope& scope,
  64. const base::string16& word);
  65. // Performs spell checking from the request queue.
  66. void PerformSpellCheck(SpellcheckRequest* param);
  67. // Represents character attributes used for filtering out characters which
  68. // are not supported by this SpellCheck object.
  69. SpellcheckCharAttribute character_attributes_;
  70. // Represents word iterators used in this spellchecker. The |text_iterator_|
  71. // splits text provided by WebKit into words, contractions, or concatenated
  72. // words. The |contraction_iterator_| splits a concatenated word extracted by
  73. // |text_iterator_| into word components so we can treat a concatenated word
  74. // consisting only of correct words as a correct word.
  75. SpellcheckWordIterator text_iterator_;
  76. SpellcheckWordIterator contraction_iterator_;
  77. // The parameters of a pending background-spellchecking request.
  78. // (When WebKit sends two or more requests, we cancel the previous
  79. // requests so we do not have to use vectors.)
  80. std::unique_ptr<SpellcheckRequest> pending_request_param_;
  81. v8::Isolate* isolate_;
  82. v8::Persistent<v8::Context> context_;
  83. mate::ScopedPersistent<v8::Object> provider_;
  84. mate::ScopedPersistent<v8::Function> spell_check_;
  85. DISALLOW_COPY_AND_ASSIGN(SpellCheckClient);
  86. };
  87. } // namespace api
  88. } // namespace atom
  89. #endif // ATOM_RENDERER_API_ATOM_API_SPELL_CHECK_CLIENT_H_