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" |
| 10 #include "base/string_util.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" |
| 15 #include "chrome/common/chrome_constants.h" |
12 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.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(const FilePath& profile_dir) |
20 : host_ready_(false) { | 24 : host_ready_(false), |
| 25 profile_dir_(profile_dir) { |
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 file_util::ReadFileToString(GetCustomDictionaryPath(), &contents); |
| 115 if (contents.empty()) |
| 116 return; |
| 117 |
| 118 CustomWordList list_of_words; |
| 119 base::SplitString(contents, '\n', &list_of_words); |
| 120 for (size_t i = 0; i < list_of_words.size(); ++i) { |
| 121 if (list_of_words[i] != "") |
| 122 custom_words->push_back(list_of_words[i]); |
| 123 } |
| 124 } |
| 125 |
| 126 void SpellCheckProfile::WriteWordToCustomDictionary(const std::string& word) { |
| 127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE) || IsTesting()); |
| 128 |
| 129 // Stored in UTF-8. |
| 130 DCHECK(IsStringUTF8(word)); |
| 131 |
| 132 std::string word_to_add(word + "\n"); |
| 133 FILE* f = file_util::OpenFile(GetCustomDictionaryPath(), "a+"); |
| 134 if (f) { |
| 135 fputs(word_to_add.c_str(), f); |
| 136 file_util::CloseFile(f); |
| 137 } |
| 138 } |
| 139 |
| 140 const FilePath& SpellCheckProfile::GetCustomDictionaryPath() { |
| 141 if (!custom_dictionary_path_.get()) { |
| 142 custom_dictionary_path_.reset( |
| 143 new FilePath(profile_dir_.Append(chrome::kCustomDictionaryFileName))); |
| 144 } |
| 145 |
| 146 return *custom_dictionary_path_; |
| 147 } |
OLD | NEW |