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
|
/* -*- Mode: C++; tab-width: 8; 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 "nsUrlClassifierProxies.h"
#include "nsUrlClassifierDBService.h"
#include "mozilla/SyncRunnable.h"
using namespace mozilla::safebrowsing;
using mozilla::NewRunnableMethod;
static nsresult
DispatchToWorkerThread(nsIRunnable* r)
{
nsIThread* t = nsUrlClassifierDBService::BackgroundThread();
if (!t)
return NS_ERROR_FAILURE;
return t->Dispatch(r, NS_DISPATCH_NORMAL);
}
NS_IMPL_ISUPPORTS(UrlClassifierDBServiceWorkerProxy, nsIUrlClassifierDBService)
NS_IMETHODIMP
UrlClassifierDBServiceWorkerProxy::Lookup(nsIPrincipal* aPrincipal,
const nsACString& aTables,
nsIUrlClassifierCallback* aCB)
{
nsCOMPtr<nsIRunnable> r = new LookupRunnable(mTarget, aPrincipal, aTables,
aCB);
return DispatchToWorkerThread(r);
}
NS_IMETHODIMP
UrlClassifierDBServiceWorkerProxy::LookupRunnable::Run()
{
(void) mTarget->Lookup(mPrincipal, mLookupTables, mCB);
return NS_OK;
}
NS_IMETHODIMP
UrlClassifierDBServiceWorkerProxy::GetTables(nsIUrlClassifierCallback* aCB)
{
nsCOMPtr<nsIRunnable> r = new GetTablesRunnable(mTarget, aCB);
return DispatchToWorkerThread(r);
}
NS_IMETHODIMP
UrlClassifierDBServiceWorkerProxy::GetTablesRunnable::Run()
{
mTarget->GetTables(mCB);
return NS_OK;
}
NS_IMETHODIMP
UrlClassifierDBServiceWorkerProxy::SetHashCompleter
(const nsACString&, nsIUrlClassifierHashCompleter*)
{
NS_NOTREACHED("This method should not be called!");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
UrlClassifierDBServiceWorkerProxy::BeginUpdate
(nsIUrlClassifierUpdateObserver* aUpdater,
const nsACString& aTables)
{
nsCOMPtr<nsIRunnable> r = new BeginUpdateRunnable(mTarget, aUpdater,
aTables);
return DispatchToWorkerThread(r);
}
NS_IMETHODIMP
UrlClassifierDBServiceWorkerProxy::BeginUpdateRunnable::Run()
{
mTarget->BeginUpdate(mUpdater, mTables);
return NS_OK;
}
NS_IMETHODIMP
UrlClassifierDBServiceWorkerProxy::BeginStream(const nsACString& aTable)
{
nsCOMPtr<nsIRunnable> r =
new BeginStreamRunnable(mTarget, aTable);
return DispatchToWorkerThread(r);
}
NS_IMETHODIMP
UrlClassifierDBServiceWorkerProxy::BeginStreamRunnable::Run()
{
mTarget->BeginStream(mTable);
return NS_OK;
}
NS_IMETHODIMP
UrlClassifierDBServiceWorkerProxy::UpdateStream(const nsACString& aUpdateChunk)
{
nsCOMPtr<nsIRunnable> r =
new UpdateStreamRunnable(mTarget, aUpdateChunk);
return DispatchToWorkerThread(r);
}
NS_IMETHODIMP
UrlClassifierDBServiceWorkerProxy::UpdateStreamRunnable::Run()
{
mTarget->UpdateStream(mUpdateChunk);
return NS_OK;
}
NS_IMETHODIMP
UrlClassifierDBServiceWorkerProxy::FinishStream()
{
nsCOMPtr<nsIRunnable> r =
NewRunnableMethod(mTarget,
&nsUrlClassifierDBServiceWorker::FinishStream);
return DispatchToWorkerThread(r);
}
NS_IMETHODIMP
UrlClassifierDBServiceWorkerProxy::DoLocalLookupRunnable::Run()
{
mTarget->DoLocalLookup(mSpec, mTables, mResults);
return NS_OK;
}
nsresult
UrlClassifierDBServiceWorkerProxy::DoLocalLookup(const nsACString& spec,
const nsACString& tables,
LookupResultArray* results)
{
// Run synchronously on background thread. NS_DISPATCH_SYNC does *not* do
// what we want -- it continues processing events on the main thread loop
// before the Dispatch returns.
nsCOMPtr<nsIRunnable> r = new DoLocalLookupRunnable(mTarget, spec, tables, results);
nsIThread* t = nsUrlClassifierDBService::BackgroundThread();
if (!t)
return NS_ERROR_FAILURE;
mozilla::SyncRunnable::DispatchToThread(t, r);
return NS_OK;
}
NS_IMETHODIMP
UrlClassifierDBServiceWorkerProxy::FinishUpdate()
{
nsCOMPtr<nsIRunnable> r =
NewRunnableMethod(mTarget,
&nsUrlClassifierDBServiceWorker::FinishUpdate);
return DispatchToWorkerThread(r);
}
NS_IMETHODIMP
UrlClassifierDBServiceWorkerProxy::CancelUpdate()
{
nsCOMPtr<nsIRunnable> r =
NewRunnableMethod(mTarget,
&nsUrlClassifierDBServiceWorker::CancelUpdate);
return DispatchToWorkerThread(r);
}
NS_IMETHODIMP
UrlClassifierDBServiceWorkerProxy::ResetDatabase()
{
nsCOMPtr<nsIRunnable> r =
NewRunnableMethod(mTarget,
&nsUrlClassifierDBServiceWorker::ResetDatabase);
return DispatchToWorkerThread(r);
}
NS_IMETHODIMP
UrlClassifierDBServiceWorkerProxy::ReloadDatabase()
{
nsCOMPtr<nsIRunnable> r =
NewRunnableMethod(mTarget,
&nsUrlClassifierDBServiceWorker::ReloadDatabase);
return DispatchToWorkerThread(r);
}
nsresult
UrlClassifierDBServiceWorkerProxy::OpenDb()
{
nsCOMPtr<nsIRunnable> r =
NewRunnableMethod(mTarget,
&nsUrlClassifierDBServiceWorker::OpenDb);
return DispatchToWorkerThread(r);
}
nsresult
UrlClassifierDBServiceWorkerProxy::CloseDb()
{
nsCOMPtr<nsIRunnable> r =
NewRunnableMethod(mTarget,
&nsUrlClassifierDBServiceWorker::CloseDb);
return DispatchToWorkerThread(r);
}
nsresult
UrlClassifierDBServiceWorkerProxy::CacheCompletions(CacheResultArray * aEntries)
{
nsCOMPtr<nsIRunnable> r = new CacheCompletionsRunnable(mTarget, aEntries);
return DispatchToWorkerThread(r);
}
NS_IMETHODIMP
UrlClassifierDBServiceWorkerProxy::CacheCompletionsRunnable::Run()
{
mTarget->CacheCompletions(mEntries);
return NS_OK;
}
nsresult
UrlClassifierDBServiceWorkerProxy::CacheMisses(PrefixArray * aEntries)
{
nsCOMPtr<nsIRunnable> r = new CacheMissesRunnable(mTarget, aEntries);
return DispatchToWorkerThread(r);
}
NS_IMETHODIMP
UrlClassifierDBServiceWorkerProxy::CacheMissesRunnable::Run()
{
mTarget->CacheMisses(mEntries);
return NS_OK;
}
NS_IMETHODIMP
UrlClassifierDBServiceWorkerProxy::SetLastUpdateTime(const nsACString& table,
uint64_t lastUpdateTime)
{
nsCOMPtr<nsIRunnable> r =
new SetLastUpdateTimeRunnable(mTarget, table, lastUpdateTime);
return DispatchToWorkerThread(r);
}
NS_IMETHODIMP
UrlClassifierDBServiceWorkerProxy::SetLastUpdateTimeRunnable::Run()
{
mTarget->SetLastUpdateTime(mTable, mUpdateTime);
return NS_OK;
}
NS_IMETHODIMP
UrlClassifierDBServiceWorkerProxy::ClearLastResults()
{
nsCOMPtr<nsIRunnable> r = new ClearLastResultsRunnable(mTarget);
return DispatchToWorkerThread(r);
}
NS_IMETHODIMP
UrlClassifierDBServiceWorkerProxy::ClearLastResultsRunnable::Run()
{
return mTarget->ClearLastResults();
}
NS_IMPL_ISUPPORTS(UrlClassifierLookupCallbackProxy,
nsIUrlClassifierLookupCallback)
NS_IMETHODIMP
UrlClassifierLookupCallbackProxy::LookupComplete
(LookupResultArray * aResults)
{
nsCOMPtr<nsIRunnable> r = new LookupCompleteRunnable(mTarget, aResults);
return NS_DispatchToMainThread(r);
}
NS_IMETHODIMP
UrlClassifierLookupCallbackProxy::LookupCompleteRunnable::Run()
{
mTarget->LookupComplete(mResults);
return NS_OK;
}
NS_IMPL_ISUPPORTS(UrlClassifierCallbackProxy,
nsIUrlClassifierCallback)
NS_IMETHODIMP
UrlClassifierCallbackProxy::HandleEvent(const nsACString& aValue)
{
nsCOMPtr<nsIRunnable> r = new HandleEventRunnable(mTarget, aValue);
return NS_DispatchToMainThread(r);
}
NS_IMETHODIMP
UrlClassifierCallbackProxy::HandleEventRunnable::Run()
{
mTarget->HandleEvent(mValue);
return NS_OK;
}
NS_IMPL_ISUPPORTS(UrlClassifierUpdateObserverProxy,
nsIUrlClassifierUpdateObserver)
NS_IMETHODIMP
UrlClassifierUpdateObserverProxy::UpdateUrlRequested
(const nsACString& aURL,
const nsACString& aTable)
{
nsCOMPtr<nsIRunnable> r =
new UpdateUrlRequestedRunnable(mTarget, aURL, aTable);
return NS_DispatchToMainThread(r);
}
NS_IMETHODIMP
UrlClassifierUpdateObserverProxy::UpdateUrlRequestedRunnable::Run()
{
mTarget->UpdateUrlRequested(mURL, mTable);
return NS_OK;
}
NS_IMETHODIMP
UrlClassifierUpdateObserverProxy::StreamFinished(nsresult aStatus,
uint32_t aDelay)
{
nsCOMPtr<nsIRunnable> r =
new StreamFinishedRunnable(mTarget, aStatus, aDelay);
return NS_DispatchToMainThread(r);
}
NS_IMETHODIMP
UrlClassifierUpdateObserverProxy::StreamFinishedRunnable::Run()
{
mTarget->StreamFinished(mStatus, mDelay);
return NS_OK;
}
NS_IMETHODIMP
UrlClassifierUpdateObserverProxy::UpdateError(nsresult aError)
{
nsCOMPtr<nsIRunnable> r =
new UpdateErrorRunnable(mTarget, aError);
return NS_DispatchToMainThread(r);
}
NS_IMETHODIMP
UrlClassifierUpdateObserverProxy::UpdateErrorRunnable::Run()
{
mTarget->UpdateError(mError);
return NS_OK;
}
NS_IMETHODIMP
UrlClassifierUpdateObserverProxy::UpdateSuccess(uint32_t aRequestedTimeout)
{
nsCOMPtr<nsIRunnable> r =
new UpdateSuccessRunnable(mTarget, aRequestedTimeout);
return NS_DispatchToMainThread(r);
}
NS_IMETHODIMP
UrlClassifierUpdateObserverProxy::UpdateSuccessRunnable::Run()
{
mTarget->UpdateSuccess(mRequestedTimeout);
return NS_OK;
}
|