Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(266)

Unified Diff: chrome/browser/spellchecker/spellcheck_profile.cc

Issue 8233040: SpellCheck refactoring: Moved user custom dictionary for spell check to SpellCheckProfile. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..0dbb88d0ccba297a6f77dad6592da6c5c5685288 100644
--- a/chrome/browser/spellchecker/spellcheck_profile.cc
+++ b/chrome/browser/spellchecker/spellcheck_profile.cc
@@ -4,7 +4,10 @@
#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"
@@ -18,6 +21,10 @@ base::LazyInstance<SpellCheckProfile::CustomWordList> g_empty_list(
SpellCheckProfile::SpellCheckProfile()
: host_ready_(false) {
+ FilePath personal_file_directory;
+ PathService::Get(chrome::DIR_USER_DATA, &personal_file_directory);
Hironori Bono 2011/10/12 02:56:56 This call may create a directory, i.e. this functi
shinyak (Google) 2011/10/12 10:46:54 Done. I made it to use lazy initialization. On 2
+ custom_dictionary_file_ =
+ personal_file_directory.Append(chrome::kCustomDictionaryFileName);
}
SpellCheckProfile::~SpellCheckProfile() {
@@ -76,25 +83,69 @@ 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::WriteWordToCustomDictionary(const std::string& word) {
+ DCHECK(!IsTesting());
+
+ 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(custom_dictionary_file_, "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> word_list(new CustomWordList());
Hironori Bono 2011/10/12 02:56:56 nit: CustomWordList() -> CustomWordList.
shinyak (Google) 2011/10/12 10:46:54 Done.
+
+ std::string contents;
+ file_util::ReadFileToString(custom_dictionary_file_, &contents);
+ CustomWordList list_of_words;
+ base::SplitString(contents, '\n', &list_of_words);
+ for (size_t i = 0; i < list_of_words.size(); ++i)
+ word_list->push_back(list_of_words[i]);
+
+ custom_words_.reset(word_list.release());
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);
}

Powered by Google App Engine
This is Rietveld 408576698