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

View File

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

View File

@ -71,6 +71,7 @@
#ifndef XPCONNECT_STANDALONE #ifndef XPCONNECT_STANDALONE
#include "nsIScriptSecurityManager.h" #include "nsIScriptSecurityManager.h"
#include "nsIURI.h" #include "nsIURI.h"
#include "nsIFileURL.h"
#include "nsNetUtil.h" #include "nsNetUtil.h"
#endif #endif
#include "jsxdrapi.h" #include "jsxdrapi.h"
@ -1389,17 +1390,26 @@ mozJSComponentLoader::ImportInto(const nsACString & aLocation,
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
} }
// Use the old component manager to find the nsIFile for nsCOMPtr<nsIIOService> ioService = do_GetIOService(&rv);
// 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));
NS_ENSURE_SUCCESS(rv, 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); NS_ENSURE_SUCCESS(rv, rv);
// Get the file belonging to it.
nsCOMPtr<nsIFile> file; nsCOMPtr<nsIFile> file;
rv = mgr->SpecForRegistryLocation(PromiseFlatCString(aLocation).get(), rv = fileURL->GetFile(getter_AddRefs(file));
getter_AddRefs(file));
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsILocalFile> componentFile = do_QueryInterface(file, &rv); nsCOMPtr<nsILocalFile> componentFile = do_QueryInterface(file, &rv);
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);

View File

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

View File

@ -37,12 +37,9 @@
* ***** END LICENSE BLOCK ***** */ * ***** END LICENSE BLOCK ***** */
function test_BrokenFile(path, shouldThrow, expectedName) { function test_BrokenFile(path, shouldThrow, expectedName) {
var f = do_get_file(path, true); var didThrow = false;
var uri = "abs:" + f.path;
print(uri);
var didThrow;
try { try {
Components.utils.import(uri); Components.utils.import(path);
} catch (ex) { } catch (ex) {
var exceptionName = ex.name; var exceptionName = ex.name;
print("ex: " + ex + "; name = " + ex.name); print("ex: " + ex + "; name = " + ex.name);
@ -55,7 +52,27 @@ function test_BrokenFile(path, shouldThrow, expectedName) {
} }
function run_test() { function run_test() {
test_BrokenFile("js/src/xpconnect/tests/unit/bogus_exports_type.jsm", true, "Error"); const C_i = Components.interfaces;
test_BrokenFile("js/src/xpconnect/tests/unit/bogus_element_type.jsm", true, "Error"); const ioService = Components.classes["@mozilla.org/network/io-service;1"]
test_BrokenFile("js/src/xpconnect/tests/unit/non_existing.jsm", true, "NS_ERROR_FILE_NOT_FOUND"); .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() { function run_test() {
var scope = {}; 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), "object");
do_check_eq(typeof(scope.XPCOMUtils.generateFactory), "function"); do_check_eq(typeof(scope.XPCOMUtils.generateFactory), "function");
// try again on the global object // try again on the global object
do_check_eq(typeof(Components.utils.import), "function"); 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), "object");
do_check_eq(typeof(XPCOMUtils.generateFactory), "function"); do_check_eq(typeof(XPCOMUtils.generateFactory), "function");
// try on a new object // try on a new object
var scope2 = {}; 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), "object");
do_check_eq(typeof(scope2.XPCOMUtils.generateFactory), "function"); 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 // make sure we throw when the second arg is bogus
var didThrow = false; var didThrow = false;
try { try {
Components.utils.import("rel:XPCOMUtils.jsm", "wrong"); Components.utils.import("resource://gre/modules/XPCOMUtils.jsm", "wrong");
} catch (ex) { } catch (ex) {
print("ex: " + ex); print("ex: " + ex);
didThrow = true; didThrow = true;