-
Notifications
You must be signed in to change notification settings - Fork 20
/
bootstrap.js
5176 lines (5058 loc) · 182 KB
/
bootstrap.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
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
const WINDOW_LOADED = -1;
const WINDOW_CLOSED = -2;
const XULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
this.__defineGetter__("patcher", function() {
delete this.patcher;
Components.utils.import("chrome://privatetab/content/patcher.jsm");
patcher.init("privateTabMod::", _log);
return patcher;
});
function install(params, reason) {
try {
Services.strings.flushBundles(); // https://bugzilla.mozilla.org/show_bug.cgi?id=719376
}
catch(e) {
Components.utils.reportError(e);
}
}
function uninstall(params, reason) {
}
function startup(params, reason) {
privateTab.init(reason);
}
function shutdown(params, reason) {
privateTab.destroy(reason);
}
var privateTab = {
initialized: false,
init: function(reason) {
if(this.initialized)
return;
this.initialized = true;
Services.scriptloader.loadSubScript("chrome://privatetab/content/log.js");
prefs.init();
_dbg = prefs.get("debug", false);
_dbgv = prefs.get("debug.verbose", false);
if(prefs.get("enablePrivateProtocol"))
this.initPrivateProtocol(reason);
this.patchPrivateBrowsingUtils(true);
this.appButtonDontChange = !prefs.get("fixAppButtonWidth");
for(var window of this.windows)
this.initWindow(window, reason);
if(reason == APP_STARTUP) {
// https://bugzilla.mozilla.org/show_bug.cgi?id=1336227
// browser.startup.blankWindow = true, Firefox 60+
var blankWindow = Services.wm.getMostRecentWindow("navigator:blank");
blankWindow && this.observe(blankWindow, "domwindowopened");
}
Services.ww.registerNotification(this);
if(this.canFilterSession)
Services.obs.addObserver(this, "sessionstore-state-write", false);
else if(
window
&& reason != APP_STARTUP
&& prefs.get("rememberClosedPrivateTabs")
) { // We may already have closed private tabs
window.setTimeout(function() {
this.dontSaveClosedPrivateTabs(true);
}.bind(this), 50);
}
},
destroy: function(reason) {
if(!this.initialized)
return;
this.initialized = false;
this.destroyPrivateProtocol(reason);
if(reason == ADDON_DISABLE || reason == ADDON_UNINSTALL)
this.askToClosePrivateTabs();
for(var window of this.windows)
this.destroyWindow(window, reason);
Services.ww.unregisterNotification(this);
if(reason != APP_SHUTDOWN) {
// nsISessionStore may save data after our shutdown
if(this.canFilterSession)
Services.obs.removeObserver(this, "sessionstore-state-write");
else
this.dontSaveClosedPrivateTabs(false);
this.addPbExitObserver(false);
this.unloadStyles();
this.restoreAppButtonWidth();
this.patchPrivateBrowsingUtils(false);
if(reason != ADDON_DISABLE)
this.saveEmptyTabLabels();
}
prefs.destroy();
this._dndPrivateNode = null;
patcher.destroy();
Components.utils.unload("chrome://privatetab/content/patcher.jsm");
},
observe: function(subject, topic, data) {
if(topic == "domwindowopened") {
if(!subject.opener) {
var aw = Services.ww.activeWindow;
if(aw && this.isTargetWindow(aw))
subject.__privateTabOpener = aw;
}
subject.addEventListener("load", this, false);
}
else if(topic == "domwindowclosed")
this.destroyWindow(subject, WINDOW_CLOSED);
else if(topic == "sessionstore-state-write")
this.filterSession(subject);
else if(topic == "browser-delayed-startup-finished") {
_log(topic + " => setupJumpLists()");
this.setupJumpListsLazy(false);
subject.setTimeout(function() {
this.setupJumpLists(true, true);
}.bind(this), 0);
}
else if(topic == "last-pb-context-exited") {
_log(topic);
var timer = Components.classes["@mozilla.org/timer;1"]
.createInstance(Components.interfaces.nsITimer);
timer.init(function() {
if(this.hasPrivate) {
_log("Looks like wrong " + topic + " (found opened private tab/window), ignore");
return;
}
if(this.cleanupClosedPrivateTabs) {
_log(topic + " => forgetAllClosedTabs()");
this.forgetAllClosedTabs();
}
this.clearSearchBars();
}.bind(this), 0, timer.TYPE_ONE_SHOT);
}
},
receiveMessage: function(msg) {
if(msg.name == "PrivateTab:ProtocolURILoaded")
this.handleProtocolBrowser(msg.target, msg.data.URI);
else if(msg.name == "PrivateTab:ProtocolReplaceTab")
return this.fixBrowserFromProtocol(msg.target, msg.data.URI);
return undefined;
},
handleEvent: function(e) {
switch(e.type) {
case "load": this.loadHandler(e); break;
case "TabOpen": this.tabOpenHandler(e); break;
case "SSTabRestoring": this.tabRestoringHandler(e); break;
case "TabSelect": this.tabSelectHandler(e); break;
case "TabClose": this.tabCloseHandler(e); break;
case "SSTabClosing": this.tabClosingHandler(e); break;
case "dragstart": this.dragStartHandler(e); break;
case "dragend": this.dragEndHandler(e); break;
case "drop": this.dropHandler(e); break;
case "popupshowing": this.popupShowingHandler(e); break;
case "ViewShowing": this.viewShowingHandler(e); break;
case "command": this.commandHandler(e); break;
case "click": this.clickHandler(e); break;
case "keydown":
case "keypress": this.keypressHandler(e); break;
case "PrivateTab:PrivateChanged": this.privateChangedHandler(e); break;
case "TabRemotenessChange": this.fixTabRemoteness(e); break;
case "SSWindowStateBusy": this.setWindowBusy(e, true); break;
case "SSWindowStateReady": this.setWindowBusy(e, false); break;
case "close":
case "beforeunload":
case "SSWindowClosing": this.windowClosingHandler(e); break;
case "aftercustomization": this.updateToolbars(e); break;
case "mouseover":
case "mouseout": this.filterMouseEvent(e);
}
},
loadHandler: function(e) {
var window = e.currentTarget;
window.removeEventListener("load", this, false);
this.initWindow(window, WINDOW_LOADED);
},
windowClosingHandler: function(e) {
var window = e.currentTarget;
_log("windowClosingHandler() [" + e.type + "]");
if(e.type == "close" || e.type == "beforeunload") {
if(e.defaultPrevented) {
_log(e.type + ": Someone already prevent window closing");
return;
}
if(
(this.isPrivateWindow(window) || this.hasPrivateTab(window))
&& this.isLastPrivate(window)
) {
_log("Closing window with last private tab(s)");
if(this.forbidCloseLastPrivate()) {
_log("Prevent closing window with last private tab(s)");
e.preventDefault();
return;
}
else {
var pt = window.privateTab;
pt._checkLastPrivate = false;
window.setTimeout(function() { // OK, seems like window stay open
pt._checkLastPrivate = true;
}, 50);
}
}
if(!this.isSeaMonkey)
return; // This is Firefox, will wait for "SSWindowClosing"
}
if( //~ todo: this looks like SeaMonkey bug... and may be fixed later
(this.isSeaMonkey || !this.isPrivateWindow(window))
&& (
!prefs.get("rememberClosedPrivateTabs")
|| prefs.get("rememberClosedPrivateTabs.cleanup") > 0
)
) {
// Note: we don't have public API to tweak closed windows data,
// so we remove all private tabs from closing window
_log(e.type + " => closePrivateTabs()");
this.closePrivateTabs(window);
}
if(this.cleanupClosedPrivateTabs)
this.forgetClosedTabs(window);
this.destroyWindowClosingHandler(window);
},
destroyWindowClosingHandler: function(window) {
window.removeEventListener("TabClose", this, true);
window.removeEventListener("TabClose", this, false);
window.removeEventListener("SSTabClosing", this, false);
window.removeEventListener("SSWindowClosing", this, true);
window.removeEventListener("close", this, false);
window.removeEventListener("beforeunload", this, false);
},
get frameScriptUID() { // See https://bugzilla.mozilla.org/show_bug.cgi?id=1051238
delete this.frameScriptUID;
return this.frameScriptUID = "?" + Date.now();
},
initPrivateProtocol: function(reason) {
if("privateProtocol" in this)
return;
Components.utils.import("chrome://privatetab/content/protocol.jsm", this);
this.privateProtocol.init(_log);
if("ppmm" in Services) {
Services.ppmm.loadProcessScript("chrome://privatetab/content/protocol-process.js" + this.frameScriptUID, true);
Services.mm.addMessageListener("PrivateTab:ProtocolURILoaded", this);
Services.mm.addMessageListener("PrivateTab:ProtocolReplaceTab", this);
}
if(prefs.get("showItemInTaskBarJumpList")) {
if(reason == APP_STARTUP)
this.setupJumpListsLazy(true);
else
this.setupJumpLists(true);
}
},
destroyPrivateProtocol: function(reason) {
if(!("privateProtocol" in this))
return;
this.privateProtocol.destroy();
Components.utils.unload("chrome://privatetab/content/protocol.jsm");
delete this.privateProtocol;
if("ppmm" in Services) {
Services.ppmm.broadcastAsyncMessage("PrivateTab:ProtocolDestroy", {});
Services.ppmm.removeDelayedProcessScript("chrome://privatetab/content/protocol-process.js" + this.frameScriptUID);
Services.mm.removeMessageListener("PrivateTab:ProtocolURILoaded", this);
Services.mm.removeMessageListener("PrivateTab:ProtocolReplaceTab", this);
}
if(prefs.get("showItemInTaskBarJumpList")) {
this.setupJumpListsLazy(false);
this.setupJumpLists(false);
}
},
get hasJumpLists() {
delete this.hasJumpLists;
return this.hasJumpLists = "@mozilla.org/windows-taskbar;1" in Components.classes
&& Components.classes["@mozilla.org/windows-taskbar;1"]
.getService(Components.interfaces.nsIWinTaskbar)
.available;
},
_jumpListsInitialized: false,
setupJumpLists: function(init, lazy) {
if(
!this.hasJumpLists
|| init == this._jumpListsInitialized
)
return;
this._jumpListsInitialized = init;
var global = Components.utils.import("resource:///modules/WindowsJumpLists.jsm", {});
if(!("tasksCfg" in global)) {
_log('setupJumpLists() failed: "tasksCfg" not found in WindowsJumpLists.jsm');
return;
}
var tasksCfg = global.tasksCfg;
function getEntryIndex(check) {
for(var i = 0, l = tasksCfg.length; i < l; ++i) {
var entry = tasksCfg[i];
if(check(entry))
return i;
}
return -1;
}
if(init) {
var sm = this.isSeaMonkey ? "SM" : "";
var getNewTabURL = function() {
if("nsIAboutNewTabService" in Components.interfaces) try { // Firefox 44+
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1204983#c89
var aboutNewTabService = Components.classes["@mozilla.org/browser/aboutnewtab-service;1"]
.getService(Components.interfaces.nsIAboutNewTabService);
return aboutNewTabService.newTabURL;
}
catch(e) {
Components.utils.reportError(e);
}
try { // Firefox 42+
var {NewTabURL} = Components.utils.import("resource:///modules/NewTabURL.jsm", {});
return NewTabURL.get();
}
catch(e) {
if(NewTabURL)
Components.utils.reportError(e);
}
return prefs.getPref("browser.newtab.url") || "about:blank";
};
var ptEntry = {
title: this.getLocalized("taskBarOpenNewPrivateTab" + sm),
description: this.getLocalized("taskBarOpenNewPrivateTabDesc" + sm),
get args() {
return "-new-tab private:" + getNewTabURL();
},
iconIndex: this.isSeaMonkey ? 0 : 4, // Private browsing mode icon
open: true,
close: true,
_privateTab: true
};
var i = getEntryIndex(function(entry) {
return entry.args == "-new-tab about:blank";
});
if(i != -1) {
tasksCfg.splice(i + 1, 0, ptEntry);
_log('setupJumpLists(): add new item after "Open new tab"');
}
else {
tasksCfg.push(ptEntry);
_log("setupJumpLists(): add new item at end");
}
this.updateJumpList = updateJumpList;
Services.prefs.addObserver("browser.newtab.url", updateJumpList, false);
Services.obs.addObserver(updateJumpList, "newtab-url-changed", false); // Firefox 42+
}
else {
var i = getEntryIndex(function(entry) {
return "_privateTab" in entry;
});
if(i != -1) {
tasksCfg.splice(i, 1);
_log("setupJumpLists(): remove item");
}
else {
_log("setupJumpLists(): item not found and can't be removed");
}
Services.prefs.removeObserver("browser.newtab.url", this.updateJumpList);
Services.obs.removeObserver(this.updateJumpList, "newtab-url-changed"); // Firefox 42+
delete this.updateJumpList;
}
function updateJumpList() {
var WinTaskbarJumpList = global.WinTaskbarJumpList;
var pending = WinTaskbarJumpList._pendingStatements;
if(!pending) {
pending = {};
Components.utils.reportError(LOG_PREFIX + "updateJumpList(): can't get state of pending statements");
}
var timer = Components.classes["@mozilla.org/timer;1"]
.createInstance(Components.interfaces.nsITimer);
var stopWait = Date.now() + 5e3;
timer.init(function() {
for(var statement in pending) {
if(Date.now() > stopWait)
timer.cancel();
return;
}
timer.cancel();
WinTaskbarJumpList.update();
_log("WinTaskbarJumpList.update()");
}, lazy ? 150 : 50, timer.TYPE_REPEATING_SLACK);
}
updateJumpList();
},
_hasDelayedStartupObserver: false,
setupJumpListsLazy: function(init) {
if(init == this._hasDelayedStartupObserver)
return;
this._hasDelayedStartupObserver = init;
// Like _onFirstWindowLoaded() from resource:///components/nsBrowserGlue.js
if(init)
Services.obs.addObserver(this, "browser-delayed-startup-finished", false);
else
Services.obs.removeObserver(this, "browser-delayed-startup-finished");
},
initWindow: function(window, reason) {
if(reason == WINDOW_LOADED && !this.isTargetWindow(window)) {
delete window.__privateTabOpener;
return;
}
_dbgv && _log("initWindow()");
var gBrowser = window.gBrowser
|| window.getBrowser(); // For SeaMonkey
window.privateTab = new API(window);
var document = window.document;
this.loadStyles(window);
this.ensureTitleModifier(document);
this.patchBrowsers(gBrowser, true);
this.patchTabIcons(window, true);
window.setTimeout(function() {
// We don't need patched functions right after window "load", so it's better to
// apply patches after any other extensions
this.patchBrowserThumbnails(window, true);
window.setTimeout(function() {
this.patchWarnAboutClosingWindow(window, true);
// Wait to not break BROWSER_NEW_TAB_URL in detached window
this.patchTabBrowserDND(window, gBrowser, true);
this.patchViewSource(window, true);
}.bind(this), 50);
this.importEmptyTabLabels();
if("TrackingProtection" in window) { // Firefox 42+
var identityPopup = document.getElementById("identity-popup");
identityPopup && identityPopup.addEventListener("popupshowing", this, true);
}
}.bind(this), 0);
if(reason == WINDOW_LOADED)
this.inheritWindowState(window);
// Show real tab state, but after small delay for better startup performance
window.setTimeout(function() {
forEach(gBrowser.tabs, function(tab) {
this.setTabState(tab);
}, this);
}.bind(this), 0);
if(this.isPrivateWindow(window)) {
// All tabs should be private... so, update state before real check
forEach(gBrowser.tabs, function(tab) {
tab.setAttribute(this.privateAttr, "true");
}, this);
var root = document.documentElement;
// We handle window before gBrowserInit.onLoad(), so set "privatebrowsingmode"
// for fixAppButtonWidth() manually
if(!PrivateBrowsingUtils.permanentPrivateBrowsing)
root.setAttribute("privatebrowsingmode", "temporary");
root.setAttribute(this.privateAttr, "true");
root.setAttribute(this.rootPrivateAttr, "true");
}
window.setTimeout(function() {
// Wait for third-party styles like https://addons.mozilla.org/addon/movable-firefox-button/
this.appButtonNA = false;
this.fixAppButtonWidth(document);
this.updateWindowTitle(gBrowser);
}.bind(this), 5);
// See https://github.com/Infocatcher/Private_Tab/issues/83
// It's better to handle "TabOpen" before other extensions, but after our waitForTab()
// with window.addEventListener("TabOpen", ..., true);
document.addEventListener("TabOpen", this, true);
window.addEventListener("SSTabRestoring", this, false);
window.addEventListener("TabSelect", this, false);
window.addEventListener("TabClose", this, true);
window.addEventListener("TabClose", this, false);
window.addEventListener("SSTabClosing", this, false);
window.addEventListener("dragstart", this, true);
window.addEventListener("dragend", this, true);
window.addEventListener("drop", this, true);
window.addEventListener("PrivateTab:PrivateChanged", this, false);
if(this.isMultiProcessWindow(window))
window.addEventListener("TabRemotenessChange", this, true);
window.addEventListener("SSWindowStateBusy", this, true);
window.addEventListener("SSWindowStateReady", this, true);
window.addEventListener("SSWindowClosing", this, true);
window.addEventListener("close", this, false);
window.addEventListener("beforeunload", this, false);
window.setTimeout(function() {
this.initHotkeys();
if(this.hotkeys)
window.addEventListener(this.keyEvent, this, this.keyHighPriority);
}.bind(this), 0);
window.setTimeout(function() {
this.initControls(document);
window.setTimeout(function() {
this.setupListAllTabs(window, true);
this.setupUndoCloseTabs(window, true);
}.bind(this), 0);
window.setTimeout(function() {
this.setHotkeysText(document);
}.bind(this), 10);
}.bind(this), 50);
this.initToolbarButton(document);
},
destroyWindow: function(window, reason) {
window.removeEventListener("load", this, false); // Window can be closed before "load"
if(reason == WINDOW_CLOSED && !this.isTargetWindow(window))
return;
_log("destroyWindow()");
if(this.isMultiProcessWindow(window)) {
var mm = window.messageManager;
mm.broadcastAsyncMessage("PrivateTab:Action", { action: "Destroy" });
mm.removeDelayedFrameScript("chrome://privatetab/content/content.js" + this.frameScriptUID);
}
var document = window.document;
var gBrowser = window.gBrowser;
var force = reason != APP_SHUTDOWN && reason != WINDOW_CLOSED;
var disable = reason == ADDON_DISABLE || reason == ADDON_UNINSTALL;
if(force) {
var isPrivateWindow = this.isPrivateWindow(window);
forEach(gBrowser.tabs, function(tab) {
if(disable && isPrivateWindow ^ this.isPrivateTab(tab)) {
if(this.toggleUsingDupTab) {
window.setTimeout(function(tab) { // Pseudo async and to not break tabs loop
this.replaceTabAndTogglePrivate(tab, isPrivateWindow);
}.bind(this), 0, tab);
}
else {
this.toggleTabPrivate(tab, isPrivateWindow);
this.fixTabState(tab, false); // Always remove private attribute
}
}
// Note: isPrivateTab() will check for private attributes in e10s mode
tab.removeAttribute(this.privateAttr);
}, this);
document.documentElement.removeAttribute(this.privateAttr);
_log("Restore title...");
if(!isPrivateWindow)
this.updateWindowTitle(gBrowser, false);
this.destroyTitleModifier(document);
}
this.patchBrowsers(gBrowser, false, !force);
this.patchTabBrowserDND(window, gBrowser, false, false, !force);
this.patchViewSource(window, false, !force);
this.patchWarnAboutClosingWindow(window, false, !force);
if(!prefs.get("allowOpenExternalLinksInPrivateTabs"))
this.patchBrowserLoadURI(window, false, !force);
this.patchSearchBar(window, false, !force);
this.patchTabIcons(window, false, !force);
this.patchBrowserThumbnails(window, false, !force);
this.unwatchAppButton(window);
document.removeEventListener("TabOpen", this, true);
window.removeEventListener("SSTabRestoring", this, false);
window.removeEventListener("TabSelect", this, false);
window.removeEventListener("dragstart", this, true);
window.removeEventListener("dragend", this, true);
window.removeEventListener("drop", this, true);
window.removeEventListener(this.keyEvent, this, this.keyHighPriority);
window.removeEventListener("PrivateTab:PrivateChanged", this, false);
if(this.isMultiProcessWindow(window))
window.removeEventListener("TabRemotenessChange", this, true);
window.removeEventListener("SSWindowStateBusy", this, true);
window.removeEventListener("SSWindowStateReady", this, true);
window.removeEventListener("aftercustomization", this, false);
if(reason != WINDOW_CLOSED) {
// See resource:///modules/sessionstore/SessionStore.jsm
// "domwindowclosed" => onClose() => "SSWindowClosing"
// This may happens after our "domwindowclosed" notification!
this.destroyWindowClosingHandler(window);
}
if("TrackingProtection" in window) { // Firefox 42+
var identityPopup = document.getElementById("identity-popup");
identityPopup && identityPopup.removeEventListener("popupshowing", this, true);
if(reason != WINDOW_CLOSED) try {
var TrackingProtection = window.TrackingProtection;
TrackingProtection.updateEnabled();
if("icon" in TrackingProtection && !TrackingProtection.enabled)
TrackingProtection.icon.removeAttribute("state");
var XULBrowserWindow = window.XULBrowserWindow;
if(
XULBrowserWindow && "_state" in XULBrowserWindow
&& "onSecurityChange" in TrackingProtection
)
TrackingProtection.onSecurityChange(XULBrowserWindow._state, true /*aIsSimulated*/);
}
catch(e) {
Components.utils.reportError(e);
}
}
this.setupListAllTabs(window, false);
this.setupUndoCloseTabs(window, false);
this.destroyControls(window, force);
window.privateTab._destroy();
delete window.privateTab;
},
get platformVersion() {
var pv = parseFloat(Services.appinfo.platformVersion);
if(Services.appinfo.name == "Pale Moon" || Services.appinfo.name == "Basilisk")
pv = pv >= 4.1 ? 56 : 28;
delete this.platformVersion;
return this.platformVersion = pv;
},
get isSeaMonkey() {
delete this.isSeaMonkey;
return this.isSeaMonkey = Services.appinfo.name == "SeaMonkey";
},
get isAustralis() {
var window = Services.wm.getMostRecentWindow("navigator:browser");
if(!window) {
_log("get isAustralis(): no browser window!");
return undefined;
}
delete this.isAustralis;
return this.isAustralis = "CustomizableUI" in window;
},
get storage() {
// Simple replacement for Application.storage
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1090880
var global = Components.utils.getGlobalForObject(Services);
var ns = "_privateTabStorage";
var storage = global[ns] || (global[ns] = global.Object.create(null));
delete this.storage;
return this.storage = {
get: function(key, defaultVal) {
if(key in storage)
return storage[key];
return defaultVal;
},
set: function(key, val) {
if(key === null)
delete storage[key];
else
storage[key] = val;
}
};
},
get windows() {
var windows = [];
var isSeaMonkey = this.isSeaMonkey;
var ws = Services.wm.getEnumerator(isSeaMonkey ? null : "navigator:browser");
while(ws.hasMoreElements()) {
var window = ws.getNext();
if(!isSeaMonkey || this.isTargetWindow(window))
windows.push(window);
}
return windows;
},
getMostRecentBrowserWindow: function() {
var window = Services.wm.getMostRecentWindow("navigator:browser");
if(window)
return window;
if(this.isSeaMonkey) for(var window of this.windows)
return window;
return null;
},
isTargetWindow: function(window) {
// Note: we can't touch document.documentElement in not yet loaded window
// (to check "windowtype"), see https://github.com/Infocatcher/Private_Tab/issues/61
// Also we don't have "windowtype" for private windows in SeaMonkey 2.19+,
// see https://github.com/Infocatcher/Private_Tab/issues/116
var loc = window.location.href;
return loc == "chrome://browser/content/browser.xul"
|| loc == "chrome://browser/content/browser.xhtml" // Firefox 69+
|| loc == "chrome://navigator/content/navigator.xul";
},
inheritWindowState: function(window) {
var args = window.arguments || undefined;
_log(
"inheritWindowState():\nwindow.opener: " + window.opener
+ "\nwindow.__privateTabOpener: " + (window.__privateTabOpener || undefined)
+ "\nwindow.arguments:\n" + (args && Array.prototype.map.call(args, String).join("\n"))
);
var opener = window.opener || window.__privateTabOpener || null;
delete window.__privateTabOpener;
var isEmptyWindow = args && !(3 in args);
var makeEmptyWindowPrivate = prefs.get("makeNewEmptyWindowsPrivate");
if((!opener || isEmptyWindow) && makeEmptyWindowPrivate == 1) {
_log("Make new empty window private");
this.toggleWindowPrivate(window, true);
return;
}
if(!opener || opener.closed || !this.isTargetWindow(opener) || !opener.gBrowser)
return;
// See chrome://browser/content/browser.js, nsBrowserAccess.prototype.openURI()
// newWindow = openDialog(getBrowserURL(), "_blank", "all,dialog=no", url, null, null, null);
if(
args && 3 in args && !(4 in args)
&& args[1] === null
&& args[2] === null
&& args[3] === null
&& !prefs.get("allowOpenExternalLinksInPrivateTabs")
) {
_log("Looks like window, opened from external application, ignore");
return;
}
if(isEmptyWindow) {
if(makeEmptyWindowPrivate == -1)
_log("Inherit private state for new empty window");
else {
_log("inheritWindowState(): Looks like new empty window, ignore");
return;
}
}
if(this.isPrivateWindow(window)) {
_log("inheritWindowState(): Ignore already private window");
return;
}
if(!this.isPrivateContent(opener))
return;
_log("Inherit private state from current tab of the opener window");
this.toggleWindowPrivate(window, true);
},
prefChanged: function(pName, pVal) {
if(pName.startsWith("key."))
this.updateHotkeys(true);
else if(pName == "keysUseKeydownEvent" || pName == "keysHighPriority")
this.updateHotkeys();
else if(pName == "fixAppButtonWidth") {
this.appButtonDontChange = !pVal;
this.restoreAppButtonWidth();
for(var window of this.windows) {
var document = window.document;
this.appButtonNA = false;
if(pVal && !this.appButtonCssURI)
this.fixAppButtonWidth(document);
this.updateTabsInTitlebar(document, true);
}
}
else if(pName.startsWith("fixAfterTabsButtonsAccessibility"))
this.reloadStyles();
else if(pName == "dragAndDropTabsBetweenDifferentWindows") {
for(var window of this.windows)
this.patchTabBrowserDND(window, window.gBrowser, pVal, true);
}
else if(pName == "makeNewEmptyTabsPrivate") {
var hide = pVal == 1;
for(var window of this.windows) {
var document = window.document;
var menuItem = document.getElementById(this.newTabMenuId);
if(menuItem)
menuItem.hidden = hide;
var appMenuItem = document.getElementById(this.newTabAppMenuId);
if(appMenuItem)
appMenuItem.hidden = hide;
}
}
else if(pName == "patchDownloads") {
if(!pVal) for(var window of this.windows)
this.updateDownloadPanel(window, this.isPrivateWindow(window));
}
else if(pName == "allowOpenExternalLinksInPrivateTabs") {
for(var window of this.windows)
this.patchBrowserLoadURI(window, !pVal);
}
else if(pName == "enablePrivateProtocol") {
if(pVal)
this.initPrivateProtocol();
else
this.destroyPrivateProtocol();
this.reloadStyles();
}
else if(pName == "showItemInTaskBarJumpList") {
if(prefs.get("enablePrivateProtocol"))
this.setupJumpLists(pVal);
}
else if(
pName == "rememberClosedPrivateTabs"
|| pName == "rememberClosedPrivateTabs.cleanup"
) {
if(
pName == "rememberClosedPrivateTabs" && !pVal
|| pName == "rememberClosedPrivateTabs.cleanup" && pVal > 0 && this.isLastPrivate()
)
this.forgetAllClosedTabs();
}
else if(pName == "usePrivateWindowStyle") {
for(var window of this.windows)
this.updateWindowTitle(window.gBrowser, undefined, true);
}
else if(pName == "stylesHighPriority" || pName == "stylesHighPriority.tree")
this.reloadStyles();
else if(pName == "debug")
_dbg = pVal;
else if(pName == "debug.verbose")
_dbgv = pVal;
},
pbuFake: function(isPrivate) {
return Object.create(PrivateBrowsingUtils, {
isWindowPrivate: {
value: function privateTabWrapper(window) {
return isPrivate; //~ todo: check call stack?
},
configurable: true,
enumerable: true,
writable: true
}
});
},
get pbuFakePrivate() {
delete this.pbuFakePrivate;
return this.pbuFakePrivate = this.pbuFake(true);
},
get pbuFakeNonPrivate() {
delete this.pbuFakeNonPrivate;
return this.pbuFakeNonPrivate = this.pbuFake(false);
},
patchTabBrowserDND: function(window, gBrowser, applyPatch, skipCheck, forceDestroy) {
if(!skipCheck && !prefs.get("dragAndDropTabsBetweenDifferentWindows"))
return;
if(applyPatch)
window._privateTabPrivateBrowsingUtils = PrivateBrowsingUtils;
else {
delete window._privateTabPrivateBrowsingUtils;
delete window.PrivateBrowsingUtils;
window.PrivateBrowsingUtils = PrivateBrowsingUtils;
}
// Note: we can't patch gBrowser.tabContainer.__proto__ nor gBrowser.__proto__:
// someone may patch instance instead of prototype...
var tabContainer = gBrowser.tabContainer;
var dndMeth = "_getDropEffectForTabDrag" in tabContainer
? "_getDropEffectForTabDrag" // Firefox 44+
: "_setEffectAllowedForDataTransfer";
this.overridePrivateBrowsingUtils(
window,
tabContainer,
dndMeth,
"gBrowser.tabContainer." + dndMeth,
true,
applyPatch,
forceDestroy
);
this.overridePrivateBrowsingUtils(
window,
gBrowser,
"swapBrowsersAndCloseOther",
"gBrowser.swapBrowsersAndCloseOther",
true,
applyPatch,
forceDestroy
);
},
patchWarnAboutClosingWindow: function(window, applyPatch, forceDestroy) {
if(this.isSeaMonkey && !("warnAboutClosingWindow" in window))
return;
this.overridePrivateBrowsingUtils(
window,
window,
"warnAboutClosingWindow",
"window.warnAboutClosingWindow",
false,
applyPatch,
forceDestroy
);
},
patchViewSource: function(window, applyPatch, forceDestroy) {
var fnViewSource = "BrowserViewSourceOfDocument";
if(!(fnViewSource in window)) {
_log("Can't patch " + fnViewSource + "(): function not found");
return;
}
if(applyPatch) {
patcher.wrapFunction(
window, fnViewSource, fnViewSource,
function before(argsOrDoc) {
if(prefs.getPref("view_source.tab")) {
var w = this.getNotPopupWindow(window, true) || window;
var isPrivate = this.isPrivateContent(w);
_log(fnViewSource + "(): wait for tab to make " + _p(isPrivate));
this.readyToOpenTab(w, isPrivate);
}
else if(!prefs.getPref("view_source.editor.external")) {
var isPrivate = this.isPrivateContent(window);
var _this = this;
_log(fnViewSource + "(): wait for window to make " + _p(isPrivate));
Services.obs.addObserver(function observer(window, topic, data) {
Services.obs.removeObserver(observer, topic);
window.addEventListener("load", function onLoad(e) {
window.removeEventListener("load", onLoad, false);
if(window.location.href != "chrome://global/content/viewSource.xul") {
_log(fnViewSource + "(): can't get view source window");
return;
}
var privacyContext = _this.getPrivacyContext(window);
if(privacyContext.usePrivateBrowsing == isPrivate)
_log(fnViewSource + "(): window already " + _p(isPrivate));
else {
_log(fnViewSource + "(): make window " + _p(isPrivate));
privacyContext.usePrivateBrowsing = isPrivate;
}
}, false);
}, "domwindowopened", false);
}
}.bind(this)
);
}
else {
patcher.unwrapFunction(window, fnViewSource, fnViewSource, forceDestroy);
}
},
patchBrowserLoadURI: function(window, applyPatch, forceDestroy) {
var gBrowser = window.gBrowser;
var browser = gBrowser.browsers && gBrowser.browsers[0];
if(!browser) {
Components.utils.reportError(LOG_PREFIX + "!!! Can't find browser to patch browser.loadURIWithFlags()");
return;
}
var browserProto = Object.getPrototypeOf(browser);
if(!browserProto || !("loadURIWithFlags" in browserProto)) {
_log("Can't patch browser: no loadURIWithFlags() method");
return;
}
if(applyPatch) {
var _this = this;
patcher.wrapFunction(
browserProto, "loadURIWithFlags", "browser.loadURIWithFlags",
function before(aURI, aFlags, aReferrerURI, aCharset, aPostData) {
var params = aFlags;
if(params && typeof params == "object") // Firefox 38+
aFlags = params.flags;
_dbgv && _log("loadURIWithFlags() flags: " + aFlags);
if(!(aFlags & Components.interfaces.nsIWebNavigation.LOAD_FLAGS_FROM_EXTERNAL))
return false;
var tab = _this.getTabForBrowser(this);
if(!tab) {
_log("loadURIWithFlags() with LOAD_FLAGS_FROM_EXTERNAL flag, tab not found!");
return false;
}
if(!_this.isPrivateTab(tab))
return false;
// See chrome://browser/content/browser.js, nsBrowserAccess.prototype.openURI()
var stack = new Error().stack;
_dbgv && _log("loadURIWithFlags(), stack:\n" + stack);
if(
stack.indexOf("addTab@chrome:") != -1
|| stack.indexOf("loadOneTab@chrome:") != -1
) {
_log("loadURIWithFlags() with LOAD_FLAGS_FROM_EXTERNAL flag => make tab not private");
_this.toggleTabPrivate(tab, false);
return false;
}
_log("loadURIWithFlags() with LOAD_FLAGS_FROM_EXTERNAL flag => open in new tab");
_this.readyToOpenTab(window, false);
gBrowser.loadOneTab(aURI || "about:blank", {
referrerURI: aReferrerURI,
fromExternal: true,
inBackground: prefs.getPref("browser.tabs.loadDivertedInBackground")
});
return true;
}
);
}
else {
patcher.unwrapFunction(browserProto, "loadURIWithFlags", "browser.loadURIWithFlags", forceDestroy);
}
},
patchSearchBar: function(window, applyPatch, forceDestroy) {
if(!this.isSeaMonkey)
return;
var document = window.document;
var searchBar = document.getElementById("searchbar");
if(!searchBar) // We can't patch node inside toolbar palette
return;
if(!("usePrivateBrowsing" in searchBar)) {
_log("patchSearchBar(): can't patch, usePrivateBrowsing property not found");
return;
}
var bakKey = "privateTabOrig::usePrivateBrowsing";
if(applyPatch == bakKey in searchBar)
return;
_log("patchSearchBar(" + applyPatch + ")");
if(applyPatch) {
var _this = this;
searchBar[bakKey] = Object.getOwnPropertyDescriptor(searchBar, "usePrivateBrowsing");
Object.defineProperty(searchBar, "usePrivateBrowsing", {
get: function() {
_log("patchSearchBar(): return state of selected tab");
var window = this.ownerDocument.defaultView.top;
var isPrivate = _this.isPrivateContent(window);
if("privateTab" in window) {
var pt = window.privateTab;
pt._clearSearchBarUndo = true;
pt._clearSearchBarValue = isPrivate;
_dbgv && _log("_clearSearchBarValue: " + isPrivate);
}
return isPrivate;
},
configurable: true,
enumerable: true
});
}