OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/spellchecker/spellcheck_profile.h" | 5 #include "chrome/browser/spellchecker/spellcheck_profile.h" |
6 | 6 |
| 7 #include "base/file_util.h" |
7 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "base/path_service.h" |
| 10 #include "base/string_split.h" |
8 #include "chrome/browser/prefs/pref_service.h" | 11 #include "chrome/browser/prefs/pref_service.h" |
9 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/browser/spellchecker/spellcheck_host.h" | 13 #include "chrome/browser/spellchecker/spellcheck_host.h" |
11 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" | 14 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" |
12 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
| 16 #include "content/browser/browser_thread.h" |
13 | 17 |
14 namespace { | 18 namespace { |
15 base::LazyInstance<SpellCheckProfile::CustomWordList> g_empty_list( | 19 base::LazyInstance<SpellCheckProfile::CustomWordList> g_empty_list( |
16 base::LINKER_INITIALIZED); | 20 base::LINKER_INITIALIZED); |
17 } // namespace | 21 } // namespace |
18 | 22 |
19 SpellCheckProfile::SpellCheckProfile() | 23 SpellCheckProfile::SpellCheckProfile() |
20 : host_ready_(false) { | 24 : host_ready_(false) { |
21 } | 25 } |
22 | 26 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 | 73 |
70 bool SpellCheckProfile::IsTesting() const { | 74 bool SpellCheckProfile::IsTesting() const { |
71 return false; | 75 return false; |
72 } | 76 } |
73 | 77 |
74 void SpellCheckProfile::StartRecordingMetrics(bool spellcheck_enabled) { | 78 void SpellCheckProfile::StartRecordingMetrics(bool spellcheck_enabled) { |
75 metrics_.reset(new SpellCheckHostMetrics()); | 79 metrics_.reset(new SpellCheckHostMetrics()); |
76 metrics_->RecordEnabledStats(spellcheck_enabled); | 80 metrics_->RecordEnabledStats(spellcheck_enabled); |
77 } | 81 } |
78 | 82 |
79 void SpellCheckProfile::SpellCheckHostInitialized( | 83 void SpellCheckProfile::ClearCustomWords() { |
80 SpellCheckProfile::CustomWordList* custom_words) { | 84 custom_words_->clear(); |
81 host_ready_ = !!host_.get() && host_->IsReady(); | 85 } |
82 custom_words_.reset(custom_words); | 86 |
| 87 FilePath SpellCheckProfile::GetCustomDictionaryDir() { |
| 88 DCHECK(!IsTesting()); |
| 89 |
| 90 FilePath personal_file_directory; |
| 91 PathService::Get(chrome::DIR_USER_DATA, &personal_file_directory); |
| 92 return personal_file_directory; |
| 93 } |
| 94 |
| 95 void SpellCheckProfile::WriteWordToCustomDictionary(const std::string& word) { |
| 96 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 97 NewRunnableMethod(this, |
| 98 &SpellCheckProfile::WriteWordToCustomDictionaryOnFileThread, word)); |
| 99 } |
| 100 |
| 101 void SpellCheckProfile::WriteWordToCustomDictionaryOnFileThread( |
| 102 const std::string& word) { |
| 103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 104 |
| 105 // Stored in UTF-8. |
| 106 std::string word_to_add(word + "\n"); |
| 107 FILE* f = file_util::OpenFile(GetCustomDictionaryFile(), "a+"); |
| 108 if (f) |
| 109 fputs(word_to_add.c_str(), f); |
| 110 file_util::CloseFile(f); |
| 111 } |
| 112 |
| 113 void SpellCheckProfile::LoadCustomDictionary() { |
| 114 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 115 NewRunnableMethod( |
| 116 this, |
| 117 &SpellCheckProfile::LoadCustomDictionaryOnFileThread)); |
| 118 } |
| 119 |
| 120 void SpellCheckProfile::LoadCustomDictionaryOnFileThread() { |
| 121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 122 |
| 123 scoped_ptr<CustomWordList> custom_words(new CustomWordList); |
| 124 |
| 125 std::string contents; |
| 126 file_util::ReadFileToString(GetCustomDictionaryFile(), &contents); |
| 127 CustomWordList list_of_words; |
| 128 base::SplitString(contents, '\n', &list_of_words); |
| 129 for (size_t i = 0; i < list_of_words.size(); ++i) { |
| 130 if (!list_of_words[i].empty()) |
| 131 custom_words->push_back(list_of_words[i]); |
| 132 } |
| 133 |
| 134 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 135 NewRunnableMethod( |
| 136 this, |
| 137 &SpellCheckProfile::CustomDictionaryLoaded, |
| 138 custom_words.release())); |
| 139 } |
| 140 |
| 141 void SpellCheckProfile::CustomDictionaryLoaded(CustomWordList* word_list) { |
| 142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || IsTesting()); |
| 143 |
| 144 custom_words_.reset(word_list); |
83 if (metrics_.get()) { | 145 if (metrics_.get()) { |
84 int count = custom_words_.get() ? custom_words_->size() : 0; | 146 int count = custom_words_.get() ? custom_words_->size() : 0; |
85 metrics_->RecordCustomWordCountStats(count); | 147 metrics_->RecordCustomWordCountStats(count); |
86 } | 148 } |
87 } | 149 } |
88 | 150 |
| 151 void SpellCheckProfile::SpellCheckHostInitialized() { |
| 152 host_ready_ = !!host_.get() && host_->IsReady(); |
| 153 } |
| 154 |
89 const SpellCheckProfile::CustomWordList& | 155 const SpellCheckProfile::CustomWordList& |
90 SpellCheckProfile::GetCustomWords() const { | 156 SpellCheckProfile::GetCustomWords() const { |
91 return custom_words_.get() ? *custom_words_ : g_empty_list.Get(); | 157 return custom_words_.get() ? *custom_words_ : g_empty_list.Get(); |
92 } | 158 } |
93 | 159 |
94 void SpellCheckProfile::CustomWordAddedLocally(const std::string& word) { | 160 void SpellCheckProfile::CustomWordAddedLocally(const std::string& word) { |
| 161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || IsTesting()); |
| 162 |
95 if (!custom_words_.get()) | 163 if (!custom_words_.get()) |
96 custom_words_.reset(new CustomWordList()); | 164 custom_words_.reset(new CustomWordList()); |
97 custom_words_->push_back(word); | 165 custom_words_->push_back(word); |
98 if (metrics_.get()) | 166 if (metrics_.get()) |
99 metrics_->RecordCustomWordCountStats(custom_words_->size()); | 167 metrics_->RecordCustomWordCountStats(custom_words_->size()); |
| 168 |
| 169 WriteWordToCustomDictionary(word); |
100 } | 170 } |
| 171 |
| 172 const FilePath& SpellCheckProfile::GetCustomDictionaryFile() { |
| 173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE) || IsTesting()); |
| 174 |
| 175 if (custom_dictionary_file_.empty()) { |
| 176 FilePath personal_file_directory = GetCustomDictionaryDir(); |
| 177 custom_dictionary_file_ = |
| 178 personal_file_directory.Append(chrome::kCustomDictionaryFileName); |
| 179 } |
| 180 |
| 181 return custom_dictionary_file_; |
| 182 } |
OLD | NEW |