From f4a2f342b6b8b953069ce9945b3a43f0cf1e9e52 Mon Sep 17 00:00:00 2001 From: joaoneves Date: Mon, 2 Mar 2020 15:35:03 +0000 Subject: [PATCH 1/5] Properly close browser --- CefGlue.Avalonia/Platform/AvaloniaControl.cs | 31 +++++++---- CefGlue.Avalonia/Platform/NativeExtensions.cs | 4 +- CefGlue.Common/CommonBrowserAdapter.cs | 22 ++++++-- .../CommonOffscreenBrowserAdapter.cs | 8 +++ CefGlue.Common/ICefBrowserHost.cs | 1 + .../CommonCefLifeSpanHandler.cs | 3 +- CefGlue.Common/Platform/IControl.cs | 4 +- CefGlue.Demo.Avalonia/BrowserView.xaml.cs | 5 ++ CefGlue.Demo.Avalonia/MainWindow.xaml.cs | 52 ++++++++++++++----- CefGlue.Demo.WPF/MainWindow.xaml.cs | 30 +++++++++-- CefGlue.Tests/Helpers/DummyControl.cs | 5 ++ .../Platform/ExtendedWpfNativeControlHost.cs | 10 +++- CefGlue.WPF/Platform/WpfControl.cs | 19 +++++-- 13 files changed, 156 insertions(+), 38 deletions(-) diff --git a/CefGlue.Avalonia/Platform/AvaloniaControl.cs b/CefGlue.Avalonia/Platform/AvaloniaControl.cs index 90ad246a..02ee67cc 100644 --- a/CefGlue.Avalonia/Platform/AvaloniaControl.cs +++ b/CefGlue.Avalonia/Platform/AvaloniaControl.cs @@ -43,15 +43,6 @@ namespace Xilium.CefGlue.Avalonia.Platform _control.LayoutUpdated += OnLayoutUpdated; } - public void Dispose() - { - if (CefRuntime.Platform == CefRuntimePlatform.MacOSX && _browserHandle != null) - { - NativeExtensions.OSX.objc_release(_browserHandle.Value); - } - _browserHandle = null; - } - private void OnLayoutUpdated(object sender, EventArgs e) { SizeChanged?.Invoke(new CefSize((int)_control.Bounds.Width, (int)_control.Bounds.Height)); @@ -146,7 +137,7 @@ namespace Xilium.CefGlue.Avalonia.Platform Dispatcher.UIThread.Post(() => { var nativeHost = new ExtendedAvaloniaNativeControlHost(browserHandle); - + if (CefRuntime.Platform == CefRuntimePlatform.MacOSX) { // in osx we need to force an extra update, otherwise the browser will have wrong dimensions when initialized @@ -164,6 +155,26 @@ namespace Xilium.CefGlue.Avalonia.Platform }); } + public void DestroyRender() + { + if (_browserHandle == null) + { + return; + } + + switch (CefRuntime.Platform) { + case CefRuntimePlatform.Windows: + NativeExtensions.Windows.DestroyWindow(_browserHandle.Value); + break; + + case CefRuntimePlatform.MacOSX: + NativeExtensions.OSX.objc_release(_browserHandle.Value); + break; + } + + _browserHandle = null; + } + protected void SetContent(Control content) { ((Panel)_control.Content).Children.Add(content); diff --git a/CefGlue.Avalonia/Platform/NativeExtensions.cs b/CefGlue.Avalonia/Platform/NativeExtensions.cs index ed0f3805..e1e156ff 100644 --- a/CefGlue.Avalonia/Platform/NativeExtensions.cs +++ b/CefGlue.Avalonia/Platform/NativeExtensions.cs @@ -26,8 +26,8 @@ namespace Xilium.CefGlue.Avalonia.Platform public static class Windows { - [DllImport("user32.dll")] - public static extern bool EnableWindow(IntPtr hWnd, bool bEnable); + [DllImport("user32.dll", EntryPoint = "DestroyWindow", CharSet = CharSet.Unicode)] + public static extern bool DestroyWindow(IntPtr hwnd); } } } diff --git a/CefGlue.Common/CommonBrowserAdapter.cs b/CefGlue.Common/CommonBrowserAdapter.cs index bac9fd98..b09f067d 100644 --- a/CefGlue.Common/CommonBrowserAdapter.cs +++ b/CefGlue.Common/CommonBrowserAdapter.cs @@ -64,8 +64,6 @@ namespace Xilium.CefGlue.Common _logger.Info($"Browser adapter disposed (Id:{GetHashCode()}"); } - Control.Dispose(); - var browserHost = BrowserHost; if (browserHost != null) { @@ -414,10 +412,17 @@ namespace Xilium.CefGlue.Common }); } - protected virtual void OnBrowserHostCreated(CefBrowserHost browserHost) { + protected virtual void OnBrowserHostCreated(CefBrowserHost browserHost) + { Control.InitializeRender(browserHost.GetWindowHandle()); } + protected virtual bool OnBrowserClose() + { + Control.DestroyRender(); + return true; + } + #region ICefBrowserHost void ICefBrowserHost.HandleBrowserCreated(CefBrowser browser) @@ -437,6 +442,17 @@ namespace Xilium.CefGlue.Common }); } + bool ICefBrowserHost.HandleBrowserClose(CefBrowser browser) + { + var result = false; + WithErrorHandling((nameof(ICefBrowserHost.HandleBrowserClose)), () => + { + result = OnBrowserClose(); + }); + + return result; + } + bool ICefBrowserHost.HandleTooltip(CefBrowser browser, string text) { WithErrorHandling((nameof(ICefBrowserHost.HandleTooltip)), () => diff --git a/CefGlue.Common/CommonOffscreenBrowserAdapter.cs b/CefGlue.Common/CommonOffscreenBrowserAdapter.cs index d28bdd7e..a529c514 100644 --- a/CefGlue.Common/CommonOffscreenBrowserAdapter.cs +++ b/CefGlue.Common/CommonOffscreenBrowserAdapter.cs @@ -307,6 +307,14 @@ namespace Xilium.CefGlue.Common _logger.Debug($"Browser resized {newWidth}x{newHeight}"); } + + protected override bool OnBrowserClose() + { + // According to cef documentation: + // If no OS window exists (window rendering disabled) returning false will cause the browser object to be destroyed immediately + return false; + } + #region IOffscreenCefBrowserHost void IOffscreenCefBrowserHost.GetViewRect(out CefRectangle rect) diff --git a/CefGlue.Common/ICefBrowserHost.cs b/CefGlue.Common/ICefBrowserHost.cs index 20352f50..109cce98 100644 --- a/CefGlue.Common/ICefBrowserHost.cs +++ b/CefGlue.Common/ICefBrowserHost.cs @@ -7,6 +7,7 @@ namespace Xilium.CefGlue.Common { void HandleBrowserCreated(CefBrowser browser); void HandleBrowserDestroyed(CefBrowser browser); + bool HandleBrowserClose(CefBrowser browser); void HandleAddressChange(CefBrowser browser, CefFrame frame, string url); void HandleTitleChange(CefBrowser browser, string title); diff --git a/CefGlue.Common/InternalHandlers/CommonCefLifeSpanHandler.cs b/CefGlue.Common/InternalHandlers/CommonCefLifeSpanHandler.cs index eba0d077..4eaa9730 100644 --- a/CefGlue.Common/InternalHandlers/CommonCefLifeSpanHandler.cs +++ b/CefGlue.Common/InternalHandlers/CommonCefLifeSpanHandler.cs @@ -24,7 +24,8 @@ namespace Xilium.CefGlue.Common.InternalHandlers protected override bool DoClose(CefBrowser browser) { - return (_owner.LifeSpanHandler?.HandleDoClose(browser) ?? false) || base.DoClose(browser); + return (_owner.LifeSpanHandler?.HandleDoClose(browser) ?? false) || + _owner.HandleBrowserClose(browser); } protected override void OnBeforeClose(CefBrowser browser) diff --git a/CefGlue.Common/Platform/IControl.cs b/CefGlue.Common/Platform/IControl.cs index a42ef8ba..5b62222c 100644 --- a/CefGlue.Common/Platform/IControl.cs +++ b/CefGlue.Common/Platform/IControl.cs @@ -4,7 +4,7 @@ using Xilium.CefGlue.Common.Helpers; namespace Xilium.CefGlue.Common.Platform { - internal interface IControl : IDisposable + internal interface IControl { event Action SizeChanged; @@ -16,5 +16,7 @@ namespace Xilium.CefGlue.Common.Platform void SetTooltip(string text); void InitializeRender(IntPtr browserHandle); + + void DestroyRender(); } } \ No newline at end of file diff --git a/CefGlue.Demo.Avalonia/BrowserView.xaml.cs b/CefGlue.Demo.Avalonia/BrowserView.xaml.cs index 1ec91ba0..e45c878c 100644 --- a/CefGlue.Demo.Avalonia/BrowserView.xaml.cs +++ b/CefGlue.Demo.Avalonia/BrowserView.xaml.cs @@ -102,5 +102,10 @@ namespace Xilium.CefGlue.Demo.Avalonia { browser.ShowDeveloperTools(); } + + public void Dispose() + { + browser.Dispose(); + } } } \ No newline at end of file diff --git a/CefGlue.Demo.Avalonia/MainWindow.xaml.cs b/CefGlue.Demo.Avalonia/MainWindow.xaml.cs index 0f4de0b4..afb5fc22 100644 --- a/CefGlue.Demo.Avalonia/MainWindow.xaml.cs +++ b/CefGlue.Demo.Avalonia/MainWindow.xaml.cs @@ -42,19 +42,45 @@ namespace Xilium.CefGlue.Demo.Avalonia private void CreateNewTab() { - var tabItems = ((IList)this.FindControl("tabControl").Items); - - var tab = new TabItem(); - tab.Header = "New Tab"; - - var view = new BrowserView(); - view.TitleChanged += title => Dispatcher.UIThread.Post(() => - { - tab.Header = title; - ToolTip.SetTip(tab, title); - }); - - tab.Content = view; + var tabItems = ((IList)this.FindControl("tabControl").Items); + + var view = new BrowserView(); + var tab = new TabItem(); + + var headerPanel = new DockPanel(); + tab.Header = headerPanel; + + var closeButton = new Button() + { + Content = "X", + Padding = new Thickness(2), + Margin = new Thickness(5, 0, 0, 0) + }; + closeButton.Click += delegate + { + view.Dispose(); + tabItems.Remove(tab); + }; + DockPanel.SetDock(closeButton, Dock.Right); + + var tabTitle = new TextBlock() + { + Text = "New Tab" + }; + headerPanel.Children.Add(tabTitle); + headerPanel.Children.Add(closeButton); + + view.TitleChanged += title => + { + Dispatcher.UIThread.Post((Action)(() => + { + tabTitle.Text = title; + ToolTip.SetTip(tab, title); + })); + }; + + tab.Content = view; + tabItems.Add(tab); } diff --git a/CefGlue.Demo.WPF/MainWindow.xaml.cs b/CefGlue.Demo.WPF/MainWindow.xaml.cs index ce83b851..50277b43 100644 --- a/CefGlue.Demo.WPF/MainWindow.xaml.cs +++ b/CefGlue.Demo.WPF/MainWindow.xaml.cs @@ -23,15 +23,37 @@ namespace Xilium.CefGlue.Demo.WPF private void CreateNewTab() { - var tab = new TabItem(); - tab.Header = "New Tab"; - var view = new BrowserView(); + var tab = new TabItem(); + + var headerPanel = new DockPanel(); + tab.Header = headerPanel; + + var closeButton = new Button() + { + Content = "X", + Padding = new Thickness(2), + Margin = new Thickness(5, 0, 0, 0) + }; + closeButton.Click += delegate + { + view.Dispose(); + tabControl.Items.Remove(tab); + }; + DockPanel.SetDock(closeButton, Dock.Right); + + var tabTitle = new TextBlock() + { + Text = "New Tab" + }; + headerPanel.Children.Add(tabTitle); + headerPanel.Children.Add(closeButton); + view.TitleChanged += title => { Application.Current.Dispatcher.BeginInvoke((Action)(() => { - tab.Header = title; + tabTitle.Text = title; tab.ToolTip = title; })); }; diff --git a/CefGlue.Tests/Helpers/DummyControl.cs b/CefGlue.Tests/Helpers/DummyControl.cs index b4d83888..a47ba075 100644 --- a/CefGlue.Tests/Helpers/DummyControl.cs +++ b/CefGlue.Tests/Helpers/DummyControl.cs @@ -122,6 +122,11 @@ namespace CefGlue.Tests.Helpers throw new NotImplementedException(); } + public void DestroyRender() + { + throw new NotImplementedException(); + } + public Task StartDrag(CefDragData dragData, CefDragOperationsMask allowedOps, int x, int y) { throw new NotImplementedException(); diff --git a/CefGlue.WPF/Platform/ExtendedWpfNativeControlHost.cs b/CefGlue.WPF/Platform/ExtendedWpfNativeControlHost.cs index 093f9a14..55a9ab9e 100644 --- a/CefGlue.WPF/Platform/ExtendedWpfNativeControlHost.cs +++ b/CefGlue.WPF/Platform/ExtendedWpfNativeControlHost.cs @@ -6,6 +6,9 @@ namespace Xilium.CefGlue.WPF.Platform { internal class ExtendedWpfNativeControlHost : HwndHost { + [DllImport("user32.dll", EntryPoint = "DestroyWindow", CharSet = CharSet.Unicode)] + private static extern bool DestroyWindow(IntPtr hwnd); + private readonly IntPtr _browserHandle; public ExtendedWpfNativeControlHost(IntPtr browserHandle) @@ -20,7 +23,12 @@ namespace Xilium.CefGlue.WPF.Platform protected override void DestroyWindowCore(HandleRef hwnd) { - // do nothing + // nothing to do + } + + public void DestroyWindow() + { + DestroyWindow(_browserHandle); } } } diff --git a/CefGlue.WPF/Platform/WpfControl.cs b/CefGlue.WPF/Platform/WpfControl.cs index 89c84374..552f9289 100644 --- a/CefGlue.WPF/Platform/WpfControl.cs +++ b/CefGlue.WPF/Platform/WpfControl.cs @@ -16,6 +16,7 @@ namespace Xilium.CefGlue.WPF.Platform internal class WpfControl : IControl { protected readonly FrameworkElement _control; + private ExtendedWpfNativeControlHost _nativeControl; public event Action SizeChanged; @@ -26,8 +27,6 @@ namespace Xilium.CefGlue.WPF.Platform control.LayoutUpdated += OnControlLayoutUpdated; } - public void Dispose() { } - private void OnControlLayoutUpdated(object sender, EventArgs e) { SizeChanged?.Invoke(new CefSize((int)_control.RenderSize.Width, (int)_control.RenderSize.Height)); @@ -109,7 +108,21 @@ namespace Xilium.CefGlue.WPF.Platform DispatcherPriority.Input, new Action(() => { - ((ContentControl) _control).Content = new ExtendedWpfNativeControlHost(browserHandle); + _nativeControl = new ExtendedWpfNativeControlHost(browserHandle); + ((ContentControl) _control).Content = _nativeControl; + }) + ); + } + + public void DestroyRender() + { + _nativeControl.DestroyWindow(); // must be destroyed on current thread + _control.Dispatcher.BeginInvoke( + DispatcherPriority.Normal, + new Action(() => + { + // dispose remaining resources + _nativeControl.Dispose(); }) ); } From 1a02897bc5d8aa4baf324f41d604648001fe6c9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joa=CC=83o=20Neves?= Date: Mon, 2 Mar 2020 17:06:01 +0000 Subject: [PATCH 2/5] Explicitly release host nsview ... still does not work --- CefGlue.Avalonia/Platform/AvaloniaControl.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CefGlue.Avalonia/Platform/AvaloniaControl.cs b/CefGlue.Avalonia/Platform/AvaloniaControl.cs index 02ee67cc..73103828 100644 --- a/CefGlue.Avalonia/Platform/AvaloniaControl.cs +++ b/CefGlue.Avalonia/Platform/AvaloniaControl.cs @@ -15,7 +15,7 @@ namespace Xilium.CefGlue.Avalonia.Platform /// internal class AvaloniaControl : Common.Platform.IControl { - private static IntPtr? _dummyHostView; + private IntPtr? _dummyHostView; private readonly Control _contextMenuDummyTarget; private IntPtr? _browserHandle; @@ -168,7 +168,9 @@ namespace Xilium.CefGlue.Avalonia.Platform break; case CefRuntimePlatform.MacOSX: + NativeExtensions.OSX.objc_release(_dummyHostView.Value); NativeExtensions.OSX.objc_release(_browserHandle.Value); + _dummyHostView = null; break; } From 6855a1b7ba756c48045ba1eac186ed4a8e620e6f Mon Sep 17 00:00:00 2001 From: joaoneves Date: Tue, 3 Mar 2020 10:27:18 +0000 Subject: [PATCH 3/5] Update avalonia --- CefGlue.Avalonia/CefGlue.Avalonia.csproj | 2 +- CefGlue.Demo.Avalonia/CefGlue.Demo.Avalonia.csproj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CefGlue.Avalonia/CefGlue.Avalonia.csproj b/CefGlue.Avalonia/CefGlue.Avalonia.csproj index 8a729079..422be6e6 100644 --- a/CefGlue.Avalonia/CefGlue.Avalonia.csproj +++ b/CefGlue.Avalonia/CefGlue.Avalonia.csproj @@ -13,7 +13,7 @@ - + diff --git a/CefGlue.Demo.Avalonia/CefGlue.Demo.Avalonia.csproj b/CefGlue.Demo.Avalonia/CefGlue.Demo.Avalonia.csproj index 7a1af150..034da0c1 100644 --- a/CefGlue.Demo.Avalonia/CefGlue.Demo.Avalonia.csproj +++ b/CefGlue.Demo.Avalonia/CefGlue.Demo.Avalonia.csproj @@ -28,7 +28,7 @@ - + From 5132b4719146c28b6d9c565e451ddf338a93f405 Mon Sep 17 00:00:00 2001 From: joaoneves Date: Tue, 3 Mar 2020 10:36:22 +0000 Subject: [PATCH 4/5] Have only one dummy nsview --- CefGlue.Avalonia/Platform/AvaloniaControl.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/CefGlue.Avalonia/Platform/AvaloniaControl.cs b/CefGlue.Avalonia/Platform/AvaloniaControl.cs index 73103828..02ee67cc 100644 --- a/CefGlue.Avalonia/Platform/AvaloniaControl.cs +++ b/CefGlue.Avalonia/Platform/AvaloniaControl.cs @@ -15,7 +15,7 @@ namespace Xilium.CefGlue.Avalonia.Platform /// internal class AvaloniaControl : Common.Platform.IControl { - private IntPtr? _dummyHostView; + private static IntPtr? _dummyHostView; private readonly Control _contextMenuDummyTarget; private IntPtr? _browserHandle; @@ -168,9 +168,7 @@ namespace Xilium.CefGlue.Avalonia.Platform break; case CefRuntimePlatform.MacOSX: - NativeExtensions.OSX.objc_release(_dummyHostView.Value); NativeExtensions.OSX.objc_release(_browserHandle.Value); - _dummyHostView = null; break; } From 9e88f5c073dedea721b94b7a791dc90ad62d2989 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joa=CC=83o=20Neves?= Date: Tue, 3 Mar 2020 10:46:27 +0000 Subject: [PATCH 5/5] Fixed browser size when attached --- CefGlue.Avalonia/Platform/AvaloniaControl.cs | 20 +------------------ .../ExtendedAvaloniaNativeControlHost.cs | 18 +++++++++++++++++ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/CefGlue.Avalonia/Platform/AvaloniaControl.cs b/CefGlue.Avalonia/Platform/AvaloniaControl.cs index 02ee67cc..4f5c3998 100644 --- a/CefGlue.Avalonia/Platform/AvaloniaControl.cs +++ b/CefGlue.Avalonia/Platform/AvaloniaControl.cs @@ -134,25 +134,7 @@ namespace Xilium.CefGlue.Avalonia.Platform NativeExtensions.OSX.objc_retain(browserHandle); } - Dispatcher.UIThread.Post(() => - { - var nativeHost = new ExtendedAvaloniaNativeControlHost(browserHandle); - - if (CefRuntime.Platform == CefRuntimePlatform.MacOSX) - { - // in osx we need to force an extra update, otherwise the browser will have wrong dimensions when initialized - IDisposable observable = null; - void UpdateNativeControlBounds(AvaloniaPropertyChangedEventArgs e) - { - observable.Dispose(); - nativeHost.TryUpdateNativeControlPosition(); - } - - observable = nativeHost.GetPropertyChangedObservable(Control.TransformedBoundsProperty).Subscribe(UpdateNativeControlBounds); - } - - SetContent(nativeHost); - }); + Dispatcher.UIThread.Post(() => SetContent(new ExtendedAvaloniaNativeControlHost(browserHandle))); } public void DestroyRender() diff --git a/CefGlue.Avalonia/Platform/ExtendedAvaloniaNativeControlHost.cs b/CefGlue.Avalonia/Platform/ExtendedAvaloniaNativeControlHost.cs index 63dcb594..72dff979 100644 --- a/CefGlue.Avalonia/Platform/ExtendedAvaloniaNativeControlHost.cs +++ b/CefGlue.Avalonia/Platform/ExtendedAvaloniaNativeControlHost.cs @@ -1,4 +1,5 @@ using System; +using Avalonia; using Avalonia.Controls; using Avalonia.Platform; @@ -13,6 +14,23 @@ namespace Xilium.CefGlue.Avalonia.Platform _browserHandle = browserHandle; } + protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) + { + base.OnAttachedToVisualTree(e); + if(CefRuntime.Platform == CefRuntimePlatform.MacOSX) + { + // in osx we need to force an extra update, otherwise the browser will have wrong dimensions when initialized + IDisposable observable = null; + void UpdateNativeControlBounds(AvaloniaPropertyChangedEventArgs _) + { + observable.Dispose(); + TryUpdateNativeControlPosition(); + } + + observable = this.GetPropertyChangedObservable(Control.TransformedBoundsProperty).Subscribe(UpdateNativeControlBounds); + } + } + protected override IPlatformHandle CreateNativeControlCore(IPlatformHandle handle) { return new PlatformHandle(_browserHandle, "HWND");