toolkit/modules/BrowserUtils.jsm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 this.EXPORTED_SYMBOLS = [ "BrowserUtils" ];
michael@0 8
michael@0 9 const {interfaces: Ci, utils: Cu, classes: Cc} = Components;
michael@0 10
michael@0 11 Cu.import("resource://gre/modules/Services.jsm");
michael@0 12
michael@0 13 this.BrowserUtils = {
michael@0 14
michael@0 15 /**
michael@0 16 * urlSecurityCheck: JavaScript wrapper for checkLoadURIWithPrincipal
michael@0 17 * and checkLoadURIStrWithPrincipal.
michael@0 18 * If |aPrincipal| is not allowed to link to |aURL|, this function throws with
michael@0 19 * an error message.
michael@0 20 *
michael@0 21 * @param aURL
michael@0 22 * The URL a page has linked to. This could be passed either as a string
michael@0 23 * or as a nsIURI object.
michael@0 24 * @param aPrincipal
michael@0 25 * The principal of the document from which aURL came.
michael@0 26 * @param aFlags
michael@0 27 * Flags to be passed to checkLoadURIStr. If undefined,
michael@0 28 * nsIScriptSecurityManager.STANDARD will be passed.
michael@0 29 */
michael@0 30 urlSecurityCheck: function(aURL, aPrincipal, aFlags) {
michael@0 31 var secMan = Services.scriptSecurityManager;
michael@0 32 if (aFlags === undefined) {
michael@0 33 aFlags = secMan.STANDARD;
michael@0 34 }
michael@0 35
michael@0 36 try {
michael@0 37 if (aURL instanceof Ci.nsIURI)
michael@0 38 secMan.checkLoadURIWithPrincipal(aPrincipal, aURL, aFlags);
michael@0 39 else
michael@0 40 secMan.checkLoadURIStrWithPrincipal(aPrincipal, aURL, aFlags);
michael@0 41 } catch (e) {
michael@0 42 let principalStr = "";
michael@0 43 try {
michael@0 44 principalStr = " from " + aPrincipal.URI.spec;
michael@0 45 }
michael@0 46 catch(e2) { }
michael@0 47
michael@0 48 throw "Load of " + aURL + principalStr + " denied.";
michael@0 49 }
michael@0 50 },
michael@0 51
michael@0 52 /**
michael@0 53 * Constructs a new URI, using nsIIOService.
michael@0 54 * @param aURL The URI spec.
michael@0 55 * @param aOriginCharset The charset of the URI.
michael@0 56 * @param aBaseURI Base URI to resolve aURL, or null.
michael@0 57 * @return an nsIURI object based on aURL.
michael@0 58 */
michael@0 59 makeURI: function(aURL, aOriginCharset, aBaseURI) {
michael@0 60 return Services.io.newURI(aURL, aOriginCharset, aBaseURI);
michael@0 61 },
michael@0 62
michael@0 63 makeFileURI: function(aFile) {
michael@0 64 return Services.io.newFileURI(aFile);
michael@0 65 },
michael@0 66
michael@0 67 /**
michael@0 68 * Return the current focus element and window. If the current focus
michael@0 69 * is in a content process, then this function returns CPOWs
michael@0 70 * (cross-process object wrappers) that refer to the focused
michael@0 71 * items. Note that calling this function synchronously contacts the
michael@0 72 * content process, which may block for a long time.
michael@0 73 *
michael@0 74 * @param document The document in question.
michael@0 75 * @return [focusedElement, focusedWindow]
michael@0 76 */
michael@0 77 getFocusSync: function(document) {
michael@0 78 let elt = document.commandDispatcher.focusedElement;
michael@0 79 var window = document.commandDispatcher.focusedWindow;
michael@0 80
michael@0 81 const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
michael@0 82 if (elt instanceof window.XULElement &&
michael@0 83 elt.localName == "browser" &&
michael@0 84 elt.namespaceURI == XUL_NS &&
michael@0 85 elt.getAttribute("remote")) {
michael@0 86 [elt, window] = elt.syncHandler.getFocusedElementAndWindow();
michael@0 87 }
michael@0 88
michael@0 89 return [elt, window];
michael@0 90 },
michael@0 91
michael@0 92 /**
michael@0 93 * For a given DOM element, returns its position in "screen"
michael@0 94 * coordinates. In a content process, the coordinates returned will
michael@0 95 * be relative to the left/top of the tab. In the chrome process,
michael@0 96 * the coordinates are relative to the user's screen.
michael@0 97 */
michael@0 98 getElementBoundingScreenRect: function(aElement) {
michael@0 99 let rect = aElement.getBoundingClientRect();
michael@0 100 let window = aElement.ownerDocument.defaultView;
michael@0 101
michael@0 102 // We need to compensate for any iframes that might shift things
michael@0 103 // over. We also need to compensate for zooming.
michael@0 104 let fullZoom = window.getInterface(Ci.nsIDOMWindowUtils).fullZoom;
michael@0 105 rect = {
michael@0 106 left: (rect.left + window.mozInnerScreenX) * fullZoom,
michael@0 107 top: (rect.top + window.mozInnerScreenY) * fullZoom,
michael@0 108 width: rect.width * fullZoom,
michael@0 109 height: rect.height * fullZoom
michael@0 110 };
michael@0 111
michael@0 112 return rect;
michael@0 113 },
michael@0 114
michael@0 115 };

mercurial