mirror of
https://gitlab.winehq.org/wine/wine-gecko.git
synced 2024-09-13 09:24:08 -07:00
Bug 821726 - allow bypassing script cache when using loadSubscript, r=bholley
This commit is contained in:
parent
c9c4a1d06f
commit
a0af27438c
@ -6,14 +6,14 @@
|
|||||||
|
|
||||||
#include "nsISupports.idl"
|
#include "nsISupports.idl"
|
||||||
|
|
||||||
[scriptable, uuid(837d0211-c448-4bb8-a9bf-922ba33b9d37)]
|
[scriptable, uuid(b21f1579-d994-4e99-a85d-a685140f3ec1)]
|
||||||
interface mozIJSSubScriptLoader : nsISupports
|
interface mozIJSSubScriptLoader : nsISupports
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* This method should only be called from JS!
|
* This method should only be called from JS!
|
||||||
* In JS, the signature looks like:
|
* In JS, the signature looks like:
|
||||||
* rv loadSubScript (url [, obj] [, charset]);
|
* rv loadSubScript (url [, obj] [, charset]);
|
||||||
* @param url the url if the sub-script, it MUST be either a file:,
|
* @param url the url of the sub-script, it MUST be either a file:,
|
||||||
* resource:, or chrome: url, and MUST be local.
|
* resource:, or chrome: url, and MUST be local.
|
||||||
* @param obj an optional object to evaluate the script onto, it
|
* @param obj an optional object to evaluate the script onto, it
|
||||||
* defaults to the global object of the caller.
|
* defaults to the global object of the caller.
|
||||||
@ -24,4 +24,19 @@ interface mozIJSSubScriptLoader : nsISupports
|
|||||||
*/
|
*/
|
||||||
[implicit_jscontext]
|
[implicit_jscontext]
|
||||||
jsval loadSubScript(in AString url, [optional] in jsval obj, [optional] in AString charset);
|
jsval loadSubScript(in AString url, [optional] in jsval obj, [optional] in AString charset);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method should only be called from JS!
|
||||||
|
* In JS, the signature looks like:
|
||||||
|
* rv = loadSubScript (url, optionsObject)
|
||||||
|
* @param url the url of the sub-script, it MUST be either a file:,
|
||||||
|
* resource:, or chrome: url, and MUST be local.
|
||||||
|
* @param optionsObject an object with parameters. Valid parameters are:
|
||||||
|
* - charset: specifying the character encoding of the file (default: ASCII)
|
||||||
|
* - target: an object to evaluate onto (default: global object of the caller)
|
||||||
|
* - ignoreCache: if set to true, will bypass the cache for reading the file.
|
||||||
|
* @retval rv the value returned by the sub-script
|
||||||
|
*/
|
||||||
|
[implicit_jscontext]
|
||||||
|
jsval loadSubScriptWithOptions(in AString url, in jsval options);
|
||||||
};
|
};
|
||||||
|
@ -24,12 +24,36 @@
|
|||||||
#include "js/OldDebugAPI.h"
|
#include "js/OldDebugAPI.h"
|
||||||
#include "nsJSPrincipals.h"
|
#include "nsJSPrincipals.h"
|
||||||
#include "xpcpublic.h" // For xpc::SystemErrorReporter
|
#include "xpcpublic.h" // For xpc::SystemErrorReporter
|
||||||
|
#include "xpcprivate.h" // For xpc::OptionsBase
|
||||||
|
|
||||||
#include "mozilla/scache/StartupCache.h"
|
#include "mozilla/scache/StartupCache.h"
|
||||||
#include "mozilla/scache/StartupCacheUtils.h"
|
#include "mozilla/scache/StartupCacheUtils.h"
|
||||||
|
|
||||||
using namespace mozilla::scache;
|
using namespace mozilla::scache;
|
||||||
using namespace JS;
|
using namespace JS;
|
||||||
|
using namespace xpc;
|
||||||
|
|
||||||
|
class MOZ_STACK_CLASS LoadSubScriptOptions : public OptionsBase {
|
||||||
|
public:
|
||||||
|
LoadSubScriptOptions(JSContext *cx = xpc_GetSafeJSContext(),
|
||||||
|
JSObject *options = nullptr)
|
||||||
|
: OptionsBase(cx, options)
|
||||||
|
, target(cx)
|
||||||
|
, charset(NullString())
|
||||||
|
, ignoreCache(false)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
virtual bool Parse() {
|
||||||
|
return ParseObject("target", &target) &&
|
||||||
|
ParseString("charset", charset) &&
|
||||||
|
ParseBoolean("ignoreCache", &ignoreCache);
|
||||||
|
}
|
||||||
|
|
||||||
|
RootedObject target;
|
||||||
|
nsString charset;
|
||||||
|
bool ignoreCache;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/* load() error msgs, XXX localize? */
|
/* load() error msgs, XXX localize? */
|
||||||
#define LOAD_ERROR_NOSERVICE "Error creating IO Service."
|
#define LOAD_ERROR_NOSERVICE "Error creating IO Service."
|
||||||
@ -172,10 +196,34 @@ mozJSSubScriptLoader::LoadSubScript(const nsAString& url,
|
|||||||
* synchronously.
|
* synchronously.
|
||||||
* target_obj: Optional object to eval the script onto (defaults to context
|
* target_obj: Optional object to eval the script onto (defaults to context
|
||||||
* global)
|
* global)
|
||||||
|
* charset: Optional character set to use for reading
|
||||||
* returns: Whatever jsval the script pointed to by the url returns.
|
* returns: Whatever jsval the script pointed to by the url returns.
|
||||||
* Should ONLY (O N L Y !) be called from JavaScript code.
|
* Should ONLY (O N L Y !) be called from JavaScript code.
|
||||||
*/
|
*/
|
||||||
|
LoadSubScriptOptions options(cx);
|
||||||
|
options.charset = charset;
|
||||||
|
options.target = targetArg.isObject() ? &targetArg.toObject() : nullptr;
|
||||||
|
return DoLoadSubScriptWithOptions(url, options, cx, retval);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
NS_IMETHODIMP
|
||||||
|
mozJSSubScriptLoader::LoadSubScriptWithOptions(const nsAString& url, const Value& optionsVal,
|
||||||
|
JSContext* cx, Value* retval)
|
||||||
|
{
|
||||||
|
if (!optionsVal.isObject())
|
||||||
|
return NS_ERROR_INVALID_ARG;
|
||||||
|
LoadSubScriptOptions options(cx, &optionsVal.toObject());
|
||||||
|
if (!options.Parse())
|
||||||
|
return NS_ERROR_INVALID_ARG;
|
||||||
|
return DoLoadSubScriptWithOptions(url, options, cx, retval);
|
||||||
|
}
|
||||||
|
|
||||||
|
nsresult
|
||||||
|
mozJSSubScriptLoader::DoLoadSubScriptWithOptions(const nsAString& url,
|
||||||
|
LoadSubScriptOptions& options,
|
||||||
|
JSContext* cx, Value* retval)
|
||||||
|
{
|
||||||
nsresult rv = NS_OK;
|
nsresult rv = NS_OK;
|
||||||
|
|
||||||
/* set the system principal if it's not here already */
|
/* set the system principal if it's not here already */
|
||||||
@ -195,17 +243,12 @@ mozJSSubScriptLoader::LoadSubScript(const nsAString& url,
|
|||||||
rv = loader->FindTargetObject(cx, &targetObj);
|
rv = loader->FindTargetObject(cx, &targetObj);
|
||||||
NS_ENSURE_SUCCESS(rv, rv);
|
NS_ENSURE_SUCCESS(rv, rv);
|
||||||
|
|
||||||
bool reusingGlobal = !JS_IsGlobalObject(targetObj);
|
|
||||||
|
|
||||||
// We base reusingGlobal off of what the loader told us, but we may not
|
// We base reusingGlobal off of what the loader told us, but we may not
|
||||||
// actually be using that object.
|
// actually be using that object.
|
||||||
RootedValue target(cx, targetArg);
|
bool reusingGlobal = !JS_IsGlobalObject(targetObj);
|
||||||
RootedObject passedObj(cx);
|
|
||||||
if (!JS_ValueToObject(cx, target, &passedObj))
|
|
||||||
return NS_ERROR_ILLEGAL_VALUE;
|
|
||||||
|
|
||||||
if (passedObj)
|
if (options.target)
|
||||||
targetObj = passedObj;
|
targetObj = options.target;
|
||||||
|
|
||||||
// Remember an object out of the calling compartment so that we
|
// Remember an object out of the calling compartment so that we
|
||||||
// can properly wrap the result later.
|
// can properly wrap the result later.
|
||||||
@ -292,10 +335,10 @@ mozJSSubScriptLoader::LoadSubScript(const nsAString& url,
|
|||||||
|
|
||||||
RootedFunction function(cx);
|
RootedFunction function(cx);
|
||||||
script = nullptr;
|
script = nullptr;
|
||||||
if (cache)
|
if (cache && !options.ignoreCache)
|
||||||
rv = ReadCachedScript(cache, cachePath, cx, mSystemPrincipal, &script);
|
rv = ReadCachedScript(cache, cachePath, cx, mSystemPrincipal, &script);
|
||||||
if (!script) {
|
if (!script) {
|
||||||
rv = ReadScript(uri, cx, targetObj, charset,
|
rv = ReadScript(uri, cx, targetObj, options.charset,
|
||||||
static_cast<const char*>(uriStr.get()), serv,
|
static_cast<const char*>(uriStr.get()), serv,
|
||||||
principal, reusingGlobal, script.address(), function.address());
|
principal, reusingGlobal, script.address(), function.address());
|
||||||
writeScript = !!script;
|
writeScript = !!script;
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
class nsIPrincipal;
|
class nsIPrincipal;
|
||||||
class nsIURI;
|
class nsIURI;
|
||||||
|
class LoadSubScriptOptions;
|
||||||
|
|
||||||
#define MOZ_JSSUBSCRIPTLOADER_CID \
|
#define MOZ_JSSUBSCRIPTLOADER_CID \
|
||||||
{ /* 829814d6-1dd2-11b2-8e08-82fa0a339b00 */ \
|
{ /* 829814d6-1dd2-11b2-8e08-82fa0a339b00 */ \
|
||||||
@ -37,5 +38,9 @@ private:
|
|||||||
bool reuseGlobal, JSScript **scriptp,
|
bool reuseGlobal, JSScript **scriptp,
|
||||||
JSFunction **functionp);
|
JSFunction **functionp);
|
||||||
|
|
||||||
|
nsresult DoLoadSubScriptWithOptions(const nsAString& url,
|
||||||
|
LoadSubScriptOptions& options,
|
||||||
|
JSContext* cx, JS::Value* retval);
|
||||||
|
|
||||||
nsCOMPtr<nsIPrincipal> mSystemPrincipal;
|
nsCOMPtr<nsIPrincipal> mSystemPrincipal;
|
||||||
};
|
};
|
||||||
|
@ -1277,7 +1277,7 @@ GetExpandedPrincipal(JSContext *cx, HandleObject arrayObj, nsIExpandedPrincipal
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Helper that tries to get a property form the options object.
|
* Helper that tries to get a property from the options object.
|
||||||
*/
|
*/
|
||||||
bool
|
bool
|
||||||
OptionsBase::ParseValue(const char *name, MutableHandleValue prop, bool *aFound)
|
OptionsBase::ParseValue(const char *name, MutableHandleValue prop, bool *aFound)
|
||||||
@ -1296,7 +1296,7 @@ OptionsBase::ParseValue(const char *name, MutableHandleValue prop, bool *aFound)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Helper that tries to get a boolean property form the options object.
|
* Helper that tries to get a boolean property from the options object.
|
||||||
*/
|
*/
|
||||||
bool
|
bool
|
||||||
OptionsBase::ParseBoolean(const char *name, bool *prop)
|
OptionsBase::ParseBoolean(const char *name, bool *prop)
|
||||||
@ -1320,7 +1320,7 @@ OptionsBase::ParseBoolean(const char *name, bool *prop)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Helper that tries to get an object property form the options object.
|
* Helper that tries to get an object property from the options object.
|
||||||
*/
|
*/
|
||||||
bool
|
bool
|
||||||
OptionsBase::ParseObject(const char *name, MutableHandleObject prop)
|
OptionsBase::ParseObject(const char *name, MutableHandleObject prop)
|
||||||
@ -1342,7 +1342,7 @@ OptionsBase::ParseObject(const char *name, MutableHandleObject prop)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Helper that tries to get a string property form the options object.
|
* Helper that tries to get a string property from the options object.
|
||||||
*/
|
*/
|
||||||
bool
|
bool
|
||||||
OptionsBase::ParseString(const char *name, nsCString &prop)
|
OptionsBase::ParseString(const char *name, nsCString &prop)
|
||||||
@ -1367,7 +1367,32 @@ OptionsBase::ParseString(const char *name, nsCString &prop)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Helper that tries to get jsid property form the options object.
|
* Helper that tries to get a string property from the options object.
|
||||||
|
*/
|
||||||
|
bool
|
||||||
|
OptionsBase::ParseString(const char *name, nsString &prop)
|
||||||
|
{
|
||||||
|
RootedValue value(mCx);
|
||||||
|
bool found;
|
||||||
|
bool ok = ParseValue(name, &value, &found);
|
||||||
|
NS_ENSURE_TRUE(ok, false);
|
||||||
|
|
||||||
|
if (!found)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (!value.isString()) {
|
||||||
|
JS_ReportError(mCx, "Expected a string value for property %s", name);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
nsDependentJSString strVal;
|
||||||
|
strVal.init(mCx, value.toString());
|
||||||
|
prop = strVal;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Helper that tries to get jsid property from the options object.
|
||||||
*/
|
*/
|
||||||
bool
|
bool
|
||||||
OptionsBase::ParseId(const char *name, MutableHandleId prop)
|
OptionsBase::ParseId(const char *name, MutableHandleId prop)
|
||||||
|
@ -3620,7 +3620,7 @@ IsSandbox(JSObject *obj);
|
|||||||
class MOZ_STACK_CLASS OptionsBase {
|
class MOZ_STACK_CLASS OptionsBase {
|
||||||
public:
|
public:
|
||||||
OptionsBase(JSContext *cx = xpc_GetSafeJSContext(),
|
OptionsBase(JSContext *cx = xpc_GetSafeJSContext(),
|
||||||
JS::HandleObject options = JS::NullPtr())
|
JSObject *options = nullptr)
|
||||||
: mCx(cx)
|
: mCx(cx)
|
||||||
, mObject(cx, options)
|
, mObject(cx, options)
|
||||||
{ }
|
{ }
|
||||||
@ -3632,6 +3632,7 @@ protected:
|
|||||||
bool ParseBoolean(const char *name, bool *prop);
|
bool ParseBoolean(const char *name, bool *prop);
|
||||||
bool ParseObject(const char *name, JS::MutableHandleObject prop);
|
bool ParseObject(const char *name, JS::MutableHandleObject prop);
|
||||||
bool ParseString(const char *name, nsCString &prop);
|
bool ParseString(const char *name, nsCString &prop);
|
||||||
|
bool ParseString(const char *name, nsString &prop);
|
||||||
bool ParseId(const char* name, JS::MutableHandleId id);
|
bool ParseId(const char* name, JS::MutableHandleId id);
|
||||||
|
|
||||||
JSContext *mCx;
|
JSContext *mCx;
|
||||||
@ -3641,7 +3642,7 @@ protected:
|
|||||||
class MOZ_STACK_CLASS SandboxOptions : public OptionsBase {
|
class MOZ_STACK_CLASS SandboxOptions : public OptionsBase {
|
||||||
public:
|
public:
|
||||||
SandboxOptions(JSContext *cx = xpc_GetSafeJSContext(),
|
SandboxOptions(JSContext *cx = xpc_GetSafeJSContext(),
|
||||||
JS::HandleObject options = JS::NullPtr())
|
JSObject *options = nullptr)
|
||||||
: OptionsBase(cx, options)
|
: OptionsBase(cx, options)
|
||||||
, wantXrays(true)
|
, wantXrays(true)
|
||||||
, wantComponents(true)
|
, wantComponents(true)
|
||||||
@ -3669,7 +3670,7 @@ protected:
|
|||||||
class MOZ_STACK_CLASS CreateObjectInOptions : public OptionsBase {
|
class MOZ_STACK_CLASS CreateObjectInOptions : public OptionsBase {
|
||||||
public:
|
public:
|
||||||
CreateObjectInOptions(JSContext *cx = xpc_GetSafeJSContext(),
|
CreateObjectInOptions(JSContext *cx = xpc_GetSafeJSContext(),
|
||||||
JS::HandleObject options = JS::NullPtr())
|
JSObject* options = nullptr)
|
||||||
: OptionsBase(cx, options)
|
: OptionsBase(cx, options)
|
||||||
, defineAs(cx, JSID_VOID)
|
, defineAs(cx, JSID_VOID)
|
||||||
{ }
|
{ }
|
||||||
|
@ -51,6 +51,12 @@ isnot(src.indexOf("return"), -1, "subscript of a subscript should have source");
|
|||||||
ns = {};
|
ns = {};
|
||||||
Services.scriptloader.loadSubScript(resolvedBase + "utf8_subscript.js", ns, "UTF-8");
|
Services.scriptloader.loadSubScript(resolvedBase + "utf8_subscript.js", ns, "UTF-8");
|
||||||
src = ns.f.toSource();
|
src = ns.f.toSource();
|
||||||
|
isnot(src.indexOf("return 42;"), -1, "encoded subscript should have correct source");
|
||||||
|
|
||||||
|
ns = {};
|
||||||
|
Services.scriptloader.loadSubScriptWithOptions(resolvedBase + "utf8_subscript.js",
|
||||||
|
{target: ns, charset: "UTF-8", ignoreCache: true});
|
||||||
|
src = ns.f.toSource();
|
||||||
isnot(src.indexOf("return 42;"), -1, "encoded subscript should have correct source");
|
isnot(src.indexOf("return 42;"), -1, "encoded subscript should have correct source");
|
||||||
]]></script>
|
]]></script>
|
||||||
</window>
|
</window>
|
||||||
|
Loading…
Reference in New Issue
Block a user