| 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" | 23 #include "components/spellcheck/common/spellcheck_messages.h" |
| 24 #include "components/spellcheck/common/spellcheck_result.h" | 24 #include "components/spellcheck/common/spellcheck_result.h" |
| 25 #include "components/spellcheck/common/spellcheck_switches.h" | 25 #include "components/spellcheck/common/spellcheck_switches.h" |
| 26 #include "components/spellcheck/renderer/spellcheck_language.h" | 26 #include "components/spellcheck/renderer/spellcheck_language.h" |
| 27 #include "components/spellcheck/renderer/spellcheck_provider.h" | 27 #include "components/spellcheck/renderer/spellcheck_provider.h" |
| 28 #include "components/spellcheck/spellcheck_build_features.h" |
| 28 #include "content/public/renderer/render_thread.h" | 29 #include "content/public/renderer/render_thread.h" |
| 29 #include "content/public/renderer/render_view.h" | 30 #include "content/public/renderer/render_view.h" |
| 30 #include "content/public/renderer/render_view_visitor.h" | 31 #include "content/public/renderer/render_view_visitor.h" |
| 31 #include "ipc/ipc_platform_file.h" | 32 #include "ipc/ipc_platform_file.h" |
| 32 #include "third_party/WebKit/public/platform/WebString.h" | 33 #include "third_party/WebKit/public/platform/WebString.h" |
| 33 #include "third_party/WebKit/public/platform/WebVector.h" | 34 #include "third_party/WebKit/public/platform/WebVector.h" |
| 34 #include "third_party/WebKit/public/web/WebTextCheckingCompletion.h" | 35 #include "third_party/WebKit/public/web/WebTextCheckingCompletion.h" |
| 35 #include "third_party/WebKit/public/web/WebTextCheckingResult.h" | 36 #include "third_party/WebKit/public/web/WebTextCheckingResult.h" |
| 36 #include "third_party/WebKit/public/web/WebTextDecorationType.h" | 37 #include "third_party/WebKit/public/web/WebTextDecorationType.h" |
| 37 #include "third_party/WebKit/public/web/WebView.h" | 38 #include "third_party/WebKit/public/web/WebView.h" |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 const std::vector<SpellCheckBDictLanguage>& bdict_languages, | 224 const std::vector<SpellCheckBDictLanguage>& bdict_languages, |
| 224 const std::set<std::string>& custom_words) { | 225 const std::set<std::string>& custom_words) { |
| 225 languages_.clear(); | 226 languages_.clear(); |
| 226 for (const auto& bdict_language : bdict_languages) { | 227 for (const auto& bdict_language : bdict_languages) { |
| 227 AddSpellcheckLanguage( | 228 AddSpellcheckLanguage( |
| 228 IPC::PlatformFileForTransitToFile(bdict_language.file), | 229 IPC::PlatformFileForTransitToFile(bdict_language.file), |
| 229 bdict_language.language); | 230 bdict_language.language); |
| 230 } | 231 } |
| 231 | 232 |
| 232 custom_dictionary_.Init(custom_words); | 233 custom_dictionary_.Init(custom_words); |
| 233 #if !defined(USE_BROWSER_SPELLCHECKER) | 234 #if !BUILDFLAG(USE_BROWSER_SPELLCHECKER) |
| 234 PostDelayedSpellCheckTask(pending_request_param_.release()); | 235 PostDelayedSpellCheckTask(pending_request_param_.release()); |
| 235 #endif | 236 #endif |
| 236 } | 237 } |
| 237 | 238 |
| 238 void SpellCheck::OnCustomDictionaryChanged( | 239 void SpellCheck::OnCustomDictionaryChanged( |
| 239 const std::set<std::string>& words_added, | 240 const std::set<std::string>& words_added, |
| 240 const std::set<std::string>& words_removed) { | 241 const std::set<std::string>& words_removed) { |
| 241 custom_dictionary_.OnCustomDictionaryChanged(words_added, words_removed); | 242 custom_dictionary_.OnCustomDictionaryChanged(words_added, words_removed); |
| 242 if (words_added.empty()) | 243 if (words_added.empty()) |
| 243 return; | 244 return; |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 } | 364 } |
| 364 } | 365 } |
| 365 | 366 |
| 366 NOTREACHED(); | 367 NOTREACHED(); |
| 367 return true; | 368 return true; |
| 368 } | 369 } |
| 369 | 370 |
| 370 bool SpellCheck::SpellCheckParagraph( | 371 bool SpellCheck::SpellCheckParagraph( |
| 371 const base::string16& text, | 372 const base::string16& text, |
| 372 WebVector<WebTextCheckingResult>* results) { | 373 WebVector<WebTextCheckingResult>* results) { |
| 373 #if !defined(USE_BROWSER_SPELLCHECKER) | 374 #if !BUILDFLAG(USE_BROWSER_SPELLCHECKER) |
| 374 // Mac and Android have their own spell checkers,so this method won't be used | 375 // Mac and Android have their own spell checkers,so this method won't be used |
| 375 DCHECK(results); | 376 DCHECK(results); |
| 376 std::vector<WebTextCheckingResult> textcheck_results; | 377 std::vector<WebTextCheckingResult> textcheck_results; |
| 377 size_t length = text.length(); | 378 size_t length = text.length(); |
| 378 size_t position_in_text = 0; | 379 size_t position_in_text = 0; |
| 379 | 380 |
| 380 // Spellcheck::SpellCheckWord() automatically breaks text into words and | 381 // Spellcheck::SpellCheckWord() automatically breaks text into words and |
| 381 // checks the spellings of the extracted words. This function sets the | 382 // checks the spellings of the extracted words. This function sets the |
| 382 // position and length of the first misspelled word and returns false when | 383 // position and length of the first misspelled word and returns false when |
| 383 // the text includes misspelled words. Therefore, we just repeat calling the | 384 // the text includes misspelled words. Therefore, we just repeat calling the |
| (...skipping 27 matching lines...) Expand all Loading... |
| 411 return false; | 412 return false; |
| 412 #else | 413 #else |
| 413 // This function is only invoked for spell checker functionality that runs | 414 // This function is only invoked for spell checker functionality that runs |
| 414 // on the render thread. OSX and Android builds don't have that. | 415 // on the render thread. OSX and Android builds don't have that. |
| 415 NOTREACHED(); | 416 NOTREACHED(); |
| 416 return true; | 417 return true; |
| 417 #endif | 418 #endif |
| 418 } | 419 } |
| 419 | 420 |
| 420 // OSX and Android use their own spell checkers | 421 // OSX and Android use their own spell checkers |
| 421 #if !defined(USE_BROWSER_SPELLCHECKER) | 422 #if !BUILDFLAG(USE_BROWSER_SPELLCHECKER) |
| 422 void SpellCheck::RequestTextChecking( | 423 void SpellCheck::RequestTextChecking( |
| 423 const base::string16& text, | 424 const base::string16& text, |
| 424 blink::WebTextCheckingCompletion* completion) { | 425 blink::WebTextCheckingCompletion* completion) { |
| 425 // Clean up the previous request before starting a new request. | 426 // Clean up the previous request before starting a new request. |
| 426 if (pending_request_param_.get()) | 427 if (pending_request_param_.get()) |
| 427 pending_request_param_->completion()->didCancelCheckingText(); | 428 pending_request_param_->completion()->didCancelCheckingText(); |
| 428 | 429 |
| 429 pending_request_param_.reset(new SpellcheckRequest( | 430 pending_request_param_.reset(new SpellcheckRequest( |
| 430 text, completion)); | 431 text, completion)); |
| 431 // We will check this text after we finish loading the hunspell dictionary. | 432 // We will check this text after we finish loading the hunspell dictionary. |
| 432 if (InitializeIfNeeded()) | 433 if (InitializeIfNeeded()) |
| 433 return; | 434 return; |
| 434 | 435 |
| 435 PostDelayedSpellCheckTask(pending_request_param_.release()); | 436 PostDelayedSpellCheckTask(pending_request_param_.release()); |
| 436 } | 437 } |
| 437 #endif | 438 #endif |
| 438 | 439 |
| 439 bool SpellCheck::InitializeIfNeeded() { | 440 bool SpellCheck::InitializeIfNeeded() { |
| 440 if (languages_.empty()) | 441 if (languages_.empty()) |
| 441 return true; | 442 return true; |
| 442 | 443 |
| 443 bool initialize_if_needed = false; | 444 bool initialize_if_needed = false; |
| 444 for (SpellcheckLanguage* language : languages_) | 445 for (SpellcheckLanguage* language : languages_) |
| 445 initialize_if_needed |= language->InitializeIfNeeded(); | 446 initialize_if_needed |= language->InitializeIfNeeded(); |
| 446 | 447 |
| 447 return initialize_if_needed; | 448 return initialize_if_needed; |
| 448 } | 449 } |
| 449 | 450 |
| 450 // OSX and Android don't have |pending_request_param_| | 451 // OSX and Android don't have |pending_request_param_| |
| 451 #if !defined(USE_BROWSER_SPELLCHECKER) | 452 #if !BUILDFLAG(USE_BROWSER_SPELLCHECKER) |
| 452 void SpellCheck::PostDelayedSpellCheckTask(SpellcheckRequest* request) { | 453 void SpellCheck::PostDelayedSpellCheckTask(SpellcheckRequest* request) { |
| 453 if (!request) | 454 if (!request) |
| 454 return; | 455 return; |
| 455 | 456 |
| 456 base::ThreadTaskRunnerHandle::Get()->PostTask( | 457 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 457 FROM_HERE, base::Bind(&SpellCheck::PerformSpellCheck, AsWeakPtr(), | 458 FROM_HERE, base::Bind(&SpellCheck::PerformSpellCheck, AsWeakPtr(), |
| 458 base::Owned(request))); | 459 base::Owned(request))); |
| 459 } | 460 } |
| 460 #endif | 461 #endif |
| 461 | 462 |
| 462 // Mac and Android use their platform engines instead. | 463 // Mac and Android use their platform engines instead. |
| 463 #if !defined(USE_BROWSER_SPELLCHECKER) | 464 #if !BUILDFLAG(USE_BROWSER_SPELLCHECKER) |
| 464 void SpellCheck::PerformSpellCheck(SpellcheckRequest* param) { | 465 void SpellCheck::PerformSpellCheck(SpellcheckRequest* param) { |
| 465 DCHECK(param); | 466 DCHECK(param); |
| 466 | 467 |
| 467 if (languages_.empty() || | 468 if (languages_.empty() || |
| 468 std::find_if(languages_.begin(), languages_.end(), | 469 std::find_if(languages_.begin(), languages_.end(), |
| 469 [](SpellcheckLanguage* language) { | 470 [](SpellcheckLanguage* language) { |
| 470 return !language->IsEnabled(); | 471 return !language->IsEnabled(); |
| 471 }) != languages_.end()) { | 472 }) != languages_.end()) { |
| 472 param->completion()->didCancelCheckingText(); | 473 param->completion()->didCancelCheckingText(); |
| 473 } else { | 474 } else { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 535 | 536 |
| 536 textcheck_results->assign(results); | 537 textcheck_results->assign(results); |
| 537 } | 538 } |
| 538 | 539 |
| 539 bool SpellCheck::IsSpellcheckEnabled() { | 540 bool SpellCheck::IsSpellcheckEnabled() { |
| 540 #if defined(OS_ANDROID) | 541 #if defined(OS_ANDROID) |
| 541 if (!spellcheck::IsAndroidSpellCheckFeatureEnabled()) return false; | 542 if (!spellcheck::IsAndroidSpellCheckFeatureEnabled()) return false; |
| 542 #endif | 543 #endif |
| 543 return spellcheck_enabled_; | 544 return spellcheck_enabled_; |
| 544 } | 545 } |
| OLD | NEW |