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/string_split.h" | |
8 #include "chrome/browser/prefs/pref_service.h" | 10 #include "chrome/browser/prefs/pref_service.h" |
9 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/browser/spellchecker/spellcheck_host.h" | 12 #include "chrome/browser/spellchecker/spellcheck_host.h" |
11 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" | 13 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" |
14 #include "chrome/common/chrome_constants.h" | |
12 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
13 | 16 |
14 namespace { | 17 namespace { |
15 base::LazyInstance<SpellCheckProfile::CustomWordList> g_empty_list( | 18 base::LazyInstance<SpellCheckProfile::CustomWordList> g_empty_list( |
16 base::LINKER_INITIALIZED); | 19 base::LINKER_INITIALIZED); |
17 } // namespace | 20 } // namespace |
18 | 21 |
19 SpellCheckProfile::SpellCheckProfile() | 22 SpellCheckProfile::SpellCheckProfile(const FilePath& profile_dir) |
20 : host_ready_(false) { | 23 : host_ready_(false), |
24 custom_dictionary_path_( | |
25 profile_dir.Append(chrome::kCustomDictionaryFileName)) { | |
Hironori Bono
2011/10/20 02:20:36
Even though file_util::Append() does not usually t
shinyak (Google)
2011/10/20 04:08:43
Done.
| |
21 } | 26 } |
22 | 27 |
23 SpellCheckProfile::~SpellCheckProfile() { | 28 SpellCheckProfile::~SpellCheckProfile() { |
24 if (host_.get()) | 29 if (host_.get()) |
25 host_->UnsetProfile(); | 30 host_->UnsetProfile(); |
26 } | 31 } |
27 | 32 |
28 SpellCheckHost* SpellCheckProfile::GetHost() { | 33 SpellCheckHost* SpellCheckProfile::GetHost() { |
29 return host_ready_ ? host_.get() : NULL; | 34 return host_ready_ ? host_.get() : NULL; |
30 } | 35 } |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
91 return custom_words_.get() ? *custom_words_ : g_empty_list.Get(); | 96 return custom_words_.get() ? *custom_words_ : g_empty_list.Get(); |
92 } | 97 } |
93 | 98 |
94 void SpellCheckProfile::CustomWordAddedLocally(const std::string& word) { | 99 void SpellCheckProfile::CustomWordAddedLocally(const std::string& word) { |
95 if (!custom_words_.get()) | 100 if (!custom_words_.get()) |
96 custom_words_.reset(new CustomWordList()); | 101 custom_words_.reset(new CustomWordList()); |
97 custom_words_->push_back(word); | 102 custom_words_->push_back(word); |
98 if (metrics_.get()) | 103 if (metrics_.get()) |
99 metrics_->RecordCustomWordCountStats(custom_words_->size()); | 104 metrics_->RecordCustomWordCountStats(custom_words_->size()); |
100 } | 105 } |
106 | |
107 void SpellCheckProfile::LoadCustomDictionary(CustomWordList* custom_words) { | |
108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE) || IsTesting()); | |
109 | |
110 if (!custom_words) | |
111 return; | |
112 | |
113 std::string contents; | |
114 CustomWordList list_of_words; | |
115 | |
116 file_util::ReadFileToString(custom_dictionary_path_, &contents); | |
117 base::SplitString(contents, '\n', &list_of_words); | |
118 for (size_t i = 0; i < list_of_words.size(); ++i) { | |
119 if (list_of_words[i] != "") | |
120 custom_words->push_back(list_of_words[i]); | |
121 } | |
122 } | |
123 | |
124 void SpellCheckProfile::WriteWordToCustomDictionary(const std::string& word) { | |
125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE) || IsTesting()); | |
126 | |
127 // Stored in UTF-8. | |
Hironori Bono
2011/10/20 02:20:36
nit: it would be good to add 'DCHECK(IsStringUTF8(
shinyak (Google)
2011/10/20 04:08:43
Done.
| |
128 std::string word_to_add(word + "\n"); | |
129 FILE* f = file_util::OpenFile(custom_dictionary_path_, "a+"); | |
130 if (f) | |
131 fputs(word_to_add.c_str(), f); | |
132 file_util::CloseFile(f); | |
Hironori Bono
2011/10/20 02:20:36
nit: it may be better to call file_util::CloseFile
shinyak (Google)
2011/10/20 04:08:43
Done.
| |
133 } | |
OLD | NEW |