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/platform_file.h"
8 #include "base/prefs/pref_member.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/strings/string_split.h"
11 #include "base/synchronization/waitable_event.h"
12 #include "chrome/browser/spellchecker/spellcheck_factory.h"
13 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h"
14 #include "chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h"
15 #include "chrome/browser/spellchecker/spellcheck_platform_mac.h"
16 #include "chrome/browser/spellchecker/spelling_service_client.h"
17 #include "chrome/common/pref_names.h"
18 #include "chrome/common/spellcheck_messages.h"
19 #include "components/user_prefs/user_prefs.h"
20 #include "content/public/browser/browser_context.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/notification_service.h"
23 #include "content/public/browser/notification_types.h"
24 #include "content/public/browser/render_process_host.h"
25 #include "ipc/ipc_platform_file.h"
27 using content::BrowserThread
;
28 using chrome::spellcheck_common::WordList
;
30 // TODO(rlp): I do not like globals, but keeping these for now during
32 // An event used by browser tests to receive status events from this class and
33 // its derived classes.
34 base::WaitableEvent
* g_status_event
= NULL
;
35 SpellcheckService::EventType g_status_type
=
36 SpellcheckService::BDICT_NOTINITIALIZED
;
38 SpellcheckService::SpellcheckService(content::BrowserContext
* context
)
40 weak_ptr_factory_(this) {
41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
42 PrefService
* prefs
= user_prefs::UserPrefs::Get(context
);
43 pref_change_registrar_
.Init(prefs
);
45 std::string language_code
;
46 std::string country_code
;
47 chrome::spellcheck_common::GetISOLanguageCountryCodeFromLocale(
48 prefs
->GetString(prefs::kSpellCheckDictionary
),
51 feedback_sender_
.reset(new spellcheck::FeedbackSender(
52 context
->GetRequestContext(), language_code
, country_code
));
54 pref_change_registrar_
.Add(
55 prefs::kEnableAutoSpellCorrect
,
56 base::Bind(&SpellcheckService::OnEnableAutoSpellCorrectChanged
,
57 base::Unretained(this)));
58 pref_change_registrar_
.Add(
59 prefs::kSpellCheckDictionary
,
60 base::Bind(&SpellcheckService::OnSpellCheckDictionaryChanged
,
61 base::Unretained(this)));
62 pref_change_registrar_
.Add(
63 prefs::kSpellCheckUseSpellingService
,
64 base::Bind(&SpellcheckService::OnUseSpellingServiceChanged
,
65 base::Unretained(this)));
66 pref_change_registrar_
.Add(
67 prefs::kEnableContinuousSpellcheck
,
68 base::Bind(&SpellcheckService::InitForAllRenderers
,
69 base::Unretained(this)));
71 OnSpellCheckDictionaryChanged();
73 custom_dictionary_
.reset(new SpellcheckCustomDictionary(context_
->GetPath()));
74 custom_dictionary_
->AddObserver(this);
75 custom_dictionary_
->Load();
78 content::NOTIFICATION_RENDERER_PROCESS_CREATED
,
79 content::NotificationService::AllSources());
82 SpellcheckService::~SpellcheckService() {
83 // Remove pref observers
84 pref_change_registrar_
.RemoveAll();
88 int SpellcheckService::GetSpellCheckLanguages(
89 content::BrowserContext
* context
,
90 std::vector
<std::string
>* languages
) {
91 PrefService
* prefs
= user_prefs::UserPrefs::Get(context
);
92 StringPrefMember accept_languages_pref
;
93 StringPrefMember dictionary_language_pref
;
94 accept_languages_pref
.Init(prefs::kAcceptLanguages
, prefs
);
95 dictionary_language_pref
.Init(prefs::kSpellCheckDictionary
, prefs
);
96 std::string dictionary_language
= dictionary_language_pref
.GetValue();
98 // Now scan through the list of accept languages, and find possible mappings
99 // from this list to the existing list of spell check languages.
100 std::vector
<std::string
> accept_languages
;
102 #if defined(OS_MACOSX)
103 if (spellcheck_mac::SpellCheckerAvailable())
104 spellcheck_mac::GetAvailableLanguages(&accept_languages
);
106 base::SplitString(accept_languages_pref
.GetValue(), ',', &accept_languages
);
108 base::SplitString(accept_languages_pref
.GetValue(), ',', &accept_languages
);
111 GetSpellCheckLanguagesFromAcceptLanguages(
112 accept_languages
, dictionary_language
, languages
);
114 for (size_t i
= 0; i
< languages
->size(); ++i
) {
115 if ((*languages
)[i
] == dictionary_language
)
122 void SpellcheckService::GetSpellCheckLanguagesFromAcceptLanguages(
123 const std::vector
<std::string
>& accept_languages
,
124 const std::string
& dictionary_language
,
125 std::vector
<std::string
>* languages
) {
126 // The current dictionary language should be there.
127 languages
->push_back(dictionary_language
);
129 for (std::vector
<std::string
>::const_iterator i
= accept_languages
.begin();
130 i
!= accept_languages
.end(); ++i
) {
131 std::string language
=
132 chrome::spellcheck_common::GetCorrespondingSpellCheckLanguage(*i
);
133 if (!language
.empty() &&
134 std::find(languages
->begin(), languages
->end(), language
) ==
136 languages
->push_back(language
);
142 bool SpellcheckService::SignalStatusEvent(
143 SpellcheckService::EventType status_type
) {
144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
148 g_status_type
= status_type
;
149 g_status_event
->Signal();
153 void SpellcheckService::StartRecordingMetrics(bool spellcheck_enabled
) {
154 metrics_
.reset(new SpellCheckHostMetrics());
155 metrics_
->RecordEnabledStats(spellcheck_enabled
);
156 OnUseSpellingServiceChanged();
159 void SpellcheckService::InitForRenderer(content::RenderProcessHost
* process
) {
160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
162 content::BrowserContext
* context
= process
->GetBrowserContext();
163 if (SpellcheckServiceFactory::GetForContext(context
) != this)
166 PrefService
* prefs
= user_prefs::UserPrefs::Get(context
);
167 IPC::PlatformFileForTransit file
= IPC::InvalidPlatformFileForTransit();
169 if (hunspell_dictionary_
->GetDictionaryFile() !=
170 base::kInvalidPlatformFileValue
) {
171 file
= IPC::GetFileHandleForProcess(
172 hunspell_dictionary_
->GetDictionaryFile(), process
->GetHandle(), false);
175 process
->Send(new SpellCheckMsg_Init(
177 custom_dictionary_
->GetWords(),
178 hunspell_dictionary_
->GetLanguage(),
179 prefs
->GetBoolean(prefs::kEnableAutoSpellCorrect
)));
180 process
->Send(new SpellCheckMsg_EnableSpellCheck(
181 prefs
->GetBoolean(prefs::kEnableContinuousSpellcheck
)));
184 SpellCheckHostMetrics
* SpellcheckService::GetMetrics() const {
185 return metrics_
.get();
188 SpellcheckCustomDictionary
* SpellcheckService::GetCustomDictionary() {
189 return custom_dictionary_
.get();
192 SpellcheckHunspellDictionary
* SpellcheckService::GetHunspellDictionary() {
193 return hunspell_dictionary_
.get();
196 spellcheck::FeedbackSender
* SpellcheckService::GetFeedbackSender() {
197 return feedback_sender_
.get();
200 bool SpellcheckService::LoadExternalDictionary(std::string language
,
203 DictionaryFormat format
) {
207 bool SpellcheckService::UnloadExternalDictionary(std::string path
) {
211 void SpellcheckService::Observe(int type
,
212 const content::NotificationSource
& source
,
213 const content::NotificationDetails
& details
) {
214 DCHECK(type
== content::NOTIFICATION_RENDERER_PROCESS_CREATED
);
215 content::RenderProcessHost
* process
=
216 content::Source
<content::RenderProcessHost
>(source
).ptr();
217 InitForRenderer(process
);
220 void SpellcheckService::OnCustomDictionaryLoaded() {
221 InitForAllRenderers();
224 void SpellcheckService::OnCustomDictionaryChanged(
225 const SpellcheckCustomDictionary::Change
& dictionary_change
) {
226 for (content::RenderProcessHost::iterator
i(
227 content::RenderProcessHost::AllHostsIterator());
228 !i
.IsAtEnd(); i
.Advance()) {
229 i
.GetCurrentValue()->Send(new SpellCheckMsg_CustomDictionaryChanged(
230 dictionary_change
.to_add(),
231 dictionary_change
.to_remove()));
235 void SpellcheckService::OnHunspellDictionaryInitialized() {
236 InitForAllRenderers();
239 void SpellcheckService::OnHunspellDictionaryDownloadBegin() {
242 void SpellcheckService::OnHunspellDictionaryDownloadSuccess() {
245 void SpellcheckService::OnHunspellDictionaryDownloadFailure() {
249 void SpellcheckService::AttachStatusEvent(base::WaitableEvent
* status_event
) {
250 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
252 g_status_event
= status_event
;
256 SpellcheckService::EventType
SpellcheckService::GetStatusEvent() {
257 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
258 return g_status_type
;
261 void SpellcheckService::InitForAllRenderers() {
262 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
263 for (content::RenderProcessHost::iterator
i(
264 content::RenderProcessHost::AllHostsIterator());
265 !i
.IsAtEnd(); i
.Advance()) {
266 content::RenderProcessHost
* process
= i
.GetCurrentValue();
267 if (process
&& process
->GetHandle())
268 InitForRenderer(process
);
272 void SpellcheckService::OnEnableAutoSpellCorrectChanged() {
273 bool enabled
= pref_change_registrar_
.prefs()->GetBoolean(
274 prefs::kEnableAutoSpellCorrect
);
275 for (content::RenderProcessHost::iterator
i(
276 content::RenderProcessHost::AllHostsIterator());
277 !i
.IsAtEnd(); i
.Advance()) {
278 content::RenderProcessHost
* process
= i
.GetCurrentValue();
279 process
->Send(new SpellCheckMsg_EnableAutoSpellCorrect(enabled
));
283 void SpellcheckService::OnSpellCheckDictionaryChanged() {
284 if (hunspell_dictionary_
.get())
285 hunspell_dictionary_
->RemoveObserver(this);
286 PrefService
* prefs
= user_prefs::UserPrefs::Get(context_
);
289 std::string dictionary
=
290 prefs
->GetString(prefs::kSpellCheckDictionary
);
291 hunspell_dictionary_
.reset(new SpellcheckHunspellDictionary(
292 dictionary
, context_
->GetRequestContext(), this));
293 hunspell_dictionary_
->AddObserver(this);
294 hunspell_dictionary_
->Load();
295 std::string language_code
;
296 std::string country_code
;
297 chrome::spellcheck_common::GetISOLanguageCountryCodeFromLocale(
298 dictionary
, &language_code
, &country_code
);
299 feedback_sender_
->OnLanguageCountryChange(language_code
, country_code
);
300 UpdateFeedbackSenderState();
303 void SpellcheckService::OnUseSpellingServiceChanged() {
304 bool enabled
= pref_change_registrar_
.prefs()->GetBoolean(
305 prefs::kSpellCheckUseSpellingService
);
307 metrics_
->RecordSpellingServiceStats(enabled
);
308 UpdateFeedbackSenderState();
311 void SpellcheckService::UpdateFeedbackSenderState() {
312 if (SpellingServiceClient::IsAvailable(
313 context_
, SpellingServiceClient::SPELLCHECK
)) {
314 feedback_sender_
->StartFeedbackCollection();
316 feedback_sender_
->StopFeedbackCollection();