public ComPtr <nsISupports> GetGlobalNsObject() { IntPtr globalObject = SpiderMonkey.CurrentGlobalOrNull(_cx); if (globalObject != IntPtr.Zero) { Guid guid = typeof(nsISupports).GUID; IntPtr pUnk = IntPtr.Zero; try { pUnk = Xpcom.XPConnect.Instance.WrapJS(_cx, globalObject, ref guid); object comObj = Xpcom.GetObjectForIUnknown(pUnk); try { return(Xpcom.QueryInterface <nsISupports>(comObj).AsComPtr()); } finally { Xpcom.FreeComObject(ref comObj); } } finally { if (pUnk != IntPtr.Zero) { Marshal.Release(pUnk); } } } return(null); }
public GeckoNode SelectSingle(string xpath) { var r = EvaluateXPathInternal(xpath); nsIDOMNode singleNode = null; switch (r.GetResultTypeAttribute()) { case nsIDOMXPathResultConsts.UNORDERED_NODE_ITERATOR_TYPE: singleNode = r.IterateNext(); var test = r.IterateNext(); if (test != null) { Xpcom.FreeComObject(ref test); Xpcom.FreeComObject(ref singleNode); Xpcom.FreeComObject(ref r); throw new GeckoDomException("There are more than 1 nodes in Single selection"); } break; case nsIDOMXPathResultConsts.FIRST_ORDERED_NODE_TYPE: case nsIDOMXPathResultConsts.ANY_UNORDERED_NODE_TYPE: singleNode = r.GetSingleNodeValueAttribute(); break; } var ret = singleNode.Wrap(GeckoNode.Create); Xpcom.FreeComObject(ref r); return(ret); }
private static void OnProfileStartup() { if (!EnableProfileMonitoring) { return; } nsIObserver addonsIntegration = null; nsIObserverService obsSvc = Xpcom.GetService <nsIObserverService>(Contracts.ObserverService); obsSvc.NotifyObservers(null, "profile-do-change", "startup"); try { addonsIntegration = Xpcom.GetService <nsIObserver>("@mozilla.org/addons/integration;1"); if (addonsIntegration != null) { addonsIntegration.Observe(null, "addons-startup", null); } obsSvc.NotifyObservers(null, "load-extension-defaults", null); } finally { Xpcom.FreeComObject(ref addonsIntegration); obsSvc.NotifyObservers(null, "profile-after-change", "startup"); NS_CreateServicesFromCategory("profile-after-change", null, "profile-after-change"); obsSvc.NotifyObservers(null, "profile-initial-state", null); Xpcom.FreeComObject(ref obsSvc); } }
/// <summary> /// Working similar to SelectSingle but not throwing exceptions on error (simply return first result) /// </summary> /// <param name="xpath"></param> /// <returns></returns> public GeckoNode SelectFirst(string xpath) { var r = EvaluateXPathInternal(xpath); nsIDOMNode singleNode = null; using (var context = new AutoJSContext()) { var jsObject = context.ConvertCOMObjectToJSObject((nsISupports)r); // TODO: (Idenally I would generate these calls via a webidl-> C# compiler but for now just do it via manually written spidermonkey calls..) var resultType = SpiderMonkey.JS_GetProperty(context.ContextPointer, jsObject, "resultType").ToInteger(); switch (resultType) { case nsIDOMXPathResultConsts.UNORDERED_NODE_ITERATOR_TYPE: singleNode = (nsIDOMNode)SpiderMonkey.JS_CallFunctionName(context.ContextPointer, jsObject, "iterateNext", new JsVal[0]).ToComObject(context.ContextPointer); break; case nsIDOMXPathResultConsts.FIRST_ORDERED_NODE_TYPE: case nsIDOMXPathResultConsts.ANY_UNORDERED_NODE_TYPE: singleNode = (nsIDOMNode)SpiderMonkey.JS_GetProperty(context.ContextPointer, jsObject, "singleNodeValue").ToComObject(context.ContextPointer); break; } var ret = singleNode.Wrap(GeckoNode.Create); Xpcom.FreeComObject(ref r); return(ret); } }
public GeckoNode SelectSingle(string xpath) { var r = EvaluateXPathInternal(xpath); var singleNode = r.GetSingleNodeValueAttribute(); var ret = singleNode.Wrap(GeckoNode.Create); Xpcom.FreeComObject(ref r); return(ret); }
private void ClearValue(string name) { if (name == null) { throw new ArgumentNullException("name"); } using (var key = new nsAString(name)) { object comObj = command.Instance.SetUserData(key, null, null); Xpcom.FreeComObject(ref comObj); } }
private nsIXPathResult EvaluateXPathInternal(string xpath) { nsIXPathResult result = null; using (var evaluator = Xpcom.CreateInstance2 <nsIDOMXPathEvaluator>(Contracts.XPathEvaluator)) { var node = DomObject; nsIDOMXPathNSResolver resolver = evaluator.Instance.CreateNSResolver(node); result = (nsIXPathResult)evaluator.Instance.Evaluate(new nsAString(xpath), node, resolver, 0, null); Xpcom.FreeComObject(ref resolver); } return(result); }
public GeckoNode SelectSingle(string xpath) { var r = EvaluateXPathInternal(xpath); nsIDOMNode singleNode = null; using (var context = new AutoJSContext(Window)) using (var jsObject = context.ConvertCOMObjectToJSObject((nsISupports)r)) { // TODO: (Idenally I would generate these calls via a webidl-> C# compiler but for now just do it via manually written spidermonkey calls..) var resultType = SpiderMonkey.JS_GetProperty(context.ContextPointer, jsObject.JSObject, "resultType").ToInteger(); #if PORTFF60 switch (resultType) { case nsIDOMXPathResultConsts.UNORDERED_NODE_ITERATOR_TYPE: singleNode = (nsIDOMNode) SpiderMonkey.JS_CallFunctionName(context.ContextPointer, jsObject, "iterateNext", new JsVal[0]).ToComObject(context.ContextPointer); var test = (SpiderMonkey.JS_CallFunctionName(context.ContextPointer, jsObject, "iterateNext", new JsVal[0])); if (!test.IsNull) { Xpcom.FreeComObject(ref singleNode); Xpcom.FreeComObject(ref r); throw new GeckoDomException("There are more than 1 nodes in Single selection"); } break; case nsIDOMXPathResultConsts.FIRST_ORDERED_NODE_TYPE: case nsIDOMXPathResultConsts.ANY_UNORDERED_NODE_TYPE: singleNode = (nsIDOMNode) SpiderMonkey.JS_GetProperty(context.ContextPointer, jsObject, "singleNodeValue") .ToComObject(context.ContextPointer); break; } #endif throw new NotImplementedException(); } #if PORTFF60 var ret = singleNode.Wrap(GeckoNode.Create); Xpcom.FreeComObject(ref r); return(ret); #endif }
internal byte[] DrawWindow(nsIDOMWindow window, uint x, uint y, uint w, uint h) { Xpcom.AssertCorrectThread(); Init(); SetValue("x", x); SetValue("y", y); SetValue("w", w); SetValue("h", h); using (var data = Xpcom.CreateInstance2 <nsIWritableVariant>("@mozilla.org/variant;1")) { data.Instance.SetAsISupports((nsISupports)window); using (var key = new nsAString("window")) { object comObj = command.Instance.SetUserData(key, data.Instance, null); Xpcom.FreeComObject(ref comObj); command.Instance.DoCommand(); comObj = command.Instance.SetUserData(key, null, null); Xpcom.FreeComObject(ref comObj); } } string base64Image = null; using (var key = new nsAString("drawResult")) { using (var drawResult = command.Instance.SetUserData(key, null, null).AsComPtr()) { if (drawResult != null) { base64Image = drawResult.Instance.GetAsWString(); } } } if (base64Image == null) { throw new InvalidOperationException(); } if (!base64Image.StartsWith("data:image/png;base64,")) { throw new InvalidOperationException(base64Image); } byte[] bytes = Convert.FromBase64String(base64Image.Substring("data:image/png;base64,".Length)); return(bytes); }
private void SetValue(string name, nsISupports value) { if (name == null) { throw new ArgumentNullException("name"); } using (var data = Xpcom.CreateInstance2 <nsIWritableVariant>("@mozilla.org/variant;1")) { data.Instance.SetAsISupports(value); using (var key = new nsAString(name)) { object comObj = command.Instance.SetUserData(key, data.Instance, null); Xpcom.FreeComObject(ref comObj); } } }
/// <summary> /// Working similar to SelectSingle but not throwing exceptions on error (simply return first result) /// </summary> /// <param name="xpath"></param> /// <returns></returns> public GeckoNode SelectFirst(string xpath) { var r = EvaluateXPathInternal(xpath); nsIDOMNode singleNode = null; switch (r.GetResultTypeAttribute()) { case nsIDOMXPathResultConsts.UNORDERED_NODE_ITERATOR_TYPE: singleNode = r.IterateNext(); break; case nsIDOMXPathResultConsts.FIRST_ORDERED_NODE_TYPE: case nsIDOMXPathResultConsts.ANY_UNORDERED_NODE_TYPE: singleNode = r.GetSingleNodeValueAttribute(); break; } var ret = singleNode.Wrap(GeckoNode.Create); Xpcom.FreeComObject(ref r); return(ret); }
protected override void OnHandleDestroyed(EventArgs e) { if (BaseWindow != null) { this.Stop(); WindowMediator.UnregisterWindow(this); if (_weakRef != null) { _weakRef.Dispose(); _weakRef = null; } nsIDocShell docShell = Xpcom.QueryInterface <nsIDocShell>(BaseWindow); if (docShell != null && !docShell.IsBeingDestroyed()) { try { var window = Xpcom.QueryInterface <nsIDOMWindow>(docShell); if (window != null) { try { var w = new WebIDL.Window(window, (nsISupports)window); if (!w.Closed) { w.Close(); } } finally { Xpcom.FreeComObject(ref window); } } } finally { Xpcom.FreeComObject(ref docShell); } } if (EventTarget != null) { RemoveFrameEvents(); DetachEvents(); EventTarget.Dispose(); EventTarget = null; } BaseWindow.Destroy(); Xpcom.FreeComObject(ref CommandParams); var webBrowserFocus = this.WebBrowserFocus; this.WebBrowserFocus = null; Xpcom.FreeComObject(ref webBrowserFocus); Xpcom.FreeComObject(ref WebNav); Xpcom.FreeComObject(ref BaseWindow); Xpcom.FreeComObject(ref WebBrowser); if (this.menu != null) { this.menu.MenuItems.Clear(); this.menu.Dispose(); this.menu = null; } #if GTK if (m_wrapper != null) { m_wrapper.Dispose(); m_wrapper = null; } #endif } base.OnHandleDestroyed(e); }
protected override void OnHandleCreated(EventArgs e) { try { InOnHandleCreate = true; #if GTK if (Xpcom.IsMono) { base.OnHandleCreated(e); if (m_wrapper != null) { m_wrapper.Init(); } } #endif if (!this.DesignMode) { Xpcom.Initialize(); WindowCreator.Register(); #if !GTK LauncherDialogFactory.Register(); #endif WebBrowser = Xpcom.CreateInstance <nsIWebBrowser>(Contracts.WebBrowser); WebBrowserFocus = (nsIWebBrowserFocus)WebBrowser; BaseWindow = (nsIBaseWindow)WebBrowser; WebNav = (nsIWebNavigation)WebBrowser; WebBrowser.SetContainerWindowAttribute(this); #if GTK if (Xpcom.IsMono && m_wrapper != null) { BaseWindow.InitWindow(m_wrapper.BrowserWindow.Handle, IntPtr.Zero, 0, 0, this.Width, this.Height); } else #endif BaseWindow.InitWindow(this.Handle, IntPtr.Zero, 0, 0, this.Width, this.Height); BaseWindow.Create(); var docShell = Xpcom.QueryInterface <nsIDocShell>(BaseWindow); // Allow visible control before finished loading see https://bugzilla.mozilla.org/show_bug.cgi?id=1138536 docShell.CreateAboutBlankContentViewer(null); Xpcom.FreeComObject(ref docShell); BaseWindow.SetVisibilityAttribute(true); Guid nsIWebProgressListenerGUID = typeof(nsIWebProgressListener).GUID; Guid nsIWebProgressListener2GUID = typeof(nsIWebProgressListener2).GUID; WebBrowser.AddWebBrowserListener(this.GetWeakReference(), ref nsIWebProgressListenerGUID); WebBrowser.AddWebBrowserListener(this.GetWeakReference(), ref nsIWebProgressListener2GUID); if (UseHttpActivityObserver) { ObserverService.AddObserver(this, ObserverNotifications.HttpRequests.HttpOnModifyRequest, false); Net.HttpActivityDistributor.AddObserver(this); } // force inital window initialization. (Events now get added after document navigation. { var domWindow = WebBrowser.GetContentDOMWindowAttribute(); EventTarget = Gecko.Interop.ExtensionMethods.AsComPtr(((nsIDOMEventTarget)domWindow)); using (var eventType = new nsAString("somedummyevent")) { EventTarget.Instance.AddEventListener(eventType, this, true, true, 2); EventTarget.Instance.RemoveEventListener(eventType, this, true); } } // history { var sessionHistory = WebNav.GetSessionHistoryAttribute(); if (sessionHistory != null) { sessionHistory.AddSHistoryListener(this); } } // this fix prevents the browser from crashing if the first page loaded is invalid (missing file, invalid URL, etc) var doc = Document; if (doc != null) { // only for html documents doc.Cookie = ""; } WindowMediator.RegisterWindow(this); } #if !GTK base.OnHandleCreated(e); #endif } finally { InOnHandleCreate = false; GeckoHandleCreated(this, EventArgs.Empty); } }
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); }
protected override void OnHandleDestroyed(EventArgs e) { if (BaseWindow != null) { this.Stop(); nsIDocShell docShell = Xpcom.QueryInterface <nsIDocShell>(BaseWindow); if (docShell != null && !docShell.IsBeingDestroyed()) { try { var window = Xpcom.QueryInterface <nsIDOMWindow>(docShell); if (window != null) { try { if (!window.GetClosedAttribute()) { window.Close(); } } finally { Xpcom.FreeComObject(ref window); } } } finally { Xpcom.FreeComObject(ref docShell); } } if (EventTarget != null) { //Remove Event Listener foreach (string sEventType in this.DefaultEvents) { using (var eventType = new nsAString(sEventType)) { EventTarget.Instance.RemoveEventListener(eventType, this, true); } } EventTarget.Dispose(); EventTarget = null; } BaseWindow.Destroy(); Xpcom.FreeComObject(ref CommandParams); var webBrowserFocus = this.WebBrowserFocus; this.WebBrowserFocus = null; Xpcom.FreeComObject(ref webBrowserFocus); Xpcom.FreeComObject(ref WebNav); Xpcom.FreeComObject(ref BaseWindow); Xpcom.FreeComObject(ref WebBrowser); #if GTK if (m_wrapper != null) { m_wrapper.Dispose(); } #endif } base.OnHandleDestroyed(e); }
private static void NS_CreateServicesFromCategory(string category, nsISupports origin, string observerTopic) { nsICategoryManager catMan = null; nsISimpleEnumerator enumerator = null; nsIUTF8StringEnumerator senumerator = null; try { catMan = Xpcom.GetService <nsICategoryManager>(Contracts.CategoryManager); if (catMan == null) { return; } enumerator = catMan.EnumerateCategory(category); if (enumerator == null) { return; } senumerator = Xpcom.QueryInterface <nsIUTF8StringEnumerator>(enumerator); if (senumerator == null) { return; } while (senumerator.HasMore()) { nsISupports serviceInstance = null; nsIObserver observer = null; try { string entryString = nsString.Get(senumerator.GetNext); string contractID = catMan.GetCategoryEntry(category, entryString); serviceInstance = Xpcom.GetService <nsISupports>(contractID); if (serviceInstance == null || observerTopic == null) { continue; } observer = Xpcom.QueryInterface <nsIObserver>(serviceInstance); if (observer == null) { continue; } observer.Observe(origin, observerTopic, ""); } catch (NotImplementedException) { } catch (OutOfMemoryException) { } catch (COMException) { } finally { Xpcom.FreeComObject(ref serviceInstance); Xpcom.FreeComObject(ref observer); } } } finally { Xpcom.FreeComObject(ref catMan); Xpcom.FreeComObject(ref enumerator); Xpcom.FreeComObject(ref senumerator); } }
protected override void OnHandleCreated(EventArgs e) { try { InOnHandleCreate = true; #if GTK if (Xpcom.IsMono) { base.OnHandleCreated(e); } #endif if (!this.DesignMode) { Xpcom.Initialize(); WindowCreator.Register(); #if !GTK LauncherDialogFactory.Register(); #endif WebBrowser = Xpcom.CreateInstance <nsIWebBrowser>(Contracts.WebBrowser); WebBrowserFocus = (nsIWebBrowserFocus)Browser; BaseWindow = (nsIBaseWindow)Browser; WebNav = (nsIWebNavigation)Browser; Browser.SetContainerWindowAttribute(this); #if GTK var drawn = false; Gtk.DrawnHandler drawnHandler = null; drawnHandler = (object o, Gtk.DrawnArgs args) => { _topLevelWindow.Drawn -= drawnHandler; drawn = true; }; _topLevelWindow.Drawn += drawnHandler; if (Xpcom.IsMono) { if (m_wrapper == null) { throw new InvalidOperationException("If GTK window is null we can't continue."); } BaseWindow.InitWindow(m_wrapper.BrowserWindow.Handle, IntPtr.Zero, 0, 0, this.Width, this.Height); } else #endif BaseWindow.InitWindow(this.Handle, IntPtr.Zero, 0, 0, this.Width, this.Height); #if GTK if (m_wrapper != null) { m_wrapper.Init(); } // Only wait for a max of 300ms. (To prevent possiblty of a hang) DateTime d = DateTime.Now; while (Gtk.Application.EventsPending() && !drawn) { Gtk.Application.RunIteration(false); if ((DateTime.Now - d).TotalMilliseconds > 300) { break; } } #endif BaseWindow.Create(); var docShell = Xpcom.QueryInterface <nsIDocShell>(BaseWindow); // Allow visible control before finished loading see https://bugzilla.mozilla.org/show_bug.cgi?id=1138536 docShell.CreateAboutBlankContentViewer(null); Xpcom.FreeComObject(ref docShell); BaseWindow.SetVisibilityAttribute(true); Guid nsIWebProgressListenerGUID = typeof(nsIWebProgressListener).GUID; Guid nsIWebProgressListener2GUID = typeof(nsIWebProgressListener2).GUID; // AddEventListener Doesn't yet work Browser.AddWebBrowserListener(this.GetWeakReference(), ref nsIWebProgressListenerGUID); Browser.AddWebBrowserListener(this.GetWeakReference(), ref nsIWebProgressListener2GUID); if (UseHttpActivityObserver) { ObserverService.AddObserver(this, ObserverNotifications.HttpRequests.HttpOnModifyRequest, false); Net.HttpActivityDistributor.AddObserver(this); } // force inital window initialization. (Events now get added after document navigation. { var domWindow = Browser.GetContentDOMWindowAttribute(); EventTarget = ((nsIDOMEventTarget)domWindow).AsComPtr(); using (var eventType = new nsAString("somedummyevent")) { EventTarget.Instance.AddEventListener(eventType, this, true, true, 2); EventTarget.Instance.RemoveEventListener(eventType, this, true); } } // history { var sessionHistory = WebNav.GetSessionHistoryAttribute(); if (sessionHistory != null) { sessionHistory.AddSHistoryListener(this); } } WindowMediator.RegisterWindow(this); } #if !GTK base.OnHandleCreated(e); #endif } finally { InOnHandleCreate = false; GeckoHandleCreated(this, EventArgs.Empty); } }