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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
|
/* 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 {Task} = require("devtools/shared/task");
const EventEmitter = require("devtools/shared/event-emitter");
const {LocalizationHelper, ELLIPSIS} = require("devtools/shared/l10n");
const {KeyShortcuts} = require("devtools/client/shared/key-shortcuts");
const JSOL = require("devtools/client/shared/vendor/jsol");
const {KeyCodes} = require("devtools/client/shared/keycodes");
// GUID to be used as a separator in compound keys. This must match the same
// constant in devtools/server/actors/storage.js,
// devtools/client/storage/test/head.js and
// devtools/server/tests/browser/head.js
const SEPARATOR_GUID = "{9d414cc5-8319-0a04-0586-c0a6ae01670a}";
loader.lazyRequireGetter(this, "TreeWidget",
"devtools/client/shared/widgets/TreeWidget", true);
loader.lazyRequireGetter(this, "TableWidget",
"devtools/client/shared/widgets/TableWidget", true);
loader.lazyRequireGetter(this, "ViewHelpers",
"devtools/client/shared/widgets/view-helpers");
loader.lazyImporter(this, "VariablesView",
"resource://devtools/client/shared/widgets/VariablesView.jsm");
/**
* Localization convenience methods.
*/
const STORAGE_STRINGS = "devtools/client/locales/storage.properties";
const L10N = new LocalizationHelper(STORAGE_STRINGS);
const GENERIC_VARIABLES_VIEW_SETTINGS = {
lazyEmpty: true,
// ms
lazyEmptyDelay: 10,
searchEnabled: true,
searchPlaceholder: L10N.getStr("storage.search.placeholder"),
preventDescriptorModifiers: true
};
const REASON = {
NEW_ROW: "new-row",
NEXT_50_ITEMS: "next-50-items",
POPULATE: "populate",
UPDATE: "update"
};
const COOKIE_KEY_MAP = {
path: "Path",
host: "Domain",
expires: "Expires",
isSecure: "Secure",
isHttpOnly: "HttpOnly",
isDomain: "HostOnly",
creationTime: "CreationTime",
lastAccessed: "LastAccessed"
};
// Maximum length of item name to show in context menu label - will be
// trimmed with ellipsis if it's longer.
const ITEM_NAME_MAX_LENGTH = 32;
function addEllipsis(name) {
if (name.length > ITEM_NAME_MAX_LENGTH) {
if (/^https?:/.test(name)) {
// For URLs, add ellipsis in the middle
const halfLen = ITEM_NAME_MAX_LENGTH / 2;
return name.slice(0, halfLen) + ELLIPSIS + name.slice(-halfLen);
}
// For other strings, add ellipsis at the end
return name.substr(0, ITEM_NAME_MAX_LENGTH) + ELLIPSIS;
}
return name;
}
/**
* 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.
*/
function StorageUI(front, target, panelWin, toolbox) {
EventEmitter.decorate(this);
this._target = target;
this._window = panelWin;
this._panelDoc = panelWin.document;
this._toolbox = toolbox;
this.front = front;
let treeNode = this._panelDoc.getElementById("storage-tree");
this.tree = new TreeWidget(treeNode, {
defaultType: "dir",
contextMenuId: "storage-tree-popup"
});
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,
cellContextMenuId: "storage-table-popup"
});
this.updateObjectSidebar = this.updateObjectSidebar.bind(this);
this.table.on(TableWidget.EVENTS.ROW_SELECTED, this.updateObjectSidebar);
this.handleScrollEnd = this.handleScrollEnd.bind(this);
this.table.on(TableWidget.EVENTS.SCROLL_END, this.handleScrollEnd);
this.editItem = this.editItem.bind(this);
this.table.on(TableWidget.EVENTS.CELL_EDIT, this.editItem);
this.sidebar = this._panelDoc.getElementById("storage-sidebar");
this.sidebar.setAttribute("width", "300");
this.view = new VariablesView(this.sidebar.firstChild,
GENERIC_VARIABLES_VIEW_SETTINGS);
this.searchBox = this._panelDoc.getElementById("storage-searchbox");
this.filterItems = this.filterItems.bind(this);
this.searchBox.addEventListener("command", this.filterItems);
let shortcuts = new KeyShortcuts({
window: this._panelDoc.defaultView,
});
let key = L10N.getStr("storage.filter.key");
shortcuts.on(key, (name, event) => {
event.preventDefault();
this.searchBox.focus();
});
this.front.listStores().then(storageTypes => {
this.populateStorageTree(storageTypes);
}).then(null, console.error);
this.onUpdate = this.onUpdate.bind(this);
this.front.on("stores-update", this.onUpdate);
this.onCleared = this.onCleared.bind(this);
this.front.on("stores-cleared", this.onCleared);
this.handleKeypress = this.handleKeypress.bind(this);
this._panelDoc.addEventListener("keypress", this.handleKeypress);
this.onTreePopupShowing = this.onTreePopupShowing.bind(this);
this._treePopup = this._panelDoc.getElementById("storage-tree-popup");
this._treePopup.addEventListener("popupshowing", this.onTreePopupShowing);
this.onTablePopupShowing = this.onTablePopupShowing.bind(this);
this._tablePopup = this._panelDoc.getElementById("storage-table-popup");
this._tablePopup.addEventListener("popupshowing", this.onTablePopupShowing);
this.onRefreshTable = this.onRefreshTable.bind(this);
this.onAddItem = this.onAddItem.bind(this);
this.onRemoveItem = this.onRemoveItem.bind(this);
this.onRemoveAllFrom = this.onRemoveAllFrom.bind(this);
this.onRemoveAll = this.onRemoveAll.bind(this);
this.onRemoveAllSessionCookies = this.onRemoveAllSessionCookies.bind(this);
this.onRemoveTreeItem = this.onRemoveTreeItem.bind(this);
this._refreshButton = this._panelDoc.getElementById("refresh-button");
this._refreshButton.addEventListener("command", this.onRefreshTable);
this._addButton = this._panelDoc.getElementById("add-button");
this._addButton.addEventListener("command", this.onAddItem);
this._tablePopupAddItem = this._panelDoc.getElementById(
"storage-table-popup-add");
this._tablePopupAddItem.addEventListener("command", this.onAddItem);
this._tablePopupDelete = this._panelDoc.getElementById(
"storage-table-popup-delete");
this._tablePopupDelete.addEventListener("command", this.onRemoveItem);
this._tablePopupDeleteAllFrom = this._panelDoc.getElementById(
"storage-table-popup-delete-all-from");
this._tablePopupDeleteAllFrom.addEventListener("command",
this.onRemoveAllFrom);
this._tablePopupDeleteAll = this._panelDoc.getElementById(
"storage-table-popup-delete-all");
this._tablePopupDeleteAll.addEventListener("command", this.onRemoveAll);
this._tablePopupDeleteAllSessionCookies = this._panelDoc.getElementById(
"storage-table-popup-delete-all-session-cookies");
this._tablePopupDeleteAllSessionCookies.addEventListener("command",
this.onRemoveAllSessionCookies);
this._treePopupDeleteAll = this._panelDoc.getElementById(
"storage-tree-popup-delete-all");
this._treePopupDeleteAll.addEventListener("command", this.onRemoveAll);
this._treePopupDeleteAllSessionCookies = this._panelDoc.getElementById(
"storage-tree-popup-delete-all-session-cookies");
this._treePopupDeleteAllSessionCookies.addEventListener("command",
this.onRemoveAllSessionCookies);
this._treePopupDelete = this._panelDoc.getElementById("storage-tree-popup-delete");
this._treePopupDelete.addEventListener("command", this.onRemoveTreeItem);
}
exports.StorageUI = StorageUI;
StorageUI.prototype = {
storageTypes: null,
shouldLoadMoreItems: true,
set animationsEnabled(value) {
this._panelDoc.documentElement.classList.toggle("no-animate", !value);
},
destroy: function () {
this.table.off(TableWidget.EVENTS.ROW_SELECTED, this.updateObjectSidebar);
this.table.off(TableWidget.EVENTS.SCROLL_END, this.handleScrollEnd);
this.table.off(TableWidget.EVENTS.CELL_EDIT, this.editItem);
this.table.destroy();
this.front.off("stores-update", this.onUpdate);
this.front.off("stores-cleared", this.onCleared);
this._panelDoc.removeEventListener("keypress", this.handleKeypress);
this.searchBox.removeEventListener("input", this.filterItems);
this.searchBox = null;
this._treePopup.removeEventListener("popupshowing", this.onTreePopupShowing);
this._refreshButton.removeEventListener("command", this.onRefreshTable);
this._addButton.removeEventListener("command", this.onAddItem);
this._tablePopupAddItem.removeEventListener("command", this.onAddItem);
this._treePopupDeleteAll.removeEventListener("command", this.onRemoveAll);
this._treePopupDeleteAllSessionCookies.removeEventListener("command",
this.onRemoveAllSessionCookies);
this._treePopupDelete.removeEventListener("command", this.onRemoveTreeItem);
this._tablePopup.removeEventListener("popupshowing", this.onTablePopupShowing);
this._tablePopupDelete.removeEventListener("command", this.onRemoveItem);
this._tablePopupDeleteAllFrom.removeEventListener("command", this.onRemoveAllFrom);
this._tablePopupDeleteAll.removeEventListener("command", this.onRemoveAll);
this._tablePopupDeleteAllSessionCookies.removeEventListener("command",
this.onRemoveAllSessionCookies);
},
/**
* Empties and hides the object viewer sidebar
*/
hideSidebar: function () {
this.view.empty();
this.sidebar.hidden = true;
this.table.clearSelection();
},
getCurrentFront: function () {
let type = this.table.datatype;
return this.storageTypes[type];
},
/**
* Make column fields editable
*
* @param {Array} editableFields
* An array of keys of columns to be made editable
*/
makeFieldsEditable: function* (editableFields) {
if (editableFields && editableFields.length > 0) {
this.table.makeFieldsEditable(editableFields);
} else if (this.table._editableFieldsEngine) {
this.table._editableFieldsEngine.destroy();
}
},
editItem: function (eventType, data) {
let front = this.getCurrentFront();
front.editItem(data);
},
/**
* 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) && this.table.items.size > 1) {
if (this.table.selectedIndex == 0) {
this.table.selectNextRow();
} else {
this.table.selectPreviousRow();
}
}
this.table.remove(name);
this.updateObjectSidebar();
},
/**
* Event handler for "stores-cleared" event coming from the storage actor.
*
* @param {object} response
* An object containing which storage types were cleared
*/
onCleared: function (response) {
function* enumPaths() {
for (let type in response) {
if (Array.isArray(response[type])) {
// Handle the legacy response with array of hosts
for (let host of response[type]) {
yield [type, host];
}
} else {
// Handle the new format that supports clearing sub-stores in a host
for (let host in response[type]) {
let paths = response[type][host];
if (!paths.length) {
yield [type, host];
} else {
for (let path of paths) {
try {
path = JSON.parse(path);
yield [type, host, ...path];
} catch (ex) {
// ignore
}
}
}
}
}
}
}
for (let path of enumPaths()) {
// Find if the path is selected (there is max one) and clear it
if (this.tree.isSelected(path)) {
this.table.clear();
this.hideSidebar();
this.emit("store-objects-cleared");
break;
}
}
},
/**
* 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) {
this.handleDeletedItems(deleted);
}
if (added) {
this.handleAddedItems(added);
}
if (changed) {
this.handleChangedItems(changed);
}
if (added || deleted || changed) {
this.emit("store-objects-updated");
}
},
/**
* Handle added items received by onUpdate
*
* @param {object} See onUpdate docs
*/
handleAddedItems: function (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)],
REASON.NEW_ROW);
}
} catch (ex) {
// Do nothing
}
}
if (this.tree.isSelected([type, host])) {
this.fetchStorageObjects(type, host, added[type][host],
REASON.NEW_ROW);
}
}
}
},
/**
* Handle deleted items received by onUpdate
*
* @param {object} See onUpdate docs
*/
handleDeletedItems: function (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 {
for (let name of deleted[type][host]) {
try {
// trying to parse names in case of indexedDB or cache
let names = JSON.parse(name);
// Is a whole cache, database or objectstore deleted?
// Then remove it from the tree.
if (names.length < 3) {
if (this.tree.isSelected([type, host, ...names])) {
this.table.clear();
this.hideSidebar();
this.tree.selectPreviousItem();
}
this.tree.remove([type, host, ...names]);
}
// Remove the item from table if currently displayed.
if (names.length > 0) {
let tableItemName = names.pop();
if (this.tree.isSelected([type, host, ...names])) {
this.removeItemFromTable(tableItemName);
}
}
} catch (ex) {
if (this.tree.isSelected([type, host])) {
this.removeItemFromTable(name);
}
}
}
}
}
}
},
/**
* Handle changed items received by onUpdate
*
* @param {object} See onUpdate docs
*/
handleChangedItems: function (changed) {
let selectedItem = this.tree.selectedItem;
if (!selectedItem) {
return;
}
let [type, host, db, objectStore] = selectedItem;
if (!changed[type] || !changed[type][host] ||
changed[type][host].length == 0) {
return;
}
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, REASON.UPDATE);
} catch (ex) {
this.fetchStorageObjects(type, host, changed[type][host], REASON.UPDATE);
}
},
/**
* 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 {Constant} reason
* See REASON constant at top of file.
*/
fetchStorageObjects: Task.async(function* (type, host, names, reason) {
let fetchOpts = reason === REASON.NEXT_50_ITEMS ? {offset: this.itemOffset}
: {};
let storageType = this.storageTypes[type];
if (reason !== REASON.NEXT_50_ITEMS &&
reason !== REASON.UPDATE &&
reason !== REASON.NEW_ROW &&
reason !== REASON.POPULATE) {
throw new Error("Invalid reason specified");
}
try {
if (reason === REASON.POPULATE) {
let subType = null;
// The indexedDB type could have sub-type data to fetch.
// If having names specified, then it means
// we are fetching details of specific database or of object store.
if (type === "indexedDB" && names) {
let [ dbName, objectStoreName ] = JSON.parse(names[0]);
if (dbName) {
subType = "database";
}
if (objectStoreName) {
subType = "object store";
}
}
this.actorSupportsAddItem = yield this._target.actorHasMethod(type, "addItem");
this.actorSupportsRemoveItem =
yield this._target.actorHasMethod(type, "removeItem");
this.actorSupportsRemoveAll =
yield this._target.actorHasMethod(type, "removeAll");
this.actorSupportsRemoveAllSessionCookies =
yield this._target.actorHasMethod(type, "removeAllSessionCookies");
yield this.resetColumns(type, host, subType);
}
let {data} = yield storageType.getStoreObjects(host, names, fetchOpts);
if (data.length) {
this.populateTable(data, reason);
}
yield this.updateToolbar();
this.emit("store-objects-updated");
} catch (ex) {
console.error(ex);
}
}),
/**
* Updates the toolbar hiding and showing buttons as appropriate.
*/
updateToolbar: Task.async(function* () {
let item = this.tree.selectedItem;
let howManyNodesIn = item ? item.length : 0;
// The first node is just a title e.g. "Cookies" so we need to be at least
// 2 nodes in to show the add button.
let canAdd = this.actorSupportsAddItem && howManyNodesIn > 1;
if (canAdd) {
this._addButton.hidden = false;
this._addButton.setAttribute("tooltiptext",
L10N.getFormatStr("storage.popupMenu.addItemLabel"));
} else {
this._addButton.hidden = true;
this._addButton.removeAttribute("tooltiptext");
}
}),
/**
* 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) {
// Ignore `from` field, which is just a protocol.js implementation
// artifact.
if (type === "from") {
continue;
}
let typeLabel = type;
try {
typeLabel = L10N.getStr("tree.labels." + type);
} catch (e) {
console.error("Unable to localize tree label type:" + type);
}
this.tree.add([{id: type, label: typeLabel, type: "store"}]);
if (!storageTypes[type].hosts) {
continue;
}
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]];
}
} catch (ex) {
// Do Nothing
}
}
if (!this.tree.selectedItem) {
this.tree.selectedItem = [type, host];
}
}
}
},
/**
* Populates the selected entry from the table in the sidebar for a more
* detailed view.
*/
updateObjectSidebar: Task.async(function* () {
let item = this.table.selectedRow;
let value;
// Get the string value (async action) and the update the UI synchronously.
if (item && item.name && item.valueActor) {
value = yield item.valueActor.string();
}
// Bail if the selectedRow is no longer selected, the item doesn't exist or the state
// changed in another way during the above yield.
if (this.table.items.size === 0 ||
!item ||
!this.table.selectedRow ||
item.uniqueKey !== this.table.selectedRow.uniqueKey) {
this.hideSidebar();
return;
}
// Start updating the UI. Everything is sync beyond this point.
this.sidebar.hidden = false;
this.view.empty();
let mainScope = this.view.addScope(L10N.getStr("storage.data.label"));
mainScope.expanded = true;
if (value) {
let itemVar = mainScope.addItem(item.name + "", {}, {relaxed: true});
// 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) {
// 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 => !["name", "value", "valueActor"].includes(e));
for (let prop of otherProps) {
let column = this.table.columns.get(prop);
if (column && column.private) {
continue;
}
let cookieProp = COOKIE_KEY_MAP[prop] || prop;
// The pseduo property of HostOnly refers to converse of isDomain property
rawObject[cookieProp] = (prop === "isDomain") ? !item[prop] : item[prop];
}
itemVar.populate(rawObject, {sorted: true});
itemVar.twisty = true;
itemVar.expanded = true;
}
} else {
// Case when displaying IndexedDB db/object store properties.
for (let key in item) {
let column = this.table.columns.get(key);
if (column && column.private) {
continue;
}
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, originalValue) {
// Find if value is URLEncoded ie
let decodedValue = "";
try {
decodedValue = decodeURIComponent(originalValue);
} catch (e) {
// Unable to decode, nothing to do
}
let value = (decodedValue && decodedValue !== originalValue)
? decodedValue : originalValue;
let json = null;
try {
json = JSOL.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);
let view = this.view;
jsonObject[name] = json;
let valueScope = view.getScopeAtIndex(1) ||
view.addScope(L10N.getStr("storage.parsedValue.label"));
valueScope.expanded = true;
let jsonVar = valueScope.addItem("", Object.create(null), {relaxed: 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 word = `[^${kv}${p}]*`;
let keyValue = `${word}${kv}${word}`;
let keyValueList = `${keyValue}(${p}${keyValue})*`;
let regex = new RegExp(`^${keyValueList}$`);
if (value.match && value.match(regex) && value.includes(kv) &&
(value.includes(p) || value.split(kv).length == 2)) {
return makeObject(kv, p);
}
}
}
// Testing for array
for (let p of separators) {
let word = `[^${p}]*`;
let wordList = `(${word}${p})+${word}`;
let regex = new RegExp(`^${wordList}$`);
if (value.match && 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) {
if (!item) {
return;
}
this.table.clear();
this.hideSidebar();
this.searchBox.value = "";
let [type, host] = item;
this.table.host = host;
this.table.datatype = type;
this.updateToolbar();
let names = null;
if (!host) {
return;
}
if (item.length > 2) {
names = [JSON.stringify(item.slice(2))];
}
this.fetchStorageObjects(type, host, names, REASON.POPULATE);
this.itemOffset = 0;
},
/**
* Resets the column headers in the storage table with the pased object `data`
*
* @param {string} type
* The type of storage corresponding to the after-reset columns in the
* table.
* @param {string} host
* The host name corresponding to the table after reset.
*
* @param {string} [subType]
* The sub type under the given type.
*/
resetColumns: function* (type, host, subtype) {
this.table.host = host;
this.table.datatype = type;
let uniqueKey = null;
let columns = {};
let editableFields = [];
let hiddenFields = [];
let privateFields = [];
let fields = yield this.getCurrentFront().getFields(subtype);
fields.forEach(f => {
if (!uniqueKey) {
this.table.uniqueId = uniqueKey = f.name;
}
if (f.editable) {
editableFields.push(f.name);
}
if (f.hidden) {
hiddenFields.push(f.name);
}
if (f.private) {
privateFields.push(f.name);
}
columns[f.name] = f.name;
let columnName;
try {
// Path key names for l10n in the case of a string change.
let name = f.name === "keyPath" ? "keyPath2" : f.name;
columnName = L10N.getStr("table.headers." + type + "." + name);
} catch (e) {
columnName = COOKIE_KEY_MAP[f.name];
}
if (!columnName) {
console.error("Unable to localize table header type:" + type + " key:" + f.name);
} else {
columns[f.name] = columnName;
}
});
this.table.setColumns(columns, null, hiddenFields, privateFields);
this.hideSidebar();
yield this.makeFieldsEditable(editableFields);
},
/**
* Populates or updates the rows in the storage table.
*
* @param {array[object]} data
* Array of objects to be populated in the storage table
* @param {Constant} reason
* See REASON constant at top of file.
*/
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).toUTCString()
: L10N.getStr("label.expires.session");
}
if (item.creationTime != null) {
item.creationTime = new Date(item.creationTime).toUTCString();
}
if (item.lastAccessed != null) {
item.lastAccessed = new Date(item.lastAccessed).toUTCString();
}
switch (reason) {
case REASON.POPULATE:
// Update without flashing the row.
this.table.push(item, true);
break;
case REASON.NEW_ROW:
case REASON.NEXT_50_ITEMS:
// Update and flash the row.
this.table.push(item, false);
break;
case REASON.UPDATE:
this.table.update(item);
if (item == this.table.selectedRow && !this.sidebar.hidden) {
this.updateObjectSidebar();
}
break;
}
this.shouldLoadMoreItems = true;
}
},
/**
* 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 == KeyCodes.DOM_VK_ESCAPE && !this.sidebar.hidden) {
// Stop Propagation to prevent opening up of split console
this.hideSidebar();
event.stopPropagation();
event.preventDefault();
}
},
/**
* Handles filtering the table
*/
filterItems() {
let value = this.searchBox.value;
this.table.filterItems(value, ["valueActor"]);
this._panelDoc.documentElement.classList.toggle("filtering", !!value);
},
/**
* Handles endless scrolling for the table
*/
handleScrollEnd: function () {
if (!this.shouldLoadMoreItems) {
return;
}
this.shouldLoadMoreItems = false;
this.itemOffset += 50;
let item = this.tree.selectedItem;
let [type, host] = item;
let names = null;
if (item.length > 2) {
names = [JSON.stringify(item.slice(2))];
}
this.fetchStorageObjects(type, host, names, REASON.NEXT_50_ITEMS);
},
/**
* Fires before a cell context menu with the "Add" or "Delete" action is
* shown. If the currently selected storage object doesn't support adding or
* removing items, prevent showing the menu.
*/
onTablePopupShowing: function (event) {
let selectedItem = this.tree.selectedItem;
let type = selectedItem[0];
// IndexedDB only supports removing items from object stores (level 4 of the tree)
if ((!this.actorSupportsAddItem && !this.actorSupportsRemoveItem &&
type !== "cookies") ||
(type === "indexedDB" && selectedItem.length !== 4)) {
event.preventDefault();
return;
}
let rowId = this.table.contextMenuRowId;
let data = this.table.items.get(rowId);
if (this.actorSupportsRemoveItem) {
let name = data[this.table.uniqueId];
let separatorRegex = new RegExp(SEPARATOR_GUID, "g");
let label = addEllipsis((name + "").replace(separatorRegex, "-"));
this._tablePopupDelete.hidden = false;
this._tablePopupDelete.setAttribute("label",
L10N.getFormatStr("storage.popupMenu.deleteLabel", label));
} else {
this._tablePopupDelete.hidden = true;
}
if (this.actorSupportsAddItem) {
this._tablePopupAddItem.hidden = false;
this._tablePopupAddItem.setAttribute("label",
L10N.getFormatStr("storage.popupMenu.addItemLabel"));
} else {
this._tablePopupAddItem.hidden = true;
}
let showDeleteAllSessionCookies = false;
if (this.actorSupportsRemoveAllSessionCookies) {
if (type === "cookies" && selectedItem.length === 2) {
showDeleteAllSessionCookies = true;
}
}
this._tablePopupDeleteAllSessionCookies.hidden = !showDeleteAllSessionCookies;
if (type === "cookies") {
let host = addEllipsis(data.host);
this._tablePopupDeleteAllFrom.hidden = false;
this._tablePopupDeleteAllFrom.setAttribute("label",
L10N.getFormatStr("storage.popupMenu.deleteAllFromLabel", host));
} else {
this._tablePopupDeleteAllFrom.hidden = true;
}
},
onTreePopupShowing: function (event) {
let showMenu = false;
let selectedItem = this.tree.selectedItem;
if (selectedItem) {
let type = selectedItem[0];
// The delete all (aka clear) action is displayed for IndexedDB object stores
// (level 4 of tree), for Cache objects (level 3) and for the whole host (level 2)
// for other storage types (cookies, localStorage, ...).
let showDeleteAll = false;
if (this.actorSupportsRemoveAll) {
let level;
if (type == "indexedDB") {
level = 4;
} else if (type == "Cache") {
level = 3;
} else {
level = 2;
}
if (selectedItem.length == level) {
showDeleteAll = true;
}
}
this._treePopupDeleteAll.hidden = !showDeleteAll;
// The delete all session cookies action is displayed for cookie object stores
// (level 2 of tree)
let showDeleteAllSessionCookies = false;
if (this.actorSupportsRemoveAllSessionCookies) {
if (type === "cookies" && selectedItem.length === 2) {
showDeleteAllSessionCookies = true;
}
}
this._treePopupDeleteAllSessionCookies.hidden = !showDeleteAllSessionCookies;
// The delete action is displayed for:
// - IndexedDB databases (level 3 of the tree)
// - Cache objects (level 3 of the tree)
let showDelete = (type == "indexedDB" || type == "Cache") &&
selectedItem.length == 3;
this._treePopupDelete.hidden = !showDelete;
if (showDelete) {
let itemName = addEllipsis(selectedItem[selectedItem.length - 1]);
this._treePopupDelete.setAttribute("label",
L10N.getFormatStr("storage.popupMenu.deleteLabel", itemName));
}
showMenu = showDeleteAll || showDelete;
}
if (!showMenu) {
event.preventDefault();
}
},
/**
* Handles refreshing the selected storage
*/
onRefreshTable: function (event) {
this.onHostSelect(event, this.tree.selectedItem);
},
/**
* Handles adding an item from the storage
*/
onAddItem: function () {
let selectedItem = this.tree.selectedItem;
if (!selectedItem) {
return;
}
let front = this.getCurrentFront();
let [, host] = selectedItem;
// Prepare to scroll into view.
this.table.scrollIntoViewOnUpdate = true;
this.table.editBookmark = createGUID();
front.addItem(this.table.editBookmark, host);
},
/**
* Handles removing an item from the storage
*/
onRemoveItem: function () {
let [, host, ...path] = this.tree.selectedItem;
let front = this.getCurrentFront();
let rowId = this.table.contextMenuRowId;
let data = this.table.items.get(rowId);
let name = data[this.table.uniqueId];
if (path.length > 0) {
name = JSON.stringify([...path, name]);
}
front.removeItem(host, name);
},
/**
* Handles removing all items from the storage
*/
onRemoveAll: function () {
let [, host, ...path] = this.tree.selectedItem;
let front = this.getCurrentFront();
let name = path.length > 0 ? JSON.stringify(path) : undefined;
front.removeAll(host, name);
},
/**
* Handles removing all session cookies from the storage
*/
onRemoveAllSessionCookies: function () {
let [, host, ...path] = this.tree.selectedItem;
let front = this.getCurrentFront();
let name = path.length > 0 ? JSON.stringify(path) : undefined;
front.removeAllSessionCookies(host, name);
},
/**
* Handles removing all cookies with exactly the same domain as the
* cookie in the selected row.
*/
onRemoveAllFrom: function () {
let [, host] = this.tree.selectedItem;
let front = this.getCurrentFront();
let rowId = this.table.contextMenuRowId;
let data = this.table.items.get(rowId);
front.removeAll(host, data.host);
},
onRemoveTreeItem: function () {
let [type, host, ...path] = this.tree.selectedItem;
if (type == "indexedDB" && path.length == 1) {
this.removeDatabase(host, path[0]);
} else if (type == "Cache" && path.length == 1) {
this.removeCache(host, path[0]);
}
},
removeDatabase: function (host, dbName) {
let front = this.getCurrentFront();
front.removeDatabase(host, dbName).then(result => {
if (result.blocked) {
let notificationBox = this._toolbox.getNotificationBox();
notificationBox.appendNotification(
L10N.getFormatStr("storage.idb.deleteBlocked", dbName),
"storage-idb-delete-blocked",
null,
notificationBox.PRIORITY_WARNING_LOW);
}
}).catch(error => {
let notificationBox = this._toolbox.getNotificationBox();
notificationBox.appendNotification(
L10N.getFormatStr("storage.idb.deleteError", dbName),
"storage-idb-delete-error",
null,
notificationBox.PRIORITY_CRITICAL_LOW);
});
},
removeCache: function (host, cacheName) {
let front = this.getCurrentFront();
front.removeItem(host, JSON.stringify([ cacheName ]));
},
};
// Helper Functions
function createGUID() {
return "{cccccccc-cccc-4ccc-yccc-cccccccccccc}".replace(/[cy]/g, c => {
let r = Math.random() * 16 | 0, v = c == "c" ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
|