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_H_ | |
6 #define CHROME_RENDERER_SPELLCHECKER_SPELLCHECK_H_ | |
7 | |
8 #include <memory> | |
9 #include <set> | |
10 #include <string> | |
11 #include <vector> | |
12 | |
13 #include "base/files/file.h" | |
14 #include "base/gtest_prod_util.h" | |
15 #include "base/macros.h" | |
16 #include "base/memory/scoped_vector.h" | |
17 #include "base/memory/weak_ptr.h" | |
18 #include "base/strings/string16.h" | |
19 #include "chrome/renderer/spellchecker/custom_dictionary_engine.h" | |
20 #include "content/public/renderer/render_thread_observer.h" | |
21 | |
22 struct SpellCheckBDictLanguage; | |
23 class SpellcheckLanguage; | |
24 struct SpellCheckResult; | |
25 | |
26 namespace blink { | |
27 class WebTextCheckingCompletion; | |
28 struct WebTextCheckingResult; | |
29 template <typename T> class WebVector; | |
30 } | |
31 | |
32 namespace IPC { | |
33 class Message; | |
34 } | |
35 | |
36 // TODO(morrita): Needs reorg with SpellCheckProvider. | |
37 // See http://crbug.com/73699. | |
38 // Shared spellchecking logic/data for a RenderProcess. All RenderViews use | |
39 // this object to perform spellchecking tasks. | |
40 class SpellCheck : public content::RenderThreadObserver, | |
41 public base::SupportsWeakPtr<SpellCheck> { | |
42 public: | |
43 // TODO(groby): I wonder if this can be private, non-mac only. | |
44 class SpellcheckRequest; | |
45 enum ResultFilter { | |
46 DO_NOT_MODIFY = 1, // Do not modify results. | |
47 USE_NATIVE_CHECKER, // Use native checker to double-check. | |
48 }; | |
49 | |
50 SpellCheck(); | |
51 ~SpellCheck() override; | |
52 | |
53 void AddSpellcheckLanguage(base::File file, const std::string& language); | |
54 | |
55 // If there are no dictionary files, then this requests them from the browser | |
56 // and does not block. In this case it returns true. | |
57 // If there are dictionary files, but their Hunspell has not been loaded, then | |
58 // this loads their Hunspell. | |
59 // If each dictionary file's Hunspell is already loaded, this does nothing. In | |
60 // both the latter cases it returns false, meaning that it is OK to continue | |
61 // spellchecking. | |
62 bool InitializeIfNeeded(); | |
63 | |
64 // SpellCheck a word. | |
65 // Returns true if spelled correctly for any language in |languages_|, false | |
66 // otherwise. | |
67 // If any spellcheck languages failed to initialize, always returns true. | |
68 // The |tag| parameter should either be a unique identifier for the document | |
69 // that the word came from (if the current platform requires it), or 0. | |
70 // In addition, finds the suggested words for a given word | |
71 // and puts them into |*optional_suggestions|. | |
72 // If the word is spelled correctly, the vector is empty. | |
73 // If optional_suggestions is NULL, suggested words will not be looked up. | |
74 // Note that doing suggest lookups can be slow. | |
75 bool SpellCheckWord(const base::char16* text_begin, | |
76 int position_in_text, | |
77 int text_length, | |
78 int tag, | |
79 int* misspelling_start, | |
80 int* misspelling_len, | |
81 std::vector<base::string16>* optional_suggestions); | |
82 | |
83 // SpellCheck a paragraph. | |
84 // Returns true if |text| is correctly spelled, false otherwise. | |
85 // If the spellchecker failed to initialize, always returns true. | |
86 bool SpellCheckParagraph( | |
87 const base::string16& text, | |
88 blink::WebVector<blink::WebTextCheckingResult>* results); | |
89 | |
90 // Requests to spellcheck the specified text in the background. This function | |
91 // posts a background task and calls SpellCheckParagraph() in the task. | |
92 #if !defined (USE_BROWSER_SPELLCHECKER) | |
93 void RequestTextChecking(const base::string16& text, | |
94 blink::WebTextCheckingCompletion* completion); | |
95 #endif | |
96 | |
97 // Creates a list of WebTextCheckingResult objects (used by WebKit) from a | |
98 // list of SpellCheckResult objects (used by Chrome). This function also | |
99 // checks misspelled words returned by the Spelling service and changes the | |
100 // underline colors of contextually-misspelled words. | |
101 void CreateTextCheckingResults( | |
102 ResultFilter filter, | |
103 int line_offset, | |
104 const base::string16& line_text, | |
105 const std::vector<SpellCheckResult>& spellcheck_results, | |
106 blink::WebVector<blink::WebTextCheckingResult>* textcheck_results); | |
107 | |
108 bool IsSpellcheckEnabled(); | |
109 | |
110 private: | |
111 friend class SpellCheckTest; | |
112 FRIEND_TEST_ALL_PREFIXES(SpellCheckTest, GetAutoCorrectionWord_EN_US); | |
113 FRIEND_TEST_ALL_PREFIXES(SpellCheckTest, | |
114 RequestSpellCheckMultipleTimesWithoutInitialization); | |
115 | |
116 // Evenly fill |optional_suggestions| with a maximum of |kMaxSuggestions| | |
117 // suggestions from |suggestions_list|. suggestions_list[i][j] is the j-th | |
118 // suggestion from the i-th language's suggestions. |optional_suggestions| | |
119 // cannot be null. | |
120 static void FillSuggestions( | |
121 const std::vector<std::vector<base::string16>>& suggestions_list, | |
122 std::vector<base::string16>* optional_suggestions); | |
123 | |
124 // RenderThreadObserver implementation: | |
125 bool OnControlMessageReceived(const IPC::Message& message) override; | |
126 | |
127 // Message handlers. | |
128 void OnInit(const std::vector<SpellCheckBDictLanguage>& bdict_languages, | |
129 const std::set<std::string>& custom_words); | |
130 void OnCustomDictionaryChanged(const std::set<std::string>& words_added, | |
131 const std::set<std::string>& words_removed); | |
132 void OnEnableSpellCheck(bool enable); | |
133 void OnRequestDocumentMarkers(); | |
134 | |
135 #if !defined (USE_BROWSER_SPELLCHECKER) | |
136 // Posts delayed spellcheck task and clear it if any. | |
137 // Takes ownership of |request|. | |
138 void PostDelayedSpellCheckTask(SpellcheckRequest* request); | |
139 | |
140 // Performs spell checking from the request queue. | |
141 void PerformSpellCheck(SpellcheckRequest* request); | |
142 | |
143 // The parameters of a pending background-spellchecking request. When WebKit | |
144 // sends a background-spellchecking request before initializing hunspell, | |
145 // we save its parameters and start spellchecking after we finish initializing | |
146 // hunspell. (When WebKit sends two or more requests, we cancel the previous | |
147 // requests so we do not have to use vectors.) | |
148 std::unique_ptr<SpellcheckRequest> pending_request_param_; | |
149 #endif | |
150 | |
151 // A vector of objects used to actually check spelling, one for each enabled | |
152 // language. | |
153 ScopedVector<SpellcheckLanguage> languages_; | |
154 | |
155 // Custom dictionary spelling engine. | |
156 CustomDictionaryEngine custom_dictionary_; | |
157 | |
158 // Remember state for spellchecking. | |
159 bool spellcheck_enabled_; | |
160 | |
161 DISALLOW_COPY_AND_ASSIGN(SpellCheck); | |
162 }; | |
163 | |
164 #endif // CHROME_RENDERER_SPELLCHECKER_SPELLCHECK_H_ | |
OLD | NEW |