toolkit/modules/BrowserUtils.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/modules/BrowserUtils.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,115 @@
     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 +this.EXPORTED_SYMBOLS = [ "BrowserUtils" ];
    1.11 +
    1.12 +const {interfaces: Ci, utils: Cu, classes: Cc} = Components;
    1.13 +
    1.14 +Cu.import("resource://gre/modules/Services.jsm");
    1.15 +
    1.16 +this.BrowserUtils = {
    1.17 +
    1.18 +  /**
    1.19 +   * urlSecurityCheck: JavaScript wrapper for checkLoadURIWithPrincipal
    1.20 +   * and checkLoadURIStrWithPrincipal.
    1.21 +   * If |aPrincipal| is not allowed to link to |aURL|, this function throws with
    1.22 +   * an error message.
    1.23 +   *
    1.24 +   * @param aURL
    1.25 +   *        The URL a page has linked to. This could be passed either as a string
    1.26 +   *        or as a nsIURI object.
    1.27 +   * @param aPrincipal
    1.28 +   *        The principal of the document from which aURL came.
    1.29 +   * @param aFlags
    1.30 +   *        Flags to be passed to checkLoadURIStr. If undefined,
    1.31 +   *        nsIScriptSecurityManager.STANDARD will be passed.
    1.32 +   */
    1.33 +  urlSecurityCheck: function(aURL, aPrincipal, aFlags) {
    1.34 +    var secMan = Services.scriptSecurityManager;
    1.35 +    if (aFlags === undefined) {
    1.36 +      aFlags = secMan.STANDARD;
    1.37 +    }
    1.38 +
    1.39 +    try {
    1.40 +      if (aURL instanceof Ci.nsIURI)
    1.41 +        secMan.checkLoadURIWithPrincipal(aPrincipal, aURL, aFlags);
    1.42 +      else
    1.43 +        secMan.checkLoadURIStrWithPrincipal(aPrincipal, aURL, aFlags);
    1.44 +    } catch (e) {
    1.45 +      let principalStr = "";
    1.46 +      try {
    1.47 +        principalStr = " from " + aPrincipal.URI.spec;
    1.48 +      }
    1.49 +      catch(e2) { }
    1.50 +
    1.51 +      throw "Load of " + aURL + principalStr + " denied.";
    1.52 +    }
    1.53 +  },
    1.54 +
    1.55 +  /**
    1.56 +   * Constructs a new URI, using nsIIOService.
    1.57 +   * @param aURL The URI spec.
    1.58 +   * @param aOriginCharset The charset of the URI.
    1.59 +   * @param aBaseURI Base URI to resolve aURL, or null.
    1.60 +   * @return an nsIURI object based on aURL.
    1.61 +   */
    1.62 +  makeURI: function(aURL, aOriginCharset, aBaseURI) {
    1.63 +    return Services.io.newURI(aURL, aOriginCharset, aBaseURI);
    1.64 +  },
    1.65 +
    1.66 +  makeFileURI: function(aFile) {
    1.67 +    return Services.io.newFileURI(aFile);
    1.68 +  },
    1.69 +
    1.70 +  /**
    1.71 +   * Return the current focus element and window. If the current focus
    1.72 +   * is in a content process, then this function returns CPOWs
    1.73 +   * (cross-process object wrappers) that refer to the focused
    1.74 +   * items. Note that calling this function synchronously contacts the
    1.75 +   * content process, which may block for a long time.
    1.76 +   *
    1.77 +   * @param document The document in question.
    1.78 +   * @return [focusedElement, focusedWindow]
    1.79 +   */
    1.80 +  getFocusSync: function(document) {
    1.81 +    let elt = document.commandDispatcher.focusedElement;
    1.82 +    var window = document.commandDispatcher.focusedWindow;
    1.83 +
    1.84 +    const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
    1.85 +    if (elt instanceof window.XULElement &&
    1.86 +        elt.localName == "browser" &&
    1.87 +        elt.namespaceURI == XUL_NS &&
    1.88 +        elt.getAttribute("remote")) {
    1.89 +      [elt, window] = elt.syncHandler.getFocusedElementAndWindow();
    1.90 +    }
    1.91 +
    1.92 +    return [elt, window];
    1.93 +  },
    1.94 +
    1.95 +  /**
    1.96 +   * For a given DOM element, returns its position in "screen"
    1.97 +   * coordinates. In a content process, the coordinates returned will
    1.98 +   * be relative to the left/top of the tab. In the chrome process,
    1.99 +   * the coordinates are relative to the user's screen.
   1.100 +   */
   1.101 +  getElementBoundingScreenRect: function(aElement) {
   1.102 +    let rect = aElement.getBoundingClientRect();
   1.103 +    let window = aElement.ownerDocument.defaultView;
   1.104 +
   1.105 +    // We need to compensate for any iframes that might shift things
   1.106 +    // over. We also need to compensate for zooming.
   1.107 +    let fullZoom = window.getInterface(Ci.nsIDOMWindowUtils).fullZoom;
   1.108 +    rect = {
   1.109 +      left: (rect.left + window.mozInnerScreenX) * fullZoom,
   1.110 +      top: (rect.top + window.mozInnerScreenY) * fullZoom,
   1.111 +      width: rect.width * fullZoom,
   1.112 +      height: rect.height * fullZoom
   1.113 +    };
   1.114 +
   1.115 +    return rect;
   1.116 +  },
   1.117 +
   1.118 +};

mercurial