Skip to content

Commit

Permalink
Allow open all new blank/empty tabs in private mode
Browse files Browse the repository at this point in the history
+ preferences:
extensions.privateTab.makeNewEmptyTabsPrivate
extensions.privateTab.makeNewEmptyWindowsPrivate
(draft for #10)
  • Loading branch information
Infocatcher committed Apr 2, 2013
1 parent d968079 commit 99056e3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
51 changes: 42 additions & 9 deletions bootstrap.js
Expand Up @@ -82,7 +82,9 @@ var windowsObserver = {
case "command": this.commandHandler(e); break;
case "click": this.clickHandler(e); break;
case "keypress": this.keypressHandler(e); break;
case "PrivateTab:PrivateChanged": this.privateChangedHandler(e);
case "PrivateTab:PrivateChanged": this.privateChangedHandler(e); break;
case "SSWindowStateBusy": this.setWindowBusy(e, true); break;
case "SSWindowStateReady": this.setWindowBusy(e, false);
}
},
loadHandler: function(e) {
Expand Down Expand Up @@ -137,6 +139,8 @@ var windowsObserver = {
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() {
Expand Down Expand Up @@ -183,6 +187,8 @@ var windowsObserver = {
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() {
Expand All @@ -209,9 +215,15 @@ var windowsObserver = {
);
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(!("arguments" in window) || !(3 in window.arguments)) {
if(isEmptyWindow) {
_log("inheritWindowState(): Looks like new empty window, ignore");
return;
}
Expand All @@ -222,11 +234,7 @@ var windowsObserver = {
if(!this.isPrivateTab(opener.gBrowser.selectedTab))
return;
_log("Inherit private state from current tab of the opener window");
//~ todo: add pref for this?
//this.getPrivacyContext(window).usePrivateBrowsing = true;
Array.forEach(window.gBrowser.tabs, function(tab) {
this.toggleTabPrivate(tab, true);
}, this);
this.toggleWindowPrivate(window, true);
},

prefChanged: function(pName, pVal) {
Expand Down Expand Up @@ -378,22 +386,31 @@ var windowsObserver = {
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(tab.ownerDocument.defaultView))
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 {
tab.ownerDocument.defaultView.setTimeout(function() {
window.setTimeout(function() {
this.setTabState(tab);
}.bind(this), 0);
}
Expand Down Expand Up @@ -750,6 +767,10 @@ var windowsObserver = {
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()
Expand Down Expand Up @@ -1514,6 +1535,17 @@ var windowsObserver = {
this.dispatchAPIEvent(tab, "PrivateTab:PrivateChanged", isPrivate);
return isPrivate;
},
toggleWindowPrivate: function(window, isPrivate) {
var gBrowser = window.gBrowser;
if(isPrivate === undefined)
this.isPrivateTab(gBrowser.selectedTab);
//~ todo: add pref for this?
//this.getPrivacyContext(window).usePrivateBrowsing = true;
_log("Make all tabs in window private");
Array.forEach(gBrowser.tabs, function(tab) {
this.toggleTabPrivate(tab, isPrivate);
}, this);
},
getTabBrowser: function(tab) {
return this.getTabBrowserFromChild(tab.linkedBrowser);
},
Expand Down Expand Up @@ -1856,6 +1888,7 @@ function API(window) {
}
API.prototype = {
_openNewTabsPrivate: undefined,
_ssWindowBusy: false,
_destroy: function() {
if(this._openNewTabsPrivate !== undefined)
this.stopToOpenTabs();
Expand Down
2 changes: 2 additions & 0 deletions defaults/preferences/prefs.js
Expand Up @@ -15,6 +15,8 @@ pref("extensions.privateTab.dragAndDropBehavior", 0);
// 2 - use target private state
pref("extensions.privateTab.dragAndDropTabsBetweenDifferentWindows", true);
pref("extensions.privateTab.rememberClosedPrivateTabs", false);
pref("extensions.privateTab.makeNewEmptyTabsPrivate", false);
pref("extensions.privateTab.makeNewEmptyWindowsPrivate", false);
pref("extensions.privateTab.sendRefererHeader", 2);
// 0 - don't send
// 1 - only if private tab opened from private tab
Expand Down

0 comments on commit 99056e3

Please sign in to comment.