Give names to all utility processes.
[chromium-blink-merge.git] / chrome / browser / spellchecker / spellcheck_service.cc
blobffc227336f94c057c68b6a905405cdf1fee2abe0
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/spellchecker/spellcheck_service.h"
7 #include "base/prefs/pref_member.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/strings/string_split.h"
10 #include "base/synchronization/waitable_event.h"
11 #include "chrome/browser/spellchecker/spellcheck_factory.h"
12 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h"
13 #include "chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h"
14 #include "chrome/browser/spellchecker/spellcheck_platform_mac.h"
15 #include "chrome/browser/spellchecker/spelling_service_client.h"
16 #include "chrome/common/pref_names.h"
17 #include "chrome/common/spellcheck_messages.h"
18 #include "components/user_prefs/user_prefs.h"
19 #include "content/public/browser/browser_context.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "content/public/browser/notification_service.h"
22 #include "content/public/browser/notification_types.h"
23 #include "content/public/browser/render_process_host.h"
24 #include "ipc/ipc_platform_file.h"
26 using content::BrowserThread;
27 using chrome::spellcheck_common::WordList;
29 // TODO(rlp): I do not like globals, but keeping these for now during
30 // transition.
31 // An event used by browser tests to receive status events from this class and
32 // its derived classes.
33 base::WaitableEvent* g_status_event = NULL;
34 SpellcheckService::EventType g_status_type =
35 SpellcheckService::BDICT_NOTINITIALIZED;
37 SpellcheckService::SpellcheckService(content::BrowserContext* context)
38 : context_(context),
39 weak_ptr_factory_(this) {
40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
41 PrefService* prefs = user_prefs::UserPrefs::Get(context);
42 pref_change_registrar_.Init(prefs);
44 std::string language_code;
45 std::string country_code;
46 chrome::spellcheck_common::GetISOLanguageCountryCodeFromLocale(
47 prefs->GetString(prefs::kSpellCheckDictionary),
48 &language_code,
49 &country_code);
50 feedback_sender_.reset(new spellcheck::FeedbackSender(
51 context->GetRequestContext(), language_code, country_code));
53 pref_change_registrar_.Add(
54 prefs::kEnableAutoSpellCorrect,
55 base::Bind(&SpellcheckService::OnEnableAutoSpellCorrectChanged,
56 base::Unretained(this)));
57 pref_change_registrar_.Add(
58 prefs::kSpellCheckDictionary,
59 base::Bind(&SpellcheckService::OnSpellCheckDictionaryChanged,
60 base::Unretained(this)));
61 pref_change_registrar_.Add(
62 prefs::kSpellCheckUseSpellingService,
63 base::Bind(&SpellcheckService::OnUseSpellingServiceChanged,
64 base::Unretained(this)));
65 pref_change_registrar_.Add(
66 prefs::kEnableContinuousSpellcheck,
67 base::Bind(&SpellcheckService::InitForAllRenderers,
68 base::Unretained(this)));
70 OnSpellCheckDictionaryChanged();
72 custom_dictionary_.reset(new SpellcheckCustomDictionary(context_->GetPath()));
73 custom_dictionary_->AddObserver(this);
74 custom_dictionary_->Load();
76 registrar_.Add(this,
77 content::NOTIFICATION_RENDERER_PROCESS_CREATED,
78 content::NotificationService::AllSources());
81 SpellcheckService::~SpellcheckService() {
82 // Remove pref observers
83 pref_change_registrar_.RemoveAll();
86 base::WeakPtr<SpellcheckService> SpellcheckService::GetWeakPtr() {
87 return weak_ptr_factory_.GetWeakPtr();
90 // static
91 int SpellcheckService::GetSpellCheckLanguages(
92 content::BrowserContext* context,
93 std::vector<std::string>* languages) {
94 PrefService* prefs = user_prefs::UserPrefs::Get(context);
95 StringPrefMember accept_languages_pref;
96 StringPrefMember dictionary_language_pref;
97 accept_languages_pref.Init(prefs::kAcceptLanguages, prefs);
98 dictionary_language_pref.Init(prefs::kSpellCheckDictionary, prefs);
99 std::string dictionary_language = dictionary_language_pref.GetValue();
101 // Now scan through the list of accept languages, and find possible mappings
102 // from this list to the existing list of spell check languages.
103 std::vector<std::string> accept_languages;
105 #if defined(OS_MACOSX)
106 if (spellcheck_mac::SpellCheckerAvailable())
107 spellcheck_mac::GetAvailableLanguages(&accept_languages);
108 else
109 base::SplitString(accept_languages_pref.GetValue(), ',', &accept_languages);
110 #else
111 base::SplitString(accept_languages_pref.GetValue(), ',', &accept_languages);
112 #endif // !OS_MACOSX
114 GetSpellCheckLanguagesFromAcceptLanguages(
115 accept_languages, dictionary_language, languages);
117 for (size_t i = 0; i < languages->size(); ++i) {
118 if ((*languages)[i] == dictionary_language)
119 return i;
121 return -1;
124 // static
125 void SpellcheckService::GetSpellCheckLanguagesFromAcceptLanguages(
126 const std::vector<std::string>& accept_languages,
127 const std::string& dictionary_language,
128 std::vector<std::string>* languages) {
129 // The current dictionary language should be there.
130 languages->push_back(dictionary_language);
132 for (std::vector<std::string>::const_iterator i = accept_languages.begin();
133 i != accept_languages.end(); ++i) {
134 std::string language =
135 chrome::spellcheck_common::GetCorrespondingSpellCheckLanguage(*i);
136 if (!language.empty() &&
137 std::find(languages->begin(), languages->end(), language) ==
138 languages->end()) {
139 languages->push_back(language);
144 // static
145 bool SpellcheckService::SignalStatusEvent(
146 SpellcheckService::EventType status_type) {
147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
149 if (!g_status_event)
150 return false;
151 g_status_type = status_type;
152 g_status_event->Signal();
153 return true;
156 void SpellcheckService::StartRecordingMetrics(bool spellcheck_enabled) {
157 metrics_.reset(new SpellCheckHostMetrics());
158 metrics_->RecordEnabledStats(spellcheck_enabled);
159 OnUseSpellingServiceChanged();
162 void SpellcheckService::InitForRenderer(content::RenderProcessHost* process) {
163 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
165 content::BrowserContext* context = process->GetBrowserContext();
166 if (SpellcheckServiceFactory::GetForContext(context) != this)
167 return;
169 PrefService* prefs = user_prefs::UserPrefs::Get(context);
170 IPC::PlatformFileForTransit file = IPC::InvalidPlatformFileForTransit();
172 if (hunspell_dictionary_->GetDictionaryFile().IsValid()) {
173 file = IPC::GetFileHandleForProcess(
174 hunspell_dictionary_->GetDictionaryFile().GetPlatformFile(),
175 process->GetHandle(), false);
178 process->Send(new SpellCheckMsg_Init(
179 file,
180 custom_dictionary_->GetWords(),
181 hunspell_dictionary_->GetLanguage(),
182 prefs->GetBoolean(prefs::kEnableAutoSpellCorrect)));
183 process->Send(new SpellCheckMsg_EnableSpellCheck(
184 prefs->GetBoolean(prefs::kEnableContinuousSpellcheck)));
187 SpellCheckHostMetrics* SpellcheckService::GetMetrics() const {
188 return metrics_.get();
191 SpellcheckCustomDictionary* SpellcheckService::GetCustomDictionary() {
192 return custom_dictionary_.get();
195 SpellcheckHunspellDictionary* SpellcheckService::GetHunspellDictionary() {
196 return hunspell_dictionary_.get();
199 spellcheck::FeedbackSender* SpellcheckService::GetFeedbackSender() {
200 return feedback_sender_.get();
203 bool SpellcheckService::LoadExternalDictionary(std::string language,
204 std::string locale,
205 std::string path,
206 DictionaryFormat format) {
207 return false;
210 bool SpellcheckService::UnloadExternalDictionary(std::string path) {
211 return false;
214 void SpellcheckService::Observe(int type,
215 const content::NotificationSource& source,
216 const content::NotificationDetails& details) {
217 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_CREATED);
218 content::RenderProcessHost* process =
219 content::Source<content::RenderProcessHost>(source).ptr();
220 InitForRenderer(process);
223 void SpellcheckService::OnCustomDictionaryLoaded() {
224 InitForAllRenderers();
227 void SpellcheckService::OnCustomDictionaryChanged(
228 const SpellcheckCustomDictionary::Change& dictionary_change) {
229 for (content::RenderProcessHost::iterator i(
230 content::RenderProcessHost::AllHostsIterator());
231 !i.IsAtEnd(); i.Advance()) {
232 i.GetCurrentValue()->Send(new SpellCheckMsg_CustomDictionaryChanged(
233 dictionary_change.to_add(),
234 dictionary_change.to_remove()));
238 void SpellcheckService::OnHunspellDictionaryInitialized() {
239 InitForAllRenderers();
242 void SpellcheckService::OnHunspellDictionaryDownloadBegin() {
245 void SpellcheckService::OnHunspellDictionaryDownloadSuccess() {
248 void SpellcheckService::OnHunspellDictionaryDownloadFailure() {
251 // static
252 void SpellcheckService::AttachStatusEvent(base::WaitableEvent* status_event) {
253 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
255 g_status_event = status_event;
258 // static
259 SpellcheckService::EventType SpellcheckService::GetStatusEvent() {
260 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
261 return g_status_type;
264 void SpellcheckService::InitForAllRenderers() {
265 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
266 for (content::RenderProcessHost::iterator i(
267 content::RenderProcessHost::AllHostsIterator());
268 !i.IsAtEnd(); i.Advance()) {
269 content::RenderProcessHost* process = i.GetCurrentValue();
270 if (process && process->GetHandle())
271 InitForRenderer(process);
275 void SpellcheckService::OnEnableAutoSpellCorrectChanged() {
276 bool enabled = pref_change_registrar_.prefs()->GetBoolean(
277 prefs::kEnableAutoSpellCorrect);
278 for (content::RenderProcessHost::iterator i(
279 content::RenderProcessHost::AllHostsIterator());
280 !i.IsAtEnd(); i.Advance()) {
281 content::RenderProcessHost* process = i.GetCurrentValue();
282 process->Send(new SpellCheckMsg_EnableAutoSpellCorrect(enabled));
286 void SpellcheckService::OnSpellCheckDictionaryChanged() {
287 if (hunspell_dictionary_.get())
288 hunspell_dictionary_->RemoveObserver(this);
289 PrefService* prefs = user_prefs::UserPrefs::Get(context_);
290 DCHECK(prefs);
292 std::string dictionary =
293 prefs->GetString(prefs::kSpellCheckDictionary);
294 hunspell_dictionary_.reset(new SpellcheckHunspellDictionary(
295 dictionary, context_->GetRequestContext(), this));
296 hunspell_dictionary_->AddObserver(this);
297 hunspell_dictionary_->Load();
298 std::string language_code;
299 std::string country_code;
300 chrome::spellcheck_common::GetISOLanguageCountryCodeFromLocale(
301 dictionary, &language_code, &country_code);
302 feedback_sender_->OnLanguageCountryChange(language_code, country_code);
303 UpdateFeedbackSenderState();
306 void SpellcheckService::OnUseSpellingServiceChanged() {
307 bool enabled = pref_change_registrar_.prefs()->GetBoolean(
308 prefs::kSpellCheckUseSpellingService);
309 if (metrics_)
310 metrics_->RecordSpellingServiceStats(enabled);
311 UpdateFeedbackSenderState();
314 void SpellcheckService::UpdateFeedbackSenderState() {
315 if (SpellingServiceClient::IsAvailable(
316 context_, SpellingServiceClient::SPELLCHECK)) {
317 feedback_sender_->StartFeedbackCollection();
318 } else {
319 feedback_sender_->StopFeedbackCollection();