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 #include "components/spellcheck/renderer/spellcheck.h" | 5 #include "components/spellcheck/renderer/spellcheck.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
14 #include "base/location.h" | 14 #include "base/location.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
18 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
19 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
20 #include "build/build_config.h" | 20 #include "build/build_config.h" |
21 #include "components/spellcheck/common/spellcheck_common.h" | 21 #include "components/spellcheck/common/spellcheck_common.h" |
22 #include "components/spellcheck/common/spellcheck_features.h" | 22 #include "components/spellcheck/common/spellcheck_features.h" |
23 #include "components/spellcheck/common/spellcheck_messages.h" | |
24 #include "components/spellcheck/common/spellcheck_result.h" | 23 #include "components/spellcheck/common/spellcheck_result.h" |
25 #include "components/spellcheck/common/spellcheck_switches.h" | 24 #include "components/spellcheck/common/spellcheck_switches.h" |
26 #include "components/spellcheck/renderer/spellcheck_language.h" | 25 #include "components/spellcheck/renderer/spellcheck_language.h" |
27 #include "components/spellcheck/renderer/spellcheck_provider.h" | 26 #include "components/spellcheck/renderer/spellcheck_provider.h" |
28 #include "components/spellcheck/spellcheck_build_features.h" | 27 #include "components/spellcheck/spellcheck_build_features.h" |
| 28 #include "content/public/common/service_manager_connection.h" |
| 29 #include "content/public/common/simple_connection_filter.h" |
29 #include "content/public/renderer/render_frame.h" | 30 #include "content/public/renderer/render_frame.h" |
30 #include "content/public/renderer/render_frame_visitor.h" | 31 #include "content/public/renderer/render_frame_visitor.h" |
31 #include "content/public/renderer/render_thread.h" | 32 #include "content/public/renderer/render_thread.h" |
32 #include "ipc/ipc_platform_file.h" | 33 #include "services/service_manager/public/cpp/bind_source_info.h" |
| 34 #include "services/service_manager/public/cpp/binder_registry.h" |
33 #include "third_party/WebKit/public/platform/WebString.h" | 35 #include "third_party/WebKit/public/platform/WebString.h" |
34 #include "third_party/WebKit/public/platform/WebVector.h" | 36 #include "third_party/WebKit/public/platform/WebVector.h" |
35 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 37 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
36 #include "third_party/WebKit/public/web/WebTextCheckingCompletion.h" | 38 #include "third_party/WebKit/public/web/WebTextCheckingCompletion.h" |
37 #include "third_party/WebKit/public/web/WebTextCheckingResult.h" | 39 #include "third_party/WebKit/public/web/WebTextCheckingResult.h" |
38 #include "third_party/WebKit/public/web/WebTextDecorationType.h" | 40 #include "third_party/WebKit/public/web/WebTextDecorationType.h" |
39 | 41 |
40 using blink::WebVector; | 42 using blink::WebVector; |
41 using blink::WebString; | 43 using blink::WebString; |
42 using blink::WebTextCheckingResult; | 44 using blink::WebTextCheckingResult; |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 // spellchecking. After the first word is typed, the provider requests a check, | 144 // spellchecking. After the first word is typed, the provider requests a check, |
143 // which in turn triggers the delayed initialization sequence in SpellCheck. | 145 // which in turn triggers the delayed initialization sequence in SpellCheck. |
144 // This does send a message to the browser side, which triggers the creation | 146 // This does send a message to the browser side, which triggers the creation |
145 // of the SpellcheckService. That does create the observer for the preference | 147 // of the SpellcheckService. That does create the observer for the preference |
146 // responsible for enabling/disabling checking, which allows subsequent changes | 148 // responsible for enabling/disabling checking, which allows subsequent changes |
147 // to that preference to be sent to all SpellCheckProviders. | 149 // to that preference to be sent to all SpellCheckProviders. |
148 // Setting |spellcheck_enabled_| to false by default prevents that mechanism, | 150 // Setting |spellcheck_enabled_| to false by default prevents that mechanism, |
149 // and as such the SpellCheckProviders will never be notified of different | 151 // and as such the SpellCheckProviders will never be notified of different |
150 // values. | 152 // values. |
151 // TODO(groby): Simplify this. | 153 // TODO(groby): Simplify this. |
152 SpellCheck::SpellCheck() : spellcheck_enabled_(true) {} | 154 SpellCheck::SpellCheck() : spellcheck_enabled_(true) { |
| 155 if (!content::ChildThread::Get()) |
| 156 return; // Can be NULL in tests. |
| 157 |
| 158 auto* service_manager_connection = |
| 159 content::ChildThread::Get()->GetServiceManagerConnection(); |
| 160 DCHECK(service_manager_connection); |
| 161 |
| 162 auto registry = base::MakeUnique<service_manager::BinderRegistry>(); |
| 163 registry->AddInterface( |
| 164 base::Bind(&SpellCheck::SpellCheckerRequest, base::Unretained(this)), |
| 165 base::ThreadTaskRunnerHandle::Get()); |
| 166 |
| 167 service_manager_connection->AddConnectionFilter( |
| 168 base::MakeUnique<content::SimpleConnectionFilter>(std::move(registry))); |
| 169 } |
153 | 170 |
154 SpellCheck::~SpellCheck() { | 171 SpellCheck::~SpellCheck() { |
155 } | 172 } |
156 | 173 |
157 void SpellCheck::FillSuggestions( | 174 void SpellCheck::FillSuggestions( |
158 const std::vector<std::vector<base::string16>>& suggestions_list, | 175 const std::vector<std::vector<base::string16>>& suggestions_list, |
159 std::vector<base::string16>* optional_suggestions) { | 176 std::vector<base::string16>* optional_suggestions) { |
160 DCHECK(optional_suggestions); | 177 DCHECK(optional_suggestions); |
161 size_t num_languages = suggestions_list.size(); | 178 size_t num_languages = suggestions_list.size(); |
162 | 179 |
(...skipping 13 matching lines...) Expand all Loading... |
176 // Only add the suggestion if it's unique. | 193 // Only add the suggestion if it's unique. |
177 if (!base::ContainsValue(*optional_suggestions, suggestion)) { | 194 if (!base::ContainsValue(*optional_suggestions, suggestion)) { |
178 optional_suggestions->push_back(suggestion); | 195 optional_suggestions->push_back(suggestion); |
179 } | 196 } |
180 if (optional_suggestions->size() >= spellcheck::kMaxSuggestions) { | 197 if (optional_suggestions->size() >= spellcheck::kMaxSuggestions) { |
181 break; | 198 break; |
182 } | 199 } |
183 } | 200 } |
184 } | 201 } |
185 | 202 |
186 bool SpellCheck::OnControlMessageReceived(const IPC::Message& message) { | 203 void SpellCheck::SpellCheckerRequest( |
187 bool handled = true; | 204 const service_manager::BindSourceInfo& source_info, |
188 IPC_BEGIN_MESSAGE_MAP(SpellCheck, message) | 205 spellcheck::mojom::SpellCheckerRequest request) { |
189 IPC_MESSAGE_HANDLER(SpellCheckMsg_Init, OnInit) | 206 spellchecker_bindings_.AddBinding(this, std::move(request)); |
190 IPC_MESSAGE_HANDLER(SpellCheckMsg_CustomDictionaryChanged, | |
191 OnCustomDictionaryChanged) | |
192 IPC_MESSAGE_HANDLER(SpellCheckMsg_EnableSpellCheck, OnEnableSpellCheck) | |
193 IPC_MESSAGE_UNHANDLED(handled = false) | |
194 IPC_END_MESSAGE_MAP() | |
195 | |
196 return handled; | |
197 } | 207 } |
198 | 208 |
199 void SpellCheck::OnInit( | 209 void SpellCheck::Initialize( |
200 const std::vector<SpellCheckBDictLanguage>& bdict_languages, | 210 std::vector<spellcheck::mojom::SpellCheckBDictLanguagePtr> dictionaries, |
201 const std::set<std::string>& custom_words) { | 211 const std::vector<std::string>& custom_words, |
| 212 bool enable) { |
202 languages_.clear(); | 213 languages_.clear(); |
203 for (const auto& bdict_language : bdict_languages) { | |
204 AddSpellcheckLanguage( | |
205 IPC::PlatformFileForTransitToFile(bdict_language.file), | |
206 bdict_language.language); | |
207 } | |
208 | 214 |
209 custom_dictionary_.Init(custom_words); | 215 for (const auto& dictionary : dictionaries) |
| 216 AddSpellcheckLanguage(std::move(dictionary->file), dictionary->language); |
| 217 |
| 218 custom_dictionary_.Init( |
| 219 std::set<std::string>(custom_words.begin(), custom_words.end())); |
210 #if !BUILDFLAG(USE_BROWSER_SPELLCHECKER) | 220 #if !BUILDFLAG(USE_BROWSER_SPELLCHECKER) |
211 PostDelayedSpellCheckTask(pending_request_param_.release()); | 221 PostDelayedSpellCheckTask(pending_request_param_.release()); |
212 #endif | 222 #endif |
213 } | |
214 | 223 |
215 void SpellCheck::OnCustomDictionaryChanged( | |
216 const std::set<std::string>& words_added, | |
217 const std::set<std::string>& words_removed) { | |
218 custom_dictionary_.OnCustomDictionaryChanged(words_added, words_removed); | |
219 if (words_added.empty()) | |
220 return; | |
221 DocumentMarkersRemover markersRemover(words_added); | |
222 content::RenderFrame::ForEach(&markersRemover); | |
223 } | |
224 | |
225 void SpellCheck::OnEnableSpellCheck(bool enable) { | |
226 spellcheck_enabled_ = enable; | 224 spellcheck_enabled_ = enable; |
227 UpdateSpellcheckEnabled updater(enable); | 225 UpdateSpellcheckEnabled updater(enable); |
228 content::RenderFrame::ForEach(&updater); | 226 content::RenderFrame::ForEach(&updater); |
229 } | 227 } |
230 | 228 |
| 229 void SpellCheck::CustomDictionaryChanged( |
| 230 const std::vector<std::string>& words_added, |
| 231 const std::vector<std::string>& words_removed) { |
| 232 const std::set<std::string> added(words_added.begin(), words_added.end()); |
| 233 |
| 234 custom_dictionary_.OnCustomDictionaryChanged( |
| 235 added, std::set<std::string>(words_removed.begin(), words_removed.end())); |
| 236 if (added.empty()) |
| 237 return; |
| 238 |
| 239 DocumentMarkersRemover markersRemover(added); |
| 240 content::RenderFrame::ForEach(&markersRemover); |
| 241 } |
| 242 |
231 // TODO(groby): Make sure we always have a spelling engine, even before | 243 // TODO(groby): Make sure we always have a spelling engine, even before |
232 // AddSpellcheckLanguage() is called. | 244 // AddSpellcheckLanguage() is called. |
233 void SpellCheck::AddSpellcheckLanguage(base::File file, | 245 void SpellCheck::AddSpellcheckLanguage(base::File file, |
234 const std::string& language) { | 246 const std::string& language) { |
235 languages_.push_back(base::MakeUnique<SpellcheckLanguage>()); | 247 languages_.push_back(base::MakeUnique<SpellcheckLanguage>()); |
236 languages_.back()->Init(std::move(file), language); | 248 languages_.back()->Init(std::move(file), language); |
237 } | 249 } |
238 | 250 |
239 bool SpellCheck::SpellCheckWord( | 251 bool SpellCheck::SpellCheckWord( |
240 const base::char16* text_begin, | 252 const base::char16* text_begin, |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
500 | 512 |
501 textcheck_results->Assign(results); | 513 textcheck_results->Assign(results); |
502 } | 514 } |
503 | 515 |
504 bool SpellCheck::IsSpellcheckEnabled() { | 516 bool SpellCheck::IsSpellcheckEnabled() { |
505 #if defined(OS_ANDROID) | 517 #if defined(OS_ANDROID) |
506 if (!spellcheck::IsAndroidSpellCheckFeatureEnabled()) return false; | 518 if (!spellcheck::IsAndroidSpellCheckFeatureEnabled()) return false; |
507 #endif | 519 #endif |
508 return spellcheck_enabled_; | 520 return spellcheck_enabled_; |
509 } | 521 } |
OLD | NEW |