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/weak_ptr.h"
17 #include "base/strings/string16.h"
18 #include "chrome/renderer/spellchecker/custom_dictionary_engine.h"
19 #include "chrome/renderer/spellchecker/spellcheck_language.h"
20 #include "content/public/renderer/render_process_observer.h"
21 #include "ipc/ipc_platform_file.h"
23 struct SpellCheckResult
;
26 class WebTextCheckingCompletion
;
27 struct WebTextCheckingResult
;
28 template <typename T
> class WebVector
;
35 // TODO(morrita): Needs reorg with SpellCheckProvider.
36 // See http://crbug.com/73699.
37 // Shared spellchecking logic/data for a RenderProcess. All RenderViews use
38 // this object to perform spellchecking tasks.
39 class SpellCheck
: public content::RenderProcessObserver
,
40 public base::SupportsWeakPtr
<SpellCheck
> {
42 // TODO(groby): I wonder if this can be private, non-mac only.
43 class SpellcheckRequest
;
45 DO_NOT_MODIFY
= 1, // Do not modify results.
46 USE_NATIVE_CHECKER
, // Use native checker to double-check.
50 ~SpellCheck() override
;
52 // TODO: Try to move that all to SpellcheckLanguage.
53 void Init(base::File file
,
54 const std::set
<std::string
>& custom_words
,
55 const std::string
& language
);
57 // If there is no dictionary file, then this requests one from the browser
58 // and does not block. In this case it returns true.
59 // If there is a dictionary file, but Hunspell has not been loaded, then
60 // this loads Hunspell.
61 // If Hunspell is already loaded, this does nothing. In both the latter cases
62 // it returns false, meaning that it is OK to continue spellchecking.
63 bool InitializeIfNeeded();
66 // Returns true if spelled correctly, false otherwise.
67 // If the spellchecker 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
* in_word
,
78 int* misspelling_start
,
80 std::vector
<base::string16
>* optional_suggestions
);
82 // SpellCheck a paragraph.
83 // Returns true if |text| is correctly spelled, false otherwise.
84 // If the spellchecker failed to initialize, always returns true.
85 bool SpellCheckParagraph(
86 const base::string16
& text
,
87 blink::WebVector
<blink::WebTextCheckingResult
>* results
);
89 // Find a possible correctly spelled word for a misspelled word. Computes an
90 // empty string if input misspelled word is too long, there is ambiguity, or
91 // the correct spelling cannot be determined.
92 // NOTE: If using the platform spellchecker, this will send a *lot* of sync
93 // IPCs. We should probably refactor this if we ever plan to take it out from
94 // behind its command line flag.
95 base::string16
GetAutoCorrectionWord(const base::string16
& word
, int tag
);
97 // Requests to spellcheck the specified text in the background. This function
98 // posts a background task and calls SpellCheckParagraph() in the task.
99 #if !defined (OS_MACOSX)
100 void RequestTextChecking(const base::string16
& text
,
101 blink::WebTextCheckingCompletion
* completion
);
104 // Creates a list of WebTextCheckingResult objects (used by WebKit) from a
105 // list of SpellCheckResult objects (used by Chrome). This function also
106 // checks misspelled words returned by the Spelling service and changes the
107 // underline colors of contextually-misspelled words.
108 void CreateTextCheckingResults(
111 const base::string16
& line_text
,
112 const std::vector
<SpellCheckResult
>& spellcheck_results
,
113 blink::WebVector
<blink::WebTextCheckingResult
>* textcheck_results
);
115 bool is_spellcheck_enabled() { return spellcheck_enabled_
; }
118 friend class SpellCheckTest
;
119 FRIEND_TEST_ALL_PREFIXES(SpellCheckTest
, GetAutoCorrectionWord_EN_US
);
120 FRIEND_TEST_ALL_PREFIXES(SpellCheckTest
,
121 RequestSpellCheckMultipleTimesWithoutInitialization
);
123 // RenderProcessObserver implementation:
124 bool OnControlMessageReceived(const IPC::Message
& message
) override
;
127 void OnInit(IPC::PlatformFileForTransit bdict_file
,
128 const std::set
<std::string
>& custom_words
,
129 const std::string
& language
,
130 bool auto_spell_correct
);
131 void OnCustomDictionaryChanged(const std::set
<std::string
>& words_added
,
132 const std::set
<std::string
>& words_removed
);
133 void OnEnableAutoSpellCorrect(bool enable
);
134 void OnEnableSpellCheck(bool enable
);
135 void OnRequestDocumentMarkers();
137 #if !defined (OS_MACOSX)
138 // Posts delayed spellcheck task and clear it if any.
139 // Takes ownership of |request|.
140 void PostDelayedSpellCheckTask(SpellcheckRequest
* request
);
142 // Performs spell checking from the request queue.
143 void PerformSpellCheck(SpellcheckRequest
* request
);
145 // The parameters of a pending background-spellchecking request. When WebKit
146 // sends a background-spellchecking request before initializing hunspell,
147 // we save its parameters and start spellchecking after we finish initializing
148 // hunspell. (When WebKit sends two or more requests, we cancel the previous
149 // requests so we do not have to use vectors.)
150 scoped_ptr
<SpellcheckRequest
> pending_request_param_
;
153 SpellcheckLanguage spellcheck_
; // Language-specific spellchecking code.
155 // Custom dictionary spelling engine.
156 CustomDictionaryEngine custom_dictionary_
;
158 // Remember state for auto spell correct.
159 bool auto_spell_correct_turned_on_
;
161 // Remember state for spellchecking.
162 bool spellcheck_enabled_
;
164 DISALLOW_COPY_AND_ASSIGN(SpellCheck
);
167 #endif // CHROME_RENDERER_SPELLCHECKER_SPELLCHECK_H_