Skip to content
This repository has been archived by the owner on Feb 26, 2022. It is now read-only.

Commit

Permalink
Bug 834958 - pwpb update selection module to ignore private windows
Browse files Browse the repository at this point in the history
- Ignore selections for private windows
  • Loading branch information
ZER0 committed Mar 14, 2013
1 parent e468f3e commit 5a5276e
Show file tree
Hide file tree
Showing 9 changed files with 658 additions and 49 deletions.
43 changes: 37 additions & 6 deletions lib/sdk/selection.js
Expand Up @@ -18,10 +18,10 @@ const { Ci, Cc } = require("chrome"),
{ EventTarget } = require("./event/target"),
{ ns } = require("./core/namespace"),
{ when: unload } = require("./system/unload"),
{ ignoreWindow } = require('./private-browsing/utils'),
{ getTabs, getTabContentWindow, getTabForContentWindow,
getAllTabContentWindows } = require('./tabs/utils'),
{ getMostRecentBrowserWindow,
windows, getFocusedWindow, getFocusedElement } = require("./window/utils"),
winUtils = require("./window/utils"),
events = require("./system/events");

// The selection types
Expand Down Expand Up @@ -101,7 +101,10 @@ const selectionListener = {
*/
function iterator() {
let selection = getSelection(DOM);
let count = selection.rangeCount || (getElementWithSelection() ? 1 : 0);
let count = 0;

if (selection)
count = selection.rangeCount || (getElementWithSelection() ? 1 : 0);

for (let i = 0; i < count; i++) {
let sel = Selection(i);
Expand All @@ -116,6 +119,33 @@ const selectionIterator = obscure({
iterator: iterator // for....of
});

/**
* Returns the most recent focused window.
* if private browsing window is most recent and not supported,
* then ignore it and return `null`, because the focused window
* can't be targeted.
*/
function getFocusedWindow() {
let window = winUtils.getFocusedWindow();

return ignoreWindow(window) ? null : window;
}

/**
* Returns the focused element in the most recent focused window
* if private browsing window is most recent and not supported,
* then ignore it and return `null`, because the focused element
* can't be targeted.
*/
function getFocusedElement() {
let element = winUtils.getFocusedElement();

if (!element || ignoreWindow(element.ownerDocument.defaultView))
return null;

return element;
}

/**
* Returns the current selection from most recent content window. Depending on
* the specified |type|, the value returned can be a string of text, stringified
Expand Down Expand Up @@ -348,10 +378,11 @@ function removeSelectionListener(window) {
function onContent(event) {
let window = event.subject.defaultView;

// We are not interested in documents without valid defaultView (e.g. XML), or
// not in a tab (e.g. Panel).
if (window && getTabForContentWindow(window))
// We are not interested in documents without valid defaultView (e.g. XML)
// that aren't in a tab (e.g. Panel); or in private windows
if (window && getTabForContentWindow(window) && !ignoreWindow(window)) {
addSelectionListener(window);
}
}

// Adds Selection listener to new documents
Expand Down
8 changes: 7 additions & 1 deletion lib/sdk/test/assert.js
Expand Up @@ -60,8 +60,14 @@ AssertionError.prototype = Object.create(Error.prototype, {
exports.AssertionError = AssertionError;

function Assert(logger) {
return Object.create(Assert.prototype, { _log: { value: logger }});
let assert = Object.create(Assert.prototype, { _log: { value: logger }});

assert.fail = assert.fail.bind(assert);
assert.pass = assert.pass.bind(assert);

return assert;
}

Assert.prototype = {
fail: function fail(e) {
if (!e || typeof(e) !== 'object') {
Expand Down
12 changes: 8 additions & 4 deletions lib/sdk/window/utils.js
Expand Up @@ -174,13 +174,15 @@ function open(uri, options) {
options = options || {};
let newWindow = windowWatcher.
openWindow(options.parent || null,
uri,
uri || URI_BROWSER,
options.name || null,
serializeFeatures(options.features || {}),
options.args || null);

return newWindow;
}


exports.open = open;

function onFocus(window) {
Expand Down Expand Up @@ -225,7 +227,7 @@ function isFocused(window) {
*/
function openDialog(options) {
options = options || {};

let features = options.features || FEATURES;
if (!!options.private &&
!array.has(features.toLowerCase().split(','), 'private')) {
Expand Down Expand Up @@ -306,7 +308,8 @@ exports.isXULBrowser = isXULBrowser;
* Returns the most recent focused window
*/
function getFocusedWindow() {
let window = getMostRecentBrowserWindow();
let window = WM.getMostRecentWindow(BROWSER);

return window ? window.document.commandDispatcher.focusedWindow : null;
}
exports.getFocusedWindow = getFocusedWindow;
Expand All @@ -315,7 +318,8 @@ exports.getFocusedWindow = getFocusedWindow;
* Returns the focused element in the most recent focused window
*/
function getFocusedElement() {
let window = getMostRecentBrowserWindow();
let window = WM.getMostRecentWindow(BROWSER);

return window ? window.document.commandDispatcher.focusedElement : null;
}
exports.getFocusedElement = getFocusedElement;
Expand Down
15 changes: 9 additions & 6 deletions test/addons/private-browsing-supported/main.js
Expand Up @@ -34,14 +34,14 @@ exports.testGetOwnerWindow = function(assert, done) {
onOpen: function(tab) {
// test that getOwnerWindow works as expected
if (is('Fennec')) {
assert.notStrictEqual(chromeWindow, getOwnerWindow(tab));
assert.ok(getOwnerWindow(tab) instanceof Ci.nsIDOMWindow);
assert.notStrictEqual(chromeWindow, getOwnerWindow(tab));
assert.ok(getOwnerWindow(tab) instanceof Ci.nsIDOMWindow);
}
else {
if (isWindowPBSupported) {
assert.notStrictEqual(chromeWindow,
getOwnerWindow(tab),
'associated window is not the same for window and window\'s tab');
'associated window is not the same for window and window\'s tab');
}
else {
assert.strictEqual(chromeWindow,
Expand Down Expand Up @@ -164,8 +164,11 @@ if (!is('Fennec')) {
}
}

merge(module.exports, require('./windows'));
merge(module.exports, require('./tabs'));
merge(module.exports, require('./page-mod'));
merge(module.exports,
require('./test-windows'),
require('./test-tabs'),
require('./test-page-mod'),
require('./test-selection')
);

require('sdk/test/runner').runTestsFromModule(module);
Expand Up @@ -45,9 +45,14 @@ exports["test page-mod on private tab"] = function (assert, done) {
// tab)
let setPrivate = isTabPBSupported || isWindowPBSupported;

let id = Date.now().toString(36);
let frameUri = "data:text/html;charset=utf-8," + id;
let uri = "data:text/html;charset=utf-8," +
encodeURIComponent(id + "<iframe src='" + frameUri + "'><iframe>");

let count = 0;
let pageMod = new PageMod({
include: "data:*",
include: [uri, frameUri],
onAttach: function(worker) {
assert.ok(worker.tab.url == uri || worker.tab.url == frameUri,
"Got a worker attached to the private window tab");
Expand All @@ -72,9 +77,6 @@ exports["test page-mod on private tab"] = function (assert, done) {
}
});

let frameUri = "data:text/html;charset=utf-8,frame";
let uri = "data:text/html;charset=utf-8," +
encodeURIComponent("document<iframe src=\"" + frameUri + "\" />");
let page = openWebpage(uri, setPrivate);
}

Expand Down

0 comments on commit 5a5276e

Please sign in to comment.