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" |
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() |
20 : host_ready_(false) { | 23 : host_ready_(false) { |
24 FilePath personal_file_directory; | |
25 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
| |
26 custom_dictionary_file_ = | |
27 personal_file_directory.Append(chrome::kCustomDictionaryFileName); | |
21 } | 28 } |
22 | 29 |
23 SpellCheckProfile::~SpellCheckProfile() { | 30 SpellCheckProfile::~SpellCheckProfile() { |
24 if (host_.get()) | 31 if (host_.get()) |
25 host_->UnsetProfile(); | 32 host_->UnsetProfile(); |
26 } | 33 } |
27 | 34 |
28 SpellCheckHost* SpellCheckProfile::GetHost() { | 35 SpellCheckHost* SpellCheckProfile::GetHost() { |
29 return host_ready_ ? host_.get() : NULL; | 36 return host_ready_ ? host_.get() : NULL; |
30 } | 37 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
69 | 76 |
70 bool SpellCheckProfile::IsTesting() const { | 77 bool SpellCheckProfile::IsTesting() const { |
71 return false; | 78 return false; |
72 } | 79 } |
73 | 80 |
74 void SpellCheckProfile::StartRecordingMetrics(bool spellcheck_enabled) { | 81 void SpellCheckProfile::StartRecordingMetrics(bool spellcheck_enabled) { |
75 metrics_.reset(new SpellCheckHostMetrics()); | 82 metrics_.reset(new SpellCheckHostMetrics()); |
76 metrics_->RecordEnabledStats(spellcheck_enabled); | 83 metrics_->RecordEnabledStats(spellcheck_enabled); |
77 } | 84 } |
78 | 85 |
79 void SpellCheckProfile::SpellCheckHostInitialized( | 86 void SpellCheckProfile::WriteWordToCustomDictionary(const std::string& word) { |
80 SpellCheckProfile::CustomWordList* custom_words) { | 87 DCHECK(!IsTesting()); |
81 host_ready_ = !!host_.get() && host_->IsReady(); | 88 |
82 custom_words_.reset(custom_words); | 89 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
90 NewRunnableMethod(this, | |
91 &SpellCheckProfile::WriteWordToCustomDictionaryOnFileThread, word)); | |
92 } | |
93 | |
94 void SpellCheckProfile::WriteWordToCustomDictionaryOnFileThread( | |
95 const std::string& word) { | |
96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
97 | |
98 // Stored in UTF-8. | |
99 std::string word_to_add(word + "\n"); | |
100 FILE* f = file_util::OpenFile(custom_dictionary_file_, "a+"); | |
101 if (f) | |
102 fputs(word_to_add.c_str(), f); | |
103 file_util::CloseFile(f); | |
104 } | |
105 | |
106 void SpellCheckProfile::LoadCustomDictionary() { | |
107 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
108 NewRunnableMethod( | |
109 this, | |
110 &SpellCheckProfile::LoadCustomDictionaryOnFileThread)); | |
111 } | |
112 | |
113 void SpellCheckProfile::LoadCustomDictionaryOnFileThread() { | |
114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
115 | |
116 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.
| |
117 | |
118 std::string contents; | |
119 file_util::ReadFileToString(custom_dictionary_file_, &contents); | |
120 CustomWordList list_of_words; | |
121 base::SplitString(contents, '\n', &list_of_words); | |
122 for (size_t i = 0; i < list_of_words.size(); ++i) | |
123 word_list->push_back(list_of_words[i]); | |
124 | |
125 custom_words_.reset(word_list.release()); | |
83 if (metrics_.get()) { | 126 if (metrics_.get()) { |
84 int count = custom_words_.get() ? custom_words_->size() : 0; | 127 int count = custom_words_.get() ? custom_words_->size() : 0; |
85 metrics_->RecordCustomWordCountStats(count); | 128 metrics_->RecordCustomWordCountStats(count); |
86 } | 129 } |
87 } | 130 } |
88 | 131 |
132 void SpellCheckProfile::SpellCheckHostInitialized() { | |
133 host_ready_ = !!host_.get() && host_->IsReady(); | |
134 } | |
135 | |
89 const SpellCheckProfile::CustomWordList& | 136 const SpellCheckProfile::CustomWordList& |
90 SpellCheckProfile::GetCustomWords() const { | 137 SpellCheckProfile::GetCustomWords() const { |
91 return custom_words_.get() ? *custom_words_ : g_empty_list.Get(); | 138 return custom_words_.get() ? *custom_words_ : g_empty_list.Get(); |
92 } | 139 } |
93 | 140 |
94 void SpellCheckProfile::CustomWordAddedLocally(const std::string& word) { | 141 void SpellCheckProfile::CustomWordAddedLocally(const std::string& word) { |
142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || IsTesting()); | |
143 | |
95 if (!custom_words_.get()) | 144 if (!custom_words_.get()) |
96 custom_words_.reset(new CustomWordList()); | 145 custom_words_.reset(new CustomWordList()); |
97 custom_words_->push_back(word); | 146 custom_words_->push_back(word); |
98 if (metrics_.get()) | 147 if (metrics_.get()) |
99 metrics_->RecordCustomWordCountStats(custom_words_->size()); | 148 metrics_->RecordCustomWordCountStats(custom_words_->size()); |
149 | |
150 WriteWordToCustomDictionary(word); | |
100 } | 151 } |
OLD | NEW |