OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_SPELLCHECK_RENDERER_SPELLCHECK_PROVIDER_H_ | 5 #ifndef COMPONENTS_SPELLCHECK_RENDERER_SPELLCHECK_PROVIDER_H_ |
6 #define COMPONENTS_SPELLCHECK_RENDERER_SPELLCHECK_PROVIDER_H_ | 6 #define COMPONENTS_SPELLCHECK_RENDERER_SPELLCHECK_PROVIDER_H_ |
7 | 7 |
8 #include <stddef.h> | |
9 #include <stdint.h> | |
10 | |
11 #include <vector> | 8 #include <vector> |
12 | 9 |
13 #include "base/id_map.h" | 10 #include "base/id_map.h" |
14 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "components/spellcheck/common/spellcheck.mojom.h" |
15 #include "components/spellcheck/spellcheck_build_features.h" | 13 #include "components/spellcheck/spellcheck_build_features.h" |
16 #include "content/public/renderer/render_frame_observer.h" | 14 #include "content/public/renderer/render_frame_observer.h" |
17 #include "content/public/renderer/render_frame_observer_tracker.h" | 15 #include "content/public/renderer/render_frame_observer_tracker.h" |
18 #include "third_party/WebKit/public/web/WebTextCheckClient.h" | 16 #include "third_party/WebKit/public/web/WebTextCheckClient.h" |
19 | 17 |
20 class SpellCheck; | 18 class SpellCheck; |
21 struct SpellCheckResult; | 19 struct SpellCheckResult; |
22 | 20 |
23 namespace blink { | 21 namespace blink { |
24 class WebTextCheckingCompletion; | 22 class WebTextCheckingCompletion; |
25 struct WebTextCheckingResult; | 23 struct WebTextCheckingResult; |
26 } | 24 } |
27 | 25 |
28 // This class deals with invoking browser-side spellcheck mechanism | 26 // This class deals with asynchronously invoking text spelling and grammar |
29 // which is done asynchronously. | 27 // checking services provided by the browser process (host). |
30 class SpellCheckProvider | 28 class SpellCheckProvider |
31 : public content::RenderFrameObserver, | 29 : public content::RenderFrameObserver, |
32 public content::RenderFrameObserverTracker<SpellCheckProvider>, | 30 public content::RenderFrameObserverTracker<SpellCheckProvider>, |
33 public blink::WebTextCheckClient { | 31 public blink::WebTextCheckClient { |
34 public: | 32 public: |
35 using WebTextCheckCompletions = IDMap<blink::WebTextCheckingCompletion*>; | 33 using WebTextCheckCompletions = IDMap<blink::WebTextCheckingCompletion*>; |
36 | 34 |
37 SpellCheckProvider(content::RenderFrame* render_frame, | 35 SpellCheckProvider(content::RenderFrame* render_frame, |
38 SpellCheck* spellcheck); | 36 SpellCheck* spellcheck); |
39 ~SpellCheckProvider() override; | 37 ~SpellCheckProvider() override; |
40 | 38 |
41 // Requests async spell and grammar checker to the platform text | 39 // Requests async spell and grammar checks from the platform text checker |
42 // checker, which is available on the browser process. The function does not | 40 // available in the browser process. The function does not have special |
43 // have special handling for partial words, as Blink guarantees that no | 41 // handling for partial words, as Blink guarantees that no request is made |
44 // request is made when typing in the middle of a word. | 42 // when typing in the middle of a word. |
45 void RequestTextChecking(const base::string16& text, | 43 void RequestTextChecking(const base::string16& text, |
46 blink::WebTextCheckingCompletion* completion); | 44 blink::WebTextCheckingCompletion* completion); |
47 | 45 |
48 // The number of ongoing IPC requests. | 46 // The number of ongoing spellcheck host requests. |
49 size_t pending_text_request_size() const { | 47 size_t pending_text_request_size() const { |
50 return text_check_completions_.size(); | 48 return text_check_completions_.size(); |
51 } | 49 } |
52 | 50 |
53 // Replace shared spellcheck data. | 51 // Replace shared spellcheck data. |
54 void set_spellcheck(SpellCheck* spellcheck) { spellcheck_ = spellcheck; } | 52 void set_spellcheck(SpellCheck* spellcheck) { spellcheck_ = spellcheck; } |
55 | 53 |
56 // Enables document-wide spellchecking. | 54 // Enables document-wide spellchecking. |
57 void EnableSpellcheck(bool enabled); | 55 void EnableSpellcheck(bool enabled); |
58 | 56 |
59 // RenderFrameObserver implementation. | 57 // content::RenderFrameObserver: |
60 bool OnMessageReceived(const IPC::Message& message) override; | 58 bool OnMessageReceived(const IPC::Message& message) override; |
61 void FocusedNodeChanged(const blink::WebNode& node) override; | 59 void FocusedNodeChanged(const blink::WebNode& node) override; |
62 | 60 |
63 private: | 61 private: |
64 friend class TestingSpellCheckProvider; | 62 friend class TestingSpellCheckProvider; |
65 | 63 |
66 // Tries to satisfy a spell check request from the cache in |last_request_|. | 64 // Sets the SpellCheckHost (for unit tests). |
| 65 void SetSpellCheckHostForTesting(spellcheck::mojom::SpellCheckHostPtr host) { |
| 66 spellcheck_host_ = std::move(host); |
| 67 } |
| 68 |
| 69 // Returns the SpellCheckHost. |
| 70 spellcheck::mojom::SpellCheckHost& GetSpellCheckHost(); |
| 71 |
| 72 // Tries to satisfy a spellcheck request from the cache in |last_request_|. |
67 // Returns true (and cancels/finishes the completion) if it can, false | 73 // Returns true (and cancels/finishes the completion) if it can, false |
68 // if the provider should forward the query on. | 74 // if the provider should forward the query on. |
69 bool SatisfyRequestFromCache(const base::string16& text, | 75 bool SatisfyRequestFromCache(const base::string16& text, |
70 blink::WebTextCheckingCompletion* completion); | 76 blink::WebTextCheckingCompletion* completion); |
71 | 77 |
72 // RenderFrameObserver implementation. | 78 // content::RenderFrameObserver: |
73 void OnDestruct() override; | 79 void OnDestruct() override; |
74 | 80 |
75 // blink::WebTextCheckClient implementation. | 81 // blink::WebTextCheckClient: |
76 void CheckSpelling( | 82 void CheckSpelling( |
77 const blink::WebString& text, | 83 const blink::WebString& text, |
78 int& offset, | 84 int& offset, |
79 int& length, | 85 int& length, |
80 blink::WebVector<blink::WebString>* optional_suggestions) override; | 86 blink::WebVector<blink::WebString>* optional_suggestions) override; |
81 void RequestCheckingOfText( | 87 void RequestCheckingOfText( |
82 const blink::WebString& text, | 88 const blink::WebString& text, |
83 blink::WebTextCheckingCompletion* completion) override; | 89 blink::WebTextCheckingCompletion* completion) override; |
84 void CancelAllPendingRequests() override; | 90 void CancelAllPendingRequests() override; |
85 | 91 |
86 #if !BUILDFLAG(USE_BROWSER_SPELLCHECKER) | 92 #if !BUILDFLAG(USE_BROWSER_SPELLCHECKER) |
87 void OnRespondSpellingService( | 93 void OnRespondSpellingService(int identifier, |
88 int identifier, | 94 const base::string16& text, |
89 bool succeeded, | 95 bool succeeded, |
90 const base::string16& text, | 96 const std::vector<SpellCheckResult>& results); |
91 const std::vector<SpellCheckResult>& results); | |
92 #endif | 97 #endif |
93 | 98 |
94 // Returns whether |text| has word characters, i.e. whether a spellchecker | 99 // Returns whether |text| has word characters, i.e. whether a spellchecker |
95 // needs to check this text. | 100 // needs to check this text. |
96 bool HasWordCharacters(const base::string16& text, int index) const; | 101 bool HasWordCharacters(const base::string16& text, int index) const; |
97 | 102 |
98 #if BUILDFLAG(USE_BROWSER_SPELLCHECKER) | 103 #if BUILDFLAG(USE_BROWSER_SPELLCHECKER) |
99 void OnRespondTextCheck( | 104 void OnRespondTextCheck( |
100 int identifier, | 105 int identifier, |
101 const base::string16& line, | 106 const base::string16& line, |
102 const std::vector<SpellCheckResult>& results); | 107 const std::vector<SpellCheckResult>& results); |
103 #endif | 108 #endif |
104 | 109 |
105 // Holds ongoing spellchecking operations, assigns IDs for the IPC routing. | 110 // Holds ongoing spellchecking operations. |
106 WebTextCheckCompletions text_check_completions_; | 111 WebTextCheckCompletions text_check_completions_; |
107 | 112 |
108 // The last text sent to the browser process to spellcheck it and its | 113 // The last text sent to the browser process for spellchecking, and its |
109 // spellchecking results. | 114 // spellcheck results and WebTextCheckCompletions identifier. |
110 base::string16 last_request_; | 115 base::string16 last_request_; |
111 blink::WebVector<blink::WebTextCheckingResult> last_results_; | 116 blink::WebVector<blink::WebTextCheckingResult> last_results_; |
| 117 int last_identifier_; |
112 | 118 |
113 // Weak pointer to shared (per RenderView) spellcheck data. | 119 // Weak pointer to shared (per renderer) spellcheck data. |
114 SpellCheck* spellcheck_; | 120 SpellCheck* spellcheck_; |
115 | 121 |
| 122 // Interface to the SpellCheckHost. |
| 123 spellcheck::mojom::SpellCheckHostPtr spellcheck_host_; |
| 124 |
116 DISALLOW_COPY_AND_ASSIGN(SpellCheckProvider); | 125 DISALLOW_COPY_AND_ASSIGN(SpellCheckProvider); |
117 }; | 126 }; |
118 | 127 |
119 #endif // COMPONENTS_SPELLCHECK_RENDERER_SPELLCHECK_PROVIDER_H_ | 128 #endif // COMPONENTS_SPELLCHECK_RENDERER_SPELLCHECK_PROVIDER_H_ |
OLD | NEW |