/// <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); }
internal CssUriValue(string aString, Uri aBaseURI, Uri aReferrer, nsIPrincipal aOriginPrincipal) { mString = aString; mURI = aBaseURI; mReferrer = aReferrer; mOriginPrincipal = aOriginPrincipal; mURIResolved = false; }
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); }
internal bool EvaluateSupportsDeclaration(string aProperty, string aValue, Uri aDocURL, Uri aBaseURL, nsIPrincipal aDocPrincipal) { nsCSSProperty propID = nsCSSProps.LookupProperty(aProperty, nsCSSProps.EnabledState.Enabled); if (propID == nsCSSProperty.Unknown) { return false; } var scanner = new nsCSSScanner(aValue, 0); var reporter = new ErrorReporter(scanner, mSheet, mChildLoader, aDocURL); InitScanner(scanner, reporter, aDocURL, aBaseURL, aDocPrincipal); using (/*var suppressErrors = */new nsAutoSuppressErrors(this)) { bool parsedOK = ParseProperty(propID) && !GetToken(true); mReporter.ClearError(); ReleaseScanner(); mTempData.ClearProperty(propID); mTempData.AssertInitialState(); return parsedOK; } }
/// <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); }
public bool IsSystemPrincipal(nsIPrincipal aPrincipal) { return true; }
internal nsresult ParseSheet(string aInput, Uri aSheetURI, Uri aBaseURI, nsIPrincipal aSheetPrincipal, uint32_t aLineNumber, bool aAllowUnsafeRules) { if (aSheetPrincipal == null) throw new ArgumentException("Must have principal here!"); if (aBaseURI == null) throw new ArgumentException("need base URI"); if (aSheetURI == null) throw new ArgumentException("need sheet URI"); if (mSheet == null) throw new ArgumentException("Must have sheet to parse into"); if (mSheet == null) return nsresult.ERROR_UNEXPECTED; #if DEBUG Uri uri = mSheet.GetSheetURI(); bool equal; Debug.Assert(aSheetURI.Equals(uri, out equal).Succeeded() && equal, "Sheet URI does not match passed URI"); Debug.Assert(mSheet.Principal().Equals(aSheetPrincipal, out equal).Succeeded() && equal, "Sheet principal does not match passed principal"); #endif var scanner = new nsCSSScanner(aInput, aLineNumber); var reporter = new ErrorReporter(scanner, mSheet, mChildLoader, aSheetURI); InitScanner(scanner, reporter, aSheetURI, aBaseURI, aSheetPrincipal); int32_t ruleCount = mSheet.StyleRuleCount(); if (0 < ruleCount) { Rule lastRule = null; mSheet.GetStyleRuleAt(ruleCount - 1, ref lastRule); if (lastRule != null) { switch (lastRule.GetKind()) { case CssRuleKind.Charset: case CssRuleKind.Import: mSection = nsCSSSection.Import; break; case CssRuleKind.Namespace: mSection = nsCSSSection.NameSpace; break; default: mSection = nsCSSSection.General; break; } lastRule = null; } } else { mSection = nsCSSSection.Charset; // sheet is empty, any rules are fair } mUnsafeRulesEnabled = aAllowUnsafeRules; nsCSSToken tk = mToken; for (;;) { // Get next non-whitespace token if (!GetToken(true)) { mReporter.OutputError(); break; } if (nsCSSTokenType.HTMLComment == tk.mType) { continue; // legal here only } if (nsCSSTokenType.AtKeyword == tk.mType) { ParseAtRule((rule, _) => AppendRule(rule), this, false); continue; } UngetToken(); if (ParseRuleSet((rule, _) => AppendRule(rule), this)) { mSection = nsCSSSection.General; } } ReleaseScanner(); mUnsafeRulesEnabled = false; // XXX check for low level errors return nsresult.OK; }
internal nsresult ParseStyleAttribute(string aAttributeValue, Uri aDocURI, Uri aBaseURI, nsIPrincipal aNodePrincipal, ref StyleRule aResult) { if (aNodePrincipal == null) throw new ArgumentException("Must have principal here!"); if (aBaseURI == null) throw new ArgumentException("need base URI"); // XXX line number? var scanner = new nsCSSScanner(aAttributeValue, 0); var reporter = new ErrorReporter(scanner, mSheet, mChildLoader, aDocURI); InitScanner(scanner, reporter, aDocURI, aBaseURI, aNodePrincipal); mSection = nsCSSSection.General; // In quirks mode, allow style declarations to have braces or not // (bug 99554). bool haveBraces; if (mNavQuirkMode && GetToken(true)) { haveBraces = nsCSSTokenType.Symbol == mToken.mType && '{' == mToken.mSymbol; UngetToken(); } else { haveBraces = false; } nsParseDeclaration parseFlags = nsParseDeclaration.AllowImportant; if (haveBraces) { parseFlags |= nsParseDeclaration.InBraces; } Declaration declaration = ParseDeclarationBlock(parseFlags); if (declaration != null) { // Create a style rule for the declaration aResult = new StyleRule(null, declaration); } else { aResult = null; } ReleaseScanner(); // XXX check for low level errors return nsresult.OK; }
internal void InitScanner(nsCSSScanner aScanner, ErrorReporter aReporter, Uri aSheetURI, Uri aBaseURI, nsIPrincipal aSheetPrincipal) { if (!(!mHTMLMediaMode)) throw new ArgumentException("Bad initial state"); if (!(!mParsingCompoundProperty)) throw new ArgumentException("Bad initial state"); if (!(mScanner == null)) throw new ArgumentException("already have scanner"); mScanner = aScanner; mReporter = aReporter; mScanner.SetErrorReporter(mReporter); mBaseURI = aBaseURI; mSheetURI = aSheetURI; mSheetPrincipal = aSheetPrincipal; mHavePushBack = false; }
internal nsresult ParseRule(string aRule, Uri aSheetURI, Uri aBaseURI, nsIPrincipal aSheetPrincipal, ref Rule aResult) { if (aSheetPrincipal == null) throw new ArgumentException("Must have principal here!"); if (aBaseURI == null) throw new ArgumentException("need base URI"); aResult = null; var scanner = new nsCSSScanner(aRule, 0); var reporter = new ErrorReporter(scanner, mSheet, mChildLoader, aSheetURI); InitScanner(scanner, reporter, aSheetURI, aBaseURI, aSheetPrincipal); mSection = nsCSSSection.Charset; // callers are responsible for rejecting invalid rules. nsCSSToken tk = mToken; // Get first non-whitespace token nsresult rv = nsresult.OK; if (!GetToken(true)) { { if (!mSuppressErrors) mReporter.ReportUnexpected("PEParseRuleWSOnly"); }; mReporter.OutputError(); rv = nsresult.ERROR_DOM_SYNTAX_ERR; } else { if (nsCSSTokenType.AtKeyword == tk.mType) { // FIXME: perhaps aInsideBlock should be true when we are? Rule result = null; ParseAtRule((rule, _) => result = rule, aResult, false); aResult = result; } else { UngetToken(); Rule result = null; ParseRuleSet((rule, _) => result = rule, aResult); aResult = result; } if (aResult != null && GetToken(true)) { // garbage after rule { if (!mSuppressErrors) mReporter.ReportUnexpected("PERuleTrailing", mToken); }; aResult = null; } if (aResult == null) { rv = nsresult.ERROR_DOM_SYNTAX_ERR; mReporter.OutputError(); } } ReleaseScanner(); return rv; }
/// <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 }
internal nsresult ParseProperty(nsCSSProperty aPropID, string aPropValue, Uri aSheetURI, Uri aBaseURI, nsIPrincipal aSheetPrincipal, Declaration aDeclaration, ref bool aChanged, bool aIsImportant, bool aIsSVGMode) { if (aSheetPrincipal == null) throw new ArgumentException("Must have principal here!"); if (aBaseURI == null) throw new ArgumentException("need base URI"); if (aDeclaration == null) throw new ArgumentException("Need declaration to parse into!"); mData.AssertInitialState(); mTempData.AssertInitialState(); aDeclaration.AssertMutable(); var scanner = new nsCSSScanner(aPropValue, 0); var reporter = new ErrorReporter(scanner, mSheet, mChildLoader, aSheetURI); InitScanner(scanner, reporter, aSheetURI, aBaseURI, aSheetPrincipal); mSection = nsCSSSection.General; scanner.SetSVGMode(aIsSVGMode); aChanged = false; // Check for unknown or preffed off properties if (nsCSSProperty.Unknown == aPropID || !nsCSSProps.IsEnabled(aPropID)) { string propName = nsCSSProps.GetStringValue(aPropID); { if (!mSuppressErrors) mReporter.ReportUnexpected("PEUnknownProperty", propName); }; { if (!mSuppressErrors) mReporter.ReportUnexpected("PEDeclDropped"); }; mReporter.OutputError(); ReleaseScanner(); return nsresult.OK; } bool parsedOK = ParseProperty(aPropID); // We should now be at EOF if (parsedOK && GetToken(true)) { { if (!mSuppressErrors) mReporter.ReportUnexpected("PEExpectEndValue", mToken); }; parsedOK = false; } if (!parsedOK) { string propName = nsCSSProps.GetStringValue(aPropID); { if (!mSuppressErrors) mReporter.ReportUnexpected("PEValueParsingError", propName); }; { if (!mSuppressErrors) mReporter.ReportUnexpected("PEDeclDropped"); }; mReporter.OutputError(); mTempData.ClearProperty(aPropID); } else { // We know we don't need to force a ValueAppended call for the new // value. So if we are not processing a shorthand, and there's // already a value for this property in the declaration at the // same importance level, then we can just copy our parsed value // directly into the declaration without going through the whole // expand/compress thing. if (!aDeclaration.TryReplaceValue(aPropID, aIsImportant, mTempData, ref aChanged)) { // Do it the slow way aDeclaration.ExpandTo(mData); aChanged = mData.TransferFromBlock(mTempData, aPropID, aIsImportant, true, false, aDeclaration); aDeclaration.CompressFrom(mData); } mReporter.ClearError(); } mTempData.AssertInitialState(); ReleaseScanner(); return nsresult.OK; }
internal bool EvaluateSupportsCondition(string aDeclaration, Uri aDocURL, Uri aBaseURL, nsIPrincipal aDocPrincipal) { var scanner = new nsCSSScanner(aDeclaration, 0); var reporter = new ErrorReporter(scanner, mSheet, mChildLoader, aDocURL); InitScanner(scanner, reporter, aDocURL, aBaseURL, aDocPrincipal); using (/*var suppressErrors = */new nsAutoSuppressErrors(this)) { bool conditionMet = false; bool parsedOK = ParseSupportsCondition(ref conditionMet) && !GetToken(true); mReporter.ClearError(); ReleaseScanner(); return parsedOK && conditionMet; } }
internal nsresult ParseDeclarations(string aBuffer, Uri aSheetURI, Uri aBaseURI, nsIPrincipal aSheetPrincipal, Declaration aDeclaration, ref bool aChanged) { aChanged = false; if (aSheetPrincipal == null) throw new ArgumentException("Must have principal here!"); var scanner = new nsCSSScanner(aBuffer, 0); var reporter = new ErrorReporter(scanner, mSheet, mChildLoader, aSheetURI); InitScanner(scanner, reporter, aSheetURI, aBaseURI, aSheetPrincipal); mSection = nsCSSSection.General; mData.AssertInitialState(); aDeclaration.ClearData(); // We could check if it was already empty, but... aChanged = true; for (;;) { // If we cleared the old decl, then we want to be calling // ValueAppended as we parse. if (!ParseDeclaration(aDeclaration, nsParseDeclaration.AllowImportant, true, ref aChanged)) { if (!SkipDeclaration(false)) { break; } } } aDeclaration.CompressFrom(mData); ReleaseScanner(); return nsresult.OK; }
public void CheckLoadURIWithPrincipal(nsIPrincipal aPrincipal, nsIURI uri, uint flags) { }
public void CheckLoadURIStrWithPrincipal(nsIPrincipal aPrincipal, nsAUTF8StringBase uri, uint flags) { }
public bool CanExecuteScripts(IntPtr cx, nsIPrincipal principal) { return true; }