1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/spellchecker/spellcheck_service.h"
#include "base/platform_file.h"
#include "base/string_split.h"
#include "base/synchronization/waitable_event.h"
#include "chrome/browser/api/prefs/pref_member.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/spellchecker/spellcheck_host_metrics.h"
#include "chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h"
#include "chrome/browser/spellchecker/spellcheck_factory.h"
#include "chrome/browser/spellchecker/spellcheck_platform_mac.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/spellcheck_messages.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/render_process_host.h"
#include "ipc/ipc_platform_file.h"
using content::BrowserThread;
using chrome::spellcheck_common::WordList;
// TODO(rlp): I do not like globals, but keeping thsese for now during
// transition.
// An event used by browser tests to receive status events from this class and
// its derived classes.
base::WaitableEvent* g_status_event = NULL;
SpellcheckService::EventType g_status_type =
SpellcheckService::BDICT_NOTINITIALIZED;
SpellcheckService::SpellcheckService(Profile* profile)
: profile_(profile),
weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
PrefService* prefs = profile_->GetPrefs();
pref_change_registrar_.Init(prefs);
pref_change_registrar_.Add(
prefs::kEnableAutoSpellCorrect,
base::Bind(&SpellcheckService::OnEnableAutoSpellCorrectChanged,
base::Unretained(this)));
base::Closure init_for_all_renderers_callback = base::Bind(
&SpellcheckService::InitForAllRenderers, base::Unretained(this));
pref_change_registrar_.Add(prefs::kSpellCheckDictionary,
init_for_all_renderers_callback);
pref_change_registrar_.Add(prefs::kEnableSpellCheck,
init_for_all_renderers_callback);
hunspell_dictionary_.reset(new SpellcheckHunspellDictionary(
profile, prefs->GetString(prefs::kSpellCheckDictionary),
profile->GetRequestContext(), this));
hunspell_dictionary_->Load();
custom_dictionary_.reset(new SpellcheckCustomDictionary(profile_));
custom_dictionary_->AddObserver(this);
custom_dictionary_->Load();
registrar_.Add(weak_ptr_factory_.GetWeakPtr(),
content::NOTIFICATION_RENDERER_PROCESS_CREATED,
content::NotificationService::AllSources());
}
SpellcheckService::~SpellcheckService() {
// Remove pref observers
pref_change_registrar_.RemoveAll();
}
// static
int SpellcheckService::GetSpellCheckLanguages(
Profile* profile,
std::vector<std::string>* languages) {
StringPrefMember accept_languages_pref;
StringPrefMember dictionary_language_pref;
accept_languages_pref.Init(prefs::kAcceptLanguages, profile->GetPrefs(),
NULL);
dictionary_language_pref.Init(prefs::kSpellCheckDictionary,
profile->GetPrefs(), NULL);
std::string dictionary_language = dictionary_language_pref.GetValue();
// Now scan through the list of accept languages, and find possible mappings
// from this list to the existing list of spell check languages.
std::vector<std::string> accept_languages;
#if defined(OS_MACOSX)
if (spellcheck_mac::SpellCheckerAvailable())
spellcheck_mac::GetAvailableLanguages(&accept_languages);
else
base::SplitString(accept_languages_pref.GetValue(), ',', &accept_languages);
#else
base::SplitString(accept_languages_pref.GetValue(), ',', &accept_languages);
#endif // !OS_MACOSX
GetSpellCheckLanguagesFromAcceptLanguages(
accept_languages, dictionary_language, languages);
for (size_t i = 0; i < languages->size(); ++i) {
if ((*languages)[i] == dictionary_language)
return i;
}
return -1;
}
// static
void SpellcheckService::GetSpellCheckLanguagesFromAcceptLanguages(
const std::vector<std::string>& accept_languages,
const std::string& dictionary_language,
std::vector<std::string>* languages) {
// The current dictionary language should be there.
languages->push_back(dictionary_language);
for (std::vector<std::string>::const_iterator i = accept_languages.begin();
i != accept_languages.end(); ++i) {
std::string language =
chrome::spellcheck_common::GetCorrespondingSpellCheckLanguage(*i);
if (!language.empty() &&
std::find(languages->begin(), languages->end(), language) ==
languages->end()) {
languages->push_back(language);
}
}
}
// static
bool SpellcheckService::SignalStatusEvent(
SpellcheckService::EventType status_type) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
if (!g_status_event)
return false;
g_status_type = status_type;
g_status_event->Signal();
return true;
}
void SpellcheckService::StartRecordingMetrics(bool spellcheck_enabled) {
metrics_.reset(new SpellCheckHostMetrics());
metrics_->RecordEnabledStats(spellcheck_enabled);
}
void SpellcheckService::InitForAllRenderers() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
for (content::RenderProcessHost::iterator i(
content::RenderProcessHost::AllHostsIterator());
!i.IsAtEnd(); i.Advance()) {
content::RenderProcessHost* process = i.GetCurrentValue();
if (process)
InitForRenderer(process);
}
}
void SpellcheckService::InitForRenderer(content::RenderProcessHost* process) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
Profile* profile = Profile::FromBrowserContext(process->GetBrowserContext());
if (SpellcheckServiceFactory::GetForProfile(profile) != this)
return;
PrefService* prefs = profile->GetPrefs();
IPC::PlatformFileForTransit file = IPC::InvalidPlatformFileForTransit();
if (hunspell_dictionary_->GetDictionaryFile() !=
base::kInvalidPlatformFileValue) {
#if defined(OS_POSIX)
file = base::FileDescriptor(hunspell_dictionary_->GetDictionaryFile(),
false);
#elif defined(OS_WIN)
BOOL ok = ::DuplicateHandle(::GetCurrentProcess(),
hunspell_dictionary_->GetDictionaryFile(),
process->GetHandle(),
&file,
0,
false,
DUPLICATE_SAME_ACCESS);
DCHECK(ok) << ::GetLastError();
#endif
}
process->Send(new SpellCheckMsg_Init(
file,
custom_dictionary_->GetWords(),
hunspell_dictionary_->GetLanguage(),
prefs->GetBoolean(prefs::kEnableAutoSpellCorrect)));
}
SpellCheckHostMetrics* SpellcheckService::GetMetrics() const {
return metrics_.get();
}
SpellcheckCustomDictionary* SpellcheckService::GetCustomDictionary() {
return custom_dictionary_.get();
}
void SpellcheckService::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_CREATED);
content::RenderProcessHost* process =
content::Source<content::RenderProcessHost>(source).ptr();
InitForRenderer(process);
}
void SpellcheckService::OnEnableAutoSpellCorrectChanged() {
bool enabled = pref_change_registrar_.prefs()->GetBoolean(
prefs::kEnableAutoSpellCorrect);
for (content::RenderProcessHost::iterator i(
content::RenderProcessHost::AllHostsIterator());
!i.IsAtEnd(); i.Advance()) {
content::RenderProcessHost* process = i.GetCurrentValue();
process->Send(new SpellCheckMsg_EnableAutoSpellCorrect(enabled));
}
}
void SpellcheckService::OnCustomDictionaryLoaded() {
InitForAllRenderers();
}
void SpellcheckService::OnCustomDictionaryWordAdded(const std::string& word) {
}
void SpellcheckService::OnCustomDictionaryWordRemoved(const std::string& word) {
}
// static
void SpellcheckService::AttachStatusEvent(base::WaitableEvent* status_event) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
g_status_event = status_event;
}
// static
SpellcheckService::EventType SpellcheckService::WaitStatusEvent() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (g_status_event)
g_status_event->Wait();
return g_status_type;
}
|