| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/browser/spellcheck_host.h" | 5 #include "chrome/browser/spellcheck_host.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 | 8 |
| 9 #include "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 13 #include "base/string_split.h" | 13 #include "base/string_split.h" |
| 14 #include "base/thread_restrictions.h" | 14 #include "base/thread_restrictions.h" |
| 15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
| 16 #include "chrome/browser/prefs/pref_member.h" | 16 #include "chrome/browser/prefs/pref_member.h" |
| 17 #include "chrome/browser/profile.h" | 17 #include "chrome/browser/profile.h" |
| 18 #include "chrome/browser/spellcheck_host_observer.h" | 18 #include "chrome/browser/spellcheck_host_observer.h" |
| 19 #include "chrome/browser/spellchecker_platform_engine.h" | 19 #include "chrome/browser/spellchecker_platform_engine.h" |
| 20 #include "chrome/common/chrome_constants.h" | 20 #include "chrome/common/chrome_constants.h" |
| 21 #include "chrome/common/chrome_paths.h" | 21 #include "chrome/common/chrome_paths.h" |
| 22 #include "chrome/common/net/url_request_context_getter.h" | 22 #include "chrome/common/net/url_request_context_getter.h" |
| 23 #include "chrome/common/notification_service.h" | 23 #include "chrome/common/notification_service.h" |
| 24 #include "chrome/common/pref_names.h" | 24 #include "chrome/common/pref_names.h" |
| 25 #include "chrome/common/spellcheck_common.h" | 25 #include "chrome/common/spellcheck_common.h" |
| 26 #include "googleurl/src/gurl.h" | 26 #include "googleurl/src/gurl.h" |
| 27 #include "third_party/hunspell/google/bdict.h" |
| 27 | 28 |
| 28 namespace { | 29 namespace { |
| 29 | 30 |
| 30 FilePath GetFirstChoiceFilePath(const std::string& language) { | 31 FilePath GetFirstChoiceFilePath(const std::string& language) { |
| 31 FilePath dict_dir; | 32 FilePath dict_dir; |
| 32 { | 33 { |
| 33 // This should not do blocking IO from the UI thread! | 34 // This should not do blocking IO from the UI thread! |
| 34 // Temporarily allow it for now. | 35 // Temporarily allow it for now. |
| 35 // http://code.google.com/p/chromium/issues/detail?id=60643 | 36 // http://code.google.com/p/chromium/issues/detail?id=60643 |
| 36 base::ThreadRestrictions::ScopedAllowIO allow_io; | 37 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 } | 281 } |
| 281 | 282 |
| 282 data_ = data; | 283 data_ = data; |
| 283 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 284 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 284 NewRunnableMethod(this, &SpellCheckHost::SaveDictionaryData)); | 285 NewRunnableMethod(this, &SpellCheckHost::SaveDictionaryData)); |
| 285 } | 286 } |
| 286 | 287 |
| 287 void SpellCheckHost::SaveDictionaryData() { | 288 void SpellCheckHost::SaveDictionaryData() { |
| 288 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 289 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 289 | 290 |
| 291 // To prevent corrupted dictionary data from causing a renderer crash, scan |
| 292 // the dictionary data and verify it is sane before save it to a file. |
| 293 if (!hunspell::BDict::Verify(data_.data(), data_.size())) { |
| 294 LOG(ERROR) << "Failure to verify the downloaded dictionary."; |
| 295 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 296 NewRunnableMethod(this, |
| 297 &SpellCheckHost::InformObserverOfInitialization)); |
| 298 return; |
| 299 } |
| 300 |
| 290 size_t bytes_written = | 301 size_t bytes_written = |
| 291 file_util::WriteFile(bdict_file_path_, data_.data(), data_.length()); | 302 file_util::WriteFile(bdict_file_path_, data_.data(), data_.length()); |
| 292 if (bytes_written != data_.length()) { | 303 if (bytes_written != data_.length()) { |
| 293 bool success = false; | 304 bool success = false; |
| 294 #if defined(OS_WIN) | 305 #if defined(OS_WIN) |
| 295 bdict_file_path_ = GetFallbackFilePath(bdict_file_path_); | 306 bdict_file_path_ = GetFallbackFilePath(bdict_file_path_); |
| 296 bytes_written = | 307 bytes_written = |
| 297 file_util::WriteFile(GetFallbackFilePath(bdict_file_path_), | 308 file_util::WriteFile(GetFallbackFilePath(bdict_file_path_), |
| 298 data_.data(), data_.length()); | 309 data_.data(), data_.length()); |
| 299 if (bytes_written == data_.length()) | 310 if (bytes_written == data_.length()) |
| 300 success = true; | 311 success = true; |
| 301 #endif | 312 #endif |
| 302 data_.clear(); | 313 data_.clear(); |
| 303 | 314 |
| 304 if (!success) { | 315 if (!success) { |
| 305 LOG(ERROR) << "Failure to save dictionary."; | 316 LOG(ERROR) << "Failure to save dictionary."; |
| 306 file_util::Delete(bdict_file_path_, false); | 317 file_util::Delete(bdict_file_path_, false); |
| 307 // To avoid trying to load a partially saved dictionary, shortcut the | 318 // To avoid trying to load a partially saved dictionary, shortcut the |
| 308 // Initialize() call. | 319 // Initialize() call. |
| 309 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 320 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 310 NewRunnableMethod(this, | 321 NewRunnableMethod(this, |
| 311 &SpellCheckHost::InformObserverOfInitialization)); | 322 &SpellCheckHost::InformObserverOfInitialization)); |
| 312 return; | 323 return; |
| 313 } | 324 } |
| 314 } | 325 } |
| 315 | 326 |
| 316 data_.clear(); | 327 data_.clear(); |
| 317 Initialize(); | 328 Initialize(); |
| 318 } | 329 } |
| OLD | NEW |