/// <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); }
/// <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 }
/// <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); }