spellcheck_provider.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_SPELLCHECK_RENDERER_SPELLCHECK_PROVIDER_H_
  5. #define COMPONENTS_SPELLCHECK_RENDERER_SPELLCHECK_PROVIDER_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "base/containers/id_map.h"
  9. #include "build/build_config.h"
  10. #include "components/spellcheck/common/spellcheck.mojom.h"
  11. #include "components/spellcheck/spellcheck_buildflags.h"
  12. #include "content/public/renderer/render_frame_observer.h"
  13. #include "mojo/public/cpp/bindings/pending_remote.h"
  14. #include "mojo/public/cpp/bindings/remote.h"
  15. #include "third_party/blink/public/web/web_text_check_client.h"
  16. #if BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  17. #include <unordered_map>
  18. #endif // BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  19. class SpellCheck;
  20. struct SpellCheckResult;
  21. namespace base {
  22. class TimeTicks;
  23. }
  24. namespace blink {
  25. class WebTextCheckingCompletion;
  26. struct WebTextCheckingResult;
  27. }
  28. namespace service_manager {
  29. class LocalInterfaceProvider;
  30. }
  31. // This class deals with asynchronously invoking text spelling and grammar
  32. // checking services provided by the browser process (host).
  33. class SpellCheckProvider : public content::RenderFrameObserver,
  34. public blink::WebTextCheckClient {
  35. public:
  36. using WebTextCheckCompletions =
  37. base::IDMap<std::unique_ptr<blink::WebTextCheckingCompletion>>;
  38. #if BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  39. // A struct to hold information related to hybrid spell check requests.
  40. struct HybridSpellCheckRequestInfo {
  41. bool used_hunspell;
  42. bool used_native;
  43. base::TimeTicks request_start_ticks;
  44. };
  45. #endif // BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  46. SpellCheckProvider(
  47. content::RenderFrame* render_frame,
  48. SpellCheck* spellcheck,
  49. service_manager::LocalInterfaceProvider* embedder_provider);
  50. SpellCheckProvider(const SpellCheckProvider&) = delete;
  51. SpellCheckProvider& operator=(const SpellCheckProvider&) = delete;
  52. ~SpellCheckProvider() override;
  53. // Requests async spell and grammar checks from the platform text checker
  54. // available in the browser process. The function does not have special
  55. // handling for partial words, as Blink guarantees that no request is made
  56. // when typing in the middle of a word.
  57. void RequestTextChecking(
  58. const std::u16string& text,
  59. std::unique_ptr<blink::WebTextCheckingCompletion> completion);
  60. // The number of ongoing spell check host requests.
  61. size_t pending_text_request_size() const {
  62. return text_check_completions_.size();
  63. }
  64. // Replace shared spellcheck data.
  65. void set_spellcheck(SpellCheck* spellcheck) { spellcheck_ = spellcheck; }
  66. // content::RenderFrameObserver:
  67. void FocusedElementChanged(const blink::WebElement& element) override;
  68. private:
  69. friend class TestingSpellCheckProvider;
  70. class DictionaryUpdateObserverImpl;
  71. // Sets the SpellCheckHost (for unit tests).
  72. void SetSpellCheckHostForTesting(
  73. mojo::PendingRemote<spellcheck::mojom::SpellCheckHost> host) {
  74. spell_check_host_.Bind(std::move(host));
  75. }
  76. // Reset dictionary_update_observer_ in TestingSpellCheckProvider dtor.
  77. void ResetDictionaryUpdateObserverForTesting();
  78. // Returns the SpellCheckHost.
  79. spellcheck::mojom::SpellCheckHost& GetSpellCheckHost();
  80. // Tries to satisfy a spellcheck request from the cache in |last_request_|.
  81. // Returns true (and cancels/finishes the completion) if it can, false
  82. // if the provider should forward the query on.
  83. bool SatisfyRequestFromCache(const std::u16string& text,
  84. blink::WebTextCheckingCompletion* completion);
  85. // content::RenderFrameObserver:
  86. void OnDestruct() override;
  87. // blink::WebTextCheckClient:
  88. bool IsSpellCheckingEnabled() const override;
  89. void CheckSpelling(
  90. const blink::WebString& text,
  91. size_t& offset,
  92. size_t& length,
  93. blink::WebVector<blink::WebString>* optional_suggestions) override;
  94. void RequestCheckingOfText(
  95. const blink::WebString& text,
  96. std::unique_ptr<blink::WebTextCheckingCompletion> completion) override;
  97. #if BUILDFLAG(USE_RENDERER_SPELLCHECKER)
  98. void OnRespondSpellingService(int identifier,
  99. const std::u16string& text,
  100. bool success,
  101. const std::vector<SpellCheckResult>& results);
  102. #endif
  103. // Returns whether |text| has word characters, i.e. whether a spellchecker
  104. // needs to check this text.
  105. bool HasWordCharacters(const std::u16string& text, size_t index) const;
  106. #if BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  107. void OnRespondTextCheck(int identifier,
  108. const std::u16string& line,
  109. const std::vector<SpellCheckResult>& results);
  110. // Makes mojo calls to the browser process to perform platform spellchecking.
  111. void RequestTextCheckingFromBrowser(const std::u16string& text);
  112. #if BUILDFLAG(IS_WIN)
  113. // Callback for when spellcheck service has been initialized on demand.
  114. void OnRespondInitializeDictionaries(
  115. const std::u16string& text,
  116. std::vector<spellcheck::mojom::SpellCheckBDictLanguagePtr> dictionaries,
  117. const std::vector<std::string>& custom_words,
  118. bool enable);
  119. // Flag indicating that the spellcheck service has been initialized and
  120. // the dictionaries have been loaded initially. Used to avoid an unnecessary
  121. // mojo call to determine this in every text check request.
  122. bool dictionaries_loaded_ = false;
  123. #endif // BUILDFLAG(IS_WIN)
  124. #endif // BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  125. // Holds ongoing spellchecking operations.
  126. WebTextCheckCompletions text_check_completions_;
  127. // The last text sent to the browser process for spellchecking, and its
  128. // spellcheck results and WebTextCheckCompletions identifier.
  129. std::u16string last_request_;
  130. blink::WebVector<blink::WebTextCheckingResult> last_results_;
  131. int last_identifier_;
  132. // Weak pointer to shared (per renderer) spellcheck data.
  133. SpellCheck* spellcheck_;
  134. // Not owned. |embedder_provider_| should outlive SpellCheckProvider.
  135. service_manager::LocalInterfaceProvider* embedder_provider_;
  136. // Interface to the SpellCheckHost.
  137. mojo::Remote<spellcheck::mojom::SpellCheckHost> spell_check_host_;
  138. // Dictionary updated observer.
  139. std::unique_ptr<DictionaryUpdateObserverImpl> dictionary_update_observer_;
  140. #if BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  141. std::unordered_map<int, HybridSpellCheckRequestInfo> hybrid_requests_info_;
  142. #endif // BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  143. base::WeakPtrFactory<SpellCheckProvider> weak_factory_{this};
  144. };
  145. #endif // COMPONENTS_SPELLCHECK_RENDERER_SPELLCHECK_PROVIDER_H_