Exemplo n.º 1
0
        private static IntPtr GetContextByName(string contextName)
        {
            Xpcom.AssertCorrectThread();

            IntPtr rt    = Runtime;
            IntPtr iterp = IntPtr.Zero;
            IntPtr cx;

            while ((cx = SpiderMonkey.JS_ContextIterator(rt, ref iterp)) != IntPtr.Zero)
            {
                IntPtr global = SpiderMonkey.JS_GetGlobalObject(cx);
                if (global != IntPtr.Zero)
                {
                    IntPtr classp = SpiderMonkey.JS_GetClass(global);
                    // get class name
                    if (classp != IntPtr.Zero)
                    {
                        string className = Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(classp));
                        if (className == contextName)
                        {
                            return(cx);
                        }
                    }
                }
            }
            return(IntPtr.Zero);
        }
Exemplo n.º 2
0
        public static IntPtr GetJSContextForDomWindow(nsIDOMWindow window)
        {
            IntPtr context = window.GetWindowRootAttribute().GetJSContextForEventHandlers();

            if (context == IntPtr.Zero)
            {
                IntPtr pUnk = Marshal.GetIUnknownForObject(window);
                Marshal.Release(pUnk);


                if (!_windowContexts.TryGetValue(pUnk, out context))
                {
                    context = IntPtr.Zero;

                    IntPtr cx;
                    IntPtr iterp = IntPtr.Zero;
                    IntPtr rt    = Runtime;
                    while ((cx = SpiderMonkey.JS_ContextIterator(rt, ref iterp)) != IntPtr.Zero)
                    {
                        IntPtr pGlobal = SpiderMonkey.JS_GetGlobalObject(cx);
                        if (pGlobal != IntPtr.Zero)
                        {
                            using (var auto = new AutoJSContext(cx))
                            {
                                nsISupports global = auto.GetGlobalNsObject();
                                if (global != null)
                                {
                                    var domWindow = Xpcom.QueryInterface <nsIDOMWindow>(global);
                                    if (domWindow != null)
                                    {
                                        try
                                        {
                                            IntPtr pUnkTest = Marshal.GetIUnknownForObject(domWindow.GetWindowAttribute());
                                            Marshal.Release(pUnkTest);

                                            if (pUnk == pUnkTest)
                                            {
                                                _windowContexts.Add(pUnk, cx);
                                                context = cx;
                                                break;
                                            }
                                        }
                                        finally
                                        {
                                            Marshal.ReleaseComObject(domWindow);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(context);
        }
Exemplo n.º 3
0
        internal static IntPtr GetJSContextForDomWindow(nsIDOMWindow window)
        {
            IntPtr            context     = IntPtr.Zero;
            nsIDOMEventTarget eventTarget = window.GetWindowRootAttribute();

            try
            {
                context = eventTarget.GetJSContextForEventHandlers();
                if (context == IntPtr.Zero)
                {
                    IntPtr pUnk = Marshal.GetIUnknownForObject(window);
                    Marshal.Release(pUnk);


                    if (!_windowContexts.TryGetValue(pUnk, out context))
                    {
                        context = IntPtr.Zero;

                        IntPtr cx;
                        IntPtr iterp = IntPtr.Zero;
                        IntPtr rt    = Runtime;
                        while ((cx = SpiderMonkey.JS_ContextIterator(rt, ref iterp)) != IntPtr.Zero)
                        {
                            IntPtr pGlobal = SpiderMonkey.DefaultObjectForContextOrNull(cx);
                            if (pGlobal != IntPtr.Zero)
                            {
                                using (var auto = new AutoJSContext(cx))
                                {
                                    using (ComPtr <nsISupports> global = auto.GetGlobalNsObject())
                                    {
                                        if (global != null)
                                        {
                                            var domWindow = Xpcom.QueryInterface <nsIDOMWindow>(global.Instance);
                                            if (domWindow != null)
                                            {
                                                try
                                                {
                                                    IntPtr pUnkTest = Marshal.GetIUnknownForObject(domWindow.GetWindowAttribute());
                                                    Marshal.Release(pUnkTest);

                                                    if (pUnk == pUnkTest)
                                                    {
                                                        _windowContexts.Add(pUnk, cx);
                                                        context = cx;
                                                        break;
                                                    }
                                                }
                                                finally
                                                {
                                                    Xpcom.FreeComObject(ref domWindow);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                if (eventTarget != null)
                {
                    Xpcom.FreeComObject(ref eventTarget);
                }
            }
            return(context);
        }