Пример #1
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);
        }