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
|
/* vim:set ts=2 sw=2 sts=2 et: */
/* 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/. */
"use strict";
const {Cu} = require("chrome");
const STORAGE_STRINGS = "chrome://browser/locale/devtools/storage.properties";
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyGetter(this, "TreeWidget",
() => require("devtools/shared/widgets/TreeWidget").TreeWidget);
XPCOMUtils.defineLazyGetter(this, "TableWidget",
() => require("devtools/shared/widgets/TableWidget").TableWidget);
XPCOMUtils.defineLazyModuleGetter(this, "EventEmitter",
"resource://gre/modules/devtools/event-emitter.js");
XPCOMUtils.defineLazyModuleGetter(this, "ViewHelpers",
"resource:///modules/devtools/ViewHelpers.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "VariablesView",
"resource:///modules/devtools/VariablesView.jsm");
let Telemetry = require("devtools/shared/telemetry");
/**
* Localization convenience methods.
*/
let L10N = new ViewHelpers.L10N(STORAGE_STRINGS);
const GENERIC_VARIABLES_VIEW_SETTINGS = {
lazyEmpty: true,
lazyEmptyDelay: 10, // ms
searchEnabled: true,
searchPlaceholder: L10N.getStr("storage.search.placeholder"),
preventDescriptorModifiers: true
};
// Columns which are hidden by default in the storage table
const HIDDEN_COLUMNS = [
"creationTime",
"isDomain",
"isSecure"
];
/**
* StorageUI is controls and builds the UI of the Storage Inspector.
*
* @param {Front} front
* Front for the storage actor
* @param {Target} target
* Interface for the page we're debugging
* @param {Window} panelWin
* Window of the toolbox panel to populate UI in.
*/
this.StorageUI = function StorageUI(front, target, panelWin) {
EventEmitter.decorate(this);
this._target = target;
this._window = panelWin;
this._panelDoc = panelWin.document;
this.front = front;
let treeNode = this._panelDoc.getElementById("storage-tree");
this.tree = new TreeWidget(treeNode, {defaultType: "dir"});
this.onHostSelect = this.onHostSelect.bind(this);
this.tree.on("select", this.onHostSelect);
let tableNode = this._panelDoc.getElementById("storage-table");
this.table = new TableWidget(tableNode, {
emptyText: L10N.getStr("table.emptyText"),
highlightUpdated: true,
});
this.displayObjectSidebar = this.displayObjectSidebar.bind(this);
this.table.on(TableWidget.EVENTS.ROW_SELECTED, this.displayObjectSidebar)
this.sidebar = this._panelDoc.getElementById("storage-sidebar");
this.sidebar.setAttribute("width", "300");
this.view = new VariablesView(this.sidebar.firstChild,
GENERIC_VARIABLES_VIEW_SETTINGS);
this.front.listStores().then(storageTypes => {
this.populateStorageTree(storageTypes);
}).then(null, Cu.reportError);
this.onUpdate = this.onUpdate.bind(this);
this.front.on("stores-update", this.onUpdate);
this.handleKeypress = this.handleKeypress.bind(this);
this._panelDoc.addEventListener("keypress", this.handleKeypress);
this._telemetry = new Telemetry();
this._telemetry.toolOpened("storage");
}
exports.StorageUI = StorageUI;
StorageUI.prototype = {
storageTypes: null,
shouldResetColumns: true,
destroy: function() {
this.front.off("stores-update", this.onUpdate);
this._panelDoc.removeEventListener("keypress", this.handleKeypress);
this._telemetry.toolClosed("storage");
},
/**
* Empties and hides the object viewer sidebar
*/
hideSidebar: function() {
this.view.empty();
this.sidebar.hidden = true;
this.table.clearSelection();
},
/**
* Removes the given item from the storage table. Reselects the next item in
* the table and repopulates the sidebar with that item's data if the item
* being removed was selected.
*/
removeItemFromTable: function(name) {
if (this.table.isSelected(name)) {
if (this.table.selectedIndex == 0) {
this.table.selectNextRow()
}
else {
this.table.selectPreviousRow();
}
this.table.remove(name);
this.displayObjectSidebar();
}
else {
this.table.remove(name);
}
},
/**
* Event handler for "stores-update" event coming from the storage actor.
*
* @param {object} argument0
* An object containing the details of the added, changed and deleted
* storage objects.
* Each of these 3 objects are of the following format:
* {
* <store_type1>: {
* <host1>: [<store_names1>, <store_name2>...],
* <host2>: [<store_names34>...], ...
* },
* <store_type2>: {
* <host1>: [<store_names1>, <store_name2>...],
* <host2>: [<store_names34>...], ...
* }, ...
* }
* Where store_type1 and store_type2 is one of cookies, indexedDB,
* sessionStorage and localStorage; host1, host2 are the host in which
* this change happened; and [<store_namesX] is an array of the names
* of the changed store objects. This array is empty for deleted object
* if the host was completely removed.
*/
onUpdate: function({ changed, added, deleted }) {
if (deleted) {
for (let type in deleted) {
for (let host in deleted[type]) {
if (!deleted[type][host].length) {
// This means that the whole host is deleted, thus the item should
// be removed from the storage tree
if (this.tree.isSelected([type, host])) {
this.table.clear();
this.hideSidebar();
this.tree.selectPreviousItem();
}
this.tree.remove([type, host]);
}
else if (this.tree.isSelected([type, host])) {
for (let name of deleted[type][host]) {
try {
// trying to parse names in case its for indexedDB
let names = JSON.parse(name);
if (!names[2]) {
if (this.tree.isSelected([type, host, names[0], names[1]])) {
this.tree.selectPreviousItem();
this.tree.remove([type, host, names[0], names[1]]);
this.table.clear();
this.hideSidebar();
}
}
else if (this.tree.isSelected([type, host, names[0], names[1]])) {
this.removeItemFromTable(names[2]);
}
}
catch (ex) {
this.removeItemFromTable(name);
}
}
}
}
}
}
if (added) {
for (let type in added) {
for (let host in added[type]) {
this.tree.add([type, {id: host, type: "url"}]);
for (let name of added[type][host]) {
try {
name = JSON.parse(name);
if (name.length == 3) {
name.splice(2, 1);
}
this.tree.add([type, host, ...name]);
if (!this.tree.selectedItem) {
this.tree.selectedItem = [type, host, name[0], name[1]];
this.fetchStorageObjects(type, host, [JSON.stringify(name)], 1);
}
} catch(ex) {}
}
if (this.tree.isSelected([type, host])) {
this.fetchStorageObjects(type, host, added[type][host], 1);
}
}
}
}
if (changed) {
let [type, host, db, objectStore] = this.tree.selectedItem;
if (changed[type] && changed[type][host]) {
if (changed[type][host].length) {
try {
let toUpdate = [];
for (let name of changed[type][host]) {
let names = JSON.parse(name);
if (names[0] == db && names[1] == objectStore && names[2]) {
toUpdate.push(name);
}
}
this.fetchStorageObjects(type, host, toUpdate, 2);
}
catch (ex) {
this.fetchStorageObjects(type, host, changed[type][host], 2);
}
}
}
}
if (added || deleted || changed) {
this.emit("store-objects-updated");
}
},
/**
* Fetches the storage objects from the storage actor and populates the
* storage table with the returned data.
*
* @param {string} type
* The type of storage. Ex. "cookies"
* @param {string} host
* Hostname
* @param {array} names
* Names of particular store objects. Empty if all are requested
* @param {number} reason
* 2 for update, 1 for new row in an existing table and 0 when
* populating a table for the first time for the given host/type
*/
fetchStorageObjects: function(type, host, names, reason) {
this.storageTypes[type].getStoreObjects(host, names).then(({data}) => {
if (!data.length) {
this.emit("store-objects-updated");
return;
}
if (this.shouldResetColumns) {
this.resetColumns(data[0], type);
}
this.populateTable(data, reason);
this.emit("store-objects-updated");
}, Cu.reportError);
},
/**
* Populates the storage tree which displays the list of storages present for
* the page.
*
* @param {object} storageTypes
* List of storages and their corresponding hosts returned by the
* StorageFront.listStores call.
*/
populateStorageTree: function(storageTypes) {
this.storageTypes = {};
for (let type in storageTypes) {
let typeLabel = L10N.getStr("tree.labels." + type);
this.tree.add([{id: type, label: typeLabel, type: "store"}]);
if (storageTypes[type].hosts) {
this.storageTypes[type] = storageTypes[type];
for (let host in storageTypes[type].hosts) {
this.tree.add([type, {id: host, type: "url"}]);
for (let name of storageTypes[type].hosts[host]) {
try {
let names = JSON.parse(name);
this.tree.add([type, host, ...names]);
if (!this.tree.selectedItem) {
this.tree.selectedItem = [type, host, names[0], names[1]];
this.fetchStorageObjects(type, host, [name], 0);
}
} catch(ex) {}
}
if (!this.tree.selectedItem) {
this.tree.selectedItem = [type, host];
this.fetchStorageObjects(type, host, null, 0);
}
}
}
}
},
/**
* Populates the selected entry from teh table in the sidebar for a more
* detailed view.
*/
displayObjectSidebar: function() {
let item = this.table.selectedRow;
if (!item) {
// Make sure that sidebar is hidden and return
this.sidebar.hidden = true;
return;
}
this.sidebar.hidden = false;
this.view.empty();
let mainScope = this.view.addScope(L10N.getStr("storage.data.label"));
mainScope.expanded = true;
if (item.name && item.valueActor) {
let itemVar = mainScope.addItem(item.name + "", {}, true);
item.valueActor.string().then(value => {
// The main area where the value will be displayed
itemVar.setGrip(value);
// May be the item value is a json or a key value pair itself
this.parseItemValue(item.name, value);
// By default the item name and value are shown. If this is the only
// information available, then nothing else is to be displayed.
let itemProps = Object.keys(item);
if (itemProps.length == 3) {
this.emit("sidebar-updated");
return;
}
// Display any other information other than the item name and value
// which may be available.
let rawObject = Object.create(null);
let otherProps =
itemProps.filter(e => e != "name" && e != "value" && e != "valueActor");
for (let prop of otherProps) {
rawObject[prop] = item[prop];
}
itemVar.populate(rawObject, {sorted: true});
itemVar.twisty = true;
itemVar.expanded = true;
this.emit("sidebar-updated");
});
return;
}
// Case when displaying IndexedDB db/object store properties.
for (let key in item) {
mainScope.addItem(key, {}, true).setGrip(item[key]);
this.parseItemValue(key, item[key]);
}
this.emit("sidebar-updated");
},
/**
* Tries to parse a string value into either a json or a key-value separated
* object and populates the sidebar with the parsed value. The value can also
* be a key separated array.
*
* @param {string} name
* The key corresponding to the `value` string in the object
* @param {string} value
* The string to be parsed into an object
*/
parseItemValue: function(name, value) {
let json = null
try {
json = JSON.parse(value);
}
catch (ex) {
json = null;
}
if (!json && value) {
json = this._extractKeyValPairs(value);
}
// return if json is null, or same as value, or just a string.
if (!json || json == value || typeof json == "string") {
return;
}
// One special case is a url which gets separated as key value pair on :
if ((json.length == 2 || Object.keys(json).length == 1) &&
((json[0] || Object.keys(json)[0]) + "").match(/^(http|file|ftp)/)) {
return;
}
let jsonObject = Object.create(null);
jsonObject[name] = json;
let valueScope = this.view.getScopeAtIndex(1) ||
this.view.addScope(L10N.getStr("storage.parsedValue.label"));
valueScope.expanded = true;
let jsonVar = valueScope.addItem("", Object.create(null), true);
jsonVar.expanded = true;
jsonVar.twisty = true;
jsonVar.populate(jsonObject, {expanded: true});
},
/**
* Tries to parse a string into an object on the basis of key-value pairs,
* separated by various separators. If failed, tries to parse for single
* separator separated values to form an array.
*
* @param {string} value
* The string to be parsed into an object or array
*/
_extractKeyValPairs: function(value) {
let makeObject = (keySep, pairSep) => {
let object = {};
for (let pair of value.split(pairSep)) {
let [key, val] = pair.split(keySep);
object[key] = val;
}
return object;
};
// Possible separators.
const separators = ["=", ":", "~", "#", "&", "\\*", ",", "\\."];
// Testing for object
for (let i = 0; i < separators.length; i++) {
let kv = separators[i];
for (let j = 0; j < separators.length; j++) {
if (i == j) {
continue;
}
let p = separators[j];
let regex = new RegExp("^([^" + kv + p + "]*" + kv + "+[^" + kv + p +
"]*" + p + "*)+$", "g");
if (value.match(regex) && value.contains(kv) &&
(value.contains(p) || value.split(kv).length == 2)) {
return makeObject(kv, p);
}
}
}
// Testing for array
for (let i = 0; i < separators.length; i++) {
let p = separators[i];
let regex = new RegExp("^[^" + p + "]+(" + p + "+[^" + p + "]*)+$", "g");
if (value.match(regex)) {
return value.split(p.replace(/\\*/g, ""));
}
}
return null;
},
/**
* Select handler for the storage tree. Fetches details of the selected item
* from the storage details and populates the storage tree.
*
* @param {string} event
* The name of the event fired
* @param {array} item
* An array of ids which represent the location of the selected item in
* the storage tree
*/
onHostSelect: function(event, item) {
this.table.clear();
this.hideSidebar();
let [type, host] = item;
let names = null;
if (!host) {
return;
}
if (item.length > 2) {
names = [JSON.stringify(item.slice(2))];
}
this.shouldResetColumns = true;
this.fetchStorageObjects(type, host, names, 0);
},
/**
* Resets the column headers in the storage table with the pased object `data`
*
* @param {object} data
* The object from which key and values will be used for naming the
* headers of the columns
* @param {string} type
* The type of storage corresponding to the after-reset columns in the
* table.
*/
resetColumns: function(data, type) {
let columns = {};
let uniqueKey = null;
for (let key in data) {
if (!uniqueKey) {
this.table.uniqueId = uniqueKey = key;
}
columns[key] = L10N.getStr("table.headers." + type + "." + key);
}
this.table.setColumns(columns, null, HIDDEN_COLUMNS);
this.shouldResetColumns = false;
this.hideSidebar();
},
/**
* Populates or updates the rows in the storage table.
*
* @param {array[object]} data
* Array of objects to be populated in the storage table
* @param {number} reason
* The reason of this populateTable call. 2 for update, 1 for new row
* in an existing table and 0 when populating a table for the first
* time for the given host/type
*/
populateTable: function(data, reason) {
for (let item of data) {
if (item.value) {
item.valueActor = item.value;
item.value = item.value.initial || "";
}
if (item.expires != null) {
item.expires = item.expires
? new Date(item.expires).toLocaleString()
: L10N.getStr("label.expires.session");
}
if (item.creationTime != null) {
item.creationTime = new Date(item.creationTime).toLocaleString();
}
if (item.lastAccessed != null) {
item.lastAccessed = new Date(item.lastAccessed).toLocaleString();
}
if (reason < 2) {
this.table.push(item, reason == 0);
}
else {
this.table.update(item);
if (item == this.table.selectedRow && !this.sidebar.hidden) {
this.displayObjectSidebar();
}
}
}
},
/**
* Handles keypress event on the body table to close the sidebar when open
*
* @param {DOMEvent} event
* The event passed by the keypress event.
*/
handleKeypress: function(event) {
if (event.keyCode == event.DOM_VK_ESCAPE && !this.sidebar.hidden) {
// Stop Propagation to prevent opening up of split console
this.hideSidebar();
event.stopPropagation();
event.preventDefault();
}
}
}
|