Bug 961529 - Add BrowserUtils module (r=felipe)

This commit is contained in:
Bill McCloskey 2014-02-08 17:40:27 -08:00
Родитель eceb0a5ba0
Коммит 6fd99b1808
3 изменённых файлов: 73 добавлений и 44 удалений

Просмотреть файл

@ -5,6 +5,8 @@
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "BrowserUtils",
"resource://gre/modules/BrowserUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Downloads", XPCOMUtils.defineLazyModuleGetter(this, "Downloads",
"resource://gre/modules/Downloads.jsm"); "resource://gre/modules/Downloads.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "DownloadLastDir", XPCOMUtils.defineLazyModuleGetter(this, "DownloadLastDir",
@ -35,42 +37,9 @@ var ContentAreaUtils = {
} }
} }
/**
* urlSecurityCheck: JavaScript wrapper for checkLoadURIWithPrincipal
* and checkLoadURIStrWithPrincipal.
* If |aPrincipal| is not allowed to link to |aURL|, this function throws with
* an error message.
*
* @param aURL
* The URL a page has linked to. This could be passed either as a string
* or as a nsIURI object.
* @param aPrincipal
* The principal of the document from which aURL came.
* @param aFlags
* Flags to be passed to checkLoadURIStr. If undefined,
* nsIScriptSecurityManager.STANDARD will be passed.
*/
function urlSecurityCheck(aURL, aPrincipal, aFlags) function urlSecurityCheck(aURL, aPrincipal, aFlags)
{ {
var secMan = Services.scriptSecurityManager; return BrowserUtils.urlSecurityCheck(aURL, aPrincipal, aFlags);
if (aFlags === undefined) {
aFlags = secMan.STANDARD;
}
try {
if (aURL instanceof Components.interfaces.nsIURI)
secMan.checkLoadURIWithPrincipal(aPrincipal, aURL, aFlags);
else
secMan.checkLoadURIStrWithPrincipal(aPrincipal, aURL, aFlags);
} catch (e) {
let principalStr = "";
try {
principalStr = " from " + aPrincipal.URI.spec;
}
catch(e2) { }
throw "Load of " + aURL + principalStr + " denied.";
}
} }
/** /**
@ -84,7 +53,6 @@ function isContentFrame(aFocusedWindow)
return (aFocusedWindow.top == window.content); return (aFocusedWindow.top == window.content);
} }
// Clientele: (Make sure you don't break any of these) // Clientele: (Make sure you don't break any of these)
// - File -> Save Page/Frame As... // - File -> Save Page/Frame As...
// - Context -> Save Page/Frame As... // - Context -> Save Page/Frame As...
@ -837,21 +805,14 @@ function makeWebBrowserPersist()
return Components.classes[persistContractID].createInstance(persistIID); return Components.classes[persistContractID].createInstance(persistIID);
} }
/**
* Constructs a new URI, using nsIIOService.
* @param aURL The URI spec.
* @param aOriginCharset The charset of the URI.
* @param aBaseURI Base URI to resolve aURL, or null.
* @return an nsIURI object based on aURL.
*/
function makeURI(aURL, aOriginCharset, aBaseURI) function makeURI(aURL, aOriginCharset, aBaseURI)
{ {
return Services.io.newURI(aURL, aOriginCharset, aBaseURI); return BrowserUtils.makeURI(aURL, aOriginCharset, aBaseURI);
} }
function makeFileURI(aFile) function makeFileURI(aFile)
{ {
return Services.io.newFileURI(aFile); return BrowserUtils.makeFileURI(aFile);
} }
function makeFilePicker() function makeFilePicker()

Просмотреть файл

@ -0,0 +1,67 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
this.EXPORTED_SYMBOLS = [ "BrowserUtils" ];
const {interfaces: Ci, utils: Cu} = Components;
Cu.import("resource://gre/modules/Services.jsm");
this.BrowserUtils = {
/**
* urlSecurityCheck: JavaScript wrapper for checkLoadURIWithPrincipal
* and checkLoadURIStrWithPrincipal.
* If |aPrincipal| is not allowed to link to |aURL|, this function throws with
* an error message.
*
* @param aURL
* The URL a page has linked to. This could be passed either as a string
* or as a nsIURI object.
* @param aPrincipal
* The principal of the document from which aURL came.
* @param aFlags
* Flags to be passed to checkLoadURIStr. If undefined,
* nsIScriptSecurityManager.STANDARD will be passed.
*/
urlSecurityCheck: function(aURL, aPrincipal, aFlags) {
var secMan = Services.scriptSecurityManager;
if (aFlags === undefined) {
aFlags = secMan.STANDARD;
}
try {
if (aURL instanceof Ci.nsIURI)
secMan.checkLoadURIWithPrincipal(aPrincipal, aURL, aFlags);
else
secMan.checkLoadURIStrWithPrincipal(aPrincipal, aURL, aFlags);
} catch (e) {
let principalStr = "";
try {
principalStr = " from " + aPrincipal.URI.spec;
}
catch(e2) { }
throw "Load of " + aURL + principalStr + " denied.";
}
},
/**
* Constructs a new URI, using nsIIOService.
* @param aURL The URI spec.
* @param aOriginCharset The charset of the URI.
* @param aBaseURI Base URI to resolve aURL, or null.
* @return an nsIURI object based on aURL.
*/
makeURI: function(aURL, aOriginCharset, aBaseURI) {
return Services.io.newURI(aURL, aOriginCharset, aBaseURI);
},
makeFileURI: function(aFile) {
return Services.io.newFileURI(aFile);
},
};

Просмотреть файл

@ -11,6 +11,7 @@ MOCHITEST_CHROME_MANIFESTS += ['tests/chrome/chrome.ini']
EXTRA_JS_MODULES += [ EXTRA_JS_MODULES += [
'AsyncShutdown.jsm', 'AsyncShutdown.jsm',
'BrowserUtils.jsm',
'CharsetMenu.jsm', 'CharsetMenu.jsm',
'debug.js', 'debug.js',
'DeferredTask.jsm', 'DeferredTask.jsm',