fix_hunspell_crash.patch 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Cheng Zhao <[email protected]>
  3. Date: Thu, 4 Oct 2018 14:57:02 -0700
  4. Subject: Make sure hunspell file is not destroyed in UI thread
  5. Submitted to Chromium at:
  6. https://chromium-review.googlesource.com/c/chromium/src/+/2206199/1
  7. diff --git a/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.cc b/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.cc
  8. index 67ec4e285d9bf6b9de300845b2c53bda435e5784..74d403aeb1739d0424874a702e64cfd8c3fe4fcb 100644
  9. --- a/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.cc
  10. +++ b/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.cc
  11. @@ -91,21 +91,28 @@ bool SaveDictionaryData(std::unique_ptr<std::string> data,
  12. } // namespace
  13. -SpellcheckHunspellDictionary::DictionaryFile::DictionaryFile() {
  14. -}
  15. +SpellcheckHunspellDictionary::DictionaryFile::DictionaryFile(
  16. + base::TaskRunner* task_runner) : task_runner_(task_runner) {}
  17. SpellcheckHunspellDictionary::DictionaryFile::~DictionaryFile() {
  18. + if (file.IsValid()) {
  19. + task_runner_->PostTask(FROM_HERE,
  20. + base::BindOnce(&CloseDictionary, std::move(file)));
  21. + }
  22. }
  23. SpellcheckHunspellDictionary::DictionaryFile::DictionaryFile(
  24. DictionaryFile&& other)
  25. - : path(other.path), file(std::move(other.file)) {}
  26. + : path(other.path),
  27. + file(std::move(other.file)),
  28. + task_runner_(std::move(other.task_runner_)) {}
  29. SpellcheckHunspellDictionary::DictionaryFile&
  30. SpellcheckHunspellDictionary::DictionaryFile::operator=(
  31. DictionaryFile&& other) {
  32. path = other.path;
  33. file = std::move(other.file);
  34. + task_runner_ = std::move(other.task_runner_);
  35. return *this;
  36. }
  37. @@ -121,16 +128,10 @@ SpellcheckHunspellDictionary::SpellcheckHunspellDictionary(
  38. #if !defined(OS_ANDROID)
  39. spellcheck_service_(spellcheck_service),
  40. #endif
  41. - download_status_(DOWNLOAD_NONE) {
  42. -}
  43. + download_status_(DOWNLOAD_NONE),
  44. + dictionary_file_(task_runner_.get()) {}
  45. SpellcheckHunspellDictionary::~SpellcheckHunspellDictionary() {
  46. - if (dictionary_file_.file.IsValid()) {
  47. - task_runner_->PostTask(
  48. - FROM_HERE,
  49. - base::BindOnce(&CloseDictionary, std::move(dictionary_file_.file)));
  50. - }
  51. -
  52. #if BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  53. // Disable the language from platform spellchecker.
  54. if (spellcheck::UseBrowserSpellChecker())
  55. @@ -324,7 +325,8 @@ void SpellcheckHunspellDictionary::DownloadDictionary(GURL url) {
  56. #if !defined(OS_ANDROID)
  57. // static
  58. SpellcheckHunspellDictionary::DictionaryFile
  59. -SpellcheckHunspellDictionary::OpenDictionaryFile(const base::FilePath& path) {
  60. +SpellcheckHunspellDictionary::OpenDictionaryFile(base::TaskRunner* task_runner,
  61. + const base::FilePath& path) {
  62. base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
  63. base::BlockingType::MAY_BLOCK);
  64. @@ -335,7 +337,7 @@ SpellcheckHunspellDictionary::OpenDictionaryFile(const base::FilePath& path) {
  65. // For systemwide installations on Windows, the default directory may not
  66. // have permissions for download. In that case, the alternate directory for
  67. // download is chrome::DIR_USER_DATA.
  68. - DictionaryFile dictionary;
  69. + DictionaryFile dictionary(task_runner);
  70. #if defined(OS_WIN)
  71. // Check if the dictionary exists in the fallback location. If so, use it
  72. @@ -377,7 +379,7 @@ SpellcheckHunspellDictionary::OpenDictionaryFile(const base::FilePath& path) {
  73. // static
  74. SpellcheckHunspellDictionary::DictionaryFile
  75. SpellcheckHunspellDictionary::InitializeDictionaryLocation(
  76. - const std::string& language) {
  77. + base::TaskRunner* task_runner, const std::string& language) {
  78. base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
  79. base::BlockingType::MAY_BLOCK);
  80. @@ -392,7 +394,7 @@ SpellcheckHunspellDictionary::InitializeDictionaryLocation(
  81. base::FilePath dict_path =
  82. spellcheck::GetVersionedFileName(language, dict_dir);
  83. - return OpenDictionaryFile(dict_path);
  84. + return OpenDictionaryFile(task_runner, dict_path);
  85. }
  86. void SpellcheckHunspellDictionary::InitializeDictionaryLocationComplete(
  87. @@ -480,7 +482,8 @@ void SpellcheckHunspellDictionary::PlatformSupportsLanguageComplete(
  88. #if !defined(OS_ANDROID) && BUILDFLAG(USE_RENDERER_SPELLCHECKER)
  89. base::PostTaskAndReplyWithResult(
  90. task_runner_.get(), FROM_HERE,
  91. - base::BindOnce(&InitializeDictionaryLocation, language_),
  92. + base::BindOnce(&InitializeDictionaryLocation,
  93. + base::RetainedRef(task_runner_.get()), language_),
  94. base::BindOnce(
  95. &SpellcheckHunspellDictionary::InitializeDictionaryLocationComplete,
  96. weak_ptr_factory_.GetWeakPtr()));
  97. diff --git a/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h b/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h
  98. index 633ec3a96b39824fc9bcf374e59eb80148a2ae27..bfb90905ee045d8f08dbd0b348204b41fd185f41 100644
  99. --- a/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h
  100. +++ b/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h
  101. @@ -99,7 +99,7 @@ class SpellcheckHunspellDictionary
  102. // blocking sequence.
  103. struct DictionaryFile {
  104. public:
  105. - DictionaryFile();
  106. + explicit DictionaryFile(base::TaskRunner* task_runner);
  107. ~DictionaryFile();
  108. DictionaryFile(DictionaryFile&& other);
  109. @@ -112,6 +112,9 @@ class SpellcheckHunspellDictionary
  110. base::File file;
  111. private:
  112. + // Task runner where the file is created.
  113. + scoped_refptr<base::TaskRunner> task_runner_;
  114. +
  115. DISALLOW_COPY_AND_ASSIGN(DictionaryFile);
  116. };
  117. @@ -126,11 +129,12 @@ class SpellcheckHunspellDictionary
  118. #if !defined(OS_ANDROID)
  119. // Figures out the location for the dictionary, verifies its contents, and
  120. // opens it.
  121. - static DictionaryFile OpenDictionaryFile(const base::FilePath& path);
  122. + static DictionaryFile OpenDictionaryFile(base::TaskRunner* task_runner,
  123. + const base::FilePath& path);
  124. // Gets the default location for the dictionary file.
  125. static DictionaryFile InitializeDictionaryLocation(
  126. - const std::string& language);
  127. + base::TaskRunner* task_runner, const std::string& language);
  128. // The reply point for PostTaskAndReplyWithResult, called after the dictionary
  129. // file has been initialized.