/// <summary> /// Create a AutoJSContext using the SafeJSContext. /// If context is IntPtr.Zero use the SafeJSContext /// </summary> /// <param name="context"></param> public AutoJSContext(IntPtr context) { if (context == IntPtr.Zero) { _jsContextStack = Xpcom.GetService <nsIThreadJSContextStack>("@mozilla.org/js/xpc/ContextStack;1"); context = _jsContextStack.GetSafeJSContextAttribute(); } _cx = context; // begin a new request JS_BeginRequest(_cx); // push the context onto the context stack _contextStack = Xpcom.GetService <nsIJSContextStack>("@mozilla.org/js/xpc/ContextStack;1"); _contextStack.Push(_cx); // obtain the system principal (no security checks) (one could get a different principal by calling securityManager.GetObjectPrincipal()) _securityManager = Xpcom.GetService <nsIScriptSecurityManager>("@mozilla.org/scriptsecuritymanager;1"); _systemPrincipal = _securityManager.GetSystemPrincipal(); _jsPrincipals = _systemPrincipal.GetJSPrincipals(_cx); _securityManager.PushContextPrincipal(_cx, IntPtr.Zero, _systemPrincipal); }
public AutoJSContext() { nsIJSRuntimeService runtimeService = (nsIJSRuntimeService)Xpcom.GetService("@mozilla.org/js/xpc/RuntimeService;1"); IntPtr jsRuntime = runtimeService.GetRuntime(); IntPtr cx = JS_NewContext(jsRuntime, 8192); nsIJSContextStack contextStack = Xpcom.GetService <nsIJSContextStack>("@mozilla.org/js/xpc/ContextStack;1"); contextStack.Push(cx); nsIPrincipal system = Xpcom.GetService <nsIScriptSecurityManager>("@mozilla.org/scriptsecuritymanager;1").GetSystemPrincipal(); IntPtr jsPrincipals = system.GetJSPrincipals(cx); JSStackFrame frame = new JSStackFrame(); frame.script = JS_CompileScriptForPrincipals(cx, JS_GetGlobalObject(cx), jsPrincipals, "", 0, "", 1); //NOTE: this code is based on the definition of JSContext from mozilla 1.8 and will not work for other versions IntPtr old = Marshal.ReadIntPtr(cx, 0x34); frame.down = old; IntPtr framePtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(JSStackFrame))); Marshal.StructureToPtr(frame, framePtr, true); // cx->fp = framePtr; Marshal.WriteIntPtr(cx, 0x34, framePtr); }
/// <summary> /// Create a AutoJSContext using the SafeJSContext. /// If context is IntPtr.Zero use the SafeJSContext /// </summary> /// <param name="context"></param> public AutoJSContext(IntPtr context) { if (context == IntPtr.Zero) { context = GlobalJSContextHolder.SafeJSContext; } _cx = context; // TODO: calling BeginRequest may not be neccessary anymore. // begin a new request SpiderMonkey.JS_BeginRequest(_cx); // TODO: pushing the context onto the context stack may not be neccessary anymore. // push the context onto the context stack _contextStack = Xpcom.GetService <nsIJSContextStack>("@mozilla.org/js/xpc/ContextStack;1"); _contextStack.Push(_cx); }
public AutoJSContext() { var jsContextStack = Xpcom.GetService <nsIThreadJSContextStack>("@mozilla.org/js/xpc/ContextStack;1"); cx = jsContextStack.GetSafeJSContextAttribute(); // begin a new request JS_BeginRequest(cx); // push the context onto the context stack nsIJSContextStack contextStack = Xpcom.GetService <nsIJSContextStack>("@mozilla.org/js/xpc/ContextStack;1"); contextStack.Push(cx); // obtain the system principal (no security checks) nsIScriptSecurityManager securityManager = Xpcom.GetService <nsIScriptSecurityManager>("@mozilla.org/scriptsecuritymanager;1"); nsIPrincipal system = securityManager.GetSystemPrincipal(); IntPtr jsPrincipals = system.GetJSPrincipals(cx); securityManager.PushContextPrincipal(cx, IntPtr.Zero, system); }
public AutoJSContext() { // obtain the JS runtime used by gecko nsIJSRuntimeService runtimeService = (nsIJSRuntimeService)Xpcom.GetService("@mozilla.org/js/xpc/RuntimeService;1"); IntPtr jsRuntime = runtimeService.GetRuntimeAttribute(); // create a new JSContext cx = JS_NewContext(jsRuntime, 8192); // begin a new request JS_BeginRequest(cx); // push the context onto the context stack nsIJSContextStack contextStack = Xpcom.GetService <nsIJSContextStack>("@mozilla.org/js/xpc/ContextStack;1"); contextStack.Push(cx); // obtain the system principal (no security checks) which we will use when compiling the empty script below nsIPrincipal system = Xpcom.GetService <nsIScriptSecurityManager>("@mozilla.org/scriptsecuritymanager;1").GetSystemPrincipal(); IntPtr jsPrincipals = system.GetJSPrincipals(cx); // create a fake stack frame JSStackFrame frame = new JSStackFrame(); frame.script = JS_CompileScriptForPrincipals(cx, JS_GetGlobalObject(cx), jsPrincipals, "", 0, "", 1); // put a pointer to the fake stack frame on the JSContext // frame.down = cx->fp IntPtr old = Marshal.ReadIntPtr(cx, OfsetOfFP); frame.down = old; IntPtr framePtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(JSStackFrame))); Marshal.StructureToPtr(frame, framePtr, true); // cx->fp = framePtr; Marshal.WriteIntPtr(cx, OfsetOfFP, framePtr); }
/// <summary> /// Create a AutoJSContext using the SafeJSContext. /// If context is IntPtr.Zero use the SafeJSContext /// </summary> /// <param name="context"></param> public AutoJSContext(IntPtr context) { if (context == IntPtr.Zero) { _jsContextStack = Xpcom.GetService<nsIThreadJSContextStack>("@mozilla.org/js/xpc/ContextStack;1"); context = _jsContextStack.GetSafeJSContextAttribute(); } _cx = context; // begin a new request SpiderMonkey.JS_BeginRequest(_cx); // push the context onto the context stack _contextStack = Xpcom.GetService<nsIJSContextStack>("@mozilla.org/js/xpc/ContextStack;1"); _contextStack.Push(_cx); // obtain the system principal (no security checks) (one could get a different principal by calling securityManager.GetObjectPrincipal()) _securityManager = Xpcom.GetService<nsIScriptSecurityManager>("@mozilla.org/scriptsecuritymanager;1"); _systemPrincipal = _securityManager.GetSystemPrincipal(); _securityManager.PushContextPrincipal(_cx, IntPtr.Zero, _systemPrincipal); }
/// <summary> /// Create a AutoJSContext using the SafeJSContext. /// If context is IntPtr.Zero use the SafeJSContext /// </summary> /// <param name="context"></param> public AutoJSContext(IntPtr context) { if (context == IntPtr.Zero) { _jsContextStack = Xpcom.GetService<nsIThreadJSContextStack>("@mozilla.org/js/xpc/ContextStack;1"); // Due to GetSafeJSContext (being changed from an attribute to a virtual method in FF 15) we can no longer call // it safely from C#. // TODO: find a better solution for this. context = _jsContextStack.GetSafeJSContext(); } _cx = context; // TODO: calling BeginRequest may not be neccessary anymore. // begin a new request SpiderMonkey.JS_BeginRequest(_cx); // TODO: pushing the context onto the context stack may not be neccessary anymore. // push the context onto the context stack _contextStack = Xpcom.GetService<nsIJSContextStack>( Contracts.JsContextStack ); _contextStack.Push(_cx); // PushContextPrinciple was removed in firefox 16. // TODO: It would be nice to get elevated security when running javascript from geckofx. #if false // obtain the system principal (no security checks) (one could get a different principal by calling securityManager.GetObjectPrincipal()) if (_securityManager == null) { _securityManager = Xpcom.GetService<nsIScriptSecurityManager>("@mozilla.org/scriptsecuritymanager;1"); } _systemPrincipal = _securityManager.GetSystemPrincipal(); _securityManager.PushContextPrincipal(_cx, IntPtr.Zero, _systemPrincipal); #endif }