/
cookiePanel.js
548 lines (451 loc) · 17.3 KB
/
cookiePanel.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
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
/* See license.txt for terms of usage */
define([
"firebug/lib/xpcom",
"firebug/lib/object",
"firebug/lib/locale",
"firebug/lib/domplate",
"firebug/lib/dom",
"firebug/lib/options",
"firebug/lib/persist",
"firebug/lib/string",
"firebug/lib/http",
"firebug/lib/css",
"firebug/lib/events",
"firebug/lib/array",
"firebug/lib/search",
"firebug/cookies/menuUtils",
"firebug/cookies/cookieReps",
"firebug/cookies/headerResizer",
"firebug/cookies/cookieObserver",
"firebug/cookies/cookieUtils",
"firebug/cookies/cookie",
"firebug/cookies/breakpoints",
"firebug/cookies/cookiePermissions",
"firebug/cookies/cookieClipboard",
],
function(Xpcom, Obj, Locale, Domplate, Dom, Options, Persist, Str, Http, Css, Events, Arr, Search,
MenuUtils, CookieReps, HeaderResizer, CookieObserver, CookieUtils, Cookie, Breakpoints,
CookiePermissions, CookieClipboard) {
with (Domplate) {
// ********************************************************************************************* //
// Constants
const Cc = Components.classes;
const Ci = Components.interfaces;
// Cookies preferences
const logEventsPref = "cookies.logEvents";
const showRejectedCookies = "cookies.showRejectedCookies";
const lastSortedColumn = "cookies.lastSortedColumn";
const hiddenColsPref = "cookies.hiddenColumns";
const removeConfirmation = "cookies.removeConfirmation";
// Services
var cookieManager = Xpcom.CCSV("@mozilla.org/cookiemanager;1", "nsICookieManager2");
// ********************************************************************************************* //
// Panel Implementation
/**
* @panel This class represents the Cookies panel that is displayed within
* Firebug UI.
*/
function CookiePanel() {}
CookiePanel.prototype = Obj.extend(Firebug.ActivablePanel,
/** @lends CookiePanel */
{
name: "cookies",
title: Locale.$STR("cookies.Panel"),
searchable: true,
breakable: true,
order: 200, // Place just after the Net panel.
initialize: function(context, doc)
{
// xxxHonza
// This initialization is made as soon as the Cookies panel
// is opened the first time.
// This means that columns are *not* resizeable within the console
// (rejected cookies) till this activation isn't executed.
// Initialize event listeners before the ancestor is called.
var hcr = HeaderResizer;
this.onMouseClick = Obj.bind(hcr.onMouseClick, hcr);
this.onMouseDown = Obj.bind(hcr.onMouseDown, hcr);
this.onMouseMove = Obj.bind(hcr.onMouseMove, hcr);
this.onMouseUp = Obj.bind(hcr.onMouseUp, hcr);
this.onMouseOut = Obj.bind(hcr.onMouseOut, hcr);
this.onContextMenu = Obj.bind(this.onContextMenu, this);
Firebug.ActivablePanel.initialize.apply(this, arguments);
Firebug.ConsolePanel.prototype.addListener(this);
// Just after the initialization, so the this.document member is set.
Firebug.CookieModule.addStyleSheet(this);
this.refresh();
},
shutdown: function()
{
Firebug.ConsolePanel.prototype.removeListener(this);
},
/**
* Renders list of cookies displayed within the Cookies panel.
*/
refresh: function()
{
if (!Firebug.CookieModule.isEnabled(this.context))
return;
// Create cookie list table.
this.table = CookieReps.CookieTable.createTable(this.panelNode);
// Cookies are displayed only for web pages.
var location = this.context.window.location;
if (!location)
return;
var protocol = location.protocol;
if (protocol.indexOf("http") != 0)
return;
// Get list of cookies for the current page.
var cookies = [];
var iter = cookieManager.enumerator;
while (iter.hasMoreElements())
{
var cookie = iter.getNext();
if (!cookie)
break;
cookie = cookie.QueryInterface(Ci.nsICookie2);
if (!CookieObserver.isCookieFromContext(this.context, cookie))
continue;
var cookieWrapper = new Cookie(CookieUtils.makeCookieObject(cookie));
cookies.push(cookieWrapper);
}
// If the filter allow it, display all rejected cookies as well.
if (Options.get(showRejectedCookies))
{
// xxxHonza the this.context.cookies is sometimes null, but
// this must be because FB isn't correctly initialized.
if (!this.context.cookies)
{
if (FBTrace.DBG_COOKIES)
{
FBTrace.sysout(
"cookies.Cookie context isn't properly initialized - ERROR: " +
this.context.getName());
}
return;
}
var activeHosts = this.context.cookies.activeHosts;
for (var hostName in activeHosts)
{
var host = activeHosts[hostName];
if (!host.rejected)
continue;
var receivedCookies = host.receivedCookies;
if (receivedCookies)
cookies = Arr.extendArray(cookies, receivedCookies);
}
}
// Generate HTML list of cookies using domplate.
if (cookies.length)
{
var header = Dom.getElementByClass(this.table, "cookieHeaderRow");
var tag = CookieReps.CookieRow.cookieTag;
var row = tag.insertRows({cookies: cookies}, header)[0];
for (var i=0; i<cookies.length; i++)
{
var cookie = cookies[i];
cookie.row = row;
Breakpoints.updateBreakpoint(this.context, cookie);
row = row.nextSibling;
}
}
if (FBTrace.DBG_COOKIES)
FBTrace.sysout("cookies.Cookie list refreshed.", cookies);
// Sort automaticaly the last sorted column. The preference stores
// two things: name of the sorted column and sort direction asc|desc.
// Example: colExpires asc
var prefValue = Options.get(lastSortedColumn);
if (prefValue) {
var values = prefValue.split(" ");
CookieReps.CookieTable.sortColumn(this.table, values[0], values[1]);
}
// Update visibility of columns according to the preferences
var hiddenCols = Options.get(hiddenColsPref);
if (hiddenCols)
this.table.setAttribute("hiddenCols", hiddenCols);
},
initializeNode: function(oldPanelNode)
{
if (FBTrace.DBG_COOKIES)
FBTrace.sysout("cookies.CookiePanel.initializeNode");
// xxxHonza
// This method isn't called when FB UI is detached. So, the columns
// are *not* resizable when FB is open in external window.
// Register event handlers for table column resizing.
this.document.addEventListener("click", this.onMouseClick, true);
this.document.addEventListener("mousedown", this.onMouseDown, true);
this.document.addEventListener("mousemove", this.onMouseMove, true);
this.document.addEventListener("mouseup", this.onMouseUp, true);
this.document.addEventListener("mouseout", this.onMouseOut, true);
this.panelNode.addEventListener("contextmenu", this.onContextMenu, false);
},
destroyNode: function()
{
if (FBTrace.DBG_COOKIES)
FBTrace.sysout("cookies.CookiePanel.destroyNode");
this.document.removeEventListener("mouseclick", this.onMouseClick, true);
this.document.removeEventListener("mousedown", this.onMouseDown, true);
this.document.removeEventListener("mousemove", this.onMouseMove, true);
this.document.removeEventListener("mouseup", this.onMouseUp, true);
this.document.removeEventListener("mouseout", this.onMouseOut, true);
this.panelNode.removeEventListener("contextmenu", this.onContextMenu, false);
},
onContextMenu: function(event)
{
Breakpoints.onContextMenu(this.context, event);
},
detach: function(oldChrome, newChrome)
{
Firebug.ActivablePanel.detach.apply(this, arguments);
},
reattach: function(doc)
{
Firebug.ActivablePanel.reattach.apply(this, arguments);
},
clear: function()
{
if (this.panelNode)
Dom.clearNode(this.panelNode);
this.table = null;
},
show: function(state)
{
// Update permission button in the toolbar.
CookiePermissions.updatePermButton(this.context);
// For backward compatibility with Firebug 1.1
//
// Firebug 1.6 removes Firebug.DisabledPanelPage, simplifies the activation
// and the following code is not necessary any more.
if (Firebug.ActivableModule && Firebug.DisabledPanelPage)
{
var shouldShow = Firebug.CookieModule.isEnabled(this.context);
this.showToolbarButtons("fbCookieButtons", shouldShow);
if (!shouldShow)
{
// The activation model has been changed in Firebug 1.4. This is
// just to keep backward compatibility.
if (Firebug.DisabledPanelPage.show)
Firebug.DisabledPanelPage.show(this, Firebug.CookieModule);
else
Firebug.CookieModule.disabledPanelPage.show(this);
return;
}
}
else
{
this.showToolbarButtons("fbCookieButtons", true);
}
if (Firebug.chrome.setGlobalAttribute)
{
Firebug.chrome.setGlobalAttribute("cmd_resumeExecution", "breakable", "true");
Firebug.chrome.setGlobalAttribute("cmd_resumeExecution", "tooltiptext",
Locale.$STR("cookies.Break On Cookie"));
}
},
hide: function()
{
this.showToolbarButtons("fbCookieButtons", false);
},
// Options menu
getOptionsMenuItems: function(context)
{
return [
MenuUtils.optionAllowGlobally(context, "cookies.AllowGlobally",
"cookies.tip.AllowGlobally", "network.cookie", "cookieBehavior"),
/*MenuUtils.optionMenu(context, "cookies.clearWhenDeny",
"cookies.tip.clearWhenDeny", Firebug.prefDomain, clearWhenDeny),*/
MenuUtils.optionMenu(context, "cookies.LogEvents",
"cookies.tip.LogEvents", Firebug.prefDomain, logEventsPref),
MenuUtils.optionMenu(context, "cookies.Confirm cookie removal",
"cookies.tip.Confirm cookie removal", Firebug.prefDomain, removeConfirmation)
];
},
getContextMenuItems: function(object, target)
{
var items = [];
// If the user clicked at a cookie row, the context menu is already
// initialized and so, bail out.
var cookieRow = Dom.getAncestorByClass(target, "cookieRow");
if (cookieRow)
return items;
// Also bail out if the user clicked on the header.
var header = Dom.getAncestorByClass(target, "cookieHeaderRow");
if (header)
return items;
// Make sure default items (cmd_copy) is removed.
CookieReps.Rep.getContextMenuItems.apply(this, arguments);
return items;
},
search: function(text)
{
if (!text)
return;
// Make previously visible nodes invisible again
if (this.matchSet)
{
for (var i in this.matchSet)
Css.removeClass(this.matchSet[i], "matched");
}
this.matchSet = [];
function findRow(node) { return Dom.getAncestorByClass(node, "cookieRow"); }
var search = new Search.TextSearch(this.panelNode, findRow);
var cookieRow = search.find(text);
if (!cookieRow)
return false;
for (; cookieRow; cookieRow = search.findNext())
{
Css.setClass(cookieRow, "matched");
this.matchSet.push(cookieRow);
}
return true;
},
getPopupObject: function(target)
{
var header = Dom.getAncestorByClass(target, "cookieHeaderRow");
if (header)
return CookieReps.CookieTable;
return Firebug.ActivablePanel.getPopupObject.apply(this, arguments);
},
findRepObject: function(cookie)
{
var strippedHost = CookieUtils.makeStrippedHost(cookie.host);
var result = null;
this.enumerateCookies(function(rep)
{
if (rep.rawHost == strippedHost &&
rep.cookie.name == cookie.name &&
rep.cookie.path == cookie.path)
{
result = rep;
return true; // break iteration
}
});
return result;
},
supportsObject: function(object)
{
return object instanceof Cookie;
},
updateSelection: function(cookie)
{
var repCookie = this.findRepObject(cookie.cookie);
if (!repCookie)
return;
CookieReps.CookieRow.toggleRow(repCookie.row, true);
Dom.scrollIntoCenterView(repCookie.row);
},
enumerateCookies: function(fn)
{
if (!this.table)
return;
var rows = Dom.getElementsByClass(this.table, "cookieRow");
for (var i=0; i<rows.length; i++)
{
var cookie = Firebug.getRepObject(rows[i]);
if (!cookie)
continue;
if (fn(cookie))
break;
}
},
getEditor: function(target, value)
{
if (!this.conditionEditor)
this.conditionEditor = new Firebug.CookieModule.ConditionEditor(this.document);
return this.conditionEditor;
},
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// Support for Break On Next
breakOnNext: function(breaking)
{
this.context.breakOnCookie = breaking;
if (FBTrace.DBG_COOKIES)
FBTrace.sysout("cookies.breakOnNext; " + context.breakOnCookie + ", " +
context.getName());
},
shouldBreakOnNext: function()
{
return this.context.breakOnCookie;
},
getBreakOnNextTooltip: function(enabled)
{
return (enabled ? Locale.$STR("cookies.Disable Break On Cookie") :
Locale.$STR("cookies.Break On Cookie"));
},
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// Console Panel Listeners
onFilterSet: function(logTypes)
{
logTypes.cookies = 1;
},
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// Panel Activation
onActivationChanged: function(enable)
{
if (FBTrace.DBG_COOKIES || FBTrace.DBG_ACTIVATION)
FBTrace.sysout("cookies.CookiePanel.onActivationChanged; " + enable);
if (enable)
{
Firebug.CookieModule.addObserver(this);
Firebug.Debugger.addListener(Firebug.CookieModule.DebuggerListener);
Firebug.Console.addListener(Firebug.CookieModule.ConsoleListener);
}
else
{
Firebug.CookieModule.removeObserver(this);
Firebug.Debugger.removeListener(Firebug.CookieModule.DebuggerListener);
Firebug.Console.removeListener(Firebug.CookieModule.ConsoleListener);
}
},
});
// ********************************************************************************************* //
// Cookie Breakpoints
/**
* @class Represents {@link Firebug.Debugger} listener. This listener is reponsible for
* providing a list of cookie-breakpoints into the Breakpoints side-panel.
*/
Firebug.CookieModule.DebuggerListener =
{
getBreakpoints: function(context, groups)
{
if (!context.cookies.breakpoints.isEmpty())
groups.push(context.cookies.breakpoints);
}
};
// ********************************************************************************************* //
// Custom output in the Console panel for: document.cookie
Firebug.CookieModule.ConsoleListener =
{
tag:
DIV({_repObject: "$object"},
DIV({"class": "documentCookieBody"})
),
log: function(context, object, className, sourceLink)
{
//xxxHonza: chromebug says it's null sometimes.
if (!context)
return;
if (object !== context.window.document.cookie)
return;
// Parse "document.cookie" string.
var cookies = CookieUtils.parseSentCookiesFromString(object);
if (!cookies || !cookies.length)
return;
// Create empty log row that serves as a container for list of cookies
// crated from the document.cookie property.
var appendObject = Firebug.ConsolePanel.prototype.appendObject;
var row = Firebug.ConsoleBase.logRow(appendObject, object, context,
"documentCookie", this, null, true);
var rowBody = Dom.getElementByClass(row, "documentCookieBody");
CookieReps.CookieTable.render(cookies, rowBody);
},
logFormatted: function(context, objects, className, sourceLink)
{
}
};
// ********************************************************************************************* //
// Registration
Firebug.registerPanel(CookiePanel);
return CookiePanel;
// ********************************************************************************************* //
}});