Change the URI argument to Components.utils.import to be a resource: URI. Bug380970, patch by Alex Vincent <ajvincent@gmail.com>, r=sayrer, sr=bsmedberg

This commit is contained in:
bzbarsky@mit.edu 2007-06-10 14:13:18 -07:00
parent 5f8019474d
commit 33a8653e15
6 changed files with 53 additions and 32 deletions

View File

@ -48,7 +48,7 @@ struct JSObject;
[ptr] native JSObjectPtr(JSObject);
[scriptable, uuid(bb66b661-4cdf-40d5-b4f6-0be2e195ef7c)]
[scriptable, uuid(89da3673-e699-4f26-9ed7-11a528011434)]
interface xpcIJSModuleLoader : nsISupports
{
/**
@ -63,11 +63,7 @@ interface xpcIJSModuleLoader : nsISupports
* targetObj, or, if 'targetObj' is not specified, on the caller's
* global object.
*
* @param registryLocation the string identifying the location of the
* module to import. For the format of the string, see
* nsIComponentManagerObsolete.registryLocationForSpec.
* Note that the format of this parameter is expected to change
* (see bug 380970).
* @param resourceURI A resource:// URI string to load the module from.
* @param targetObj the object to install the exported properties on.
* If this parameter is null or is a primitive value, this
* method throws an exception.
@ -81,7 +77,7 @@ interface xpcIJSModuleLoader : nsISupports
*
* (This comment is duplicated to nsIXPCComponents_Utils.)
*/
void /* JSObject */ import(in AUTF8String registryLocation
void /* JSObject */ import(in AUTF8String aResourceURI
/* , [optional] in JSObject targetObj */);
/**
@ -89,7 +85,7 @@ interface xpcIJSModuleLoader : nsISupports
* 'targetObj' (if != null) as described for importModule() and
* returns the module's global object.
*/
[noscript] JSObjectPtr importInto(in AUTF8String registryLocation,
[noscript] JSObjectPtr importInto(in AUTF8String aResourceURI,
in JSObjectPtr targetObj,
in nsIXPCNativeCallContext cc);
};

View File

@ -56,10 +56,8 @@ REQUIRES = xpcom \
CPPSRCS = mozJSComponentLoader.cpp mozJSSubScriptLoader.cpp
EXTRA_PP_COMPONENTS = XPCOMUtils.jsm
EXTRA_JS_MODULES = XPCOMUtils.jsm
include $(topsrcdir)/config/rules.mk
DEFINES += -DJSFILE -DJS_THREADSAFE

View File

@ -71,6 +71,7 @@
#ifndef XPCONNECT_STANDALONE
#include "nsIScriptSecurityManager.h"
#include "nsIURI.h"
#include "nsIFileURL.h"
#include "nsNetUtil.h"
#endif
#include "jsxdrapi.h"
@ -1389,17 +1390,26 @@ mozJSComponentLoader::ImportInto(const nsACString & aLocation,
NS_ENSURE_SUCCESS(rv, rv);
}
// Use the old component manager to find the nsIFile for
// aLocation. The needed method is still used in the new manager,
// just not exposed on an interface.
nsCOMPtr<nsIComponentManager> cm;
rv = NS_GetComponentManager(getter_AddRefs(cm));
nsCOMPtr<nsIIOService> ioService = do_GetIOService(&rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIComponentManagerObsolete> mgr = do_QueryInterface(cm, &rv);
nsCAutoString scheme;
rv = ioService->ExtractScheme(aLocation, scheme);
if (NS_FAILED(rv) ||
!scheme.EqualsLiteral("resource")) {
*_retval = nsnull;
return NS_ERROR_INVALID_ARG;
}
// Get the resource:// URI.
nsCOMPtr<nsIURI> resURI;
rv = ioService->NewURI(aLocation, nsnull, nsnull, getter_AddRefs(resURI));
nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(resURI, &rv);
NS_ENSURE_SUCCESS(rv, rv);
// Get the file belonging to it.
nsCOMPtr<nsIFile> file;
rv = mgr->SpecForRegistryLocation(PromiseFlatCString(aLocation).get(),
getter_AddRefs(file));
rv = fileURL->GetFile(getter_AddRefs(file));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsILocalFile> componentFile = do_QueryInterface(file, &rv);
NS_ENSURE_SUCCESS(rv, rv);

View File

@ -107,7 +107,7 @@ BarComponent.prototype =
flags: 0
};
Components.utils.import("rel:XPCOMUtils.jsm");
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
var NSGetModule = XPCOMUtils.generateNSGetModule([
{

View File

@ -37,12 +37,9 @@
* ***** END LICENSE BLOCK ***** */
function test_BrokenFile(path, shouldThrow, expectedName) {
var f = do_get_file(path, true);
var uri = "abs:" + f.path;
print(uri);
var didThrow;
var didThrow = false;
try {
Components.utils.import(uri);
Components.utils.import(path);
} catch (ex) {
var exceptionName = ex.name;
print("ex: " + ex + "; name = " + ex.name);
@ -55,7 +52,27 @@ function test_BrokenFile(path, shouldThrow, expectedName) {
}
function run_test() {
test_BrokenFile("js/src/xpconnect/tests/unit/bogus_exports_type.jsm", true, "Error");
test_BrokenFile("js/src/xpconnect/tests/unit/bogus_element_type.jsm", true, "Error");
test_BrokenFile("js/src/xpconnect/tests/unit/non_existing.jsm", true, "NS_ERROR_FILE_NOT_FOUND");
const C_i = Components.interfaces;
const ioService = Components.classes["@mozilla.org/network/io-service;1"]
.getService(C_i.nsIIOService);
const resProt = ioService.getProtocolHandler("resource")
.QueryInterface(C_i.nsIResProtocolHandler);
var curdir = do_get_file("js/src/xpconnect/tests/unit");
var curURI = ioService.newFileURI(curdir);
resProt.setSubstitution("test", curURI);
test_BrokenFile("resource://test/bogus_exports_type.jsm", true, "Error");
test_BrokenFile("resource://test/bogus_element_type.jsm", true, "Error");
test_BrokenFile("resource://test/non_existing.jsm",
true,
"NS_ERROR_FILE_NOT_FOUND");
test_BrokenFile("chrome://test/content/test.jsm",
true,
"NS_ERROR_ILLEGAL_VALUE");
resProt.setSubstitution("test", null);
}

View File

@ -39,19 +39,19 @@
function run_test() {
var scope = {};
Components.utils.import("rel:XPCOMUtils.jsm", scope);
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm", scope);
do_check_eq(typeof(scope.XPCOMUtils), "object");
do_check_eq(typeof(scope.XPCOMUtils.generateFactory), "function");
// try again on the global object
do_check_eq(typeof(Components.utils.import), "function");
Components.utils.import("rel:XPCOMUtils.jsm");
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
do_check_eq(typeof(XPCOMUtils), "object");
do_check_eq(typeof(XPCOMUtils.generateFactory), "function");
// try on a new object
var scope2 = {};
Components.utils.import("rel:XPCOMUtils.jsm", scope2);
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm", scope2);
do_check_eq(typeof(scope2.XPCOMUtils), "object");
do_check_eq(typeof(scope2.XPCOMUtils.generateFactory), "function");
@ -60,7 +60,7 @@ function run_test() {
// make sure we throw when the second arg is bogus
var didThrow = false;
try {
Components.utils.import("rel:XPCOMUtils.jsm", "wrong");
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm", "wrong");
} catch (ex) {
print("ex: " + ex);
didThrow = true;