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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
|
/* ************************************************************************
*
* CookieKeeper - a Mozilla add-on
* (c) 2013-2016 Yvon TANGUY
*
* ***** BEGIN LICENSE BLOCK **********************************************
*
* 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/.
*
* ***** END LICENSE BLOCK **********************************************
*
* Public API
*
* ************************************************************************
*/
"use strict";
var EXPORTED_SYMBOLS = ["CookieKeeper"];
Components.utils.import("resource://gre/modules/osfile.jsm");
Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/devtools/Console.jsm");
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
Components.utils.import("resource://gre/modules/Task.jsm");
Components.utils.import("resource://gre/modules/FileUtils.jsm");
Components.utils.import("resource://gre/modules/NetUtil.jsm");
Components.utils.import("resource://gre/modules/Timer.jsm");
Components.utils.import("resource://gre/modules/Sqlite.jsm");
var cookieManagers = [];
{
let self = this;
Services.scriptloader.loadSubScript("chrome://cookiekeeper/content/common.js", self);
Services.scriptloader.loadSubScript("chrome://cookiekeeper/content/ck.cookies.js", self);
Services.scriptloader.loadSubScript("chrome://cookiekeeper/content/ck.dom_storage.js", self);
Services.scriptloader.loadSubScript("chrome://cookiekeeper/content/ck.local_shared_object.js", self);
Services.scriptloader.loadSubScript("chrome://cookiekeeper/content/ck.private.js", self);
}
/**
* @brief Common/Public CookieKeeper function.
*
* @par Observer example:
* see https://developer.mozilla.org/en-US/Add-ons/Overlay_Extensions/XUL_School/Observer_Notifications
~~~~~~~~~~~~~{.js}
let pCObserver = {
observe : function(aSubject, aTopic, aData) {
if (aTopic == CookieKeeper.OBS_TOPIC) {
if (aData == CookieKeeper.OBS_STARTUP || aData == CookieKeeper.OBS_SHUTDOWN) {
} else {
var pCookie = aSubject.wrappedJSObject.pcookie;
var action = "added";
if (aData == CookieKeeper.OBS_DEL) {
action = "removed";
}
window.alert("PCookie " + action + ": " + pCookie.host + ":" + pCookie.name);
}
}
}
~~~~~~~~~~~~~
*/
var timerSaveId = null;
function saveProtectedListTimer() {
function timerCallback() {
timerSaveId = null;
CookieKeeperPrivate.saveProtectedList(false);
}
if (timerSaveId != null) {
clearTimeout(timerSaveId);
}
timerSaveId = setTimeout(timerCallback, 10000);
}
var CookieKeeper = {
get API_VERSION() { return 3; }, /// The CookieKeeper public API version
get OBS_TOPIC() { return CK_OBS_PCOOKIE_TOPIC; }, /// Observer ProtectedCookies topic
get OBS_STARTUP() { return CK_OBS_PCOOKIE_STARTUP; }, /// Add-on is starting
get OBS_SHUTDOWN() { return CK_OBS_PCOOKIE_SHUTDOWN; }, /// Add-on is shutting down
get OBS_ADD() { return CK_OBS_PCOOKIE_ADD; }, /// A protected cookie is added
get OBS_DEL() { return CK_OBS_PCOOKIE_DEL; }, /// A protected cookie is removed
// Preferences panel open options:
get PREFPANE_OPTIONS() { return CK_PREFPANE_OPTIONS; }, /// To show the CookieKeeper preference pane
get PREFPANE_PCOOKIES() { return CK_PREFPANE_PCOOKIES; }, /// To show the protected cookies pane
get PREFPANE_BROWSER() { return CK_PREFPANE_BROWSER; }, /// To show the browser preferences pane
get PREFPANE_PERMS() { return CK_PREFPANE_PERMISSIONS; }, /// To show the cookies permissions pane
get COOKIE_TYPE_CONTAINER() { return COOKIE_TYPE_CONTAINER; }, // A container (directory/folder)
get COOKIE_TYPE_STANDARD() { return COOKIE_TYPE_STANDARD; }, // A standard cookie
get COOKIE_TYPE_STORAGE() { return COOKIE_TYPE_STORAGE; }, // DOM Storage
get COOKIE_TYPE_LSO() { return COOKIE_TYPE_LSO; }, // LSO (Flash cookie)
/**
* @brief Called only by the CookieKeeper bootstrap startup() method
*/
init: function() {
CookieKeeperPrivate.init();
gCookieStorage.init();
gDomStorage.init();
gLocalSharedObject.init();
CookieKeeperPrivate.loadProtectedList(false);
Services.obs.notifyObservers(null, CK_OBS_PCOOKIE_TOPIC, CK_OBS_PCOOKIE_STARTUP);
},
/**
* @brief Called only by the CookieKeeper bootstrap shutdown() method
*/
uninit: function(isAppShutdown) {
if (!isAppShutdown) {
Services.obs.notifyObservers(null, CK_OBS_PCOOKIE_TOPIC, CK_OBS_PCOOKIE_SHUTDOWN);
}
if (timerSaveId != null) {
clearTimeout(timerSaveId);
timerSaveId = null;
}
CookieKeeperPrivate.saveProtectedList(true);
gDomStorage.uninit();
gLocalSharedObject.uninit();
gCookieStorage.uninit();
CookieKeeperPrivate.uninit();
// Help the garbage collector:
CookieKeeperPrivate = null;
gCookieStorage = null;
gDomStorage = null;
gLocalSharedObject = null;
cookieManagers = null;
},
/**
* @brief Show the main CookieKeeper window. If the dialogue is already opened, it will be focus.
* @param window The DOM window.
* @param aDomain [optional] The domain to search cookies for.
*/
showMainWindow: function(window, aDomain) {
var ckws = CookieKeeperPrivate.getCookieWindows();
var opened = false;
for (let i=0; i<ckws.length && opened == false; i++) {
let ckw = ckws[i];
if (ckw.id == CK_WINDOWS_ID[CK_WINDOW_MAIN_IDX]) {
ckw.window.focus();
opened = true;
if (typeof(aDomain) == "string") {
ckw.window.document.getElementById("textBoxSearch").value = aDomain;
ckw.window.gCookieKeeper.onFilter();
}
}
}
if (!opened) {
let ckWindow = window.open(
"chrome://cookiekeeper/content/cookiekeeper.xul",
"cookieKeeperWindow",
"chrome,toolbar,centerscreen,resizable,minimizable");
if (typeof(aDomain) == "string") {
ckWindow.window.arguments = [aDomain];
}
}
},
/**
* @brief Show the CookieKeeper preferences dialogue. If the window is already opened, it will be focus.
* @param window A DOM window.
* @param aPane The pane to show (PREFPANE_OPTIONS/PREFPANE_PCOOKIES/PREFPANE_PERMS/PREFPANE_BROWSER)
* if null, show the last pane.
* @note If the window is already opened, the aPane argument will not be honored.
*/
showPreferencesDialogue: function(window, aPane) {
const SHOW_HTML_PREFS = "extensions.cookiekeeper.use_html_prefs";
const PREF_HTML_URL = "chrome://cookiekeeper/content/preferences.html";
let showHTMLPanel = false;
if (Services.prefs.getPrefType(SHOW_HTML_PREFS) == Services.prefs.PREF_BOOL) {
showHTMLPanel = Services.prefs.getBoolPref(SHOW_HTML_PREFS);
}
if (showHTMLPanel) {
let url = PREF_HTML_URL;
if (typeof(aPane) == "string" && aPane.length > 0) {
url = PREF_HTML_URL + "#" + aPane;
}
try {
// Search for an existing opened tab first
// https://developer.mozilla.org/en-US/Add-ons/Code_snippets/Tabbed_browser
let browserEnumerator = Services.wm.getEnumerator("navigator:browser");
let found = false;
while (!found && browserEnumerator.hasMoreElements()) {
let browserWin = browserEnumerator.getNext();
let tabbrowser = browserWin.gBrowser;
// Check each tab of this browser instance
let numTabs = tabbrowser.browsers.length;
for (let index = 0; index < numTabs; index++) {
let currentBrowser = tabbrowser.getBrowserAtIndex(index);
if (PREF_HTML_URL == currentBrowser.currentURI.spec) {
// The URL is already opened. Select this tab.
tabbrowser.selectedTab = tabbrowser.tabContainer.childNodes[index];
// Focus *this* browser-window
browserWin.focus();
found = true;
break;
}
}
}
if (!found) {
let recentWindow = Services.wm.getMostRecentWindow("navigator:browser");
if (recentWindow) {
// Use an existing browser window
recentWindow.delayedOpenTab(url, null, null, null, null);
} else if (APP_MOZ_CONST[Services.appinfo.ID].NAME == THUNDERBIRD) {
// Prevent Thunderbird to crash
window.open(url, "cookieKeeperPrefsWindow",
"chrome,toolbar,centerscreen,resizable,minimizable,width=1024,height=650");
} else {
window.open(url);
}
}
} catch (e) {
log.e(e);
if (APP_MOZ_CONST[Services.appinfo.ID].NAME == THUNDERBIRD) {
// Prevent Thunderbird to crash
window.open(url, "cookieKeeperPrefsWindow",
"chrome,toolbar,centerscreen,resizable,minimizable,width=1024,height=650");
} else {
window.open(url);
}
}
return;
}
let ckws = CookieKeeperPrivate.getCookieWindows();
let opened = false;
for (let i=0; i<ckws.length && opened == false; i++) {
let ckw = ckws[i];
if (ckw.id == CK_WINDOWS_ID[CK_WINDOW_PREFS_IDX]) {
ckw.window.focus();
opened = true;
}
}
if (!opened) {
let dialogOptions = "chrome,centerscreen,resizable,titlebar,toolbar";
if (Services.appinfo.OS == "Darwin") {
// Don't really know why, but recommanded by Mozilla on MacOSX
dialogOptions = dialogOptions + ",dialog=yes";
}
window.openDialog("chrome://cookiekeeper/content/prefs.main.xul",
"cookieKeeperPrefsWindow",
dialogOptions,
aPane);
}
},
/**
* @brief Close all CookieKeeper windows.
*/
closeCookieWindow: function() {
function timeoutClose(aWin) {
try {
aWin.close();
if (!aWin.closed) {
aWin.setTimeout(function() { timeoutClose(aWin); }, 50);
}
} catch (ex) {
Components.utils.reportError(ex);
}
}
var ckws = CookieKeeperPrivate.getCookieWindows();
for (let i=ckws.length-1; i>=0; i--) {
let ckw = ckws[i];
timeoutClose(ckw.window);
}
},
/**
* @brief Get the protected items
* @return an object with two properties (array):
* - protected cookies (cookies)
* - protected storage (storage)
* - protected flash cookies (lso)
* { cookies : [], storage : [], lso : [] }
*/
getProtectedItems: function() {
let protectedList = { cookies : [], storage : [], lso : [] }
let protList = CookieKeeperPrivate._protectedList.cookies;
let pll = protList.length;
for (let i=0; i<pll; i++) {
let pi = protList[i];
let pc = gCookieStorage.makeProtectedObject(pi.host, pi.name, pi.path);
protectedList.cookies.push(pc);
}
protList = CookieKeeperPrivate._protectedList.storage;
pll = protList.length;
for (let i=0; i<pll; i++) {
let pi = protList[i];
let ps = gDomStorage.makeProtectedObject(pi.scope, pi.key);
protectedList.storage.push(ps);
}
protList = CookieKeeperPrivate._protectedList.lso;
pll = protList.length;
for (let i=0; i<pll; i++) {
let pi = protList[i];
let pl = gLocalSharedObject.makeProtectedObject(pi.host, pi.name, pi.file, pi.isSettings);
protectedList.lso.push(pl);
}
return protectedList;
},
/**
* @brief Test if this item is protected
* @param aItem an item (cookie or dom storage)
* @return true if the item is protected
* @note
* - If aItem is a cookie, it may have the property "type" set to COOKIE_TYPE_STANDARD.
* - If aItem is a cookie, it must have host/name/path properties (string)
* - If aItem is a dom storage, it must have the property "type" set to COOKIE_TYPE_STORAGE.
* - If aItem is a dom storage, it must have scope/name properties (string)
*/
isProtectedItem: function(aItem) {
var protList = [];
var isProtected = false;
switch (aItem.getType()) {
case COOKIE_TYPE_STANDARD:
protList = CookieKeeperPrivate._protectedList.cookies;
break;
case COOKIE_TYPE_STORAGE:
protList = CookieKeeperPrivate._protectedList.storage;
break;
case COOKIE_TYPE_LSO:
protList = CookieKeeperPrivate._protectedList.lso;
break;
default:
log.e("Unknown cookie type " + aItem.getType());
break;
}
for (let i=0; i<protList.length && !isProtected; i++) {
let pi = protList[i];
isProtected = pi.equals(aItem);
}
return isProtected;
},
/**
* @brief Add a protected item to the list
* @param aItem A protected item (cookie: host/name/path; storage: scope/key)
* @return true if the protected item has been added
* @par
* Notify the observer: OBS_TOPIC/OBS_ADD
*/
addProtectedItem: function(aItem) {
if (aItem == null) {
log.e("addProtectedItem: Item is not a valid cookie item");
return false;
}
var protList = [];
var isProtected = false;
var type = aItem.getType();
switch (type) {
case COOKIE_TYPE_STANDARD:
protList = CookieKeeperPrivate._protectedList.cookies;
break;
case COOKIE_TYPE_STORAGE:
protList = CookieKeeperPrivate._protectedList.storage;
break;
case COOKIE_TYPE_LSO:
protList = CookieKeeperPrivate._protectedList.lso;
break;
default:
log.e("addProtectedItem: item is invalid", aItem);
return false;
}
for (let i=0; i<protList.length && !isProtected; i++) {
let pi = protList[i];
isProtected = pi.equals(aItem);
}
if (!isProtected) {
// Create two protected item, to be sure to not return an iten in our list
// If the item is modified by an observer, it won't modify ours.
let pi = {};
let data = {};
switch (type) {
case COOKIE_TYPE_STANDARD:
let pc = gCookieStorage.makeProtectedObject(aItem.host, aItem.name, aItem.path);
pi = gCookieStorage.makeProtectedObject(aItem.host, aItem.name, aItem.path);
data = {pcookie : pc, pstorage : null, plso : null};
break;
case COOKIE_TYPE_STORAGE:
let ps = gDomStorage.makeProtectedObject(aItem.scope, aItem.key);
pi = gDomStorage.makeProtectedObject(aItem.scope, aItem.key);
data = {pcookie : null, pstorage : ps, plso : null};
break;
case COOKIE_TYPE_LSO:
let pl = gLocalSharedObject.makeProtectedObject(aItem.host, aItem.name, aItem.file, aItem.isSettings);
pi = gLocalSharedObject.makeProtectedObject(aItem.host, aItem.name, aItem.file, aItem.isSettings);
data = {pcookie : null, pstorage : null, plso : pl};
}
protList.push(pi);
CookieKeeperPrivate._protectedList.lastUpdate = Date.now();
saveProtectedListTimer();
let subject = { wrappedJSObject: data };
Services.obs.notifyObservers(subject, CK_OBS_PCOOKIE_TOPIC, CK_OBS_PCOOKIE_ADD);
} else {
log.w("Protected item already added: ", aItem);
}
return !isProtected;
},
/**
* @brief Remove a protected item
* @param aItem A protected item (cookie: host/name/path; storage: scope/key)
* @return true if the protected item has been removed
* @par
* Notify the observer: OBS_TOPIC/OBS_DEL
*/
removeProtectedItem: function(aItem) {
if (aItem == null) {
log.e("removeProtectedItem: Item is null");
return false;
}
var protList = [];
var exists = false;
var type = aItem.getType();
switch (type) {
case COOKIE_TYPE_STANDARD:
protList = CookieKeeperPrivate._protectedList.cookies;
break;
case COOKIE_TYPE_STORAGE:
protList = CookieKeeperPrivate._protectedList.storage;
break;
case COOKIE_TYPE_LSO:
protList = CookieKeeperPrivate._protectedList.lso;
break;
default:
log.e("addProtectedItem: item is invalid", aItem);
return false;
}
for (let i=0; i<protList.length; i++) {
let pi = protList[i];
if (pi.equals(aItem)) {
exists = true;
protList.splice(i, 1);
CookieKeeperPrivate._protectedList.lastUpdate = Date.now();
saveProtectedListTimer();
let data = { pcookie : null, pstorage : null, plso : null };
switch (type) {
case COOKIE_TYPE_STANDARD:
data.pcookie = pi;
break;
case COOKIE_TYPE_STORAGE:
data.pstorage = pi;
break;
case COOKIE_TYPE_LSO:
data.plso = pi;
break;
}
let subject = { wrappedJSObject: data };
Services.obs.notifyObservers(subject, CK_OBS_PCOOKIE_TOPIC, CK_OBS_PCOOKIE_DEL);
i = protList.length;
}
}
if (DEBUG && !exists) {
log.w("Protected item do not exists: ", aItem);
}
return exists;
},
/**
* @brief Load the cookies objects (Cookeis, DOM Storage, LSO)
* @param aCallbackFunction: A function which will be called with the cookies items in an array
* @return The number of expected callback function call (normally 3 callback for CK 1.9.0).
* @note The loading is async, which means that the function will return before the callback function is called.
*/
loadCookiesASync: function(aCallbackFunction) {
if (cookieManagers != null) {
let nCallback = 0;
for (let i=0; i<cookieManagers.length; i++) {
try {
cookieManagers[i].fetchItems(aCallbackFunction);
nCallback++;
} catch (ex) {
log.e(ex);
}
}
return nCallback;
}
return 0;
},
/**
* Clear unprotected cookies/dom storage.
* @param atExit Boolean, must be true only when the mozilla application is closing.
* - If true, read the preferences to know if we need to clear cookies/dom storage.
* - If false, clear unprotected cookies and dom storage. Clear HSTS only if wanted.
*/
clearAllCookies: function(atExit) {
var sPrefs = Services.prefs;
var clearCookies = true;
var clearStorage = true;
var clearLSO = false;
var clearHSTS = false;
if (typeof(atExit) == "boolean" && atExit == true) {
clearCookies = sPrefs.getPrefType(PREF_CLEAR_COOKIES_AT_EXIT) == sPrefs.PREF_BOOL ? sPrefs.getBoolPref(PREF_CLEAR_COOKIES_AT_EXIT) : false;
clearStorage = sPrefs.getPrefType(PREF_CLEAR_DOM_AT_EXIT) == sPrefs.PREF_BOOL ? sPrefs.getBoolPref(PREF_CLEAR_DOM_AT_EXIT) : false;
clearLSO = sPrefs.getPrefType(PREF_CLEAR_LSO_AT_EXIT) == sPrefs.PREF_BOOL ? sPrefs.getBoolPref(PREF_CLEAR_LSO_AT_EXIT) : false;
clearHSTS = sPrefs.getPrefType(PREF_CLEAR_HSTS_AT_EXIT) == sPrefs.PREF_BOOL ? sPrefs.getBoolPref(PREF_CLEAR_HSTS_AT_EXIT) : false;
} else {
clearLSO = sPrefs.getPrefType(PREF_CLEAR_LSO_ON_DEMAND) == sPrefs.PREF_BOOL ? sPrefs.getBoolPref(PREF_CLEAR_LSO_ON_DEMAND) : false;
clearHSTS = sPrefs.getPrefType(PREF_CLEAR_HSTS_ON_DEMAND) == sPrefs.PREF_BOOL ? sPrefs.getBoolPref(PREF_CLEAR_HSTS_ON_DEMAND) : false;
}
if (clearCookies) {
log.d("Clear unprotected cookies.");
gCookieStorage.clearUnwanted(CookieKeeperPrivate._protectedList.cookies);
}
if (clearStorage) {
log.d("Clear unprotected storage.");
gDomStorage.clearUnwanted(CookieKeeperPrivate._protectedList.storage);
}
if (clearLSO) {
log.d("Clear unprotected lso.");
gLocalSharedObject.clearUnwanted();
}
if (clearHSTS) {
log.d("Clear HSTS informations.");
try {
let sss = Cc["@mozilla.org/ssservice;1"].getService(Ci.nsISiteSecurityService);
sss.clearAll();
} catch (ex) {
// nsISiteSecurityService do not exists, HSTS informations may be in the permissions
let enumerator = Services.perms.enumerator;
let permsList = [];
while (enumerator.hasMoreElements()) {
let nextPermission = enumerator.getNext().QueryInterface(Ci.nsIPermission);
if (nextPermission.type == "sts/use" || nextPermission.type == "sts/subd") {
permsList.push(nextPermission);
}
}
for (let i=0; i<permsList.length; i++) {
let perm = permsList[i];
if (perm.principal) {
Services.perms.removeFromPrincipal(perm.principal, perm.type);
} else {
Services.perms.remove(perm.host, perm.type);
}
}
} // catch()
} // clearHSTS
} // clearAllCookies
};
|