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