forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaboutUrlClassifier.js
500 lines (410 loc) · 15.5 KB
/
aboutUrlClassifier.js
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/* 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/. */
ChromeUtils.import("resource://gre/modules/Services.jsm");
const UPDATE_BEGIN = "safebrowsing-update-begin";
const UPDATE_FINISH = "safebrowsing-update-finished";
const JSLOG_PREF = "browser.safebrowsing.debug";
function unLoad() {
window.removeEventListener("unload", unLoad);
Provider.uninit();
Cache.uninit();
Debug.uninit();
}
function onLoad() {
window.removeEventListener("load", onLoad);
window.addEventListener("unload", unLoad);
Provider.init();
Cache.init();
Debug.init();
}
/*
* Provider
*/
var Provider = {
providers: null,
updatingProvider: "",
init() {
this.providers = new Set();
let branch = Services.prefs.getBranch("browser.safebrowsing.provider.");
let children = branch.getChildList("", {});
for (let child of children) {
this.providers.add(child.split(".")[0]);
}
this.register();
this.render();
this.refresh();
},
uninit() {
Services.obs.removeObserver(this.onBeginUpdate, UPDATE_BEGIN);
Services.obs.removeObserver(this.onFinishUpdate, UPDATE_FINISH);
},
onBeginUpdate(aSubject, aTopic, aData) {
this.updatingProvider = aData;
let p = this.updatingProvider;
// Disable update button for the provider while we are doing update.
document.getElementById("update-" + p).disabled = true;
let elem = document.getElementById(p + "-col-lastupdateresult");
document.l10n.setAttributes(elem, "url-classifier-updating");
},
onFinishUpdate(aSubject, aTopic, aData) {
let p = this.updatingProvider;
this.updatingProvider = "";
// It is possible that we get update-finished event only because
// about::url-classifier is opened after update-begin event is fired.
if (p === "") {
this.refresh();
return;
}
this.refresh([p]);
document.getElementById("update-" + p).disabled = false;
let elem = document.getElementById(p + "-col-lastupdateresult");
if (aData.startsWith("success")) {
document.l10n.setAttributes(elem, "url-classifier-success");
} else if (aData.startsWith("update error")) {
document.l10n.setAttributes(elem, "url-classifier-update-error", {error: [aData.split(": ")[1]]});
} else if (aData.startsWith("download error")) {
document.l10n.setAttributes(elem, "url-classifier-download-error", {error: [aData.split(": ")[1]]});
} else {
elem.childNodes[0].nodeValue = aData;
}
},
register() {
// Handle begin update
this.onBeginUpdate = this.onBeginUpdate.bind(this);
Services.obs.addObserver(this.onBeginUpdate, UPDATE_BEGIN);
// Handle finish update
this.onFinishUpdate = this.onFinishUpdate.bind(this);
Services.obs.addObserver(this.onFinishUpdate, UPDATE_FINISH);
},
// This should only be called once because we assume number of providers
// won't change.
render() {
let tbody = document.getElementById("provider-table-body");
for (let provider of this.providers) {
let tr = document.createElement("tr");
let cols = document.getElementById("provider-head-row").childNodes;
for (let column of cols) {
if (!column.id) {
continue;
}
let td = document.createElement("td");
td.id = provider + "-" + column.id;
if (column.id === "col-update") {
let btn = document.createElement("button");
btn.id = "update-" + provider;
btn.addEventListener("click", () => { this.update(provider); });
document.l10n.setAttributes(btn, "url-classifier-trigger-update");
td.appendChild(btn);
} else if (column.id === "col-lastupdateresult") {
document.l10n.setAttributes(td, "url-classifier-not-available");
} else {
td.appendChild(document.createTextNode(""));
}
tr.appendChild(td);
}
tbody.appendChild(tr);
}
},
refresh(listProviders = this.providers) {
for (let provider of listProviders) {
let values = {};
values["col-provider"] = provider;
let pref = "browser.safebrowsing.provider." + provider + ".lastupdatetime";
let lut = Services.prefs.getCharPref(pref, "");
values["col-lastupdatetime"] = lut ? new Date(lut * 1) : null;
pref = "browser.safebrowsing.provider." + provider + ".nextupdatetime";
let nut = Services.prefs.getCharPref(pref, "");
values["col-nextupdatetime"] = nut ? new Date(nut * 1) : null;
let listmanager = Cc["@mozilla.org/url-classifier/listmanager;1"]
.getService(Ci.nsIUrlListManager);
let bot = listmanager.getBackOffTime(provider);
values["col-backofftime"] = bot ? new Date(bot * 1) : null;
for (let key of Object.keys(values)) {
let elem = document.getElementById(provider + "-" + key);
if (values[key]) {
elem.removeAttribute("data-l10n-id");
elem.childNodes[0].nodeValue = values[key];
} else {
document.l10n.setAttributes(elem, "url-classifier-not-available");
}
}
}
},
// Call update for the provider.
update(provider) {
let listmanager = Cc["@mozilla.org/url-classifier/listmanager;1"]
.getService(Ci.nsIUrlListManager);
let pref = "browser.safebrowsing.provider." + provider + ".lists";
let tables = Services.prefs.getCharPref(pref, "");
if (!listmanager.forceUpdates(tables)) {
// This may because of back-off algorithm.
let elem = document.getElementById(provider + "-col-lastupdateresult");
document.l10n.setAttributes(elem, "url-classifier-cannot-update");
}
},
};
/*
* Cache
*/
var Cache = {
// Tables that show cahe entries.
showCacheEnties: null,
init() {
this.showCacheEnties = new Set();
this.register();
this.render();
},
uninit() {
Services.obs.removeObserver(this.refresh, UPDATE_FINISH);
},
register() {
this.refresh = this.refresh.bind(this);
Services.obs.addObserver(this.refresh, UPDATE_FINISH);
},
render() {
this.createCacheEntries();
let refreshBtn = document.getElementById("refresh-cache-btn");
refreshBtn.addEventListener("click", () => { this.refresh(); });
let clearBtn = document.getElementById("clear-cache-btn");
clearBtn.addEventListener("click", () => {
let dbservice = Cc["@mozilla.org/url-classifier/dbservice;1"]
.getService(Ci.nsIUrlClassifierDBService);
dbservice.clearCache();
// Since clearCache is async call, we just simply assume it will be
// updated in 100 milli-seconds.
setTimeout(() => { this.refresh(); }, 100);
});
},
refresh() {
this.clearCacheEntries();
this.createCacheEntries();
},
clearCacheEntries() {
let ctbody = document.getElementById("cache-table-body");
while (ctbody.firstChild) {
ctbody.firstChild.remove();
}
let cetbody = document.getElementById("cache-entries-table-body");
while (cetbody.firstChild) {
cetbody.firstChild.remove();
}
},
createCacheEntries() {
function createRow(tds, body, cols) {
let tr = document.createElement("tr");
tds.forEach(function(v, i, a) {
let td = document.createElement("td");
if (i == 0 && tds.length != cols) {
td.setAttribute("colspan", cols - tds.length + 1);
}
if (typeof v === "object") {
if (v.l10n) {
document.l10n.setAttributes(td, v.l10n);
} else {
td.removeAttribute("data-l10n-id");
td.appendChild(v);
}
} else {
td.removeAttribute("data-l10n-id");
td.textContent = v;
}
tr.appendChild(td);
});
body.appendChild(tr);
}
let dbservice = Cc["@mozilla.org/url-classifier/dbservice;1"]
.getService(Ci.nsIUrlClassifierInfo);
for (let provider of Provider.providers) {
let pref = "browser.safebrowsing.provider." + provider + ".lists";
let tables = Services.prefs.getCharPref(pref, "").split(",");
for (let table of tables) {
dbservice.getCacheInfo(table, {
onGetCacheComplete: (aCache) => {
let entries = aCache.entries;
if (entries.length === 0) {
this.showCacheEnties.delete(table);
return;
}
let positiveCacheCount = 0;
for (let i = 0; i < entries.length ; i++) {
let entry = entries.queryElementAt(i, Ci.nsIUrlClassifierCacheEntry);
let matches = entry.matches;
positiveCacheCount += matches.length;
// If we don't have to show cache entries for this table then just
// skip the following code.
if (!this.showCacheEnties.has(table)) {
continue;
}
let tds = [table, entry.prefix, new Date(entry.expiry * 1000).toString()];
let j = 0;
do {
if (matches.length >= 1) {
let match =
matches.queryElementAt(j, Ci.nsIUrlClassifierPositiveCacheEntry);
let list = [match.fullhash, new Date(match.expiry * 1000).toString()];
tds = tds.concat(list);
} else {
tds = tds.concat([{l10n: "url-classifier-not-available"}, {l10n: "url-classifier-not-available"}]);
}
createRow(tds, document.getElementById("cache-entries-table-body"), 5);
j++;
tds = [""];
} while (j < matches.length);
}
// Create cache information entries.
let chk = document.createElement("input");
chk.type = "checkbox";
chk.checked = this.showCacheEnties.has(table);
chk.addEventListener("click", () => {
if (chk.checked) {
this.showCacheEnties.add(table);
} else {
this.showCacheEnties.delete(table);
}
this.refresh();
});
let tds = [table, entries.length, positiveCacheCount, chk];
createRow(tds, document.getElementById("cache-table-body"), tds.length);
},
});
}
}
let entries_div = document.getElementById("cache-entries");
entries_div.style.display = this.showCacheEnties.size == 0 ? "none" : "block";
},
};
/*
* Debug
*/
var Debug = {
// url-classifier NSPR Log modules.
modules: ["UrlClassifierDbService",
"nsChannelClassifier",
"UrlClassifierProtocolParser",
"UrlClassifierStreamUpdater",
"UrlClassifierPrefixSet",
"ApplicationReputation"],
init() {
this.register();
this.render();
this.refresh();
},
uninit() {
Services.prefs.removeObserver(JSLOG_PREF, this.refreshJSDebug);
},
register() {
this.refreshJSDebug = this.refreshJSDebug.bind(this);
Services.prefs.addObserver(JSLOG_PREF, this.refreshJSDebug);
},
render() {
// This function update the log module text field if we click
// safebrowsing log module check box.
function logModuleUpdate(module) {
let txt = document.getElementById("log-modules");
let chk = document.getElementById("chk-" + module);
let dst = chk.checked ? "," + module + ":5" : "";
let re = new RegExp(",?" + module + ":[0-9]");
let str = txt.value.replace(re, dst);
if (chk.checked) {
str = txt.value === str ? str + dst : str;
}
txt.value = str.replace(/^,/, "");
}
let setLog = document.getElementById("set-log-modules");
setLog.addEventListener("click", this.nsprlog);
let setLogFile = document.getElementById("set-log-file");
setLogFile.addEventListener("click", this.logfile);
let setJSLog = document.getElementById("js-log");
setJSLog.addEventListener("click", this.jslog);
let modules = document.getElementById("log-modules");
let sbModules = document.getElementById("sb-log-modules");
for (let module of this.modules) {
let container = document.createElement("div");
container.className = "toggle-container-with-text";
sbModules.appendChild(container);
let chk = document.createElement("input");
chk.id = "chk-" + module;
chk.type = "checkbox";
chk.checked = true;
chk.addEventListener("click", () => { logModuleUpdate(module); });
container.appendChild(chk, modules);
let label = document.createElement("label");
label.for = chk.id;
label.appendChild(document.createTextNode(module));
container.appendChild(label, modules);
}
this.modules.map(logModuleUpdate);
let file = Services.dirsvc.get("TmpD", Ci.nsIFile);
file.append("safebrowsing.log");
let logFile = document.getElementById("log-file");
logFile.value = file.path;
let curLog = document.getElementById("cur-log-modules");
curLog.childNodes[0].nodeValue = "";
let curLogFile = document.getElementById("cur-log-file");
curLogFile.childNodes[0].nodeValue = "";
},
refresh() {
this.refreshJSDebug();
// Disable configure log modules if log modules are already set
// by environment variable.
let env = Cc["@mozilla.org/process/environment;1"]
.getService(Ci.nsIEnvironment);
let logModules = env.get("MOZ_LOG") ||
env.get("MOZ_LOG_MODULES") ||
env.get("NSPR_LOG_MODULES");
if (logModules.length > 0) {
document.getElementById("set-log-modules").disabled = true;
for (let module of this.modules) {
document.getElementById("chk-" + module).disabled = true;
}
let curLogModules = document.getElementById("cur-log-modules");
curLogModules.childNodes[0].nodeValue = logModules;
}
// Disable set log file if log file is already set
// by environment variable.
let logFile = env.get("MOZ_LOG_FILE") || env.get("NSPR_LOG_FILE");
if (logFile.length > 0) {
document.getElementById("set-log-file").disabled = true;
document.getElementById("log-file").value = logFile;
}
},
refreshJSDebug() {
let enabled = Services.prefs.getBoolPref(JSLOG_PREF, false);
let jsChk = document.getElementById("js-log");
jsChk.checked = enabled;
let curJSLog = document.getElementById("cur-js-log");
if (enabled) {
document.l10n.setAttributes(curJSLog, "url-classifier-enabled");
} else {
document.l10n.setAttributes(curJSLog, "url-classifier-disabled");
}
},
jslog() {
let enabled = Services.prefs.getBoolPref(JSLOG_PREF, false);
Services.prefs.setBoolPref(JSLOG_PREF, !enabled);
},
nsprlog() {
// Turn off debugging for all the modules.
let children = Services.prefs.getBranch("logging.").getChildList("", {});
for (let pref of children) {
if (!pref.startsWith("config.")) {
Services.prefs.clearUserPref(`logging.${pref}`);
}
}
let value = document.getElementById("log-modules").value;
let logModules = value.split(",");
for (let module of logModules) {
let [key, value] = module.split(":");
Services.prefs.setIntPref(`logging.${key}`, parseInt(value, 10));
}
let curLogModules = document.getElementById("cur-log-modules");
curLogModules.childNodes[0].nodeValue = value;
},
logfile() {
let logFile = document.getElementById("log-file").value.trim();
Services.prefs.setCharPref("logging.config.LOG_FILE", logFile);
let curLogFile = document.getElementById("cur-log-file");
curLogFile.childNodes[0].nodeValue = logFile;
},
};