forked from Infocatcher/Private_Tab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.js
More file actions
2153 lines (2081 loc) · 70.2 KB
/
bootstrap.js
File metadata and controls
2153 lines (2081 loc) · 70.2 KB
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 LOG_PREFIX = "[Private Tab] ";
Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
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) {
windowsObserver.init(reason);
}
function shutdown(params, reason) {
windowsObserver.destroy(reason);
}
var windowsObserver = {
initialized: false,
init: function(reason) {
if(this.initialized)
return;
this.initialized = true;
prefs.init();
this.initHotkeys();
this.appButtonDontChange = !prefs.get("fixAppButtonWidth");
this.windows.forEach(function(window) {
this.initWindow(window, reason);
}, this);
Services.ww.registerNotification(this);
},
destroy: function(reason) {
if(!this.initialized)
return;
this.initialized = false;
this.windows.forEach(function(window) {
this.destroyWindow(window, reason);
}, this);
Services.ww.unregisterNotification(this);
this.unloadStyles();
this.restoreAppButtonWidth();
prefs.destroy();
this._dndPrivateNode = null;
},
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);
},
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 "dragstart": this.dragStartHandler(e); break;
case "dragend": this.dragEndHandler(e); break;
case "drop": this.dropHandler(e); break;
case "popupshowing": this.popupShowingHandler(e); break;
case "command": this.commandHandler(e); break;
case "click": this.clickHandler(e); break;
case "keypress": this.keypressHandler(e); break;
case "PrivateTab:PrivateChanged": this.privateChangedHandler(e); break;
case "SSWindowStateBusy": this.setWindowBusy(e, true); break;
case "SSWindowStateReady": this.setWindowBusy(e, false);
}
},
loadHandler: function(e) {
var window = e.originalTarget.defaultView;
window.removeEventListener("load", this, false);
this.initWindow(window, WINDOW_LOADED);
},
initWindow: function(window, reason) {
if(reason == WINDOW_LOADED && !this.isTargetWindow(window)) {
delete window.__privateTabOpener;
return;
}
var document = window.document;
this.loadStyles(window);
var gBrowser = window.gBrowser
|| window.getBrowser(); // For SeaMonkey
this.ensureTitleModifier(document);
this.patchBrowsers(gBrowser, 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.patchTabBrowserDND(window, gBrowser, true);
}.bind(this), 0);
if(reason == WINDOW_LOADED)
this.inheritWindowState(window);
Array.forEach(gBrowser.tabs, function(tab) {
this.setTabState(tab);
}, this);
if(this.isPrivateWindow(window)) {
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");
}
this.appButtonNA = false;
this.fixAppButtonWidth(document);
window.setTimeout(function() {
this.updateWindowTitle(gBrowser);
}.bind(this), 0);
window.addEventListener("TabOpen", this, false);
window.addEventListener("SSTabRestoring", this, false);
window.addEventListener("TabSelect", this, false);
window.addEventListener("TabClose", this, false);
window.addEventListener("dragstart", this, true);
window.addEventListener("dragend", this, true);
window.addEventListener("drop", this, true);
window.addEventListener("PrivateTab:PrivateChanged", this, false);
window.addEventListener("SSWindowStateBusy", this, true);
window.addEventListener("SSWindowStateReady", this, true);
if(this.hotkeys)
window.addEventListener("keypress", this, true);
window.setTimeout(function() {
this.initControls(document);
window.setTimeout(function() {
this.initHotkeysText(document);
}.bind(this), 10);
}.bind(this), 50);
this.initToolbarButton(document);
window.privateTab = new API(window);
},
destroyWindow: function(window, reason) {
window.removeEventListener("load", this, false); // Window can be closed before "load"
if(reason == WINDOW_CLOSED && !this.isTargetWindow(window))
return;
var force = reason != APP_SHUTDOWN && reason != WINDOW_CLOSED;
var disable = reason == ADDON_DISABLE || reason == ADDON_UNINSTALL;
if(force) {
var document = window.document;
var gBrowser = window.gBrowser;
var isPrivateWindow = this.isPrivateWindow(window);
Array.forEach(gBrowser.tabs, function(tab) {
tab.removeAttribute(this.privateAttr);
if(disable && isPrivateWindow ^ this.isPrivateTab(tab)) {
this.toggleTabPrivate(tab, isPrivateWindow);
this.fixTabState(tab, false); // Always remove this.privateAttr
}
}, this);
document.documentElement.removeAttribute(this.privateAttr);
_log("Restore title...");
if(!isPrivateWindow)
this.updateWindowTitle(gBrowser, false);
this.destroyTitleModifier(document);
this.patchBrowsers(gBrowser, false);
this.patchTabBrowserDND(window, gBrowser, false);
}
this.unwatchAppButton(window);
window.removeEventListener("TabOpen", this, false);
window.removeEventListener("SSTabRestoring", this, false);
window.removeEventListener("TabSelect", this, false);
window.removeEventListener("TabClose", this, false);
window.removeEventListener("dragstart", this, true);
window.removeEventListener("dragend", this, true);
window.removeEventListener("drop", this, true);
window.removeEventListener("keypress", this, true);
window.removeEventListener("PrivateTab:PrivateChanged", this, false);
window.removeEventListener("SSWindowStateBusy", this, true);
window.removeEventListener("SSWindowStateReady", this, true);
this.destroyControls(window, force);
},
get windows() {
var windows = [];
var isSeaMonkey = Services.appinfo.name == "SeaMonkey";
var ws = Services.wm.getEnumerator(isSeaMonkey ? null : "navigator:browser");
while(ws.hasMoreElements()) {
var window = ws.getNext();
if(this.isTargetWindow(window))
windows.push(window);
}
return windows;
},
isTargetWindow: function(window) {
var winType = window.document.documentElement.getAttribute("windowtype");
return winType == "navigator:browser"
|| winType == "navigator:private"; // SeaMonkey >= 2.19a1 (2013-03-27)
},
inheritWindowState: function(window) {
_log(
"inheritWindowState():\nwindow.opener: " + window.opener
+ "\nwindow.__privateTabOpener: " + (window.__privateTabOpener || undefined)
+ "\nwindow.arguments:\n" + Array.map(window.arguments || [], String).join("\n")
);
var opener = window.opener || window.__privateTabOpener || null;
delete window.__privateTabOpener;
var isEmptyWindow = !("arguments" in window) || !(3 in window.arguments);
if((!opener || isEmptyWindow) && prefs.get("makeNewEmptyWindowsPrivate")) {
_log("Make new empty window private");
this.toggleWindowPrivate(window, true);
return;
}
if(!opener || opener.closed || !this.isTargetWindow(opener) || !opener.gBrowser)
return;
if(isEmptyWindow) {
_log("inheritWindowState(): Looks like new empty window, ignore");
return;
}
if(this.isPrivateWindow(window)) {
_log("inheritWindowState(): Ignore already private window");
return;
}
if(!this.isPrivateTab(opener.gBrowser.selectedTab))
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();
else if(pName == "fixAppButtonWidth") {
this.appButtonDontChange = !pVal;
this.restoreAppButtonWidth();
this.windows.forEach(function(window) {
var document = window.document;
this.appButtonNA = false;
if(pVal && !this.appButtonCssURI)
this.fixAppButtonWidth(document);
this.updateAppButtonWidth(document, true);
}, this);
}
else if(pName == "dragAndDropTabsBetweenDifferentWindows") {
this.windows.forEach(function(window) {
this.patchTabBrowserDND(window, window.gBrowser, pVal);
}, this);
}
else if(pName == "makeNewEmptyTabsPrivate") {
this.windows.forEach(function(window) {
var document = window.document;
var menuItem = document.getElementById(this.newTabMenuId);
if(menuItem)
menuItem.hidden = pVal;
var appMenuItem = document.getElementById(this.newTabAppMenuId);
if(appMenuItem)
appMenuItem.hidden = pVal;
}, this);
}
},
get pbuFake() {
delete this.pbuFake;
return this.pbuFake = Object.create(PrivateBrowsingUtils, {
isWindowPrivate: {
value: function privateTabWrapper(window) {
return true; //~ todo: check call stack?
},
configurable: true,
enumerable: true
}
});
},
patchTabBrowserDND: function(window, gBrowser, applyPatch) {
if(
!prefs.get("dragAndDropTabsBetweenDifferentWindows")
&& (applyPatch || !("_privateTabPrivateBrowsingUtils" in window))
)
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...
this.overridePrivateBrowsingUtils(
window,
gBrowser.tabContainer,
"_setEffectAllowedForDataTransfer",
"gBrowser.tabContainer._setEffectAllowedForDataTransfer",
applyPatch
);
this.overridePrivateBrowsingUtils(
window,
gBrowser,
"swapBrowsersAndCloseOther",
"gBrowser.swapBrowsersAndCloseOther",
applyPatch
);
},
patchBrowsers: function(gBrowser, applyPatch) {
var browser = gBrowser.browsers && gBrowser.browsers[0];
if(!browser) {
Components.utils.reportError(LOG_PREFIX + "!!! Can't find browser to patch browser.swapDocShells()");
return;
}
var browserProto = Object.getPrototypeOf(browser);
if(!browserProto || !("swapDocShells" in browserProto)) {
_log("Can't patch browser: no swapDocShells() method");
return;
}
if(applyPatch) {
_log("Patch browser.__proto__.swapDocShells() method");
var _this = this;
patcher.wrapFunction(
browserProto, "swapDocShells", "browser.swapDocShells",
function before(otherBrowser) {
try {
before.isPrivate = otherBrowser.webNavigation
.QueryInterface(Components.interfaces.nsILoadContext)
.usePrivateBrowsing;
_log("swapDocShells(): usePrivateBrowsing: " + before.isPrivate);
}
catch(e) {
Components.utils.reportError(e);
}
},
function after(ret, otherBrowser) {
var isPrivate = after.before.isPrivate;
if(isPrivate !== undefined) try {
this.webNavigation
.QueryInterface(Components.interfaces.nsILoadContext)
.usePrivateBrowsing = isPrivate;
_log("swapDocShells(): set usePrivateBrowsing to " + isPrivate);
var tab = _this.getTabForBrowser(this);
tab && _this.dispatchAPIEvent(tab, "PrivateTab:PrivateChanged", isPrivate);
}
catch(e) {
Components.utils.reportError(e);
}
}
);
}
else {
_log("Restore browser.__proto__.swapDocShells() method");
patcher.unwrapFunction(browserProto, "swapDocShells", "browser.swapDocShells");
}
},
overridePrivateBrowsingUtils: function(window, obj, meth, key, applyPatch) {
if(!obj || !(meth in obj)) {
Components.utils.reportError(LOG_PREFIX + "!!! Can't find " + key + "()");
return;
}
if(applyPatch) {
var pbuOrig = PrivateBrowsingUtils;
var pbuFake = this.pbuFake;
var restoreTimer = 0;
patcher.wrapFunction(
obj, meth, key,
function before(event) {
//_log("[patcher] Override PrivateBrowsingUtils.isWindowPrivate()");
window.PrivateBrowsingUtils = pbuFake;
window.clearTimeout(restoreTimer);
restoreTimer = window.setTimeout(function() { // Restore anyway
if(window.PrivateBrowsingUtils != pbuOrig)
window.PrivateBrowsingUtils = pbuOrig;
}, 0);
},
function after(ret, event) {
window.PrivateBrowsingUtils = pbuOrig;
}
);
}
else {
patcher.unwrapFunction(obj, meth, key);
}
},
tabOpenHandler: function(e) {
var tab = e.originalTarget || e.target;
if("_privateTabIgnore" in tab) {
delete tab._privateTabIgnore;
return;
}
var gBrowser = this.getTabBrowser(tab);
var window = tab.ownerDocument.defaultView;
//~ todo: try get real tab owner!
var isPrivate;
if(!this.isEmptyTab(tab, gBrowser)) {
if(this.isPrivateTab(gBrowser.selectedTab))
isPrivate = true;
else if(this.isPrivateWindow(window))
isPrivate = false; // Override browser behavior!
}
else if(
window.privateTab
&& !window.privateTab._ssWindowBusy
&& prefs.get("makeNewEmptyTabsPrivate")
) {
_log("Make new empty tab private");
isPrivate = true;
}
_log(
"Tab opened: " + (tab.getAttribute("label") || "").substr(0, 256)
+ "\nInherit private state: " + isPrivate
);
if(isPrivate != undefined)
this.toggleTabPrivate(tab, isPrivate);
else {
window.setTimeout(function() {
this.setTabState(tab);
}.bind(this), 0);
}
},
tabRestoringHandler: function(e) {
var tab = e.originalTarget || e.target;
if("_privateTabIgnore" in tab) {
delete tab._privateTabIgnore;
return;
}
_log("Tab restored: " + (tab.getAttribute("label") || "").substr(0, 256));
var isPrivate = tab.hasAttribute(this.privateAttr);
if(this.isPrivateTab(tab) != isPrivate) {
_log("Make restored tab " + (isPrivate ? "private" : "not private"));
this.toggleTabPrivate(tab, isPrivate);
}
},
tabCloseHandler: function(e) {
if(prefs.get("rememberClosedPrivateTabs"))
return;
var tab = e.originalTarget || e.target;
if(!this.isPrivateTab(tab))
return;
_log(
"Private tab closed: " + (tab.getAttribute("label") || "").substr(0, 256)
+ "\nTry don't save it in undo close history"
);
var window = tab.ownerDocument.defaultView;
var tabState = this.ss.getTabState(tab);
//_log("Closed tab state:\n" + state);
if(Services.appinfo.name == "SeaMonkey")
window.setTimeout(this.forgetClosedTab.bind(this, window, tabState), 0);
else
this.forgetClosedTab(window, tabState);
},
forgetClosedTab: function(window, tabState) {
var closedTabs = JSON.parse(this.ss.getClosedTabData(window));
for(var i = 0, l = closedTabs.length; i < l; ++i) {
var closedTab = closedTabs[i];
var state = closedTab.state;
//_log("Found closed tab:\n" + JSON.stringify(state));
if(JSON.stringify(state) == tabState) {
this.ss.forgetClosedTab(window, i);
_log("Forget about closed tab #" + i);
break;
}
}
},
tabSelectHandler: function(e) {
var tab = e.originalTarget || e.target;
var window = tab.ownerDocument.defaultView;
var browser = tab.linkedBrowser;
if(
!browser
|| !browser.webProgress
|| browser.webProgress.isLoadingDocument
) {
_log("Selected tab not yet loaded, wait");
window.setTimeout(function() {
this.updateWindowTitle(window.gBrowser);
}.bind(this), 0);
}
else {
this.updateWindowTitle(window.gBrowser);
}
window.setTimeout(function() {
// Someone may change "usePrivateBrowsing"...
// It's good to show real state
if(tab.parentNode) // Ignore removed tabs
this.setTabState(tab);
}.bind(this), 50);
},
_dndPrivateNode: null,
get dndPrivateNode() {
try { // We can get "can't access dead object" error here
var node = this._dndPrivateNode;
if(node.parentNode && node.ownerDocument)
return node;
}
catch(e) {
}
return null;
},
dragStartHandler: function(e) {
var window = e.currentTarget;
var sourceNode = this._dndPrivateNode = this.isPrivateTab(window.gBrowser.selectedTab)
? e.originalTarget || e.target
: null;
sourceNode && _log(e.type + ": mark <" + sourceNode.nodeName + "> " + sourceNode + " node as private");
},
dragEndHandler: function(e) {
this._dndPrivateNode && _log(e.type + " => this._dndPrivateNode = null");
this._dndPrivateNode = null;
},
dropHandler: function(e) {
var window = e.currentTarget;
var dt = e.dataTransfer;
var sourceNode = dt.mozSourceNode || dt.sourceNode;
if(!sourceNode) {
_log(e.type + ": missing source node, ignore");
return;
}
var isPrivateSource = sourceNode == this.dndPrivateNode;
this._dndPrivateNode = null;
_log(e.type + ": from " + (isPrivateSource ? "private" : "not private") + " tab");
var targetTab;
if(e.view.top == window) {
var trg = e.originalTarget || e.target;
targetTab = this.getTabFromChild(trg);
if(
sourceNode
&& sourceNode instanceof window.XULElement
&& this.getTabFromChild(sourceNode)
&& sourceNode.ownerDocument.defaultView == window
&& (targetTab || this.getTabBarFromChild(trg))
) {
_log(e.type + ": tab was dragged into tab or tab bar in the same window, ignore");
return;
}
}
else if(e.view.top == window.content) {
var trg = e.target;
var cs = trg.ownerDocument.defaultView.getComputedStyle(trg, null);
var userModify = "userModify" in cs ? cs.userModify : cs.MozUserModify;
if(userModify == "read-write") {
_log("Dropped into editable node, ignore");
return;
}
targetTab = window.gBrowser.selectedTab;
}
var isPrivateTarget = targetTab
? this.isPrivateTab(targetTab)
: this.isPrivateWindow(window);
_log("Will use target private state (from " + (targetTab ? "tab" : "window") + ")");
var isPrivate;
var dndBehavior = prefs.get("dragAndDropBehavior", 0);
if(dndBehavior == 1) {
isPrivate = isPrivateSource;
_log("Will use source private state: " + isPrivateSource);
}
else if(dndBehavior == 2) {
isPrivate = isPrivateTarget;
_log("Will use target private state: " + isPrivateTarget);
}
else {
isPrivate = isPrivateSource || isPrivateTarget;
_log("Will use source or target private state: " + isPrivateSource + " || " + isPrivateTarget);
}
var origIsPrivate;
if(targetTab && dndBehavior != 2 && isPrivate != this.isPrivateTab(targetTab)) {
origIsPrivate = !isPrivate;
_log(
"Dropped link may be opened in already existing tab, so make it "
+ (isPrivate ? "private" : "not private")
);
this.toggleTabPrivate(targetTab, isPrivate, true);
}
this.waitForTab(window, function(tab) {
if(!tab) {
if(!targetTab)
return;
tab = targetTab;
}
if(origIsPrivate != undefined) {
if(tab == targetTab) {
_log("Highlight target tab as " + (isPrivate ? "private" : "not private"));
this.dispatchAPIEvent(targetTab, "PrivateTab:PrivateChanged", isPrivate);
}
else {
_log("Restore private state of target tab");
this.toggleTabPrivate(targetTab, origIsPrivate, true);
}
}
tab._privateTabIgnore = true; // We should always set this flag!
_log(
"drop: make " + (tab == targetTab ? "current" : "new") + " tab "
+ (isPrivate ? "private" : "not private")
);
// Strange things happens in private windows, so we force set private flag
if(this.isPrivateTab(tab) != isPrivate || isPrivate)
this.toggleTabPrivate(tab, isPrivate);
else
_log("Already correct private state, ignore");
}.bind(this));
},
popupShowingHandler: function(e) {
var popup = e.target;
if(popup != e.currentTarget)
return;
var window = popup.ownerDocument.defaultView;
if(popup.id == "appmenu-popup")
this.initAppMenu(window, popup);
else if(popup.id == "contentAreaContextMenu")
this.updatePageContext(window);
else if(popup.localName == "tooltip")
this.updateTabTooltip(window);
else
this.updateTabContext(window);
},
updatePageContext: function(window) {
var document = window.document;
var gContextMenu = window.gContextMenu;
var noLink = !gContextMenu
|| (!gContextMenu.onSaveableLink && !gContextMenu.onPlainTextLink);
var inNewTab = document.getElementById("context-openlinkintab");
if(
noLink
&& gContextMenu && gContextMenu.onMailtoLink
&& inNewTab && !inNewTab.hidden
) {
// See chrome://browser/content/nsContextMenu.js
// Simple way to inherit
// var shouldShow = this.onSaveableLink || isMailtoInternal || this.onPlainTextLink;
noLink = false;
}
if(!noLink && !gContextMenu.linkURL)
noLink = true;
var mi = document.getElementById(this.contextId);
mi.hidden = noLink;
var hideNotPrivate = this.isPrivateTab(window.gBrowser.selectedTab);
// Hide "Open Link in New Tab/Window" from page context menu on private tabs:
// we inherit private state, so here should be only "Open Link in New Private Tab/Window"
var inNewWin = document.getElementById("context-openlink");
var inNewPrivateWin = document.getElementById("context-openlinkprivate")
|| document.getElementById("context-openlinkinprivatewindow"); // SeaMonkey 2.19a1
if(inNewTab && !noLink)
inNewTab.hidden = hideNotPrivate;
if(inNewWin && inNewPrivateWin && !noLink)
inNewWin.hidden = hideNotPrivate || this.isPrivateWindow(window);
},
updateTabTooltip: function(window) {
var document = window.document;
var tab = document.tooltipNode;
var hide = !tab || tab.localName != "tab" || !this.isPrivateTab(tab);
var label = document.getElementById(this.tabTipId);
if(label)
label.hidden = hide;
},
updateTabContext: function(window) {
var document = window.document;
_log("updateTabContext()");
var tab = this.getContextTab(window);
var hide = !tab || tab.localName != "tab";
var mi = document.getElementById(this.tabContextId);
mi.hidden = hide;
if(!hide) {
var check = this.isPrivateTab(tab);
if(check)
mi.setAttribute("checked", "true");
else
mi.removeAttribute("checked");
var accel = document.getAnonymousElementByAttribute(mi, "class", "menu-accel-container");
if(accel)
accel.hidden = tab != window.gBrowser.selectedTab;
//mi.disabled = this.isPendingTab(tab);
}
},
commandHandler: function(e) {
this.handleCommandFromEvent(e, e.shiftKey || e.ctrlKey || e.altKey || e.metaKey);
},
clickHandler: function(e) {
if(e.button == 1 && e.target.getAttribute("disabled") != "true")
this.handleCommandFromEvent(e, true, true);
},
handleCommandFromEvent: function(e, shifted, closeMenus) {
var trg = e.target;
var cmd = trg.getAttribute(this.cmdAttr);
var window = trg.ownerDocument.defaultView;
this.handleCommand(window, cmd, shifted, closeMenus, e);
if(closeMenus) {
window.closeMenus(trg);
var mp = trg.parentNode;
if("triggerNode" in mp) {
var tn = mp._privateTabTriggerNode || mp.triggerNode;
tn && window.closeMenus(tn);
}
}
},
handleCommand: function(window, cmd, shifted, closeMenus, e) {
_log("handleCommand: " + cmd);
if(cmd == "openInNewPrivateTab")
this.openInNewPrivateTab(window, shifted);
else if(cmd == "openNewPrivateTab")
this.openNewPrivateTab(window);
else if(cmd == "toggleTabPrivate")
this.toggleContextTabPrivate(window);
else if(cmd == "openPlacesInNewPrivateTab")
this.openPlaceInNewPrivateTab(window, shifted, e);
else {
var caller = Components.stack.caller;
throw new Error(LOG_PREFIX + 'Unknown command: "' + cmd + '"', caller.filename, caller.lineNumber);
}
},
keypressHandler: function(e) {
var keys = this.hotkeys;
for(var kId in keys) {
var k = keys[kId];
if(
e.ctrlKey == k.ctrlKey
&& e.altKey == k.altKey
&& e.shiftKey == k.shiftKey
&& e.metaKey == k.metaKey
&& e.getModifierState("OS") == k.osKey
&& (
k.char && String.fromCharCode(e.charCode).toUpperCase() == k.char
|| k.code && e.keyCode == k.code
)
) {
_log(e.type + ": matched key: " + kId);
if(e.defaultPrevented && !prefs.get("keysIgnoreDefaultPrevented")) {
_log(e.type + ": event.defaultPrevented => do nothing");
return;
}
var window = e.currentTarget;
if(k.forbidInTextFields) {
var fe = window.document.commandDispatcher.focusedElement;
if(
fe && (
fe instanceof window.HTMLInputElement
|| fe instanceof window.HTMLTextAreaElement
)
) {
try { // Throws on not-text input elements
if(typeof fe.selectionStart == "number") {
_log("Don't use single char hotkey in text field");
return;
}
}
catch(e) {
}
}
}
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
this.handleCommand(window, kId);
}
}
},
privateChangedHandler: function(e) {
var tab = e.originalTarget || e.target;
var isPrivate = e.detail == 1;
this.setTabState(tab, isPrivate);
var gBrowser = tab.ownerDocument.defaultView.gBrowser;
if(gBrowser.selectedTab == tab) {
_log(e.type + " + gBrowser.selectedTab == tab => updateWindowTitle()");
this.updateWindowTitle(gBrowser, isPrivate);
}
},
setWindowBusy: function(e, busy) {
_log("setWindowBusy(): " + busy);
e.currentTarget.privateTab._ssWindowBusy = busy;
},
openInNewPrivateTab: function(window, toggleInBackground) {
// Based on nsContextMenu.prototype.openLinkInTab()
var gContextMenu = window.gContextMenu;
var uri = gContextMenu.linkURL;
var doc = gContextMenu.target.ownerDocument;
window.urlSecurityCheck(uri, doc.nodePrincipal);
this.openURIInNewPrivateTab(window, uri, doc, {
toggleInBackground: toggleInBackground
});
},
openPlaceInNewPrivateTab: function(window, toggleInBackground, e) {
var mi = e && e.target;
if(!mi)
return;
_log("openPlaceInNewPrivateTab(): " + mi.nodeName + " " + mi.getAttribute("label"));
var placesContext = mi.parentNode;
var view = placesContext._view;
var node = view.selectedNode;
var top = this.getTopWindow(window.top);
try {
if(!window.PlacesUIUtils.checkURLSecurity(node, top))
return;
}
catch(e) {
Components.utils.reportError(e);
}
var loadInBackgroundPref = "browser.tabs.loadBookmarksInBackground";
this.openURIInNewPrivateTab(top, node.uri, null, {
toggleInBackground: toggleInBackground,
loadInBackgroundPref: prefs.getPref(loadInBackgroundPref) != undefined && loadInBackgroundPref,
openAsChild: window.top == top.content
});
},
openURIInNewPrivateTab: function(window, uri, sourceDocument, options) {
var toggleInBackground = "toggleInBackground" in options && options.toggleInBackground;
var loadInBackgroundPref = options.loadInBackgroundPref || "browser.tabs.loadInBackground";
var openAsChild = "openAsChild" in options ? options.openAsChild : true;
var relatedToCurrent;
var w = this.getNotPopupWindow(window);
if(w && w != window) {
relatedToCurrent = openAsChild = false;
w.setTimeout(w.focus, 0);
window = w;
}
var gBrowser = window.gBrowser;
if(openAsChild) {
// http://piro.sakura.ne.jp/xul/_treestyletab.html.en#api
if("TreeStyleTabService" in window)
window.TreeStyleTabService.readyToOpenChildTab(gBrowser.selectedTab);
// Tab Kit https://addons.mozilla.org/firefox/addon/tab-kit/
// TabKit 2nd Edition https://addons.mozilla.org/firefox/addon/tabkit-2nd-edition/
if("tabkit" in window)
window.tabkit.addingTab("related");
}
var referer = null;
if(sourceDocument) {
var sendReferer = prefs.get("sendRefererHeader");
if(
sendReferer > 0
&& (sendReferer > 1 || this.isPrivateWindow(sourceDocument.defaultView))
)
referer = sourceDocument.documentURIObject;
}
this.readyToOpenPrivateTab(window);
var tab = gBrowser.addTab(uri, {
referrerURI: referer,
charset: sourceDocument ? sourceDocument.characterSet : null,
ownerTab: gBrowser.selectedTab,
relatedToCurrent: relatedToCurrent
});
var inBackground = prefs.get("loadInBackground");
if(inBackground == -1)
inBackground = prefs.getPref(loadInBackgroundPref);
if(toggleInBackground)
inBackground = !inBackground;
if(!inBackground)
gBrowser.selectedTab = tab;
if(openAsChild && "tabkit" in window)
window.tabkit.addingTabOver();
this.dispatchAPIEvent(tab, "PrivateTab:OpenInNewTab", openAsChild);
return tab;
},
openNewPrivateTab: function(window) {
var w = this.getNotPopupWindow(window);
if(w && w != window) {
w.setTimeout(w.focus, 0);
window = w;
}
var gBrowser = window.gBrowser;
this.readyToOpenPrivateTab(window);
var tab = gBrowser.selectedTab = gBrowser.addTab(window.BROWSER_NEW_TAB_URL);
if("focusAndSelectUrlBar" in window)
window.setTimeout(window.focusAndSelectUrlBar, 0);
else if("WindowFocusTimerCallback" in window) // SeaMonkey
window.setTimeout(window.WindowFocusTimerCallback, 0, window.gURLBar);
this.dispatchAPIEvent(tab, "PrivateTab:OpenNewTab");
return tab;
},
readyToOpenPrivateTab: function(window) {
this.waitForTab(window, function(tab) {
if(!tab)
return;
tab._privateTabIgnore = true;
this.toggleTabPrivate(tab, true);
}.bind(this));
},
waitForTab: function(window, callback) {
_log("waitForTab()");
function tabOpen(e) {
window.removeEventListener("TabOpen", tabOpen, true);
window.clearTimeout(timer);
var tab = e.originalTarget || e.target;
_log("waitForTab(): opened tab");
callback(tab);
};
window.addEventListener("TabOpen", tabOpen, true);
var timer = window.setTimeout(function() {
window.removeEventListener("TabOpen", tabOpen, true);
_log("waitForTab(): nothing");
callback(null);
}, 0);
},
getNotPopupWindow: function(window) {
if(window.toolbar.visible)
return window;
if(prefs.get("dontUseTabsInPopupWindows")) try {
Components.utils.import("resource:///modules/RecentWindow.jsm");
return RecentWindow.getMostRecentBrowserWindow({
allowPopups: false
});
}
catch(e) {
}
return null;
},
getContextTab: function(window) {
if("TabContextMenu" in window)
return window.TabContextMenu.contextTab;
var cm = this.getTabContextMenu(window.document);
return cm.triggerNode && window.gBrowser.mContextTab;
},
toggleContextTabPrivate: function(window) {
var tab = this.getContextTab(window)
|| window.gBrowser.selectedTab; // For hotkey
var isPrivate = this.toggleTabPrivate(tab);
if(this.isPendingTab(tab))
this.fixTabState(tab, isPrivate);
else if(prefs.get("toggleTabPrivateAutoReload")) {
var browser = tab.linkedBrowser;
if(!browser.webProgress.isLoadingDocument)
tab.linkedBrowser.reload();
}
if(tab == this.getTabBrowser(tab).selectedTab) {
this.updateTabContext(window);
this.updateTabTooltip(window);
if("TabScope" in window && "_updateTitle" in window.TabScope && window.TabScope._tab)
window.TabScope._updateTitle();
}
},
cmdAttr: "privateTab-command",
toolbarButtonId: "privateTab-toolbar-openNewPrivateTab",
contextId: "privateTab-context-openInNewPrivateTab",
tabContextId: "privateTab-tabContext-toggleTabPrivate",
newTabMenuId: "privateTab-menu-openNewPrivateTab",
newTabAppMenuId: "privateTab-appMenu-openNewPrivateTab",
tabTipId: "privateTab-tooltip-isPrivateTabLabel",
tabScopeTipId: "privateTab-tabScope-isPrivateTabLabel",
placesContextId: "privateTab-places-openInNewPrivateTab",
getToolbox: function(window) {
return window.gNavToolbox || window.getNavToolbox();
},
getPaletteButton: function(window) {
var btns = this.getToolbox(window)
.palette
.getElementsByAttribute("id", this.toolbarButtonId);
return btns.length && btns[0];
},
getTabContextMenu: function(document) {
return document.getElementById("tabContextMenu")
|| document.getAnonymousElementByAttribute(
document.defaultView.gBrowser,
"anonid",
"tabContextMenu"
);
},
getTabTooltip: function(document) {
var tabTip = document.getElementById("tabbrowser-tab-tooltip");
if(!tabTip) { // SeaMonkey
var gBrowser = document.defaultView.gBrowser;
var tabStrip = document.getAnonymousElementByAttribute(gBrowser, "anonid", "strip");
if(tabStrip && tabStrip.firstChild && tabStrip.firstChild.localName == "tooltip")
tabTip = tabStrip.firstChild;
}
return tabTip;
},
initToolbarButton: function(document) {
var tbId = this.toolbarButtonId;
var tb = this.createNode(document, "toolbarbutton", tbId, {
id: tbId,
"class": "toolbarbutton-1 chromeclass-toolbar-additional",
removable: "true",
label: this.getLocalized("openNewPrivateTab"),
tooltiptext: this.getLocalized("openNewPrivateTabTip"),
"privateTab-command": "openNewPrivateTab"
});