OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_RENDERER_SPELLCHECKER_SPELLCHECK_H_ | 5 #ifndef CHROME_RENDERER_SPELLCHECKER_SPELLCHECK_H_ |
6 #define CHROME_RENDERER_SPELLCHECKER_SPELLCHECK_H_ | 6 #define CHROME_RENDERER_SPELLCHECKER_SPELLCHECK_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
| 10 #include <queue> |
10 #include <vector> | 11 #include <vector> |
11 | 12 |
12 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/memory/weak_ptr.h" |
14 #include "base/platform_file.h" | 17 #include "base/platform_file.h" |
15 #include "base/string16.h" | 18 #include "base/string16.h" |
16 #include "base/time.h" | 19 #include "base/time.h" |
17 #include "chrome/renderer/spellchecker/spellcheck_worditerator.h" | 20 #include "chrome/renderer/spellchecker/spellcheck_worditerator.h" |
18 #include "content/public/renderer/render_process_observer.h" | 21 #include "content/public/renderer/render_process_observer.h" |
19 #include "ipc/ipc_platform_file.h" | 22 #include "ipc/ipc_platform_file.h" |
| 23 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" |
20 #include "unicode/uscript.h" | 24 #include "unicode/uscript.h" |
21 | 25 |
22 class Hunspell; | 26 class Hunspell; |
23 struct SpellCheckResult; | 27 struct SpellCheckResult; |
24 | 28 |
25 namespace file_util { | 29 namespace file_util { |
26 class MemoryMappedFile; | 30 class MemoryMappedFile; |
27 } | 31 } |
28 | 32 |
| 33 namespace WebKit { |
| 34 class WebTextCheckingCompletion; |
| 35 struct WebTextCheckingResult; |
| 36 } |
| 37 |
| 38 namespace spellcheck { |
| 39 // Converts vector<SpellCheckResult> to WebVector<WebTextCheckingResult> |
| 40 // for WebKit. |
| 41 void ToWebResultList( |
| 42 int offset, |
| 43 const std::vector<SpellCheckResult>& results, |
| 44 WebKit::WebVector<WebKit::WebTextCheckingResult>* web_results); |
| 45 |
| 46 WebKit::WebVector<WebKit::WebTextCheckingResult> ToWebResultList( |
| 47 int offset, |
| 48 const std::vector<SpellCheckResult>& results); |
| 49 } |
| 50 |
29 // TODO(morrita): Needs reorg with SpellCheckProvider. | 51 // TODO(morrita): Needs reorg with SpellCheckProvider. |
30 // See http://crbug.com/73699. | 52 // See http://crbug.com/73699. |
31 class SpellCheck : public content::RenderProcessObserver { | 53 class SpellCheck : public content::RenderProcessObserver, |
| 54 public base::SupportsWeakPtr<SpellCheck> { |
32 public: | 55 public: |
33 SpellCheck(); | 56 SpellCheck(); |
34 virtual ~SpellCheck(); | 57 virtual ~SpellCheck(); |
35 | 58 |
36 void Init(base::PlatformFile file, | 59 void Init(base::PlatformFile file, |
37 const std::vector<std::string>& custom_words, | 60 const std::vector<std::string>& custom_words, |
38 const std::string& language); | 61 const std::string& language); |
39 | 62 |
40 // SpellCheck a word. | 63 // SpellCheck a word. |
41 // Returns true if spelled correctly, false otherwise. | 64 // Returns true if spelled correctly, false otherwise. |
42 // If the spellchecker failed to initialize, always returns true. | 65 // If the spellchecker failed to initialize, always returns true. |
43 // The |tag| parameter should either be a unique identifier for the document | 66 // The |tag| parameter should either be a unique identifier for the document |
44 // that the word came from (if the current platform requires it), or 0. | 67 // that the word came from (if the current platform requires it), or 0. |
45 // In addition, finds the suggested words for a given word | 68 // In addition, finds the suggested words for a given word |
46 // and puts them into |*optional_suggestions|. | 69 // and puts them into |*optional_suggestions|. |
47 // If the word is spelled correctly, the vector is empty. | 70 // If the word is spelled correctly, the vector is empty. |
48 // If optional_suggestions is NULL, suggested words will not be looked up. | 71 // If optional_suggestions is NULL, suggested words will not be looked up. |
49 // Note that Doing suggest lookups can be slow. | 72 // Note that Doing suggest lookups can be slow. |
50 bool SpellCheckWord(const char16* in_word, | 73 bool SpellCheckWord(const char16* in_word, |
51 int in_word_len, | 74 int in_word_len, |
52 int tag, | 75 int tag, |
53 int* misspelling_start, | 76 int* misspelling_start, |
54 int* misspelling_len, | 77 int* misspelling_len, |
55 std::vector<string16>* optional_suggestions); | 78 std::vector<string16>* optional_suggestions); |
56 | 79 |
57 // SpellCheck a paragrpah. | 80 // SpellCheck a paragrpah. |
58 // Returns true if |text| is correctly spelled, false otherwise. | 81 // Returns true if |text| is correctly spelled, false otherwise. |
59 // If the spellchecker failed to initialize, always returns true. | 82 // If the spellchecker failed to initialize, always returns true. |
60 // The |tag| parameter should either be a unique identifier for the document, | |
61 // or 0. | |
62 bool SpellCheckParagraph(const string16& text, | 83 bool SpellCheckParagraph(const string16& text, |
63 int tag, | |
64 std::vector<SpellCheckResult>* results); | 84 std::vector<SpellCheckResult>* results); |
65 | 85 |
66 // Find a possible correctly spelled word for a misspelled word. Computes an | 86 // Find a possible correctly spelled word for a misspelled word. Computes an |
67 // empty string if input misspelled word is too long, there is ambiguity, or | 87 // empty string if input misspelled word is too long, there is ambiguity, or |
68 // the correct spelling cannot be determined. | 88 // the correct spelling cannot be determined. |
69 // NOTE: If using the platform spellchecker, this will send a *lot* of sync | 89 // NOTE: If using the platform spellchecker, this will send a *lot* of sync |
70 // IPCs. We should probably refactor this if we ever plan to take it out from | 90 // IPCs. We should probably refactor this if we ever plan to take it out from |
71 // behind its command line flag. | 91 // behind its command line flag. |
72 string16 GetAutoCorrectionWord(const string16& word, int tag); | 92 string16 GetAutoCorrectionWord(const string16& word, int tag); |
73 | 93 |
| 94 // Requests to spellcheck the specified text in the background. This function |
| 95 // posts a background task and calls SpellCheckParagraph() in the task. |
| 96 void RequestTextChecking(const string16& text, |
| 97 int offset, |
| 98 WebKit::WebTextCheckingCompletion* completion); |
| 99 |
74 // Returns true if the spellchecker delegate checking to a system-provided | 100 // Returns true if the spellchecker delegate checking to a system-provided |
75 // checker on the browser process. | 101 // checker on the browser process. |
76 bool is_using_platform_spelling_engine() const { | 102 bool is_using_platform_spelling_engine() const { |
77 return is_using_platform_spelling_engine_; | 103 return is_using_platform_spelling_engine_; |
78 } | 104 } |
79 | 105 |
80 private: | 106 private: |
81 FRIEND_TEST_ALL_PREFIXES(SpellCheckTest, GetAutoCorrectionWord_EN_US); | 107 FRIEND_TEST_ALL_PREFIXES(SpellCheckTest, GetAutoCorrectionWord_EN_US); |
| 108 FRIEND_TEST_ALL_PREFIXES(SpellCheckTest, |
| 109 RequestSpellCheckMultipleTimesWithoutInitialization); |
| 110 |
| 111 class SpellCheckRequestParam; |
82 | 112 |
83 // RenderProcessObserver implementation: | 113 // RenderProcessObserver implementation: |
84 virtual bool OnControlMessageReceived(const IPC::Message& message) OVERRIDE; | 114 virtual bool OnControlMessageReceived(const IPC::Message& message) OVERRIDE; |
85 | 115 |
86 // Message handlers. | 116 // Message handlers. |
87 void OnInit(IPC::PlatformFileForTransit bdict_file, | 117 void OnInit(IPC::PlatformFileForTransit bdict_file, |
88 const std::vector<std::string>& custom_words, | 118 const std::vector<std::string>& custom_words, |
89 const std::string& language, | 119 const std::string& language, |
90 bool auto_spell_correct); | 120 bool auto_spell_correct); |
91 void OnWordAdded(const std::string& word); | 121 void OnWordAdded(const std::string& word); |
92 void OnEnableAutoSpellCorrect(bool enable); | 122 void OnEnableAutoSpellCorrect(bool enable); |
93 | 123 |
94 // Initializes the Hunspell dictionary, or does nothing if |hunspell_| is | 124 // Initializes the Hunspell dictionary, or does nothing if |hunspell_| is |
95 // non-null. This blocks. | 125 // non-null. This blocks. |
96 void InitializeHunspell(); | 126 void InitializeHunspell(); |
97 | 127 |
98 // If there is no dictionary file, then this requests one from the browser | 128 // If there is no dictionary file, then this requests one from the browser |
99 // and does not block. In this case it returns true. | 129 // and does not block. In this case it returns true. |
100 // If there is a dictionary file, but Hunspell has not been loaded, then | 130 // If there is a dictionary file, but Hunspell has not been loaded, then |
101 // this loads Hunspell. | 131 // this loads Hunspell. |
102 // If Hunspell is already loaded, this does nothing. In both the latter cases | 132 // If Hunspell is already loaded, this does nothing. In both the latter cases |
103 // it returns false, meaning that it is OK to continue spellchecking. | 133 // it returns false, meaning that it is OK to continue spellchecking. |
104 bool InitializeIfNeeded(); | 134 bool InitializeIfNeeded(); |
105 | 135 |
106 // When called, relays the request to check the spelling to the proper | 136 // When called, relays the request to check the spelling to the proper |
107 // backend, either hunspell or a platform-specific backend. | 137 // backend, either hunspell or a platform-specific backend. |
108 bool CheckSpelling(const string16& word_to_check, int tag); | 138 bool CheckSpelling(const string16& word_to_check, int tag); |
109 | 139 |
| 140 // Posts delayed spellcheck task and clear it if any. |
| 141 void PostDelayedSpellCheckTask(); |
| 142 |
| 143 // Performs spell checking from the request queue. |
| 144 void PerformSpellCheck(); |
| 145 |
110 // When called, relays the request to fill the list with suggestions to | 146 // When called, relays the request to fill the list with suggestions to |
111 // the proper backend, either hunspell or a platform-specific backend. | 147 // the proper backend, either hunspell or a platform-specific backend. |
112 void FillSuggestionList(const string16& wrong_word, | 148 void FillSuggestionList(const string16& wrong_word, |
113 std::vector<string16>* optional_suggestions); | 149 std::vector<string16>* optional_suggestions); |
114 | 150 |
115 // Returns whether or not the given word is a contraction of valid words | 151 // Returns whether or not the given word is a contraction of valid words |
116 // (e.g. "word:word"). | 152 // (e.g. "word:word"). |
117 bool IsValidContraction(const string16& word, int tag); | 153 bool IsValidContraction(const string16& word, int tag); |
118 | 154 |
119 // Add the given custom word to |hunspell_|. | 155 // Add the given custom word to |hunspell_|. |
(...skipping 20 matching lines...) Expand all Loading... |
140 SpellcheckWordIterator text_iterator_; | 176 SpellcheckWordIterator text_iterator_; |
141 SpellcheckWordIterator contraction_iterator_; | 177 SpellcheckWordIterator contraction_iterator_; |
142 | 178 |
143 // Remember state for auto spell correct. | 179 // Remember state for auto spell correct. |
144 bool auto_spell_correct_turned_on_; | 180 bool auto_spell_correct_turned_on_; |
145 | 181 |
146 // True if a platform-specific spellchecking engine is being used, | 182 // True if a platform-specific spellchecking engine is being used, |
147 // and False if hunspell is being used. | 183 // and False if hunspell is being used. |
148 bool is_using_platform_spelling_engine_; | 184 bool is_using_platform_spelling_engine_; |
149 | 185 |
150 // This flags whether we have ever been initialized, or have asked the browser | 186 // This flags is true if we have been intialized. |
151 // for a dictionary. The value indicates whether we should request a | 187 // The value indicates whether we should request a |
152 // dictionary from the browser when the render view asks us to check the | 188 // dictionary from the browser when the render view asks us to check the |
153 // spelling of a word. | 189 // spelling of a word. |
154 bool initialized_; | 190 bool initialized_; |
155 | 191 |
| 192 // This flags is true if we have requested dictionary. |
| 193 bool dictionary_requested_; |
| 194 |
| 195 // The parameters of a pending background-spellchecking request. When WebKit |
| 196 // sends a background-spellchecking request before initializing hunspell, |
| 197 // we save its parameters and start spellchecking after we finish initializing |
| 198 // hunspell. (When WebKit sends two or more requests, we cancel the previous |
| 199 // requests so we do not have to use vectors.) |
| 200 scoped_refptr<SpellCheckRequestParam> pending_request_param_; |
| 201 |
| 202 // The set of params already requested. When finishing the request, the params |
| 203 // should be removed from the set. |
| 204 std::queue<scoped_refptr<SpellCheckRequestParam> > requested_params_; |
| 205 |
156 DISALLOW_COPY_AND_ASSIGN(SpellCheck); | 206 DISALLOW_COPY_AND_ASSIGN(SpellCheck); |
157 }; | 207 }; |
158 | 208 |
159 #endif // CHROME_RENDERER_SPELLCHECKER_SPELLCHECK_H_ | 209 #endif // CHROME_RENDERER_SPELLCHECKER_SPELLCHECK_H_ |
OLD | NEW |