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