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 #668 from Gozala/cleanup/hidden-frame
Browse files Browse the repository at this point in the history
fix Bug 724632 - Rewrite hidden-frame in more conventional style r=@erikvold
  • Loading branch information
Gozala committed Dec 13, 2012
2 parents da55fc6 + 58aabad commit 234a142
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 134 deletions.
25 changes: 14 additions & 11 deletions doc/module-source/sdk/frame/hidden-frame.md
Expand Up @@ -59,6 +59,20 @@ Creates a hidden frame.
content the moment they are added.
</api>

<api name="element">
@property {DOMElement}
The host application frame in which the page is loaded.
</api>

<api name="ready">
@event

This event is emitted when the DOM for a hidden frame content is ready.
It is equivalent to the `DOMContentLoaded` event for the content page in
a hidden frame.
</api>
</api>

<api name="add">
@function
Register a hidden frame, preparing it to load content.
Expand All @@ -70,14 +84,3 @@ Register a hidden frame, preparing it to load content.
Unregister a hidden frame, unloading any content that was loaded in it.
@param hiddenFrame {HiddenFrame} the frame to remove
</api>

<api name="element">
@property {DOMElement}
The host application frame in which the page is loaded.
</api>

<api name="onReady">
@property {array}
Functions to call when the frame is ready to load content.
</api>
</api>
6 changes: 2 additions & 4 deletions lib/sdk/content/worker.js
Expand Up @@ -20,6 +20,7 @@ const { Cortex } = require('../deprecated/cortex');
const { sandbox, evaluate, load } = require("../loader/sandbox");
const { merge } = require('../util/object');
const xulApp = require("../system/xul-app");
const { getInnerId } = require("../window/utils")
const USE_JS_PROXIES = !xulApp.versionInRange(xulApp.platformVersion,
"17.0a2", "*");
const { getTabForWindow } = require('../tabs/helpers');
Expand Down Expand Up @@ -453,10 +454,7 @@ const Worker = EventEmitter.compose({
// We can't watch for unload event on page's window object as it
// prevents bfcache from working:
// https://developer.mozilla.org/En/Working_with_BFCache
this._windowID = this._window.
QueryInterface(Ci.nsIInterfaceRequestor).
getInterface(Ci.nsIDOMWindowUtils).
currentInnerWindowID;
this._windowID = getInnerId(this._window);
observers.add("inner-window-destroyed",
this._documentUnload = this._documentUnload.bind(this));

Expand Down
232 changes: 114 additions & 118 deletions lib/sdk/frame/hidden-frame.js
Expand Up @@ -10,18 +10,22 @@ module.metadata = {
"stability": "experimental"
};

const {Cc, Ci} = require("chrome");
const { Cc, Ci } = require("chrome");
const errors = require("../deprecated/errors");
const apiUtils = require("../deprecated/api-utils");
const timer = require("../timers");
const { Class } = require("../core/heritage");
const { List, addListItem, removeListItem } = require("../util/list");
const { EventTarget } = require("../event/target");
const { emit } = require("../event/core");
const { create: makeFrame } = require("./utils");
const { defer, resolve } = require("../core/promise");
const { when: unload } = require("../system/unload");
const { validateOptions, getTypeOf } = require("../deprecated/api-utils");

const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";

let hostFrame, hostDocument, hiddenWindow, isHostFrameReady = false;

let appShellService = Cc["@mozilla.org/appshell/appShellService;1"].
getService(Ci.nsIAppShellService);
hiddenWindow = appShellService.hiddenDOMWindow;

let hiddenWindow = appShellService.hiddenDOMWindow;

if (!hiddenWindow) {
throw new Error([
Expand All @@ -31,142 +35,134 @@ if (!hiddenWindow) {
].join(""));
}

// Check if we can use the hidden window itself to host our iframes.
// If it's not a suitable host, the hostFrame will be lazily created
// by the first HiddenFrame instance.
if (hiddenWindow.location.protocol == "chrome:" &&
(hiddenWindow.document.contentType == "application/vnd.mozilla.xul+xml" ||
hiddenWindow.document.contentType == "application/xhtml+xml")) {
hostFrame = hiddenWindow;
hostDocument = hiddenWindow.document;
isHostFrameReady = true;
}

function setHostFrameReady() {
hostDocument = hostFrame.contentDocument;
hostFrame.removeEventListener("DOMContentLoaded", setHostFrameReady, false);
isHostFrameReady = true;
}

// This cache is used to access friend properties between functions
// without exposing them on the public API.
let cache = [];

exports.HiddenFrame = apiUtils.publicConstructor(HiddenFrame);

function HiddenFrame(options) {
options = options || {};
let self = this;
let validOptions = apiUtils.validateOptions(options, {
onReady: {
is: ["undefined", "function", "array"],
ok: function(v) {
if (apiUtils.getTypeOf(v) === "array") {
// make sure every item is a function
return v.every(function (item) typeof(item) === "function")
}
return true;
}
},
onUnload: {
is: ["undefined", "function"]
let elements = new WeakMap();

function contentLoaded(target) {
var deferred = defer();
target.addEventListener("DOMContentLoaded", function DOMContentLoaded(event) {
// "DOMContentLoaded" events from nested frames propagate up to target,
// ignore events unless it's DOMContentLoaded for the given target.
if (event.target === target || event.target === target.contentDocument) {
target.removeEventListener("DOMContentLoaded", DOMContentLoaded, false);
deferred.resolve(target);
}
});
}, false);
return deferred.promise;
}

for (let key in validOptions) {
let val = validOptions[key];
if (typeof(val) != "undefined")
options[key] = val;
function makeHostFrame() {
// Check if we can use the hidden window itself to host our iframes.
// If it's not a suitable host, the hostFrame will be lazily created
// by the first HiddenFrame instance.
if (hiddenWindow.location.protocol == "chrome:" &&
(hiddenWindow.document.contentType == "application/vnd.mozilla.xul+xml" ||
hiddenWindow.document.contentType == "application/xhtml+xml")) {
return resolve(hiddenWindow)
}
else {
return contentLoaded(makeFrame(hiddenWindow.document, {
// Ugly ugly hack. This is the most lightweight "chrome:" file I could
// find on the tree.
// This hack should be removed by proper platform support on bug 565388
uri: "chrome://global/content/mozilla.xhtml",
nodeName: "iframe",
allowJavascript: true,
allowPlugins: true,
allowAuth: true
}));
}
}
var hostFrame = makeHostFrame();

require("../util/collection").addCollectionProperty(this, "onReady");
if (options.onReady)
this.onReady.add(options.onReady);
if (options.onUnload)
this.onUnload = options.onUnload;

if (!hostFrame) {
hostFrame = hiddenWindow.document.createElement("iframe");

// ugly ugly hack. This is the most lightweight chrome:// file I could find on the tree
// This hack should be removed by proper platform support on bug 565388
hostFrame.setAttribute("src", "chrome://global/content/mozilla.xhtml");
hostFrame.addEventListener("DOMContentLoaded", setHostFrameReady, false);
function FrameOptions(options) {
options = options || {}
return validateOptions(options, FrameOptions.validator);
}
FrameOptions.validator = {
onReady: {
is: ["undefined", "function", "array"],
ok: function(v) {
if (getTypeOf(v) === "array") {
// make sure every item is a function
return v.every(function (item) typeof(item) === "function")
}
return true;
}
},
onUnload: {
is: ["undefined", "function"]
}
};

hiddenWindow.document.body.appendChild(hostFrame);
var HiddenFrame = Class({
extends: EventTarget,
initialize: function initialize(options) {
options = FrameOptions(options);
EventTarget.prototype.initialize.call(this, options);
},
get element() {
return elements.get(this);
},
toString: function toString() {
return "[object Frame]"
}
});
exports.HiddenFrame = HiddenFrame

this.toString = function toString() "[object Frame]";
function isFrameCached(frame) {
// Function returns `true` if frame was already cached.
return cache.some(function(value) {
return value === frame
})
}

exports.add = function JP_SDK_Frame_add(frame) {
function addHidenFrame(frame) {
if (!(frame instanceof HiddenFrame))
throw new Error("The object to be added must be a HiddenFrame.");
throw Error("The object to be added must be a HiddenFrame.");

// This instance was already added.
if (cache.filter(function (v) v.frame === frame)[0])
return frame;

function createElement() {
hostFrame.removeEventListener("DOMContentLoaded", createElement, false);

let element = hostDocument.createElementNS(XUL_NS, "iframe");

element.setAttribute("type", "content");
hostDocument.documentElement.appendChild(element);

/* Public API: hiddenFrame.element */
frame.__defineGetter__("element", function () element);

// Notify consumers that the frame is ready.
function onReadyListener(event) {
element.removeEventListener("DOMContentLoaded", onReadyListener, false);
if (event.target == element.contentDocument) {
for (let handler in frame.onReady)
errors.catchAndLog(function () handler.call(frame))();
}
}
element.addEventListener("DOMContentLoaded", onReadyListener, false);

cache.push({
frame: frame,
element: element,
unload: function unload() {
// Call before removing to let a chance to avoid "dead object" exception
if (typeof frame.onUnload === "function")
frame.onUnload();
hostDocument.documentElement.removeChild(element);
}
if (isFrameCached(frame)) return frame;
else cache.push(frame);

hostFrame.then(function(hostFrame) {
let element = makeFrame(hostFrame.document, {
nodeName: "iframe",
type: "content",
allowJavascript: true,
allowPlugins: true,
allowAuth: true,
});
}

/* Begin element construction or schedule it for later */
if (isHostFrameReady) {
createElement();
} else {
hostFrame.addEventListener("DOMContentLoaded", createElement, false);
}
elements.set(frame, element);
return contentLoaded(element);
}).then(function onFrameReady(element) {
emit(frame, "ready");
}, console.exception);

return frame;
}
exports.add = addHidenFrame

exports.remove = function remove(frame) {
function removeHiddenFrame(frame) {
if (!(frame instanceof HiddenFrame))
throw new Error("The object to be removed must be a HiddenFrame.");
throw Error("The object to be removed must be a HiddenFrame.");

let entry = cache.filter(function (v) v.frame === frame)[0];
if (!entry)
return;
if (!isFrameCached(frame)) return;

// Remove from cache before calling in order to avoid loop
cache.splice(cache.indexOf(entry), 1);
entry.unload();
cache.splice(cache.indexOf(frame), 1);
emit(frame, "unload")
let element = frame.element
if (element) element.parentNode.removeChild(element)
}
exports.remove = removeHiddenFrame;

require("../system/unload").when(function () {
for each (let entry in cache.slice())
exports.remove(entry.frame);
unload(function () {
cache.splice(0).forEach(removeHiddenFrame);

if (hostFrame && hostFrame !== hiddenWindow)
hiddenWindow.document.body.removeChild(hostFrame);
hostFrame.then(function(frame) {
if (frame && frame !== hiddenWindow) frame.parentNode.removeChild(frame);
});
});
3 changes: 2 additions & 1 deletion lib/sdk/frame/utils.js
Expand Up @@ -31,8 +31,9 @@ const XUL = 'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul';
function create(document, options) {
options = options || {};
let remote = 'remote' in options && options.remote === true;
let nodeName = 'nodeName' in options && options.nodeName || 'browser';

let frame = document.createElementNS(XUL, 'browser');
let frame = document.createElementNS(XUL, nodeName);
// Type="content" is mandatory to enable stuff here:
// http://mxr.mozilla.org/mozilla-central/source/content/base/src/nsFrameLoader.cpp#1776
frame.setAttribute('type', options.type || 'content');
Expand Down

0 comments on commit 234a142

Please sign in to comment.