Skip to content

Commit

Permalink
Merge pull request mozilla#830 from ochameau/tabs-pb
Browse files Browse the repository at this point in the history
Bug 838981: Ignore private window in tabs API when addon doesn't have private-browsing permission. r=@Gozala
  • Loading branch information
ochameau committed Mar 5, 2013
2 parents b0f21bb + 282229f commit a9345e7
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 16 deletions.
11 changes: 5 additions & 6 deletions lib/sdk/tabs/tabs-firefox.js
Expand Up @@ -22,8 +22,7 @@ Object.defineProperties(tabs, {
});
return undefined;
}
// Open in active window if new window was not required..

// Open in active window if new window was not required.

let activeWindow = windows.activeWindow;
let privateState = !!options.isPrivate;
Expand All @@ -35,7 +34,7 @@ Object.defineProperties(tabs, {
// find a window in the state that we need
let window = getWindow(privateState);
if (window) {
window.tabs.open(options);
window.tabs.open(options);
}
// open a window in the state that we need
else {
Expand All @@ -52,9 +51,9 @@ Object.defineProperties(tabs, {

function getWindow(privateState) {
for each (let window in windows) {
if (privateState === isPrivate(window)) {
return window;
}
if (privateState === isPrivate(window)) {
return window;
}
}
return null;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/sdk/window/utils.js
Expand Up @@ -248,7 +248,8 @@ function windows(type, options) {
let winEnum = WM.getEnumerator(type);
while (winEnum.hasMoreElements()) {
let window = winEnum.getNext().QueryInterface(Ci.nsIDOMWindow);
// only add unprivate windows when pb is not supported
// Only add non-private windows when pb permission isn't set,
// unless an option forces the addition of them.
if (options.includePrivate || !isWindowPrivate(window)) {
list.push(window);
}
Expand Down
6 changes: 4 additions & 2 deletions lib/sdk/windows/tabs-firefox.js
Expand Up @@ -16,7 +16,7 @@ const { getOwnerWindow, getActiveTab, getTabs,
openTab } = require("../tabs/utils");
const { Options } = require("../tabs/common");
const { observer: tabsObserver } = require("../tabs/observer");
const { isWindowPrivate } = require("../private-browsing/utils");
const { ignoreWindow, isWindowPrivate } = require("../private-browsing/utils");

const TAB_BROWSER = "tabbrowser";

Expand Down Expand Up @@ -91,7 +91,9 @@ const WindowTabTracker = Trait.compose({
tabsObserver.removeListener("deactivate", this._onTabDeactivate);
},
_onTabEvent: function _onTabEvent(type, tab) {
if (this._window === getOwnerWindow(tab)) {
// Accept only tabs for the watched window, and ignore private tabs
// if addon doesn't have private permission
if (this._window === getOwnerWindow(tab) && !ignoreWindow(this._window)) {
let options = this._tabOptions.shift() || {};
options.tab = tab;
options.window = this._public;
Expand Down
9 changes: 5 additions & 4 deletions test/addons/private-browsing-supported/main.js
Expand Up @@ -40,13 +40,13 @@ exports.testGetOwnerWindow = function(assert, done) {
else {
if (isWindowPBSupported) {
assert.notStrictEqual(chromeWindow,
getOwnerWindow(tab),
'associated window is not the same for window and window\'s tab');
getOwnerWindow(tab),
'associated window is not the same for window and window\'s tab');
}
else {
assert.strictEqual(chromeWindow,
getOwnerWindow(tab),
'associated window is the same for window and window\'s tab');
getOwnerWindow(tab),
'associated window is the same for window and window\'s tab');
}
}

Expand Down Expand Up @@ -165,5 +165,6 @@ if (!is('Fennec')) {
}

merge(module.exports, require('./windows'));
merge(module.exports, require('./tabs'));

require('sdk/test/runner').runTestsFromModule(module);
30 changes: 30 additions & 0 deletions test/addons/private-browsing-supported/tabs.js
@@ -0,0 +1,30 @@
const tabs = require('sdk/tabs');
const { is } = require('sdk/system/xul-app');
const { isPrivate } = require('sdk/private-browsing');
const pbUtils = require('sdk/private-browsing/utils');
const { getOwnerWindow } = require('sdk/private-browsing/window/utils');

exports.testPrivateTabsAreListed = function (assert, done) {
let originalTabCount = tabs.length;

tabs.open({
url: 'about:blank',
isPrivate: true,
onOpen: function(tab) {
let win = getOwnerWindow(tab);
// PWPB case
if (pbUtils.isWindowPBSupported || pbUtils.isTabPBSupported) {
assert.ok(isPrivate(tab), "tab is private");
assert.equal(tabs.length, originalTabCount + 1,
'New private window\'s tab are visible in tabs list');
}
else {
// Global case, openDialog didn't opened a private window/tab
assert.ok(!isPrivate(tab), "tab isn't private");
assert.equal(tabs.length, originalTabCount + 1,
'New non-private window\'s tab is visible in tabs list');
}
tab.close(done);
}
});
}
41 changes: 38 additions & 3 deletions test/test-tabs-common.js
Expand Up @@ -7,7 +7,9 @@ const { Loader } = require('sdk/test/loader');
const { browserWindows } = require('sdk/windows');
const tabs = require('sdk/tabs');
const { isPrivate } = require('sdk/private-browsing');
const { isWindowPBSupported, isTabPBSupported } = require('sdk/private-browsing/utils');
const { openDialog } = require('sdk/window/utils');
const pbUtils = require('sdk/private-browsing/utils');
const { isWindowPrivate } = require('sdk/window/utils');

const URL = 'data:text/html;charset=utf-8,<html><head><title>#title#</title></head></html>';

Expand Down Expand Up @@ -299,7 +301,7 @@ exports.testTabContentTypeAndReload = function(test) {
});
};

// test that it is possible to open a private tab
// test that it isn't possible to open a private tab without the private permission
exports.testTabOpenPrivate = function(test) {
test.waitUntilDone();

Expand All @@ -309,11 +311,44 @@ exports.testTabOpenPrivate = function(test) {
isPrivate: true,
onReady: function(tab) {
test.assertEqual(tab.url, url, 'opened correct tab');
test.assertEqual(isPrivate(tab), false, 'private tabs arenot supported by default');
test.assertEqual(isPrivate(tab), false, 'private tabs are not supported by default');

tab.close(function() {
test.done();
});
}
});
}

// We need permission flag in order to see private window's tabs
exports.testPrivateAreNotListed = function (test) {
test.waitUntilDone();
let originalTabCount = tabs.length;

let win = openDialog({
private: true
});

win.addEventListener("load", function onload() {
win.removeEventListener("load", onload);

// PWPB case
if (pbUtils.isWindowPBSupported) {
test.assert(isWindowPrivate(win), "window is private");
test.assertEqual(tabs.length, originalTabCount,
'New private window\'s tab isn\'t visible in tabs list');
}
else {
// Global case, openDialog didn't opened a private window/tab
test.assert(!isWindowPrivate(win), "window is private");
test.assertEqual(tabs.length, originalTabCount + 1,
'New non-private window\'s tab is visible in tabs list');
}

win.addEventListener("unload", function onunload() {
win.removeEventListener('unload', onunload);
test.done();
});
win.close();
});
}

0 comments on commit a9345e7

Please sign in to comment.