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_H_
6 #define CHROME_RENDERER_SPELLCHECKER_SPELLCHECK_H_
12 #include "base/files/file.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/macros.h"
15 #include "base/memory/scoped_ptr.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_process_observer.h"
22 struct SpellCheckBDictLanguage
;
23 class SpellcheckLanguage
;
24 struct SpellCheckResult
;
27 class WebTextCheckingCompletion
;
28 struct WebTextCheckingResult
;
29 template <typename T
> class WebVector
;
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::RenderProcessObserver
,
41 public base::SupportsWeakPtr
<SpellCheck
> {
43 // TODO(groby): I wonder if this can be private, non-mac only.
44 class SpellcheckRequest
;
46 DO_NOT_MODIFY
= 1, // Do not modify results.
47 USE_NATIVE_CHECKER
, // Use native checker to double-check.
51 ~SpellCheck() override
;
53 void AddSpellcheckLanguage(base::File file
, const std::string
& language
);
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
62 bool InitializeIfNeeded();
65 // Returns true if spelled correctly for any language in |languages_|, false
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
,
79 int* misspelling_start
,
81 std::vector
<base::string16
>* optional_suggestions
);
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
);
90 // Find a possible correctly spelled word for a misspelled word. Computes an
91 // empty string if input misspelled word is too long, there is ambiguity, or
92 // the correct spelling cannot be determined.
93 // NOTE: If using the platform spellchecker, this will send a *lot* of sync
94 // IPCs. We should probably refactor this if we ever plan to take it out from
95 // behind its command line flag.
96 base::string16
GetAutoCorrectionWord(const base::string16
& word
, int tag
);
98 // Requests to spellcheck the specified text in the background. This function
99 // posts a background task and calls SpellCheckParagraph() in the task.
100 #if !defined (USE_BROWSER_SPELLCHECKER)
101 void RequestTextChecking(const base::string16
& text
,
102 blink::WebTextCheckingCompletion
* completion
);
105 // Creates a list of WebTextCheckingResult objects (used by WebKit) from a
106 // list of SpellCheckResult objects (used by Chrome). This function also
107 // checks misspelled words returned by the Spelling service and changes the
108 // underline colors of contextually-misspelled words.
109 void CreateTextCheckingResults(
112 const base::string16
& line_text
,
113 const std::vector
<SpellCheckResult
>& spellcheck_results
,
114 blink::WebVector
<blink::WebTextCheckingResult
>* textcheck_results
);
116 bool IsSpellcheckEnabled();
119 friend class SpellCheckTest
;
120 FRIEND_TEST_ALL_PREFIXES(SpellCheckTest
, GetAutoCorrectionWord_EN_US
);
121 FRIEND_TEST_ALL_PREFIXES(SpellCheckTest
,
122 RequestSpellCheckMultipleTimesWithoutInitialization
);
124 // Evenly fill |optional_suggestions| with a maximum of |kMaxSuggestions|
125 // suggestions from |suggestions_list|. suggestions_list[i][j] is the j-th
126 // suggestion from the i-th language's suggestions. |optional_suggestions|
128 static void FillSuggestions(
129 const std::vector
<std::vector
<base::string16
>>& suggestions_list
,
130 std::vector
<base::string16
>* optional_suggestions
);
132 // RenderProcessObserver implementation:
133 bool OnControlMessageReceived(const IPC::Message
& message
) override
;
136 void OnInit(const std::vector
<SpellCheckBDictLanguage
>& bdict_languages
,
137 const std::set
<std::string
>& custom_words
,
138 bool auto_spell_correct
);
139 void OnCustomDictionaryChanged(const std::set
<std::string
>& words_added
,
140 const std::set
<std::string
>& words_removed
);
141 void OnEnableAutoSpellCorrect(bool enable
);
142 void OnEnableSpellCheck(bool enable
);
143 void OnRequestDocumentMarkers();
145 #if !defined (USE_BROWSER_SPELLCHECKER)
146 // Posts delayed spellcheck task and clear it if any.
147 // Takes ownership of |request|.
148 void PostDelayedSpellCheckTask(SpellcheckRequest
* request
);
150 // Performs spell checking from the request queue.
151 void PerformSpellCheck(SpellcheckRequest
* request
);
153 // The parameters of a pending background-spellchecking request. When WebKit
154 // sends a background-spellchecking request before initializing hunspell,
155 // we save its parameters and start spellchecking after we finish initializing
156 // hunspell. (When WebKit sends two or more requests, we cancel the previous
157 // requests so we do not have to use vectors.)
158 scoped_ptr
<SpellcheckRequest
> pending_request_param_
;
161 // A vector of objects used to actually check spelling, one for each enabled
163 ScopedVector
<SpellcheckLanguage
> languages_
;
165 // Custom dictionary spelling engine.
166 CustomDictionaryEngine custom_dictionary_
;
168 // Remember state for auto spell correct.
169 bool auto_spell_correct_turned_on_
;
171 // Remember state for spellchecking.
172 bool spellcheck_enabled_
;
174 DISALLOW_COPY_AND_ASSIGN(SpellCheck
);
177 #endif // CHROME_RENDERER_SPELLCHECKER_SPELLCHECK_H_