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

Commit

Permalink
Merge pull request #858 from erikvold/851161
Browse files Browse the repository at this point in the history
Fix Bug 851161 preventing panels from being used with per-window private browsing r=@Gozala(cherry picked from commit 707eca7)
  • Loading branch information
erikvold committed Mar 15, 2013
1 parent c1192a1 commit 2f4a772
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 56 deletions.
8 changes: 7 additions & 1 deletion lib/sdk/content/symbiont.js
Expand Up @@ -15,6 +15,7 @@ const hiddenFrames = require('../frame/hidden-frame');
const observers = require('../deprecated/observer-service');
const unload = require('../system/unload');
const { getDocShell } = require("../frame/utils");
const { ignoreWindow } = require('../private-browsing/utils');

const assetsURI = require('../self').data.url();

Expand Down Expand Up @@ -107,6 +108,7 @@ const Symbiont = Worker.resolve({
this._unregisterListener();

this._frame = frame;

getDocShell(frame).allowJavascript = this.allow.script;
frame.setAttribute("src", this._contentURL);

Expand Down Expand Up @@ -142,8 +144,12 @@ const Symbiont = Worker.resolve({
this._loadEvent = 'start';
observers.add('document-element-inserted',
this._loadListener = function onStart(doc) {

let window = doc.defaultView;

if (ignoreWindow(window)) {
return;
}

if (window && window == frame.contentWindow) {
self._unregisterListener();
self._onInit();
Expand Down
67 changes: 20 additions & 47 deletions lib/sdk/panel.js
@@ -1,7 +1,6 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

// The panel module currently supports only Firefox.
Expand All @@ -13,24 +12,26 @@ module.metadata = {
}
};

const { Cc, Ci } = require("chrome");

const { Ci } = require("chrome");
const { validateOptions: valid } = require('./deprecated/api-utils');
const { Symbiont } = require('./content/content');
const { EventEmitter } = require('./deprecated/events');
const timer = require('./timers');
const { setTimeout } = require('./timers');
const runtime = require('./system/runtime');
const { getMostRecentBrowserWindow } = require('./window/utils');
const { getDocShell } = require("./frame/utils");

const windowMediator = Cc['@mozilla.org/appshell/window-mediator;1'].
getService(Ci.nsIWindowMediator);
const { getWindow } = require('./panel/window');
const { isPrivateBrowsingSupported } = require('./self');
const { isWindowPBSupported } = require('./private-browsing/utils');

const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
ON_SHOW = 'popupshown',
ON_HIDE = 'popuphidden',
validNumber = { is: ['number', 'undefined', 'null'] };

if (isPrivateBrowsingSupported && isWindowPBSupported) {
throw Error('The panel module cannot be used with per-window private browsing at the moment, see Bug 816257');
}

/**
* Emits show and hide events.
*/
Expand Down Expand Up @@ -115,7 +116,15 @@ const Panel = Symbiont.resolve({
/* Public API: Panel.show */
show: function show(anchor) {
anchor = anchor || null;
let document = getWindow(anchor).document;
let anchorWindow = getWindow(anchor);

// If there is no open window, or the anchor is in a private window
// then we will not be able to display the panel
if (!anchorWindow) {
return;
}

let document = anchorWindow.document;
let xulPanel = this._xulPanel;
if (!xulPanel) {
xulPanel = this._xulPanel = document.createElementNS(XUL_NS, 'panel');
Expand Down Expand Up @@ -143,6 +152,7 @@ const Panel = Symbiont.resolve({
frame.setAttribute('type', 'content');
frame.setAttribute('flex', '1');
frame.setAttribute('transparent', 'transparent');

if (runtime.OS === "Darwin") {
frame.style.borderRadius = "6px";
frame.style.padding = "1px";
Expand Down Expand Up @@ -203,7 +213,7 @@ const Panel = Symbiont.resolve({
// Wait for the XBL binding to be constructed
function waitForBinding() {
if (!xulPanel.openPopup) {
timer.setTimeout(waitForBinding, 50);
setTimeout(waitForBinding, 50);
return;
}
xulPanel.openPopup(anchor, position, x, y);
Expand Down Expand Up @@ -363,40 +373,3 @@ const Panel = Symbiont.resolve({
});
exports.Panel = function(options) Panel(options)
exports.Panel.prototype = Panel.prototype;

function getWindow(anchor) {
let window;

if (anchor) {
let anchorWindow = anchor.ownerDocument.defaultView.top;
let anchorDocument = anchorWindow.document;

let enumerator = windowMediator.getEnumerator("navigator:browser");
while (enumerator.hasMoreElements()) {
let enumWindow = enumerator.getNext();

// Check if the anchor is in this browser window.
if (enumWindow == anchorWindow) {
window = anchorWindow;
break;
}

// Check if the anchor is in a browser tab in this browser window.
let browser = enumWindow.gBrowser.getBrowserForDocument(anchorDocument);
if (browser) {
window = enumWindow;
break;
}

// Look in other subdocuments (sidebar, etc.)?
}
}

// If we didn't find the anchor's window (or we have no anchor),
// return the most recent browser window.
if (!window)
window = getMostRecentBrowserWindow();

return window;
}

51 changes: 51 additions & 0 deletions lib/sdk/panel/window.js
@@ -0,0 +1,51 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';

const { getMostRecentBrowserWindow, windows: getWindows } = require('../window/utils');
const { ignoreWindow } = require('../private-browsing/utils');
const { isPrivateBrowsingSupported } = require('../self');

function getWindow(anchor) {
let window;
let windows = getWindows("navigator:browser", {
includePrivate: isPrivateBrowsingSupported
});

if (anchor) {
let anchorWindow = anchor.ownerDocument.defaultView.top;
let anchorDocument = anchorWindow.document;

// loop thru supported windows
for each(let enumWindow in windows) {
// Check if the anchor is in this browser window.
if (enumWindow == anchorWindow) {
window = anchorWindow;
break;
}

// Check if the anchor is in a browser tab in this browser window.
let browser = enumWindow.gBrowser.getBrowserForDocument(anchorDocument);
if (browser) {
window = enumWindow;
break;
}

// Look in other subdocuments (sidebar, etc.)?
}
}

// If we didn't find the anchor's window (or we have no anchor),
// return the most recent browser window.
if (!window)
window = getMostRecentBrowserWindow();

// if the window is not supported, then it should be ignored
if (ignoreWindow(window)) {
return null;
}

return window;
}
exports.getWindow = getWindow;
8 changes: 4 additions & 4 deletions lib/sdk/window/utils.js
Expand Up @@ -175,19 +175,19 @@ function open(uri, options) {
exports.open = open;

function onFocus(window) {
let deferred = defer();
let { resolve, promise } = defer();

if (isFocused(window)) {
deferred.resolve(window);
resolve(window);
}
else {
window.addEventListener("focus", function focusListener() {
window.removeEventListener("focus", focusListener, true);
deferred.resolve(window);
resolve(window);
}, true);
}

return deferred.promise;
return promise;
}
exports.onFocus = onFocus;

Expand Down
3 changes: 2 additions & 1 deletion test/addons/private-browsing-supported/main.js
Expand Up @@ -168,7 +168,8 @@ merge(module.exports,
require('./test-windows'),
require('./test-tabs'),
require('./test-page-mod'),
require('./test-selection')
require('./test-selection'),
require('./test-panel')
);

require('sdk/test/runner').runTestsFromModule(module);
16 changes: 16 additions & 0 deletions test/addons/private-browsing-supported/test-panel.js
@@ -0,0 +1,16 @@
'use strict';

const { isWindowPBSupported } = require('sdk/private-browsing/utils');

if (isWindowPBSupported) {
exports.testRequirePanel = function (assert) {
try {
require('panel');
}
catch(e) {
assert.ok(e.message.match(/Bug 816257/), 'Bug 816257 is mentioned');
return;
}
assert.fail('the panel module should throw an error');
}
}
2 changes: 2 additions & 0 deletions test/addons/private-browsing-supported/test-tabs.js
@@ -1,3 +1,5 @@
'use strict';

const tabs = require('sdk/tabs');
const { is } = require('sdk/system/xul-app');
const { isPrivate } = require('sdk/private-browsing');
Expand Down

0 comments on commit 2f4a772

Please sign in to comment.