Add a function to create a bookmark app from a WebApplicationInfo.
[chromium-blink-merge.git] / chrome / renderer / spellchecker / spellcheck.h
blob7c74bdb663b9e3a2d823aea22b61fc70dc9b25c3
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 <string>
9 #include <vector>
11 #include "base/files/file.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/strings/string16.h"
16 #include "chrome/renderer/spellchecker/custom_dictionary_engine.h"
17 #include "chrome/renderer/spellchecker/spellcheck_language.h"
18 #include "content/public/renderer/render_process_observer.h"
19 #include "ipc/ipc_platform_file.h"
20 #include "third_party/WebKit/public/platform/WebVector.h"
22 struct SpellCheckResult;
24 namespace blink {
25 class WebTextCheckingCompletion;
26 struct WebTextCheckingResult;
29 // TODO(morrita): Needs reorg with SpellCheckProvider.
30 // See http://crbug.com/73699.
31 // Shared spellchecking logic/data for a RenderProcess. All RenderViews use
32 // this object to perform spellchecking tasks.
33 class SpellCheck : public content::RenderProcessObserver,
34 public base::SupportsWeakPtr<SpellCheck> {
35 public:
36 // TODO(groby): I wonder if this can be private, non-mac only.
37 class SpellcheckRequest;
38 enum ResultFilter {
39 DO_NOT_MODIFY = 1, // Do not modify results.
40 USE_NATIVE_CHECKER, // Use native checker to double-check.
43 SpellCheck();
44 virtual ~SpellCheck();
46 // TODO: Try to move that all to SpellcheckLanguage.
47 void Init(base::File file,
48 const std::set<std::string>& custom_words,
49 const std::string& language);
51 // If there is no dictionary file, then this requests one from the browser
52 // and does not block. In this case it returns true.
53 // If there is a dictionary file, but Hunspell has not been loaded, then
54 // this loads Hunspell.
55 // If Hunspell is already loaded, this does nothing. In both the latter cases
56 // it returns false, meaning that it is OK to continue spellchecking.
57 bool InitializeIfNeeded();
59 // SpellCheck a word.
60 // Returns true if spelled correctly, false otherwise.
61 // If the spellchecker failed to initialize, always returns true.
62 // The |tag| parameter should either be a unique identifier for the document
63 // that the word came from (if the current platform requires it), or 0.
64 // In addition, finds the suggested words for a given word
65 // and puts them into |*optional_suggestions|.
66 // If the word is spelled correctly, the vector is empty.
67 // If optional_suggestions is NULL, suggested words will not be looked up.
68 // Note that Doing suggest lookups can be slow.
69 bool SpellCheckWord(const base::char16* in_word,
70 int in_word_len,
71 int tag,
72 int* misspelling_start,
73 int* misspelling_len,
74 std::vector<base::string16>* optional_suggestions);
76 // SpellCheck a paragraph.
77 // Returns true if |text| is correctly spelled, false otherwise.
78 // If the spellchecker failed to initialize, always returns true.
79 bool SpellCheckParagraph(
80 const base::string16& text,
81 blink::WebVector<blink::WebTextCheckingResult>* results);
83 // Find a possible correctly spelled word for a misspelled word. Computes an
84 // empty string if input misspelled word is too long, there is ambiguity, or
85 // the correct spelling cannot be determined.
86 // NOTE: If using the platform spellchecker, this will send a *lot* of sync
87 // IPCs. We should probably refactor this if we ever plan to take it out from
88 // behind its command line flag.
89 base::string16 GetAutoCorrectionWord(const base::string16& word, int tag);
91 // Requests to spellcheck the specified text in the background. This function
92 // posts a background task and calls SpellCheckParagraph() in the task.
93 #if !defined (OS_MACOSX)
94 void RequestTextChecking(const base::string16& text,
95 blink::WebTextCheckingCompletion* completion);
96 #endif
98 // Creates a list of WebTextCheckingResult objects (used by WebKit) from a
99 // list of SpellCheckResult objects (used by Chrome). This function also
100 // checks misspelled words returned by the Spelling service and changes the
101 // underline colors of contextually-misspelled words.
102 void CreateTextCheckingResults(
103 ResultFilter filter,
104 int line_offset,
105 const base::string16& line_text,
106 const std::vector<SpellCheckResult>& spellcheck_results,
107 blink::WebVector<blink::WebTextCheckingResult>* textcheck_results);
109 bool is_spellcheck_enabled() { return spellcheck_enabled_; }
111 private:
112 friend class SpellCheckTest;
113 FRIEND_TEST_ALL_PREFIXES(SpellCheckTest, GetAutoCorrectionWord_EN_US);
114 FRIEND_TEST_ALL_PREFIXES(SpellCheckTest,
115 RequestSpellCheckMultipleTimesWithoutInitialization);
117 // RenderProcessObserver implementation:
118 virtual bool OnControlMessageReceived(const IPC::Message& message) OVERRIDE;
120 // Message handlers.
121 void OnInit(IPC::PlatformFileForTransit bdict_file,
122 const std::set<std::string>& custom_words,
123 const std::string& language,
124 bool auto_spell_correct);
125 void OnCustomDictionaryChanged(
126 const std::vector<std::string>& words_added,
127 const std::vector<std::string>& words_removed);
128 void OnEnableAutoSpellCorrect(bool enable);
129 void OnEnableSpellCheck(bool enable);
130 void OnRequestDocumentMarkers();
132 #if !defined (OS_MACOSX)
133 // Posts delayed spellcheck task and clear it if any.
134 // Takes ownership of |request|.
135 void PostDelayedSpellCheckTask(SpellcheckRequest* request);
137 // Performs spell checking from the request queue.
138 void PerformSpellCheck(SpellcheckRequest* request);
140 // The parameters of a pending background-spellchecking request. When WebKit
141 // sends a background-spellchecking request before initializing hunspell,
142 // we save its parameters and start spellchecking after we finish initializing
143 // hunspell. (When WebKit sends two or more requests, we cancel the previous
144 // requests so we do not have to use vectors.)
145 scoped_ptr<SpellcheckRequest> pending_request_param_;
146 #endif
148 SpellcheckLanguage spellcheck_; // Language-specific spellchecking code.
150 // Custom dictionary spelling engine.
151 CustomDictionaryEngine custom_dictionary_;
153 // Remember state for auto spell correct.
154 bool auto_spell_correct_turned_on_;
156 // Remember state for spellchecking.
157 bool spellcheck_enabled_;
159 DISALLOW_COPY_AND_ASSIGN(SpellCheck);
162 #endif // CHROME_RENDERER_SPELLCHECKER_SPELLCHECK_H_