Bug 705188 - Use IDL for mozIJSSubScriptLoader::LoadSubScript, {xpcIJSModuleLoader,nsIXPCComponents_Utils}::Import; r=bz

This commit is contained in:
Ms2ger 2011-12-18 11:09:16 +01:00
Родитель 98244f7259
Коммит ddf06918bc
10 изменённых файлов: 101 добавлений и 177 удалений

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

@ -1057,7 +1057,7 @@ DetectByteOrderMark(const unsigned char* aBytes, PRInt32 aLen, nsCString& oChars
/* static */ nsresult
nsScriptLoader::ConvertToUTF16(nsIChannel* aChannel, const PRUint8* aData,
PRUint32 aLength, const nsString& aHintCharset,
PRUint32 aLength, const nsAString& aHintCharset,
nsIDocument* aDocument, nsString& aString)
{
if (!aLength) {

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

@ -177,7 +177,7 @@ public:
*/
static nsresult ConvertToUTF16(nsIChannel* aChannel, const PRUint8* aData,
PRUint32 aLength,
const nsString& aHintCharset,
const nsAString& aHintCharset,
nsIDocument* aDocument, nsString& aString);
/**

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

@ -40,7 +40,7 @@
#include "nsISupports.idl"
[scriptable, uuid(8792d77e-1dd2-11b2-ac7f-9bc9be4f2916)]
[scriptable, uuid(837d0211-c448-4bb8-a9bf-922ba33b9d37)]
interface mozIJSSubScriptLoader : nsISupports
{
/**
@ -53,11 +53,9 @@ interface mozIJSSubScriptLoader : nsISupports
* defaults to the global object of the caller.
* @param charset optionally specifies the character encoding of
* the file. If absent, the file is interpreted
as ASCII.
* as ASCII.
* @retval rv the value returned by the sub-script
*/
void loadSubScript (in wstring url);
[implicit_jscontext]
jsval loadSubScript(in AString url, [optional] in jsval obj, [optional] in AString charset);
};

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

@ -48,7 +48,7 @@ struct JSObject;
[ptr] native JSObjectPtr(JSObject);
[scriptable, uuid(3f945a8e-58ca-47ba-a789-82d022e837fd)]
[scriptable, uuid(243d1a31-db9f-47a1-9922-55a1ad5515fb)]
interface xpcIJSModuleLoader : nsISupports
{
/**
@ -81,8 +81,8 @@ interface xpcIJSModuleLoader : nsISupports
*
* (This comment is duplicated to nsIXPCComponents_Utils.)
*/
void /* JSObject */ import(in AUTF8String aResourceURI
/* , [optional] in JSObject targetObj */);
[implicit_jscontext,optional_argc]
jsval import(in AUTF8String aResourceURI, [optional] in jsval targetObj);
/**
* Imports the JS module at aResourceURI to the JS object

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

@ -152,7 +152,7 @@ interface ScheduledGCCallback : nsISupports
/**
* interface of Components.utils
*/
[scriptable, uuid(d41d626c-c222-4c38-ba5f-6499baff3b11)]
[scriptable, uuid(a5213836-fd24-4b36-8916-fff222b23cf3)]
interface nsIXPCComponents_Utils : nsISupports
{
@ -231,8 +231,8 @@ interface nsIXPCComponents_Utils : nsISupports
*
* (This comment is duplicated from xpcIJSModuleLoader.)
*/
void /* JSObject */ import(in AUTF8String registryLocation
/*, [optional] in JSObject targetObj */);
[implicit_jscontext,optional_argc]
jsval import(in AUTF8String aResourceURI, [optional] in jsval targetObj);
/*
* Unloads the JS module at 'registryLocation'. Existing references to the

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

@ -1,4 +1,4 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
@ -340,20 +340,15 @@ private:
};
static nsresult
ReportOnCaller(nsAXPCNativeCallContext *cc,
ReportOnCaller(JSContext *callerContext,
const char *format, ...) {
if (!cc) {
if (!callerContext) {
return NS_ERROR_FAILURE;
}
va_list ap;
va_start(ap, format);
nsresult rv;
JSContext *callerContext;
rv = cc->GetJSContext(&callerContext);
NS_ENSURE_SUCCESS(rv, rv);
char *buf = JS_vsmprintf(format, ap);
if (!buf) {
return NS_ERROR_OUT_OF_MEMORY;
@ -1014,71 +1009,38 @@ mozJSComponentLoader::UnloadModules()
#endif
}
/* [JSObject] import (in AUTF8String registryLocation,
[optional] in JSObject targetObj ); */
NS_IMETHODIMP
mozJSComponentLoader::Import(const nsACString & registryLocation)
mozJSComponentLoader::Import(const nsACString& registryLocation,
const JS::Value& targetObj,
JSContext* cx,
PRUint8 optionalArgc,
JS::Value* retval)
{
// This function should only be called from JS.
nsresult rv;
NS_TIME_FUNCTION_FMT("%s (line %d) (file: %s)", MOZ_FUNCTION_NAME,
__LINE__, registryLocation.BeginReading());
nsCOMPtr<nsIXPConnect> xpc =
do_GetService(kXPConnectServiceContractID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsAXPCNativeCallContext *cc = nsnull;
rv = xpc->GetCurrentNativeCallContext(&cc);
NS_ENSURE_SUCCESS(rv, rv);
#ifdef DEBUG
{
// ensure that we are being call from JS, from this method
nsCOMPtr<nsIInterfaceInfo> info;
rv = cc->GetCalleeInterface(getter_AddRefs(info));
NS_ENSURE_SUCCESS(rv, rv);
nsXPIDLCString name;
info->GetName(getter_Copies(name));
NS_ASSERTION(nsCRT::strcmp("nsIXPCComponents_Utils", name.get()) == 0,
"Components.utils.import must only be called from JS.");
PRUint16 methodIndex;
const nsXPTMethodInfo *methodInfo;
rv = info->GetMethodInfoForName("import", &methodIndex, &methodInfo);
NS_ENSURE_SUCCESS(rv, rv);
PRUint16 calleeIndex;
rv = cc->GetCalleeMethodIndex(&calleeIndex);
NS_ASSERTION(calleeIndex == methodIndex,
"Components.utils.import called from another utils method.");
}
#endif
JSContext *cx = nsnull;
rv = cc->GetJSContext(&cx);
NS_ENSURE_SUCCESS(rv, rv);
JSAutoRequest ar(cx);
JSObject *targetObject = nsnull;
PRUint32 argc = 0;
rv = cc->GetArgc(&argc);
NS_ENSURE_SUCCESS(rv, rv);
if (argc > 1) {
if (optionalArgc) {
// The caller passed in the optional second argument. Get it.
jsval *argv = nsnull;
rv = cc->GetArgvPtr(&argv);
NS_ENSURE_SUCCESS(rv, rv);
if (!JSVAL_IS_OBJECT(argv[1])) {
return ReportOnCaller(cc, ERROR_SCOPE_OBJ,
if (!JSVAL_IS_OBJECT(targetObj)) {
return ReportOnCaller(cx, ERROR_SCOPE_OBJ,
PromiseFlatCString(registryLocation).get());
}
targetObject = JSVAL_TO_OBJECT(argv[1]);
targetObject = JSVAL_TO_OBJECT(targetObj);
} else {
// Our targetObject is the caller's global object. Find it by
// walking the calling object's parent chain.
nsresult rv;
nsCOMPtr<nsIXPConnect> xpc =
do_GetService(kXPConnectServiceContractID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsAXPCNativeCallContext *cc = nsnull;
rv = xpc->GetCurrentNativeCallContext(&cc);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIXPConnectWrappedNative> wn;
rv = cc->GetCalleeWrapper(getter_AddRefs(wn));
@ -1100,17 +1062,14 @@ mozJSComponentLoader::Import(const nsACString & registryLocation)
}
JSObject *globalObj = nsnull;
rv = ImportInto(registryLocation, targetObject, cc, &globalObj);
nsresult rv = ImportInto(registryLocation, targetObject, cx, &globalObj);
if (globalObj && !JS_WrapObject(cx, &globalObj)) {
NS_ERROR("can't wrap return value");
return NS_ERROR_FAILURE;
}
jsval *retval = nsnull;
cc->GetRetValPtr(&retval);
if (retval)
*retval = OBJECT_TO_JSVAL(globalObj);
*retval = OBJECT_TO_JSVAL(globalObj);
return rv;
}
@ -1122,6 +1081,18 @@ mozJSComponentLoader::ImportInto(const nsACString & aLocation,
JSObject * targetObj,
nsAXPCNativeCallContext * cc,
JSObject * *_retval)
{
JSContext *callercx;
nsresult rv = cc->GetJSContext(&callercx);
NS_ENSURE_SUCCESS(rv, rv);
return ImportInto(aLocation, targetObj, callercx, _retval);
}
nsresult
mozJSComponentLoader::ImportInto(const nsACString & aLocation,
JSObject * targetObj,
JSContext * callercx,
JSObject * *_retval)
{
nsresult rv;
*_retval = nsnull;
@ -1196,8 +1167,6 @@ mozJSComponentLoader::ImportInto(const nsACString & aLocation,
if (!JSVAL_IS_VOID(exception)) {
// An exception was thrown during compilation. Propagate it
// out to our caller so they can report it.
JSContext *callercx;
cc->GetJSContext(&callercx);
JS_SetPendingException(callercx, exception);
return NS_OK;
}

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

@ -1,4 +1,4 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
@ -93,6 +93,11 @@ class mozJSComponentLoader : public mozilla::ModuleLoader,
char **location,
jsval *exception);
nsresult ImportInto(const nsACString & aLocation,
JSObject * targetObj,
JSContext * callercx,
JSObject * *_retval);
nsCOMPtr<nsIComponentManager> mCompMgr;
nsCOMPtr<nsIJSRuntimeService> mRuntimeService;
nsCOMPtr<nsIThreadJSContextStack> mContextStack;

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

@ -106,7 +106,7 @@ ReportError(JSContext *cx, const char *msg)
nsresult
mozJSSubScriptLoader::ReadScript(nsIURI *uri, JSContext *cx, JSObject *target_obj,
jschar *charset, const char *uriStr,
const nsAString& charset, const char *uriStr,
nsIIOService *serv, nsIPrincipal *principal,
JSScript **scriptp)
{
@ -153,10 +153,10 @@ mozJSSubScriptLoader::ReadScript(nsIURI *uri, JSContext *cx, JSObject *target_ob
* exceptions, including the source/line number */
er = JS_SetErrorReporter(cx, mozJSLoaderErrorReporter);
if (charset) {
if (!charset.IsVoid()) {
nsString script;
rv = nsScriptLoader::ConvertToUTF16(nsnull, reinterpret_cast<const PRUint8*>(buf.get()), len,
nsDependentString(reinterpret_cast<PRUnichar*>(charset)), nsnull, script);
charset, nsnull, script);
if (NS_FAILED(rv)) {
JSPRINCIPALS_DROP(cx, jsPrincipals);
@ -180,9 +180,12 @@ mozJSSubScriptLoader::ReadScript(nsIURI *uri, JSContext *cx, JSObject *target_ob
return NS_OK;
}
NS_IMETHODIMP /* args and return value are delt with using XPConnect and JSAPI */
mozJSSubScriptLoader::LoadSubScript (const PRUnichar * aURL
/* [, JSObject *target_obj] */)
NS_IMETHODIMP
mozJSSubScriptLoader::LoadSubScript(const nsAString& url,
const JS::Value& target,
const nsAString& charset,
JSContext* cx,
JS::Value* retval)
{
/*
* Loads a local url and evals it into the current cx
@ -195,48 +198,19 @@ mozJSSubScriptLoader::LoadSubScript (const PRUnichar * aURL
* Should ONLY (O N L Y !) be called from JavaScript code.
*/
/* gotta define most of this stuff up here because of all the gotos,
* defined the rest up here to be consistent */
nsresult rv;
JSBool ok;
nsresult rv = NS_OK;
#ifdef NS_FUNCTION_TIMER
NS_TIME_FUNCTION_FMT("%s (line %d) (url: %s)", MOZ_FUNCTION_NAME,
__LINE__, NS_LossyConvertUTF16toASCII(aURL).get());
#else
(void)aURL; // prevent compiler warning
__LINE__, NS_LossyConvertUTF16toASCII(url).get());
#endif
/* get JS things from the CallContext */
nsCOMPtr<nsIXPConnect> xpc = do_GetService(nsIXPConnect::GetCID());
if (!xpc) return NS_ERROR_FAILURE;
nsAXPCNativeCallContext *cc = nsnull;
rv = xpc->GetCurrentNativeCallContext(&cc);
if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
JSContext *cx;
rv = cc->GetJSContext (&cx);
if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
PRUint32 argc;
rv = cc->GetArgc (&argc);
if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
jsval *argv;
rv = cc->GetArgvPtr (&argv);
if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
jsval *rval;
rv = cc->GetRetValPtr (&rval);
if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
/* set mJSPrincipals if it's not here already */
if (!mSystemPrincipal) {
nsCOMPtr<nsIScriptSecurityManager> secman =
do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID);
if (!secman)
return rv;
return NS_OK;
rv = secman->GetSystemPrincipal(getter_AddRefs(mSystemPrincipal));
if (NS_FAILED(rv) || !mSystemPrincipal)
@ -245,80 +219,55 @@ mozJSSubScriptLoader::LoadSubScript (const PRUnichar * aURL
JSAutoRequest ar(cx);
JSString *url;
JSObject *target_obj = nsnull;
jschar *charset = nsnull;
ok = JS_ConvertArguments (cx, argc, argv, "S / o W", &url, &target_obj, &charset);
if (!ok) {
/* let the exception raised by JS_ConvertArguments show through */
return NS_OK;
}
JSObject* targetObj;
if (!JS_ValueToObject(cx, target, &targetObj))
return NS_ERROR_ILLEGAL_VALUE;
JSAutoByteString urlbytes(cx, url);
if (!urlbytes) {
return NS_OK;
}
if (!target_obj) {
/* if the user didn't provide an object to eval onto, find the global
* object by walking the parent chain of the calling object */
if (!targetObj) {
// If the user didn't provide an object to eval onto, find the global
// object by walking the parent chain of the calling object.
nsCOMPtr<nsIXPConnect> xpc = do_GetService(nsIXPConnect::GetCID());
NS_ENSURE_TRUE(xpc, NS_ERROR_FAILURE);
#ifdef DEBUG_rginda
JSObject *got_glob = JS_GetGlobalObject (cx);
fprintf (stderr, "JS_GetGlobalObject says glob is %p.\n", got_glob);
target_obj = JS_GetPrototype (cx, got_glob);
fprintf (stderr, "That glob's prototype is %p.\n", target_obj);
target_obj = JS_GetParent (cx, got_glob);
fprintf (stderr, "That glob's parent is %p.\n", target_obj);
#endif
nsAXPCNativeCallContext *cc = nsnull;
rv = xpc->GetCurrentNativeCallContext(&cc);
NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);
nsCOMPtr<nsIXPConnectWrappedNative> wn;
rv = cc->GetCalleeWrapper (getter_AddRefs(wn));
if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
rv = cc->GetCalleeWrapper(getter_AddRefs(wn));
NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);
rv = wn->GetJSObject (&target_obj);
if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
rv = wn->GetJSObject(&targetObj);
NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);
#ifdef DEBUG_rginda
fprintf (stderr, "Parent chain: %p", target_obj);
#endif
JSObject *maybe_glob = JS_GetParent (cx, target_obj);
while (maybe_glob != nsnull) {
#ifdef DEBUG_rginda
fprintf (stderr, ", %p", maybe_glob);
#endif
target_obj = maybe_glob;
maybe_glob = JS_GetParent (cx, maybe_glob);
JSObject *maybeGlob = JS_GetParent(cx, targetObj);
while (maybeGlob) {
targetObj = maybeGlob;
maybeGlob = JS_GetParent(cx, maybeGlob);
}
#ifdef DEBUG_rginda
fprintf (stderr, "\n");
#endif
}
// Remember an object out of the calling compartment so that we
// can properly wrap the result later.
nsCOMPtr<nsIPrincipal> principal = mSystemPrincipal;
JSObject *result_obj = target_obj;
target_obj = JS_FindCompilationScope(cx, target_obj);
if (!target_obj)
JSObject *result_obj = targetObj;
targetObj = JS_FindCompilationScope(cx, targetObj);
if (!targetObj)
return NS_ERROR_FAILURE;
if (target_obj != result_obj) {
if (targetObj != result_obj) {
nsCOMPtr<nsIScriptSecurityManager> secman =
do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID);
if (!secman)
return NS_ERROR_FAILURE;
rv = secman->GetObjectPrincipal(cx, target_obj, getter_AddRefs(principal));
rv = secman->GetObjectPrincipal(cx, targetObj, getter_AddRefs(principal));
NS_ENSURE_SUCCESS(rv, rv);
#ifdef DEBUG_rginda
fprintf (stderr, "Final global: %p\n", target_obj);
#endif
}
JSAutoEnterCompartment ac;
if (!ac.enter(cx, target_obj))
if (!ac.enter(cx, targetObj))
return NS_ERROR_UNEXPECTED;
/* load up the url. From here on, failures are reflected as ``custom''
@ -356,7 +305,7 @@ mozJSSubScriptLoader::LoadSubScript (const PRUnichar * aURL
// Make sure to explicitly create the URI, since we'll need the
// canonicalized spec.
rv = NS_NewURI(getter_AddRefs(uri), urlbytes.ptr(), nsnull, serv);
rv = NS_NewURI(getter_AddRefs(uri), NS_LossyConvertUTF16toASCII(url).get(), nsnull, serv);
if (NS_FAILED(rv)) {
return ReportError(cx, LOAD_ERROR_NOURI);
}
@ -398,7 +347,8 @@ mozJSSubScriptLoader::LoadSubScript (const PRUnichar * aURL
if (cache)
rv = ReadCachedScript(cache, cachePath, cx, &script);
if (!script) {
rv = ReadScript(uri, cx, target_obj, charset, (char *)uriStr.get(), serv,
rv = ReadScript(uri, cx, targetObj, charset,
static_cast<const char*>(uriStr.get()), serv,
principal, &script);
writeScript = true;
}
@ -406,11 +356,11 @@ mozJSSubScriptLoader::LoadSubScript (const PRUnichar * aURL
if (NS_FAILED(rv) || !script)
return rv;
ok = JS_ExecuteScriptVersion(cx, target_obj, script, rval, version);
bool ok = JS_ExecuteScriptVersion(cx, targetObj, script, retval, version);
if (ok) {
JSAutoEnterCompartment rac;
if (!rac.enter(cx, result_obj) || !JS_WrapValue(cx, rval))
if (!rac.enter(cx, result_obj) || !JS_WrapValue(cx, retval))
return NS_ERROR_UNEXPECTED;
}
@ -418,7 +368,5 @@ mozJSSubScriptLoader::LoadSubScript (const PRUnichar * aURL
WriteCachedScript(cache, cachePath, cx, script);
}
cc->SetReturnValueWasSet (ok);
return NS_OK;
}

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

@ -65,7 +65,7 @@ public:
private:
nsresult ReadScript(nsIURI *uri, JSContext *cx, JSObject *target_obj,
jschar *charset, const char *uriStr,
const nsAString& charset, const char *uriStr,
nsIIOService *serv, nsIPrincipal *principal,
JSScript **scriptp);

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

@ -3640,13 +3640,17 @@ xpc_EvalInSandbox(JSContext *cx, JSObject *sandbox, const nsAString& source,
* [optional] in JSObject targetObj);
*/
NS_IMETHODIMP
nsXPCComponents_Utils::Import(const nsACString & registryLocation)
nsXPCComponents_Utils::Import(const nsACString& registryLocation,
const JS::Value& targetObj,
JSContext* cx,
PRUint8 optionalArgc,
JS::Value* retval)
{
nsCOMPtr<xpcIJSModuleLoader> moduleloader =
do_GetService(MOZJSCOMPONENTLOADER_CONTRACTID);
if (!moduleloader)
return NS_ERROR_FAILURE;
return moduleloader->Import(registryLocation);
return moduleloader->Import(registryLocation, targetObj, cx, optionalArgc, retval);
}
/* unload (in AUTF8String registryLocation);