ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / spellchecker / spellcheck_service.cc
blobf40924e8c18e3ff48c2c981a1514e2ff5b7ce827
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 pref_change_registrar_.Add(
45 prefs::kEnableAutoSpellCorrect,
46 base::Bind(&SpellcheckService::OnEnableAutoSpellCorrectChanged,
47 base::Unretained(this)));
48 pref_change_registrar_.Add(
49 prefs::kSpellCheckDictionary,
50 base::Bind(&SpellcheckService::OnSpellCheckDictionaryChanged,
51 base::Unretained(this)));
52 pref_change_registrar_.Add(
53 prefs::kSpellCheckUseSpellingService,
54 base::Bind(&SpellcheckService::OnUseSpellingServiceChanged,
55 base::Unretained(this)));
56 pref_change_registrar_.Add(
57 prefs::kEnableContinuousSpellcheck,
58 base::Bind(&SpellcheckService::InitForAllRenderers,
59 base::Unretained(this)));
61 OnSpellCheckDictionaryChanged();
63 custom_dictionary_.reset(new SpellcheckCustomDictionary(context_->GetPath()));
64 custom_dictionary_->AddObserver(this);
65 custom_dictionary_->Load();
67 registrar_.Add(this,
68 content::NOTIFICATION_RENDERER_PROCESS_CREATED,
69 content::NotificationService::AllSources());
72 SpellcheckService::~SpellcheckService() {
73 // Remove pref observers
74 pref_change_registrar_.RemoveAll();
77 // static
78 int SpellcheckService::GetSpellCheckLanguages(
79 content::BrowserContext* context,
80 std::vector<std::string>* languages) {
81 PrefService* prefs = user_prefs::UserPrefs::Get(context);
82 StringPrefMember accept_languages_pref;
83 StringPrefMember dictionary_language_pref;
84 accept_languages_pref.Init(prefs::kAcceptLanguages, prefs);
85 dictionary_language_pref.Init(prefs::kSpellCheckDictionary, prefs);
86 std::string dictionary_language = dictionary_language_pref.GetValue();
88 // Now scan through the list of accept languages, and find possible mappings
89 // from this list to the existing list of spell check languages.
90 std::vector<std::string> accept_languages;
92 #if defined(OS_MACOSX)
93 if (spellcheck_mac::SpellCheckerAvailable())
94 spellcheck_mac::GetAvailableLanguages(&accept_languages);
95 else
96 base::SplitString(accept_languages_pref.GetValue(), ',', &accept_languages);
97 #else
98 base::SplitString(accept_languages_pref.GetValue(), ',', &accept_languages);
99 #endif // !OS_MACOSX
101 GetSpellCheckLanguagesFromAcceptLanguages(
102 accept_languages, dictionary_language, languages);
104 for (size_t i = 0; i < languages->size(); ++i) {
105 if ((*languages)[i] == dictionary_language)
106 return i;
108 return -1;
111 // static
112 void SpellcheckService::GetSpellCheckLanguagesFromAcceptLanguages(
113 const std::vector<std::string>& accept_languages,
114 const std::string& dictionary_language,
115 std::vector<std::string>* languages) {
116 // The current dictionary language should be there.
117 languages->push_back(dictionary_language);
119 for (std::vector<std::string>::const_iterator i = accept_languages.begin();
120 i != accept_languages.end(); ++i) {
121 std::string language =
122 chrome::spellcheck_common::GetCorrespondingSpellCheckLanguage(*i);
123 if (!language.empty() &&
124 std::find(languages->begin(), languages->end(), language) ==
125 languages->end()) {
126 languages->push_back(language);
131 // static
132 bool SpellcheckService::SignalStatusEvent(
133 SpellcheckService::EventType status_type) {
134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
136 if (!g_status_event)
137 return false;
138 g_status_type = status_type;
139 g_status_event->Signal();
140 return true;
143 void SpellcheckService::StartRecordingMetrics(bool spellcheck_enabled) {
144 metrics_.reset(new SpellCheckHostMetrics());
145 metrics_->RecordEnabledStats(spellcheck_enabled);
146 OnUseSpellingServiceChanged();
149 void SpellcheckService::InitForRenderer(content::RenderProcessHost* process) {
150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
152 content::BrowserContext* context = process->GetBrowserContext();
153 if (SpellcheckServiceFactory::GetForContext(context) != this)
154 return;
156 PrefService* prefs = user_prefs::UserPrefs::Get(context);
157 IPC::PlatformFileForTransit file = IPC::InvalidPlatformFileForTransit();
159 if (hunspell_dictionary_->GetDictionaryFile().IsValid()) {
160 file = IPC::GetFileHandleForProcess(
161 hunspell_dictionary_->GetDictionaryFile().GetPlatformFile(),
162 process->GetHandle(), false);
165 process->Send(new SpellCheckMsg_Init(
166 file,
167 custom_dictionary_->GetWords(),
168 hunspell_dictionary_->GetLanguage(),
169 prefs->GetBoolean(prefs::kEnableAutoSpellCorrect)));
170 process->Send(new SpellCheckMsg_EnableSpellCheck(
171 prefs->GetBoolean(prefs::kEnableContinuousSpellcheck)));
174 SpellCheckHostMetrics* SpellcheckService::GetMetrics() const {
175 return metrics_.get();
178 SpellcheckCustomDictionary* SpellcheckService::GetCustomDictionary() {
179 return custom_dictionary_.get();
182 SpellcheckHunspellDictionary* SpellcheckService::GetHunspellDictionary() {
183 return hunspell_dictionary_.get();
186 bool SpellcheckService::LoadExternalDictionary(std::string language,
187 std::string locale,
188 std::string path,
189 DictionaryFormat format) {
190 return false;
193 bool SpellcheckService::UnloadExternalDictionary(std::string path) {
194 return false;
197 void SpellcheckService::Observe(int type,
198 const content::NotificationSource& source,
199 const content::NotificationDetails& details) {
200 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_CREATED);
201 content::RenderProcessHost* process =
202 content::Source<content::RenderProcessHost>(source).ptr();
203 InitForRenderer(process);
206 void SpellcheckService::OnCustomDictionaryLoaded() {
207 InitForAllRenderers();
210 void SpellcheckService::OnCustomDictionaryChanged(
211 const SpellcheckCustomDictionary::Change& dictionary_change) {
212 for (content::RenderProcessHost::iterator i(
213 content::RenderProcessHost::AllHostsIterator());
214 !i.IsAtEnd(); i.Advance()) {
215 i.GetCurrentValue()->Send(new SpellCheckMsg_CustomDictionaryChanged(
216 dictionary_change.to_add(),
217 dictionary_change.to_remove()));
221 void SpellcheckService::OnHunspellDictionaryInitialized() {
222 InitForAllRenderers();
225 void SpellcheckService::OnHunspellDictionaryDownloadBegin() {
228 void SpellcheckService::OnHunspellDictionaryDownloadSuccess() {
231 void SpellcheckService::OnHunspellDictionaryDownloadFailure() {
234 // static
235 void SpellcheckService::AttachStatusEvent(base::WaitableEvent* status_event) {
236 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
238 g_status_event = status_event;
241 // static
242 SpellcheckService::EventType SpellcheckService::GetStatusEvent() {
243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
244 return g_status_type;
247 void SpellcheckService::InitForAllRenderers() {
248 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
249 for (content::RenderProcessHost::iterator i(
250 content::RenderProcessHost::AllHostsIterator());
251 !i.IsAtEnd(); i.Advance()) {
252 content::RenderProcessHost* process = i.GetCurrentValue();
253 if (process && process->GetHandle())
254 InitForRenderer(process);
258 void SpellcheckService::OnEnableAutoSpellCorrectChanged() {
259 bool enabled = pref_change_registrar_.prefs()->GetBoolean(
260 prefs::kEnableAutoSpellCorrect);
261 for (content::RenderProcessHost::iterator i(
262 content::RenderProcessHost::AllHostsIterator());
263 !i.IsAtEnd(); i.Advance()) {
264 content::RenderProcessHost* process = i.GetCurrentValue();
265 process->Send(new SpellCheckMsg_EnableAutoSpellCorrect(enabled));
269 void SpellcheckService::OnSpellCheckDictionaryChanged() {
270 if (hunspell_dictionary_.get())
271 hunspell_dictionary_->RemoveObserver(this);
272 PrefService* prefs = user_prefs::UserPrefs::Get(context_);
273 DCHECK(prefs);
275 std::string dictionary =
276 prefs->GetString(prefs::kSpellCheckDictionary);
277 hunspell_dictionary_.reset(new SpellcheckHunspellDictionary(
278 dictionary, context_->GetRequestContext(), this));
279 hunspell_dictionary_->AddObserver(this);
280 hunspell_dictionary_->Load();
283 void SpellcheckService::OnUseSpellingServiceChanged() {
284 bool enabled = pref_change_registrar_.prefs()->GetBoolean(
285 prefs::kSpellCheckUseSpellingService);
286 if (metrics_)
287 metrics_->RecordSpellingServiceStats(enabled);