Creates a scoped, fake "system principal" security context. This class is used primarly to work around bugs in gecko which prevent methods on nsIDOMCSSStyleSheet from working outside of javascript.
Inheritance: IDisposable
        /// <summary>
        /// Get a bitmap of the current browsers Window.
        /// Works on Visible or InVisible windows.
        /// </summary>
        /// <param name="xOffset"></param>
        /// <param name="yOffset"></param>
        /// <param name="width">Width length of returned bitmap in pixels</param>
        /// <param name="height">Height length of returned bitmap in pixels</param>
        /// <returns></returns>
        /// <throws>ArgumentException if width or height is zero</throws>
        public Bitmap GetBitmap(uint xOffset, uint yOffset, uint width, uint height)
        {
            if (width == 0)
                throw new ArgumentException("width");

            if (height == 0)
                throw new ArgumentException("height");

            // Use of the canvas technique was inspired by: the abduction! firefox plugin by Rowan Lewis
            // https://addons.mozilla.org/en-US/firefox/addon/abduction/

            // Some opertations fail without a proper JSContext.
            using (AutoJSContext jsContext = new AutoJSContext())
            {
                GeckoCanvasElement canvas = (GeckoCanvasElement)m_browser.Document.CreateElement("canvas");
                canvas.Width = width;
                canvas.Height = height;

                nsIDOMHTMLCanvasElement canvasPtr = (nsIDOMHTMLCanvasElement)canvas.DomObject;
                nsIDOMCanvasRenderingContext2D context;
                using (nsAString str = new nsAString("2d"))
                {
                    context = (nsIDOMCanvasRenderingContext2D)canvasPtr.MozGetIPCContext(str);
                }

                using (nsAString color = new nsAString("rgb(255,255,255)"))
                {
                    context.DrawWindow((nsIDOMWindow)m_browser.Window.DomWindow, xOffset, yOffset, width, height, color, 0);
                }

                string data = canvas.toDataURL("image/png");
                byte[] bytes = Convert.FromBase64String(data.Substring("data:image/png;base64,".Length));
                return (Bitmap)Bitmap.FromStream(new System.IO.MemoryStream(bytes));
            }
        }
Beispiel #2
0
            /// <summary>
            /// Inserts a rule at the specified position in the collection.  The return value is the index in the list where the item was actually inserted,
            /// or -1 if the rule contains syntax errors and could not be added to the collection.
            /// </summary>
            /// <param name="index"></param>
            /// <param name="rule"></param>
            public int Insert(int index, string rule)
            {
                if (IsReadOnly)
                {
                    throw new InvalidOperationException("This collection is read-only.");
                }
                else if (index < 0 || index > Count)
                {
                    throw new ArgumentOutOfRangeException("index");
                }
                else if (string.IsNullOrEmpty(rule))
                {
                    return(-1);
                }

                const int NS_ERROR_DOM_SYNTAX_ERR = unchecked ((int)0x8053000c);

                using (AutoJSContext context = new AutoJSContext())
                {
                    index = (int)StyleSheet._DomStyleSheet.InsertRule(new nsAString(rule), (uint)index);
                }

                return(index);
            }
        public void EnableJavascriptDebugger()
        {
            if (m_javascriptDebuggingEnabled)
                return;

            using (var a = new AutoJSContext())
            {
                var jsd = Xpcom.GetService<jsdIDebuggerService>("@mozilla.org/js/jsd/debugger-service;1");
                jsd.SetErrorHookAttribute(new JSErrorHandler(this));
                nsIJSRuntimeService runtime = Xpcom.GetService<nsIJSRuntimeService>("@mozilla.org/js/xpc/RuntimeService;1");
                jsd.ActivateDebugger(runtime.GetRuntimeAttribute());
                Marshal.ReleaseComObject(runtime);
                Marshal.ReleaseComObject(jsd);
            }

            m_javascriptDebuggingEnabled = true;
        }
            public string ToString()
            {
                //		TODO		if (!IsString)
                //					throw new NotImplementedException("JsVal.ToString() only supported when the type is String.");

                using (AutoJSContext context = new AutoJSContext())
                {
                    return Marshal.PtrToStringAnsi(JS_EncodeString(context.ContextPointer, Ptr));
                }
            }