OLD | NEW |
| (Empty) |
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 | |
5 #ifndef CHROME_RENDERER_SPELLCHECKER_SPELLCHECK_PROVIDER_H_ | |
6 #define CHROME_RENDERER_SPELLCHECKER_SPELLCHECK_PROVIDER_H_ | |
7 | |
8 #include <stddef.h> | |
9 #include <stdint.h> | |
10 | |
11 #include <vector> | |
12 | |
13 #include "base/id_map.h" | |
14 #include "base/macros.h" | |
15 #include "content/public/renderer/render_view_observer.h" | |
16 #include "content/public/renderer/render_view_observer_tracker.h" | |
17 #include "third_party/WebKit/public/web/WebSpellCheckClient.h" | |
18 | |
19 class RenderView; | |
20 class SpellCheck; | |
21 class SpellCheckMarker; | |
22 struct SpellCheckResult; | |
23 | |
24 namespace blink { | |
25 class WebString; | |
26 class WebTextCheckingCompletion; | |
27 struct WebTextCheckingResult; | |
28 } | |
29 | |
30 // This class deals with invoking browser-side spellcheck mechanism | |
31 // which is done asynchronously. | |
32 class SpellCheckProvider | |
33 : public content::RenderViewObserver, | |
34 public content::RenderViewObserverTracker<SpellCheckProvider>, | |
35 public blink::WebSpellCheckClient { | |
36 public: | |
37 typedef IDMap<blink::WebTextCheckingCompletion> WebTextCheckCompletions; | |
38 | |
39 SpellCheckProvider(content::RenderView* render_view, | |
40 SpellCheck* spellcheck); | |
41 ~SpellCheckProvider() override; | |
42 | |
43 // Requests async spell and grammar checker to the platform text | |
44 // checker, which is available on the browser process. | |
45 void RequestTextChecking( | |
46 const base::string16& text, | |
47 blink::WebTextCheckingCompletion* completion, | |
48 const std::vector<SpellCheckMarker>& markers); | |
49 | |
50 // The number of ongoing IPC requests. | |
51 size_t pending_text_request_size() const { | |
52 return text_check_completions_.size(); | |
53 } | |
54 | |
55 // Replace shared spellcheck data. | |
56 void set_spellcheck(SpellCheck* spellcheck) { spellcheck_ = spellcheck; } | |
57 | |
58 // Enables document-wide spellchecking. | |
59 void EnableSpellcheck(bool enabled); | |
60 | |
61 // RenderViewObserver implementation. | |
62 bool OnMessageReceived(const IPC::Message& message) override; | |
63 void FocusedNodeChanged(const blink::WebNode& node) override; | |
64 | |
65 private: | |
66 friend class TestingSpellCheckProvider; | |
67 | |
68 // Tries to satisfy a spell check request from the cache in |last_request_|. | |
69 // Returns true (and cancels/finishes the completion) if it can, false | |
70 // if the provider should forward the query on. | |
71 bool SatisfyRequestFromCache(const base::string16& text, | |
72 blink::WebTextCheckingCompletion* completion); | |
73 | |
74 // RenderViewObserver implementation. | |
75 void OnDestruct() override; | |
76 | |
77 // blink::WebSpellCheckClient implementation. | |
78 void spellCheck( | |
79 const blink::WebString& text, | |
80 int& offset, | |
81 int& length, | |
82 blink::WebVector<blink::WebString>* optional_suggestions) override; | |
83 void checkTextOfParagraph( | |
84 const blink::WebString& text, | |
85 blink::WebTextCheckingTypeMask mask, | |
86 blink::WebVector<blink::WebTextCheckingResult>* results) override; | |
87 | |
88 void requestCheckingOfText( | |
89 const blink::WebString& text, | |
90 const blink::WebVector<uint32_t>& markers, | |
91 const blink::WebVector<unsigned>& marker_offsets, | |
92 blink::WebTextCheckingCompletion* completion) override; | |
93 | |
94 void showSpellingUI(bool show) override; | |
95 bool isShowingSpellingUI() override; | |
96 void updateSpellingUIWithMisspelledWord( | |
97 const blink::WebString& word) override; | |
98 | |
99 #if !defined(USE_BROWSER_SPELLCHECKER) | |
100 void OnRespondSpellingService( | |
101 int identifier, | |
102 bool succeeded, | |
103 const base::string16& text, | |
104 const std::vector<SpellCheckResult>& results); | |
105 #endif | |
106 | |
107 // Returns whether |text| has word characters, i.e. whether a spellchecker | |
108 // needs to check this text. | |
109 bool HasWordCharacters(const base::string16& text, int index) const; | |
110 | |
111 #if defined(USE_BROWSER_SPELLCHECKER) | |
112 void OnAdvanceToNextMisspelling(); | |
113 void OnRespondTextCheck( | |
114 int identifier, | |
115 const base::string16& line, | |
116 const std::vector<SpellCheckResult>& results); | |
117 void OnToggleSpellPanel(bool is_currently_visible); | |
118 #endif | |
119 | |
120 // Holds ongoing spellchecking operations, assigns IDs for the IPC routing. | |
121 WebTextCheckCompletions text_check_completions_; | |
122 | |
123 // The last text sent to the browser process to spellcheck it and its | |
124 // spellchecking results. | |
125 base::string16 last_request_; | |
126 blink::WebVector<blink::WebTextCheckingResult> last_results_; | |
127 | |
128 // True if the browser is showing the spelling panel for us. | |
129 bool spelling_panel_visible_; | |
130 | |
131 // Weak pointer to shared (per RenderView) spellcheck data. | |
132 SpellCheck* spellcheck_; | |
133 | |
134 DISALLOW_COPY_AND_ASSIGN(SpellCheckProvider); | |
135 }; | |
136 | |
137 #endif // CHROME_RENDERER_SPELLCHECKER_SPELLCHECK_PROVIDER_H_ | |
OLD | NEW |