Allow only one bookmark to be added for multiple fast starring
[chromium-blink-merge.git] / chrome / renderer / spellchecker / spellcheck.h
blob85283e9aa7a33a193e8722c85e2853b272cad949
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_
8 #include <set>
9 #include <string>
10 #include <vector>
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;
26 namespace blink {
27 class WebTextCheckingCompletion;
28 struct WebTextCheckingResult;
29 template <typename T> class WebVector;
32 namespace IPC {
33 class Message;
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> {
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.
50 SpellCheck();
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
61 // spellchecking.
62 bool InitializeIfNeeded();
64 // SpellCheck a word.
65 // Returns true if spelled correctly, false otherwise.
66 // If the spellchecker failed to initialize, always returns true.
67 // The |tag| parameter should either be a unique identifier for the document
68 // that the word came from (if the current platform requires it), or 0.
69 // In addition, finds the suggested words for a given word
70 // and puts them into |*optional_suggestions|.
71 // If the word is spelled correctly, the vector is empty.
72 // If optional_suggestions is NULL, suggested words will not be looked up.
73 // Note that Doing suggest lookups can be slow.
74 bool SpellCheckWord(const base::char16* in_word,
75 int in_word_len,
76 int tag,
77 int* misspelling_start,
78 int* misspelling_len,
79 std::vector<base::string16>* optional_suggestions);
81 // SpellCheck a paragraph.
82 // Returns true if |text| is correctly spelled, false otherwise.
83 // If the spellchecker failed to initialize, always returns true.
84 bool SpellCheckParagraph(
85 const base::string16& text,
86 blink::WebVector<blink::WebTextCheckingResult>* results);
88 // Find a possible correctly spelled word for a misspelled word. Computes an
89 // empty string if input misspelled word is too long, there is ambiguity, or
90 // the correct spelling cannot be determined.
91 // NOTE: If using the platform spellchecker, this will send a *lot* of sync
92 // IPCs. We should probably refactor this if we ever plan to take it out from
93 // behind its command line flag.
94 base::string16 GetAutoCorrectionWord(const base::string16& word, int tag);
96 // Requests to spellcheck the specified text in the background. This function
97 // posts a background task and calls SpellCheckParagraph() in the task.
98 #if !defined (OS_MACOSX)
99 void RequestTextChecking(const base::string16& text,
100 blink::WebTextCheckingCompletion* completion);
101 #endif
103 // Creates a list of WebTextCheckingResult objects (used by WebKit) from a
104 // list of SpellCheckResult objects (used by Chrome). This function also
105 // checks misspelled words returned by the Spelling service and changes the
106 // underline colors of contextually-misspelled words.
107 void CreateTextCheckingResults(
108 ResultFilter filter,
109 int line_offset,
110 const base::string16& line_text,
111 const std::vector<SpellCheckResult>& spellcheck_results,
112 blink::WebVector<blink::WebTextCheckingResult>* textcheck_results);
114 bool is_spellcheck_enabled() { return spellcheck_enabled_; }
116 private:
117 friend class SpellCheckTest;
118 FRIEND_TEST_ALL_PREFIXES(SpellCheckTest, GetAutoCorrectionWord_EN_US);
119 FRIEND_TEST_ALL_PREFIXES(SpellCheckTest,
120 RequestSpellCheckMultipleTimesWithoutInitialization);
122 // RenderProcessObserver implementation:
123 bool OnControlMessageReceived(const IPC::Message& message) override;
125 // Message handlers.
126 void OnInit(const std::vector<SpellCheckBDictLanguage>& bdict_languages,
127 const std::set<std::string>& custom_words,
128 bool auto_spell_correct);
129 void OnCustomDictionaryChanged(const std::set<std::string>& words_added,
130 const std::set<std::string>& words_removed);
131 void OnEnableAutoSpellCorrect(bool enable);
132 void OnEnableSpellCheck(bool enable);
133 void OnRequestDocumentMarkers();
135 #if !defined (OS_MACOSX)
136 // Posts delayed spellcheck task and clear it if any.
137 // Takes ownership of |request|.
138 void PostDelayedSpellCheckTask(SpellcheckRequest* request);
140 // Performs spell checking from the request queue.
141 void PerformSpellCheck(SpellcheckRequest* request);
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 scoped_ptr<SpellcheckRequest> pending_request_param_;
149 #endif
151 // A vector of objects used to actually check spelling, one for each enabled
152 // language.
153 ScopedVector<SpellcheckLanguage> languages_;
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_