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

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

Issue 9289007: Profiles: Move the Spelling system onto a ProfileKeyedServiceFactory. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Remove stray mark Created 8 years, 11 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
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/file_util.h"
8 #include "base/lazy_instance.h" 8 #include "base/lazy_instance.h"
9 #include "base/logging.h"
9 #include "base/string_split.h" 10 #include "base/string_split.h"
10 #include "base/string_util.h" 11 #include "base/string_util.h"
11 #include "chrome/browser/prefs/pref_service.h" 12 #include "chrome/browser/prefs/pref_service.h"
12 #include "chrome/browser/profiles/profile.h" 13 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/spellchecker/spellcheck_host.h" 14 #include "chrome/browser/spellchecker/spellcheck_host.h"
14 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" 15 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h"
15 #include "chrome/common/chrome_constants.h" 16 #include "chrome/common/chrome_constants.h"
17 #include "chrome/common/chrome_notification_types.h"
16 #include "chrome/common/pref_names.h" 18 #include "chrome/common/pref_names.h"
19 #include "chrome/common/spellcheck_messages.h"
20 #include "content/public/browser/notification_service.h"
21 #include "content/public/browser/render_process_host.h"
17 22
18 using content::BrowserThread; 23 using content::BrowserThread;
19 24
20 namespace { 25 namespace {
21 base::LazyInstance<SpellCheckProfile::CustomWordList> g_empty_list = 26 base::LazyInstance<SpellCheckProfile::CustomWordList> g_empty_list =
22 LAZY_INSTANCE_INITIALIZER; 27 LAZY_INSTANCE_INITIALIZER;
23 } // namespace 28 } // namespace
24 29
25 SpellCheckProfile::SpellCheckProfile(const FilePath& profile_dir) 30 SpellCheckProfile::SpellCheckProfile(Profile* profile)
Hajime Morrita 2012/01/25 01:52:38 It's a bit unfortunate that SpellCheckProfile has
26 : host_ready_(false), 31 : profile_(profile),
27 profile_dir_(profile_dir) { 32 host_ready_(false),
33 profile_dir_(profile->GetPath()) {
34 PrefService* prefs = profile_->GetPrefs();
35 pref_change_registrar_.Init(prefs);
36 pref_change_registrar_.Add(prefs::kSpellCheckDictionary, this);
37 pref_change_registrar_.Add(prefs::kEnableSpellCheck, this);
38 pref_change_registrar_.Add(prefs::kEnableAutoSpellCorrect, this);
28 } 39 }
29 40
30 SpellCheckProfile::~SpellCheckProfile() { 41 SpellCheckProfile::~SpellCheckProfile() {
31 if (host_.get()) 42 // Remove pref observers
32 host_->UnsetProfile(); 43 pref_change_registrar_.RemoveAll();
33 } 44 }
34 45
35 SpellCheckHost* SpellCheckProfile::GetHost() { 46 SpellCheckHost* SpellCheckProfile::GetHost() {
36 return host_ready_ ? host_.get() : NULL; 47 return host_ready_ ? host_.get() : NULL;
37 } 48 }
38 49
39 SpellCheckProfile::ReinitializeResult SpellCheckProfile::ReinitializeHost( 50 void SpellCheckProfile::ReinitializeSpellCheckHost(bool force) {
40 bool force, 51 PrefService* pref = profile_->GetPrefs();
41 bool enable, 52 SpellCheckProfile::ReinitializeResult result = ReinitializeHostImpl(
42 const std::string& language, 53 force,
43 net::URLRequestContextGetter* request_context) { 54 pref->GetBoolean(prefs::kEnableSpellCheck),
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || IsTesting()); 55 pref->GetString(prefs::kSpellCheckDictionary),
45 // If we are already loading the spellchecker, and this is just a hint to 56 profile_->GetRequestContext());
46 // load the spellchecker, do nothing. 57 if (result == SpellCheckProfile::REINITIALIZE_REMOVED_HOST) {
47 if (!force && host_.get()) 58 // The spellchecker has been disabled.
48 return REINITIALIZE_DID_NOTHING; 59 for (content::RenderProcessHost::iterator i(
49 60 content::RenderProcessHost::AllHostsIterator());
50 host_ready_ = false; 61 !i.IsAtEnd(); i.Advance()) {
51 62 content::RenderProcessHost* process = i.GetCurrentValue();
52 bool host_deleted = false; 63 process->Send(new SpellCheckMsg_Init(IPC::InvalidPlatformFileForTransit(),
53 if (host_.get()) { 64 std::vector<std::string>(),
54 host_->UnsetProfile(); 65 std::string(),
55 host_.reset(NULL); 66 false));
56 host_deleted = true; 67 }
57 } 68 }
58
59 if (enable) {
60 // Retrieve the (perhaps updated recently) dictionary name from preferences.
61 host_.reset(CreateHost(this, language, request_context, metrics_.get()));
62 return REINITIALIZE_CREATED_HOST;
63 }
64
65 return host_deleted ? REINITIALIZE_REMOVED_HOST : REINITIALIZE_DID_NOTHING;
66 } 69 }
67 70
68 SpellCheckHost* SpellCheckProfile::CreateHost( 71 SpellCheckHost* SpellCheckProfile::CreateHost(
69 SpellCheckProfileProvider* provider, 72 SpellCheckProfileProvider* provider,
70 const std::string& language, 73 const std::string& language,
71 net::URLRequestContextGetter* request_context, 74 net::URLRequestContextGetter* request_context,
72 SpellCheckHostMetrics* metrics) { 75 SpellCheckHostMetrics* metrics) {
73 return SpellCheckHost::Create( 76 return SpellCheckHost::Create(
74 provider, language, request_context, metrics); 77 provider, language, request_context, metrics);
75 } 78 }
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 DCHECK(IsStringUTF8(word)); 135 DCHECK(IsStringUTF8(word));
133 136
134 std::string word_to_add(word + "\n"); 137 std::string word_to_add(word + "\n");
135 FILE* f = file_util::OpenFile(GetCustomDictionaryPath(), "a+"); 138 FILE* f = file_util::OpenFile(GetCustomDictionaryPath(), "a+");
136 if (f) { 139 if (f) {
137 fputs(word_to_add.c_str(), f); 140 fputs(word_to_add.c_str(), f);
138 file_util::CloseFile(f); 141 file_util::CloseFile(f);
139 } 142 }
140 } 143 }
141 144
145 void SpellCheckProfile::Shutdown() {
146 if (host_.get())
147 host_->UnsetProfile();
148
149 profile_ = NULL;
150 }
151
152 void SpellCheckProfile::Observe(int type,
153 const content::NotificationSource& source,
154 const content::NotificationDetails& details) {
155 switch (type) {
156 case chrome::NOTIFICATION_PREF_CHANGED: {
157 std::string* pref_name_in = content::Details<std::string>(details).ptr();
158 PrefService* prefs = content::Source<PrefService>(source).ptr();
159 DCHECK(pref_name_in && prefs);
160 if (*pref_name_in == prefs::kSpellCheckDictionary ||
161 *pref_name_in == prefs::kEnableSpellCheck) {
162 ReinitializeSpellCheckHost(true);
163 } else if (*pref_name_in == prefs::kEnableAutoSpellCorrect) {
164 bool enabled = prefs->GetBoolean(prefs::kEnableAutoSpellCorrect);
165 for (content::RenderProcessHost::iterator i(
166 content::RenderProcessHost::AllHostsIterator());
167 !i.IsAtEnd(); i.Advance()) {
168 content::RenderProcessHost* process = i.GetCurrentValue();
169 process->Send(new SpellCheckMsg_EnableAutoSpellCorrect(enabled));
170 }
171 }
172 }
173 }
174 }
175
176 SpellCheckProfile::ReinitializeResult SpellCheckProfile::ReinitializeHostImpl(
177 bool force,
178 bool enable,
179 const std::string& language,
180 net::URLRequestContextGetter* request_context) {
181 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || IsTesting());
182 // If we are already loading the spellchecker, and this is just a hint to
183 // load the spellchecker, do nothing.
184 if (!force && host_.get())
185 return REINITIALIZE_DID_NOTHING;
186
187 host_ready_ = false;
188
189 bool host_deleted = false;
190 if (host_.get()) {
191 host_->UnsetProfile();
192 host_.reset(NULL);
193 host_deleted = true;
194 }
195
196 if (enable) {
197 // Retrieve the (perhaps updated recently) dictionary name from preferences.
198 host_.reset(CreateHost(this, language, request_context, metrics_.get()));
199 return REINITIALIZE_CREATED_HOST;
200 }
201
202 return host_deleted ? REINITIALIZE_REMOVED_HOST : REINITIALIZE_DID_NOTHING;
203 }
204
142 const FilePath& SpellCheckProfile::GetCustomDictionaryPath() { 205 const FilePath& SpellCheckProfile::GetCustomDictionaryPath() {
143 if (!custom_dictionary_path_.get()) { 206 if (!custom_dictionary_path_.get()) {
144 custom_dictionary_path_.reset( 207 custom_dictionary_path_.reset(
145 new FilePath(profile_dir_.Append(chrome::kCustomDictionaryFileName))); 208 new FilePath(profile_dir_.Append(chrome::kCustomDictionaryFileName)));
146 } 209 }
147 210
148 return *custom_dictionary_path_; 211 return *custom_dictionary_path_;
149 } 212 }
OLDNEW
« no previous file with comments | « chrome/browser/spellchecker/spellcheck_profile.h ('k') | chrome/browser/spellchecker/spellcheck_profile_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698