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

Side by Side Diff: chrome/browser/spellchecker/spellcheck_profile.cc

Issue 8345034: SpellCheck: the custom dictionary should be per profile. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Fixed. 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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;
Hironori Bono 2011/10/20 04:48:50 nit: move this declaration above Line 116.
shinyak (Google) 2011/10/20 12:04:52 Done.
114 CustomWordList list_of_words;
115
116 file_util::ReadFileToString(GetCustomDictionaryPath(), &contents);
117 base::SplitString(contents, '\n', &list_of_words);
Hironori Bono 2011/10/20 04:48:50 nit: need 'if (!contents.empty()) {' to avoid call
shinyak (Google) 2011/10/20 12:04:52 Done.
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.
128 DCHECK(IsStringUTF8(word));
129
130 std::string word_to_add(word + "\n");
131 FILE* f = file_util::OpenFile(GetCustomDictionaryPath(), "a+");
132 if (f) {
133 fputs(word_to_add.c_str(), f);
134 file_util::CloseFile(f);
135 }
136 }
137
138 const FilePath& SpellCheckProfile::GetCustomDictionaryPath() {
139 if (custom_dictionary_path_.empty())
140 custom_dictionary_path_ = profile_dir_.Append(
141 chrome::kCustomDictionaryFileName);
Hironori Bono 2011/10/20 04:48:50 nit: I prefer wrapping this line before 'profile_d
shinyak (Google) 2011/10/20 12:04:52 Done.
142
143 return custom_dictionary_path_;
144 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698