addon-sdk/source/lib/sdk/frame/utils.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/lib/sdk/frame/utils.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,100 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +'use strict';
     1.9 +
    1.10 +module.metadata = {
    1.11 +  "stability": "experimental"
    1.12 +};
    1.13 +
    1.14 +const { Ci } = require("chrome");
    1.15 +const XUL = 'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul';
    1.16 +
    1.17 +function eventTarget(frame) {
    1.18 +  return getDocShell(frame).chromeEventHandler;
    1.19 +}
    1.20 +exports.eventTarget = eventTarget;
    1.21 +
    1.22 +function getDocShell(frame) {
    1.23 +  let { frameLoader } = frame.QueryInterface(Ci.nsIFrameLoaderOwner);
    1.24 +  return frameLoader && frameLoader.docShell;
    1.25 +}
    1.26 +exports.getDocShell = getDocShell;
    1.27 +
    1.28 +/**
    1.29 + * Creates a XUL `browser` element in a privileged document.
    1.30 + * @params {nsIDOMDocument} document
    1.31 + * @params {String} options.type
    1.32 + *    By default is 'content' for possible values see:
    1.33 + *    https://developer.mozilla.org/en/XUL/iframe#a-browser.type
    1.34 + * @params {String} options.uri
    1.35 + *    URI of the document to be loaded into created frame.
    1.36 + * @params {Boolean} options.remote
    1.37 + *    If `true` separate process will be used for this frame, also in such
    1.38 + *    case all the following options are ignored.
    1.39 + * @params {Boolean} options.allowAuth
    1.40 + *    Whether to allow auth dialogs. Defaults to `false`.
    1.41 + * @params {Boolean} options.allowJavascript
    1.42 + *    Whether to allow Javascript execution. Defaults to `false`.
    1.43 + * @params {Boolean} options.allowPlugins
    1.44 + *    Whether to allow plugin execution. Defaults to `false`.
    1.45 + */
    1.46 +function create(target, options) {
    1.47 +  target = target instanceof Ci.nsIDOMDocument ? target.documentElement :
    1.48 +           target instanceof Ci.nsIDOMWindow ? target.document.documentElement :
    1.49 +           target;
    1.50 +  options = options || {};
    1.51 +  let remote = options.remote || false;
    1.52 +  let namespaceURI = options.namespaceURI || XUL;
    1.53 +  let isXUL = namespaceURI === XUL;
    1.54 +  let nodeName = isXUL && options.browser ? 'browser' : 'iframe';
    1.55 +  let document = target.ownerDocument;
    1.56 +
    1.57 +  let frame = document.createElementNS(namespaceURI, nodeName);
    1.58 +  // Type="content" is mandatory to enable stuff here:
    1.59 +  // http://mxr.mozilla.org/mozilla-central/source/content/base/src/nsFrameLoader.cpp#1776
    1.60 +  frame.setAttribute('type', options.type || 'content');
    1.61 +  frame.setAttribute('src', options.uri || 'about:blank');
    1.62 +
    1.63 +  target.appendChild(frame);
    1.64 +
    1.65 +  // Load in separate process if `options.remote` is `true`.
    1.66 +  // http://mxr.mozilla.org/mozilla-central/source/content/base/src/nsFrameLoader.cpp#1347
    1.67 +  if (remote) {
    1.68 +    if (isXUL) {
    1.69 +      // We remove XBL binding to avoid execution of code that is not going to
    1.70 +      // work because browser has no docShell attribute in remote mode
    1.71 +      // (for example)
    1.72 +      frame.setAttribute('style', '-moz-binding: none;');
    1.73 +      frame.setAttribute('remote', 'true');
    1.74 +    }
    1.75 +    else {
    1.76 +      frame.QueryInterface(Ci.nsIMozBrowserFrame);
    1.77 +      frame.createRemoteFrameLoader(null);
    1.78 +    }
    1.79 +  }
    1.80 +
    1.81 +
    1.82 +
    1.83 +  // If browser is remote it won't have a `docShell`.
    1.84 +  if (!remote) {
    1.85 +    let docShell = getDocShell(frame);
    1.86 +    docShell.allowAuth = options.allowAuth || false;
    1.87 +    docShell.allowJavascript = options.allowJavascript || false;
    1.88 +    docShell.allowPlugins = options.allowPlugins || false;
    1.89 +
    1.90 +    // Control whether the document can move/resize the window. Requires
    1.91 +    // recently added platform capability, so we test to avoid exceptions
    1.92 +    // in cases where capability is not present yet.
    1.93 +    if ("allowWindowControl" in docShell && "allowWindowControl" in options)
    1.94 +      docShell.allowWindowControl = !!options.allowWindowControl;
    1.95 +  }
    1.96 +
    1.97 +  return frame;
    1.98 +}
    1.99 +exports.create = create;
   1.100 +
   1.101 +function swapFrameLoaders(from, to)
   1.102 +  from.QueryInterface(Ci.nsIFrameLoaderOwner).swapFrameLoaders(to)
   1.103 +exports.swapFrameLoaders = swapFrameLoaders;

mercurial