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); }
public static IntPtr GetJSContextForDomWindow(nsIDOMWindow window) { Xpcom.AssertCorrectThread(); IntPtr pUnk = Marshal.GetIUnknownForObject(window); Marshal.Release(pUnk); IntPtr context; if (!_windowContexts.TryGetValue(pUnk, out context)) { context = IntPtr.Zero; foreach (IntPtr cx in _unknownContexts) { 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); _unknownContexts.Remove(cx); context = cx; break; } } finally { Marshal.ReleaseComObject(domWindow); } } } } } } } return context; }
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); }
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; }