Index: chrome/browser/spellchecker/spellcheck_profile.cc |
diff --git a/chrome/browser/spellchecker/spellcheck_profile.cc b/chrome/browser/spellchecker/spellcheck_profile.cc |
index dd79b96df26c7b3bc7693640c05221f625564f34..509b3cba1eac1c4c5dbb705400233a4ef6000db7 100644 |
--- a/chrome/browser/spellchecker/spellcheck_profile.cc |
+++ b/chrome/browser/spellchecker/spellcheck_profile.cc |
@@ -4,12 +4,16 @@ |
#include "chrome/browser/spellchecker/spellcheck_profile.h" |
+#include "base/file_util.h" |
#include "base/lazy_instance.h" |
+#include "base/path_service.h" |
+#include "base/string_split.h" |
#include "chrome/browser/prefs/pref_service.h" |
#include "chrome/browser/profiles/profile.h" |
#include "chrome/browser/spellchecker/spellcheck_host.h" |
#include "chrome/browser/spellchecker/spellcheck_host_metrics.h" |
#include "chrome/common/pref_names.h" |
+#include "content/browser/browser_thread.h" |
namespace { |
base::LazyInstance<SpellCheckProfile::CustomWordList> g_empty_list( |
@@ -76,25 +80,103 @@ void SpellCheckProfile::StartRecordingMetrics(bool spellcheck_enabled) { |
metrics_->RecordEnabledStats(spellcheck_enabled); |
} |
-void SpellCheckProfile::SpellCheckHostInitialized( |
- SpellCheckProfile::CustomWordList* custom_words) { |
- host_ready_ = !!host_.get() && host_->IsReady(); |
- custom_words_.reset(custom_words); |
+void SpellCheckProfile::ClearCustomWords() { |
+ custom_words_->clear(); |
+} |
+ |
+FilePath SpellCheckProfile::GetCustomDictionaryDir() { |
+ DCHECK(!IsTesting()); |
+ |
+ FilePath personal_file_directory; |
+ PathService::Get(chrome::DIR_USER_DATA, &personal_file_directory); |
+ return personal_file_directory; |
+} |
+ |
+void SpellCheckProfile::WriteWordToCustomDictionary(const std::string& word) { |
+ BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
+ NewRunnableMethod(this, |
+ &SpellCheckProfile::WriteWordToCustomDictionaryOnFileThread, word)); |
+} |
+ |
+void SpellCheckProfile::WriteWordToCustomDictionaryOnFileThread( |
+ const std::string& word) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
+ |
+ // Stored in UTF-8. |
+ std::string word_to_add(word + "\n"); |
+ FILE* f = file_util::OpenFile(GetCustomDictionaryFile(), "a+"); |
+ if (f) |
+ fputs(word_to_add.c_str(), f); |
+ file_util::CloseFile(f); |
+} |
+ |
+void SpellCheckProfile::LoadCustomDictionary() { |
+ BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
+ NewRunnableMethod( |
+ this, |
+ &SpellCheckProfile::LoadCustomDictionaryOnFileThread)); |
+} |
+ |
+void SpellCheckProfile::LoadCustomDictionaryOnFileThread() { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
+ |
+ scoped_ptr<CustomWordList> custom_words(new CustomWordList); |
+ |
+ std::string contents; |
+ file_util::ReadFileToString(GetCustomDictionaryFile(), &contents); |
+ CustomWordList list_of_words; |
+ base::SplitString(contents, '\n', &list_of_words); |
+ for (size_t i = 0; i < list_of_words.size(); ++i) { |
+ if (!list_of_words[i].empty()) |
+ custom_words->push_back(list_of_words[i]); |
+ } |
+ |
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
+ NewRunnableMethod( |
+ this, |
+ &SpellCheckProfile::CustomDictionaryLoaded, |
+ custom_words.release())); |
+} |
+ |
+void SpellCheckProfile::CustomDictionaryLoaded(CustomWordList* word_list) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || IsTesting()); |
+ |
+ custom_words_.reset(word_list); |
if (metrics_.get()) { |
int count = custom_words_.get() ? custom_words_->size() : 0; |
metrics_->RecordCustomWordCountStats(count); |
} |
} |
+void SpellCheckProfile::SpellCheckHostInitialized() { |
+ host_ready_ = !!host_.get() && host_->IsReady(); |
+} |
+ |
const SpellCheckProfile::CustomWordList& |
SpellCheckProfile::GetCustomWords() const { |
return custom_words_.get() ? *custom_words_ : g_empty_list.Get(); |
} |
void SpellCheckProfile::CustomWordAddedLocally(const std::string& word) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || IsTesting()); |
+ |
if (!custom_words_.get()) |
custom_words_.reset(new CustomWordList()); |
custom_words_->push_back(word); |
if (metrics_.get()) |
metrics_->RecordCustomWordCountStats(custom_words_->size()); |
+ |
+ WriteWordToCustomDictionary(word); |
+} |
+ |
+const FilePath& SpellCheckProfile::GetCustomDictionaryFile() { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE) || IsTesting()); |
+ |
+ if (custom_dictionary_file_.empty()) { |
+ FilePath personal_file_directory = GetCustomDictionaryDir(); |
+ custom_dictionary_file_ = |
+ personal_file_directory.Append(chrome::kCustomDictionaryFileName); |
+ } |
+ |
+ return custom_dictionary_file_; |
} |