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 is no dictionary file, then this requests one from the browser
56 // and does not block. In this case it returns true.
57 // If there is a dictionary file, but Hunspell has not been loaded, then
58 // this loads Hunspell.
59 // If Hunspell is already loaded, this does nothing. In both the latter cases
60 // it returns false, meaning that it is OK to continue spellchecking.
61 bool InitializeIfNeeded();
64 // Returns true if spelled correctly, false otherwise.
65 // If the spellchecker failed to initialize, always returns true.
66 // The |tag| parameter should either be a unique identifier for the document
67 // that the word came from (if the current platform requires it), or 0.
68 // In addition, finds the suggested words for a given word
69 // and puts them into |*optional_suggestions|.
70 // If the word is spelled correctly, the vector is empty.
71 // If optional_suggestions is NULL, suggested words will not be looked up.
72 // Note that Doing suggest lookups can be slow.
73 bool SpellCheckWord(const base::char16
* in_word
,
76 int* misspelling_start
,
78 std::vector
<base::string16
>* optional_suggestions
);
80 // SpellCheck a paragraph.
81 // Returns true if |text| is correctly spelled, false otherwise.
82 // If the spellchecker failed to initialize, always returns true.
83 bool SpellCheckParagraph(
84 const base::string16
& text
,
85 blink::WebVector
<blink::WebTextCheckingResult
>* results
);
87 // Find a possible correctly spelled word for a misspelled word. Computes an
88 // empty string if input misspelled word is too long, there is ambiguity, or
89 // the correct spelling cannot be determined.
90 // NOTE: If using the platform spellchecker, this will send a *lot* of sync
91 // IPCs. We should probably refactor this if we ever plan to take it out from
92 // behind its command line flag.
93 base::string16
GetAutoCorrectionWord(const base::string16
& word
, int tag
);
95 // Requests to spellcheck the specified text in the background. This function
96 // posts a background task and calls SpellCheckParagraph() in the task.
97 #if !defined (OS_MACOSX)
98 void RequestTextChecking(const base::string16
& text
,
99 blink::WebTextCheckingCompletion
* completion
);
102 // Creates a list of WebTextCheckingResult objects (used by WebKit) from a
103 // list of SpellCheckResult objects (used by Chrome). This function also
104 // checks misspelled words returned by the Spelling service and changes the
105 // underline colors of contextually-misspelled words.
106 void CreateTextCheckingResults(
109 const base::string16
& line_text
,
110 const std::vector
<SpellCheckResult
>& spellcheck_results
,
111 blink::WebVector
<blink::WebTextCheckingResult
>* textcheck_results
);
113 bool is_spellcheck_enabled() { return spellcheck_enabled_
; }
116 friend class SpellCheckTest
;
117 FRIEND_TEST_ALL_PREFIXES(SpellCheckTest
, GetAutoCorrectionWord_EN_US
);
118 FRIEND_TEST_ALL_PREFIXES(SpellCheckTest
,
119 RequestSpellCheckMultipleTimesWithoutInitialization
);
121 // RenderProcessObserver implementation:
122 bool OnControlMessageReceived(const IPC::Message
& message
) override
;
125 void OnInit(const std::vector
<SpellCheckBDictLanguage
>& bdict_languages
,
126 const std::set
<std::string
>& custom_words
,
127 bool auto_spell_correct
);
128 void OnCustomDictionaryChanged(const std::set
<std::string
>& words_added
,
129 const std::set
<std::string
>& words_removed
);
130 void OnEnableAutoSpellCorrect(bool enable
);
131 void OnEnableSpellCheck(bool enable
);
132 void OnRequestDocumentMarkers();
134 #if !defined (OS_MACOSX)
135 // Posts delayed spellcheck task and clear it if any.
136 // Takes ownership of |request|.
137 void PostDelayedSpellCheckTask(SpellcheckRequest
* request
);
139 // Performs spell checking from the request queue.
140 void PerformSpellCheck(SpellcheckRequest
* request
);
142 // The parameters of a pending background-spellchecking request. When WebKit
143 // sends a background-spellchecking request before initializing hunspell,
144 // we save its parameters and start spellchecking after we finish initializing
145 // hunspell. (When WebKit sends two or more requests, we cancel the previous
146 // requests so we do not have to use vectors.)
147 scoped_ptr
<SpellcheckRequest
> pending_request_param_
;
150 // A vector of objects used to actually check spelling, one for each enabled
152 ScopedVector
<SpellcheckLanguage
> languages_
;
154 // Custom dictionary spelling engine.
155 CustomDictionaryEngine custom_dictionary_
;
157 // Remember state for auto spell correct.
158 bool auto_spell_correct_turned_on_
;
160 // Remember state for spellchecking.
161 bool spellcheck_enabled_
;
163 DISALLOW_COPY_AND_ASSIGN(SpellCheck
);
166 #endif // CHROME_RENDERER_SPELLCHECKER_SPELLCHECK_H_