| 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 "chrome/renderer/spellchecker/spellcheck.h" | 5 #include "chrome/renderer/spellchecker/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> |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 DISALLOW_COPY_AND_ASSIGN(UpdateSpellcheckEnabled); | 55 DISALLOW_COPY_AND_ASSIGN(UpdateSpellcheckEnabled); |
| 56 }; | 56 }; |
| 57 | 57 |
| 58 bool UpdateSpellcheckEnabled::Visit(content::RenderView* render_view) { | 58 bool UpdateSpellcheckEnabled::Visit(content::RenderView* render_view) { |
| 59 SpellCheckProvider* provider = SpellCheckProvider::Get(render_view); | 59 SpellCheckProvider* provider = SpellCheckProvider::Get(render_view); |
| 60 DCHECK(provider); | 60 DCHECK(provider); |
| 61 provider->EnableSpellcheck(enabled_); | 61 provider->EnableSpellcheck(enabled_); |
| 62 return true; | 62 return true; |
| 63 } | 63 } |
| 64 | 64 |
| 65 class DocumentMarkersCollector : public content::RenderViewVisitor { | |
| 66 public: | |
| 67 DocumentMarkersCollector() {} | |
| 68 ~DocumentMarkersCollector() override {} | |
| 69 const std::vector<uint32_t>& markers() const { return markers_; } | |
| 70 bool Visit(content::RenderView* render_view) override; | |
| 71 | |
| 72 private: | |
| 73 std::vector<uint32_t> markers_; | |
| 74 DISALLOW_COPY_AND_ASSIGN(DocumentMarkersCollector); | |
| 75 }; | |
| 76 | |
| 77 bool DocumentMarkersCollector::Visit(content::RenderView* render_view) { | |
| 78 if (!render_view || !render_view->GetWebView()) | |
| 79 return true; | |
| 80 WebVector<uint32_t> markers; | |
| 81 render_view->GetWebView()->spellingMarkers(&markers); | |
| 82 for (size_t i = 0; i < markers.size(); ++i) | |
| 83 markers_.push_back(markers[i]); | |
| 84 // Visit all render views. | |
| 85 return true; | |
| 86 } | |
| 87 | |
| 88 class DocumentMarkersRemover : public content::RenderViewVisitor { | 65 class DocumentMarkersRemover : public content::RenderViewVisitor { |
| 89 public: | 66 public: |
| 90 explicit DocumentMarkersRemover(const std::set<std::string>& words); | 67 explicit DocumentMarkersRemover(const std::set<std::string>& words); |
| 91 ~DocumentMarkersRemover() override {} | 68 ~DocumentMarkersRemover() override {} |
| 92 bool Visit(content::RenderView* render_view) override; | 69 bool Visit(content::RenderView* render_view) override; |
| 93 | 70 |
| 94 private: | 71 private: |
| 95 WebVector<WebString> words_; | 72 WebVector<WebString> words_; |
| 96 DISALLOW_COPY_AND_ASSIGN(DocumentMarkersRemover); | 73 DISALLOW_COPY_AND_ASSIGN(DocumentMarkersRemover); |
| 97 }; | 74 }; |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 } | 181 } |
| 205 } | 182 } |
| 206 | 183 |
| 207 bool SpellCheck::OnControlMessageReceived(const IPC::Message& message) { | 184 bool SpellCheck::OnControlMessageReceived(const IPC::Message& message) { |
| 208 bool handled = true; | 185 bool handled = true; |
| 209 IPC_BEGIN_MESSAGE_MAP(SpellCheck, message) | 186 IPC_BEGIN_MESSAGE_MAP(SpellCheck, message) |
| 210 IPC_MESSAGE_HANDLER(SpellCheckMsg_Init, OnInit) | 187 IPC_MESSAGE_HANDLER(SpellCheckMsg_Init, OnInit) |
| 211 IPC_MESSAGE_HANDLER(SpellCheckMsg_CustomDictionaryChanged, | 188 IPC_MESSAGE_HANDLER(SpellCheckMsg_CustomDictionaryChanged, |
| 212 OnCustomDictionaryChanged) | 189 OnCustomDictionaryChanged) |
| 213 IPC_MESSAGE_HANDLER(SpellCheckMsg_EnableSpellCheck, OnEnableSpellCheck) | 190 IPC_MESSAGE_HANDLER(SpellCheckMsg_EnableSpellCheck, OnEnableSpellCheck) |
| 214 IPC_MESSAGE_HANDLER(SpellCheckMsg_RequestDocumentMarkers, | |
| 215 OnRequestDocumentMarkers) | |
| 216 IPC_MESSAGE_UNHANDLED(handled = false) | 191 IPC_MESSAGE_UNHANDLED(handled = false) |
| 217 IPC_END_MESSAGE_MAP() | 192 IPC_END_MESSAGE_MAP() |
| 218 | 193 |
| 219 return handled; | 194 return handled; |
| 220 } | 195 } |
| 221 | 196 |
| 222 void SpellCheck::OnInit( | 197 void SpellCheck::OnInit( |
| 223 const std::vector<SpellCheckBDictLanguage>& bdict_languages, | 198 const std::vector<SpellCheckBDictLanguage>& bdict_languages, |
| 224 const std::set<std::string>& custom_words) { | 199 const std::set<std::string>& custom_words) { |
| 225 languages_.clear(); | 200 languages_.clear(); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 244 DocumentMarkersRemover markersRemover(words_added); | 219 DocumentMarkersRemover markersRemover(words_added); |
| 245 content::RenderView::ForEach(&markersRemover); | 220 content::RenderView::ForEach(&markersRemover); |
| 246 } | 221 } |
| 247 | 222 |
| 248 void SpellCheck::OnEnableSpellCheck(bool enable) { | 223 void SpellCheck::OnEnableSpellCheck(bool enable) { |
| 249 spellcheck_enabled_ = enable; | 224 spellcheck_enabled_ = enable; |
| 250 UpdateSpellcheckEnabled updater(enable); | 225 UpdateSpellcheckEnabled updater(enable); |
| 251 content::RenderView::ForEach(&updater); | 226 content::RenderView::ForEach(&updater); |
| 252 } | 227 } |
| 253 | 228 |
| 254 void SpellCheck::OnRequestDocumentMarkers() { | |
| 255 DocumentMarkersCollector collector; | |
| 256 content::RenderView::ForEach(&collector); | |
| 257 content::RenderThread::Get()->Send( | |
| 258 new SpellCheckHostMsg_RespondDocumentMarkers(collector.markers())); | |
| 259 } | |
| 260 | |
| 261 // TODO(groby): Make sure we always have a spelling engine, even before | 229 // TODO(groby): Make sure we always have a spelling engine, even before |
| 262 // AddSpellcheckLanguage() is called. | 230 // AddSpellcheckLanguage() is called. |
| 263 void SpellCheck::AddSpellcheckLanguage(base::File file, | 231 void SpellCheck::AddSpellcheckLanguage(base::File file, |
| 264 const std::string& language) { | 232 const std::string& language) { |
| 265 languages_.push_back(new SpellcheckLanguage()); | 233 languages_.push_back(new SpellcheckLanguage()); |
| 266 languages_.back()->Init(std::move(file), language); | 234 languages_.back()->Init(std::move(file), language); |
| 267 } | 235 } |
| 268 | 236 |
| 269 bool SpellCheck::SpellCheckWord( | 237 bool SpellCheck::SpellCheckWord( |
| 270 const base::char16* text_begin, | 238 const base::char16* text_begin, |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 523 misspelled_word.length(), kNoTag, | 491 misspelled_word.length(), kNoTag, |
| 524 &unused_misspelling_start, &unused_misspelling_length, | 492 &unused_misspelling_start, &unused_misspelling_length, |
| 525 nullptr)) { | 493 nullptr)) { |
| 526 decoration = SpellCheckResult::GRAMMAR; | 494 decoration = SpellCheckResult::GRAMMAR; |
| 527 } | 495 } |
| 528 } | 496 } |
| 529 | 497 |
| 530 results.push_back(WebTextCheckingResult( | 498 results.push_back(WebTextCheckingResult( |
| 531 static_cast<WebTextDecorationType>(decoration), | 499 static_cast<WebTextDecorationType>(decoration), |
| 532 line_offset + spellcheck_result.location, spellcheck_result.length, | 500 line_offset + spellcheck_result.location, spellcheck_result.length, |
| 533 replacement, spellcheck_result.hash)); | 501 replacement)); |
| 534 } | 502 } |
| 535 | 503 |
| 536 textcheck_results->assign(results); | 504 textcheck_results->assign(results); |
| 537 } | 505 } |
| 538 | 506 |
| 539 bool SpellCheck::IsSpellcheckEnabled() { | 507 bool SpellCheck::IsSpellcheckEnabled() { |
| 540 #if defined(OS_ANDROID) | 508 #if defined(OS_ANDROID) |
| 541 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( | 509 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 542 spellcheck::switches::kEnableAndroidSpellChecker)) { | 510 spellcheck::switches::kEnableAndroidSpellChecker)) { |
| 543 return false; | 511 return false; |
| 544 } | 512 } |
| 545 #endif | 513 #endif |
| 546 return spellcheck_enabled_; | 514 return spellcheck_enabled_; |
| 547 } | 515 } |
| OLD | NEW |