Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
Bug 1195735, r=zer0, a=sylvestre
Browse files Browse the repository at this point in the history
--HG--
extra : source : dcf488b739183e1f256e4ecdc55f432c6c323c43
  • Loading branch information
gijsk committed Oct 1, 2015
1 parent 16e3007 commit 455fbae
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
29 changes: 27 additions & 2 deletions addon-sdk/source/lib/sdk/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const { contract } = require("./util/contract");
const { on, off, emit, setListeners } = require("./event/core");
const { EventTarget } = require("./event/target");
const domPanel = require("./panel/utils");
const { getDocShell } = require('./frame/utils');
const { events } = require("./panel/events");
const systemEvents = require("./system/events");
const { filter, pipe, stripListeners } = require("./event/utils");
Expand Down Expand Up @@ -73,9 +74,26 @@ let panelContract = contract(merge({
contentStyleFile: merge(Object.create(loaderContract.rules.contentScriptFile), {
msg: 'The `contentStyleFile` option must be a local URL or an array of URLs'
}),
contextMenu: boolean
contextMenu: boolean,
allow: {
is: ['object', 'undefined', 'null'],
map: function (allow) { return { script: !allow || allow.script !== false }}
},
}, displayContract.rules, loaderContract.rules));

function Allow(panel) {
return {
get script() { return getDocShell(viewFor(panel).backgroundFrame).allowJavascript; },
set script(value) { return setScriptState(panel, value); },
};
}

function setScriptState(panel, value) {
let view = viewFor(panel);
getDocShell(view.backgroundFrame).allowJavascript = value;
getDocShell(view.viewFrame).allowJavascript = value;
view.setAttribute("sdkscriptenabled", "" + value);
}

function isDisposed(panel) !views.has(panel);

Expand Down Expand Up @@ -147,7 +165,8 @@ const Panel = Class({
}

// Setup view
let view = domPanel.make();
let viewOptions = {allowJavascript: !model.allow || (model.allow.script !== false)};
let view = domPanel.make(null, viewOptions);
panels.set(view, this);
views.set(this, view);

Expand Down Expand Up @@ -212,6 +231,12 @@ const Panel = Class({
workerFor(this).detach();
},

get allow() { return Allow(this); },
set allow(value) {
let allowJavascript = panelContract({ allow: value }).allow.script;
return setScriptState(this, value);
},

/* Public API: Panel.isShowing */
get isShowing() !isDisposed(this) && domPanel.isOpen(viewFor(this)),

Expand Down
24 changes: 18 additions & 6 deletions addon-sdk/source/lib/sdk/panel/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const { platform } = require("../system");
const { getMostRecentBrowserWindow, getOwnerBrowserWindow,
getHiddenWindow, getScreenPixelsPerCSSPixel } = require("../window/utils");

const { create: createFrame, swapFrameLoaders } = require("../frame/utils");
const { create: createFrame, swapFrameLoaders, getDocShell } = require("../frame/utils");
const { window: addonWindow } = require("../addon/window");
const { isNil } = require("../lang/type");
const { data } = require('../self');
Expand Down Expand Up @@ -233,10 +233,11 @@ function setupPanelFrame(frame) {
}
}

function make(document) {
function make(document, options) {
document = document || getMostRecentBrowserWindow().document;
let panel = document.createElementNS(XUL_NS, "panel");
panel.setAttribute("type", "arrow");
panel.setAttribute("sdkscriptenabled", "" + options.allowJavascript);

// Note that panel is a parent of `viewFrame` who's `docShell` will be
// configured at creation time. If `panel` and there for `viewFrame` won't
Expand All @@ -245,7 +246,7 @@ function make(document) {
attach(panel, document);

let frameOptions = {
allowJavascript: true,
allowJavascript: options.allowJavascript,
allowPlugins: true,
allowAuth: true,
allowWindowControl: false,
Expand All @@ -270,8 +271,16 @@ function make(document) {
// See Bug 886329
if (target !== this) return;

try { swapFrameLoaders(backgroundFrame, viewFrame); }
catch(error) { console.exception(error); }
try {
swapFrameLoaders(backgroundFrame, viewFrame);
// We need to re-set this because... swapFrameLoaders. Or something.
let shouldEnableScript = panel.getAttribute("sdkscriptenabled") == "true";
getDocShell(backgroundFrame).allowJavascript = shouldEnableScript;
getDocShell(viewFrame).allowJavascript = shouldEnableScript;
}
catch(error) {
console.exception(error);
}
events.emit(type, { subject: panel });
}

Expand Down Expand Up @@ -315,6 +324,7 @@ function make(document) {


panel.backgroundFrame = backgroundFrame;
panel.viewFrame = viewFrame;

// Store event listener on the panel instance so that it won't be GC-ed
// while panel is alive.
Expand All @@ -340,8 +350,10 @@ function detach(panel) {
exports.detach = detach;

function dispose(panel) {
panel.backgroundFrame.parentNode.removeChild(panel.backgroundFrame);
panel.backgroundFrame.remove();
panel.viewFrame.remove();
panel.backgroundFrame = null;
panel.viewFrame = null;
events.off("document-element-inserted", panel.onContentChange);
panel.onContentChange = null;
detach(panel);
Expand Down

0 comments on commit 455fbae

Please sign in to comment.