forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mozPersonalDictionary.cpp
397 lines (322 loc) · 10.6 KB
/
mozPersonalDictionary.cpp
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozPersonalDictionary.h"
#include "nsIUnicharInputStream.h"
#include "nsReadableUtils.h"
#include "nsIFile.h"
#include "nsAppDirectoryServiceDefs.h"
#include "nsIObserverService.h"
#include "nsIPrefService.h"
#include "nsIPrefBranch.h"
#include "nsIWeakReference.h"
#include "nsCRT.h"
#include "nsNetUtil.h"
#include "nsStringEnumerator.h"
#include "nsUnicharInputStream.h"
#include "nsIRunnable.h"
#include "nsThreadUtils.h"
#include "nsProxyRelease.h"
#define MOZ_PERSONAL_DICT_NAME "persdict.dat"
/**
* This is the most braindead implementation of a personal dictionary possible.
* There is not much complexity needed, though. It could be made much faster,
* and probably should, but I don't see much need for more in terms of interface.
*
* Allowing personal words to be associated with only certain dictionaries maybe.
*
* TODO:
* Implement the suggestion record.
*/
NS_IMPL_CYCLE_COLLECTING_ADDREF(mozPersonalDictionary)
NS_IMPL_CYCLE_COLLECTING_RELEASE(mozPersonalDictionary)
NS_INTERFACE_MAP_BEGIN(mozPersonalDictionary)
NS_INTERFACE_MAP_ENTRY(mozIPersonalDictionary)
NS_INTERFACE_MAP_ENTRY(nsIObserver)
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozIPersonalDictionary)
NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(mozPersonalDictionary)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTION(mozPersonalDictionary, mEncoder)
class mozPersonalDictionaryLoader MOZ_FINAL : public nsRunnable
{
public:
explicit mozPersonalDictionaryLoader(mozPersonalDictionary *dict) : mDict(dict)
{
}
NS_IMETHOD Run()
{
mDict->SyncLoad();
// Release the dictionary on the main thread
mozPersonalDictionary *dict;
mDict.forget(&dict);
nsCOMPtr<nsIThread> mainThread = do_GetMainThread();
if (mainThread) {
NS_ProxyRelease(mainThread, static_cast<mozIPersonalDictionary *>(dict));
} else {
// It's better to leak the dictionary than to release it on a wrong thread
NS_WARNING("Cannot get main thread, leaking mozPersonalDictionary.");
}
return NS_OK;
}
private:
nsRefPtr<mozPersonalDictionary> mDict;
};
mozPersonalDictionary::mozPersonalDictionary()
: mDirty(false),
mIsLoaded(false),
mMonitor("mozPersonalDictionary::mMonitor")
{
}
mozPersonalDictionary::~mozPersonalDictionary()
{
}
nsresult mozPersonalDictionary::Init()
{
nsCOMPtr<nsIObserverService> svc =
do_GetService("@mozilla.org/observer-service;1");
NS_ENSURE_STATE(svc);
// we want to reload the dictionary if the profile switches
nsresult rv = svc->AddObserver(this, "profile-do-change", true);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
rv = svc->AddObserver(this, "profile-before-change", true);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
Load();
return NS_OK;
}
void mozPersonalDictionary::WaitForLoad()
{
if (mIsLoaded) {
return;
}
mozilla::MonitorAutoLock mon(mMonitor);
if (!mIsLoaded) {
mon.Wait();
}
}
nsresult mozPersonalDictionary::LoadInternal()
{
nsresult rv;
mozilla::MonitorAutoLock mon(mMonitor);
if (mIsLoaded) {
return NS_OK;
}
rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(mFile));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
if (!mFile) {
return NS_ERROR_FAILURE;
}
rv = mFile->Append(NS_LITERAL_STRING(MOZ_PERSONAL_DICT_NAME));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
nsCOMPtr<nsIEventTarget> target = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
nsCOMPtr<nsIRunnable> runnable = new mozPersonalDictionaryLoader(this);
rv = target->Dispatch(runnable, NS_DISPATCH_NORMAL);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
NS_IMETHODIMP mozPersonalDictionary::Load()
{
nsresult rv = LoadInternal();
if (NS_FAILED(rv)) {
mIsLoaded = true;
}
return rv;
}
void mozPersonalDictionary::SyncLoad()
{
MOZ_ASSERT(!NS_IsMainThread());
mozilla::MonitorAutoLock mon(mMonitor);
if (mIsLoaded) {
return;
}
SyncLoadInternal();
mIsLoaded = true;
mon.Notify();
}
void mozPersonalDictionary::SyncLoadInternal()
{
MOZ_ASSERT(!NS_IsMainThread());
//FIXME Deinst -- get dictionary name from prefs;
nsresult rv;
bool dictExists;
rv = mFile->Exists(&dictExists);
if (NS_FAILED(rv)) {
return;
}
if (!dictExists) {
// Nothing is really wrong...
return;
}
nsCOMPtr<nsIInputStream> inStream;
NS_NewLocalFileInputStream(getter_AddRefs(inStream), mFile);
nsCOMPtr<nsIUnicharInputStream> convStream;
rv = nsSimpleUnicharStreamFactory::GetInstance()->
CreateInstanceFromUTF8Stream(inStream, getter_AddRefs(convStream));
if (NS_FAILED(rv)) {
return;
}
// we're rereading to get rid of the old data -- we shouldn't have any, but...
mDictionaryTable.Clear();
char16_t c;
uint32_t nRead;
bool done = false;
do{ // read each line of text into the string array.
if( (NS_OK != convStream->Read(&c, 1, &nRead)) || (nRead != 1)) break;
while(!done && ((c == '\n') || (c == '\r'))){
if( (NS_OK != convStream->Read(&c, 1, &nRead)) || (nRead != 1)) done = true;
}
if (!done){
nsAutoString word;
while((c != '\n') && (c != '\r') && !done){
word.Append(c);
if( (NS_OK != convStream->Read(&c, 1, &nRead)) || (nRead != 1)) done = true;
}
mDictionaryTable.PutEntry(word.get());
}
} while(!done);
mDirty = false;
}
// A little helper function to add the key to the list.
// This is not threadsafe, and only safe if the consumer does not
// modify the list.
static PLDHashOperator
AddHostToStringArray(nsUnicharPtrHashKey *aEntry, void *aArg)
{
static_cast<nsTArray<nsString>*>(aArg)->AppendElement(nsDependentString(aEntry->GetKey()));
return PL_DHASH_NEXT;
}
/* void Save (); */
NS_IMETHODIMP mozPersonalDictionary::Save()
{
nsCOMPtr<nsIFile> theFile;
nsresult res;
WaitForLoad();
if(!mDirty) return NS_OK;
//FIXME Deinst -- get dictionary name from prefs;
res = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(theFile));
if(NS_FAILED(res)) return res;
if(!theFile)return NS_ERROR_FAILURE;
res = theFile->Append(NS_LITERAL_STRING(MOZ_PERSONAL_DICT_NAME));
if(NS_FAILED(res)) return res;
nsCOMPtr<nsIOutputStream> outStream;
NS_NewSafeLocalFileOutputStream(getter_AddRefs(outStream), theFile, PR_CREATE_FILE | PR_WRONLY | PR_TRUNCATE ,0664);
// get a buffered output stream 4096 bytes big, to optimize writes
nsCOMPtr<nsIOutputStream> bufferedOutputStream;
res = NS_NewBufferedOutputStream(getter_AddRefs(bufferedOutputStream), outStream, 4096);
if (NS_FAILED(res)) return res;
nsTArray<nsString> array(mDictionaryTable.Count());
mDictionaryTable.EnumerateEntries(AddHostToStringArray, &array);
uint32_t bytesWritten;
nsAutoCString utf8Key;
for (uint32_t i = 0; i < array.Length(); ++i ) {
CopyUTF16toUTF8(array[i], utf8Key);
bufferedOutputStream->Write(utf8Key.get(), utf8Key.Length(), &bytesWritten);
bufferedOutputStream->Write("\n", 1, &bytesWritten);
}
nsCOMPtr<nsISafeOutputStream> safeStream = do_QueryInterface(bufferedOutputStream);
NS_ASSERTION(safeStream, "expected a safe output stream!");
if (safeStream) {
res = safeStream->Finish();
if (NS_FAILED(res)) {
NS_WARNING("failed to save personal dictionary file! possible data loss");
}
}
return res;
}
/* readonly attribute nsIStringEnumerator GetWordList() */
NS_IMETHODIMP mozPersonalDictionary::GetWordList(nsIStringEnumerator **aWords)
{
NS_ENSURE_ARG_POINTER(aWords);
*aWords = nullptr;
WaitForLoad();
nsTArray<nsString> *array = new nsTArray<nsString>(mDictionaryTable.Count());
if (!array)
return NS_ERROR_OUT_OF_MEMORY;
mDictionaryTable.EnumerateEntries(AddHostToStringArray, array);
array->Sort();
return NS_NewAdoptingStringEnumerator(aWords, array);
}
/* boolean Check (in wstring word, in wstring language); */
NS_IMETHODIMP mozPersonalDictionary::Check(const char16_t *aWord, const char16_t *aLanguage, bool *aResult)
{
NS_ENSURE_ARG_POINTER(aWord);
NS_ENSURE_ARG_POINTER(aResult);
WaitForLoad();
*aResult = (mDictionaryTable.GetEntry(aWord) || mIgnoreTable.GetEntry(aWord));
return NS_OK;
}
/* void AddWord (in wstring word); */
NS_IMETHODIMP mozPersonalDictionary::AddWord(const char16_t *aWord, const char16_t *aLang)
{
WaitForLoad();
mDictionaryTable.PutEntry(aWord);
mDirty = true;
return NS_OK;
}
/* void RemoveWord (in wstring word); */
NS_IMETHODIMP mozPersonalDictionary::RemoveWord(const char16_t *aWord, const char16_t *aLang)
{
WaitForLoad();
mDictionaryTable.RemoveEntry(aWord);
mDirty = true;
return NS_OK;
}
/* void IgnoreWord (in wstring word); */
NS_IMETHODIMP mozPersonalDictionary::IgnoreWord(const char16_t *aWord)
{
// avoid adding duplicate words to the ignore list
if (aWord && !mIgnoreTable.GetEntry(aWord))
mIgnoreTable.PutEntry(aWord);
return NS_OK;
}
/* void EndSession (); */
NS_IMETHODIMP mozPersonalDictionary::EndSession()
{
WaitForLoad();
Save(); // save any custom words at the end of a spell check session
mIgnoreTable.Clear();
return NS_OK;
}
/* void AddCorrection (in wstring word, in wstring correction); */
NS_IMETHODIMP mozPersonalDictionary::AddCorrection(const char16_t *word, const char16_t *correction, const char16_t *lang)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
/* void RemoveCorrection (in wstring word, in wstring correction); */
NS_IMETHODIMP mozPersonalDictionary::RemoveCorrection(const char16_t *word, const char16_t *correction, const char16_t *lang)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
/* void GetCorrection (in wstring word, [array, size_is (count)] out wstring words, out uint32_t count); */
NS_IMETHODIMP mozPersonalDictionary::GetCorrection(const char16_t *word, char16_t ***words, uint32_t *count)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
/* void observe (in nsISupports aSubject, in string aTopic, in wstring aData); */
NS_IMETHODIMP mozPersonalDictionary::Observe(nsISupports *aSubject, const char *aTopic, const char16_t *aData)
{
if (!nsCRT::strcmp(aTopic, "profile-do-change")) {
// The observer is registered in Init() which calls Load and in turn
// LoadInternal(); i.e. Observe() can't be called before Load().
WaitForLoad();
mIsLoaded = false;
Load(); // load automatically clears out the existing dictionary table
} else if (!nsCRT::strcmp(aTopic, "profile-before-change")) {
Save();
}
return NS_OK;
}