From a152b2e6dba789707581169a592d30155e06b22c Mon Sep 17 00:00:00 2001 From: Alex Maitland Date: Tue, 30 Nov 2021 13:46:13 +1000 Subject: [PATCH] WinForms - Improve hosting of popups using ChromiumHostControl - Add IChromiumWebBrowserBase which is can be used for ChromiumWebBrowser instances and ChromiumHostControl - Reduce code duplication for extensions methods - Update WinForms Example --- CefSharp.OffScreen/ChromiumWebBrowser.cs | 3 +- CefSharp.WinForms.Example/BrowserForm.cs | 62 +-- .../BrowserTabUserControl.cs | 30 +- CefSharp.WinForms/ChromiumWebBrowser.cs | 4 +- CefSharp.WinForms/Host/ChromiumHostControl.cs | 20 +- .../Host/ChromiumHostControlBase.cs | 7 - .../Host/HostControlExtensions.cs | 458 ------------------ CefSharp.WinForms/WebBrowserExtensions.cs | 4 +- CefSharp.Wpf/ChromiumWebBrowser.cs | 1 + .../IChromiumWebBrowserBase.cs | 43 +- CefSharp/IWebBrowser.cs | 142 +----- .../Partial/ChromiumWebBrowser.Partial.cs | 12 +- CefSharp/WebBrowserExtensions.cs | 227 +++------ 13 files changed, 166 insertions(+), 847 deletions(-) delete mode 100644 CefSharp.WinForms/Host/HostControlExtensions.cs rename CefSharp.WinForms/Host/IChromiumHostControl.cs => CefSharp/IChromiumWebBrowserBase.cs (83%) diff --git a/CefSharp.OffScreen/ChromiumWebBrowser.cs b/CefSharp.OffScreen/ChromiumWebBrowser.cs index f50a5f1a50..8988c7af4c 100644 --- a/CefSharp.OffScreen/ChromiumWebBrowser.cs +++ b/CefSharp.OffScreen/ChromiumWebBrowser.cs @@ -284,6 +284,7 @@ protected virtual void Dispose(bool disposing) FocusHandler = new NoFocusHandler(); browser = null; + BrowserCore = null; if (managedCefBrowserAdapter != null) { @@ -656,7 +657,7 @@ public IJavascriptObjectRepository JavascriptObjectRepository /// Has Focus - Always False /// /// returns false - bool IWebBrowser.Focus() + bool IChromiumWebBrowserBase.Focus() { // no control to focus for offscreen browser return false; diff --git a/CefSharp.WinForms.Example/BrowserForm.cs b/CefSharp.WinForms.Example/BrowserForm.cs index 47c3d68057..09e2dd5912 100644 --- a/CefSharp.WinForms.Example/BrowserForm.cs +++ b/CefSharp.WinForms.Example/BrowserForm.cs @@ -229,7 +229,7 @@ private void UndoMenuItemClick(object sender, EventArgs e) var control = GetCurrentTabControl(); if (control != null) { - control.BrowserControl.Undo(); + control.Browser.Undo(); } } @@ -238,7 +238,7 @@ private void RedoMenuItemClick(object sender, EventArgs e) var control = GetCurrentTabControl(); if (control != null) { - control.BrowserControl.Redo(); + control.Browser.Redo(); } } @@ -247,7 +247,7 @@ private void CutMenuItemClick(object sender, EventArgs e) var control = GetCurrentTabControl(); if (control != null) { - control.BrowserControl.Cut(); + control.Browser.Cut(); } } @@ -256,7 +256,7 @@ private void CopyMenuItemClick(object sender, EventArgs e) var control = GetCurrentTabControl(); if (control != null) { - control.BrowserControl.Copy(); + control.Browser.Copy(); } } @@ -265,7 +265,7 @@ private void PasteMenuItemClick(object sender, EventArgs e) var control = GetCurrentTabControl(); if (control != null) { - control.BrowserControl.Paste(); + control.Browser.Paste(); } } @@ -274,7 +274,7 @@ private void DeleteMenuItemClick(object sender, EventArgs e) var control = GetCurrentTabControl(); if (control != null) { - control.BrowserControl.Delete(); + control.Browser.Delete(); } } @@ -283,7 +283,7 @@ private void SelectAllMenuItemClick(object sender, EventArgs e) var control = GetCurrentTabControl(); if (control != null) { - control.BrowserControl.SelectAll(); + control.Browser.SelectAll(); } } @@ -292,7 +292,7 @@ private void PrintToolStripMenuItemClick(object sender, EventArgs e) var control = GetCurrentTabControl(); if (control != null) { - control.BrowserControl.Print(); + control.Browser.Print(); } } @@ -304,7 +304,7 @@ private async void ShowDevToolsMenuItemClick(object sender, EventArgs e) var isDevToolsOpen = await control.CheckIfDevToolsIsOpenAsync(); if (!isDevToolsOpen) { - control.BrowserControl.ShowDevTools(); + control.Browser.ShowDevTools(); } } } @@ -317,7 +317,7 @@ private async void ShowDevToolsDockedMenuItemClick(object sender, EventArgs e) var isDevToolsOpen = await control.CheckIfDevToolsIsOpenAsync(); if (!isDevToolsOpen) { - var chromiumWebBrowser = control.BrowserControl as ChromiumWebBrowser; + var chromiumWebBrowser = control.Browser as ChromiumWebBrowser; if (chromiumWebBrowser != null && chromiumWebBrowser.LifeSpanHandler != null) { control.ShowDevToolsDocked(); @@ -337,7 +337,7 @@ private async void CloseDevToolsMenuItemClick(object sender, EventArgs e) var isDevToolsOpen = await control.CheckIfDevToolsIsOpenAsync(); if (isDevToolsOpen) { - control.BrowserControl.CloseDevTools(); + control.Browser.CloseDevTools(); } } } @@ -347,14 +347,14 @@ private void ZoomInToolStripMenuItemClick(object sender, EventArgs e) var control = GetCurrentTabControl(); if (control != null) { - var task = control.BrowserControl.GetZoomLevelAsync(); + var task = control.Browser.GetZoomLevelAsync(); task.ContinueWith(previous => { if (previous.Status == TaskStatus.RanToCompletion) { var currentLevel = previous.Result; - control.BrowserControl.SetZoomLevel(currentLevel + ZoomIncrement); + control.Browser.SetZoomLevel(currentLevel + ZoomIncrement); } else { @@ -369,13 +369,13 @@ private void ZoomOutToolStripMenuItemClick(object sender, EventArgs e) var control = GetCurrentTabControl(); if (control != null) { - var task = control.BrowserControl.GetZoomLevelAsync(); + var task = control.Browser.GetZoomLevelAsync(); task.ContinueWith(previous => { if (previous.Status == TaskStatus.RanToCompletion) { var currentLevel = previous.Result; - control.BrowserControl.SetZoomLevel(currentLevel - ZoomIncrement); + control.Browser.SetZoomLevel(currentLevel - ZoomIncrement); } else { @@ -390,7 +390,7 @@ private void CurrentZoomLevelToolStripMenuItemClick(object sender, EventArgs e) var control = GetCurrentTabControl(); if (control != null) { - var task = control.BrowserControl.GetZoomLevelAsync(); + var task = control.Browser.GetZoomLevelAsync(); task.ContinueWith(previous => { if (previous.Status == TaskStatus.RanToCompletion) @@ -411,7 +411,7 @@ private void DoesActiveElementAcceptTextInputToolStripMenuItemClick(object sende var control = GetCurrentTabControl(); if (control != null) { - var frame = control.BrowserControl.GetFocusedFrame(); + var frame = control.Browser.GetFocusedFrame(); //Execute extension method frame.ActiveElementAcceptsTextInput().ContinueWith(task => @@ -450,7 +450,7 @@ private void DoesElementWithIdExistToolStripMenuItemClick(object sender, EventAr var control = GetCurrentTabControl(); if (control != null) { - var frame = control.BrowserControl.GetFocusedFrame(); + var frame = control.Browser.GetFocusedFrame(); //Execute extension method frame.ElementWithIdExists(dialog.Value).ContinueWith(task => @@ -486,7 +486,7 @@ private void GoToDemoPageToolStripMenuItemClick(object sender, EventArgs e) var control = GetCurrentTabControl(); if (control != null) { - control.BrowserControl.LoadUrl("custom://cefsharp/ScriptedMethodsTest.html"); + control.Browser.LoadUrl("custom://cefsharp/ScriptedMethodsTest.html"); } } @@ -495,7 +495,7 @@ private void InjectJavascriptCodeToolStripMenuItemClick(object sender, EventArgs var control = GetCurrentTabControl(); if (control != null) { - var frame = control.BrowserControl.GetFocusedFrame(); + var frame = control.Browser.GetFocusedFrame(); //Execute extension method frame.ListenForEvent("test-button", "click"); @@ -515,7 +515,7 @@ private async void PrintToPdfToolStripMenuItemClick(object sender, EventArgs e) if (dialog.ShowDialog() == DialogResult.OK) { - var success = await control.BrowserControl.PrintToPdfAsync(dialog.FileName, new PdfPrintSettings + var success = await control.Browser.PrintToPdfAsync(dialog.FileName, new PdfPrintSettings { MarginType = CefPdfPrintMarginType.Custom, MarginBottom = 10, @@ -544,7 +544,7 @@ private void OpenDataUrlToolStripMenuItemClick(object sender, EventArgs e) if (control != null) { const string html = "Test

Html Encoded in URL!

"; - control.BrowserControl.LoadHtml(html, false); + control.Browser.LoadHtml(html, false); } } @@ -553,7 +553,7 @@ private void OpenHttpBinOrgToolStripMenuItemClick(object sender, EventArgs e) var control = GetCurrentTabControl(); if (control != null) { - control.BrowserControl.LoadUrl("https://httpbin.org/"); + control.Browser.LoadUrl("https://httpbin.org/"); } } @@ -562,7 +562,7 @@ private void RunFileDialogToolStripMenuItemClick(object sender, EventArgs e) var control = GetCurrentTabControl(); if (control != null) { - control.BrowserControl.GetBrowserHost().RunFileDialog(CefFileDialogMode.Open, "Open", null, new List { "*.*" }, 0, new RunFileDialogCallback()); + control.Browser.GetBrowserHost().RunFileDialog(CefFileDialogMode.Open, "Open", null, new List { "*.*" }, 0, new RunFileDialogCallback()); } } @@ -572,9 +572,9 @@ private void LoadExtensionsToolStripMenuItemClick(object sender, EventArgs e) if (control != null) { //The sample extension only works for http(s) schemes - if (control.BrowserControl.GetMainFrame().Url.StartsWith("http")) + if (control.Browser.GetMainFrame().Url.StartsWith("http")) { - var requestContext = control.BrowserControl.GetBrowserHost().RequestContext; + var requestContext = control.Browser.GetBrowserHost().RequestContext; const string cefSharpExampleResourcesFolder = #if !NETCOREAPP @@ -612,7 +612,7 @@ private void LoadExtensionsToolStripMenuItemClick(object sender, EventArgs e) GetActiveBrowser = (extension, isIncognito) => { //Return the active browser for which the extension will act upon - return control.BrowserControl.BrowserCore; + return control.Browser.BrowserCore; } }; @@ -630,16 +630,16 @@ private void JavascriptBindingStressTestToolStripMenuItemClick(object sender, Ev var control = GetCurrentTabControl(); if (control != null) { - control.BrowserControl.LoadUrl(CefExample.BindingTestUrl); - control.BrowserControl.LoadingStateChanged += (o, args) => + control.Browser.LoadUrl(CefExample.BindingTestUrl); + control.Browser.LoadingStateChanged += (o, args) => { if (args.IsLoading == false) { Task.Delay(10000).ContinueWith(t => { - if (control.BrowserControl != null) + if (control.Browser != null) { - control.BrowserControl.Reload(); + control.Browser.Reload(); } }); } diff --git a/CefSharp.WinForms.Example/BrowserTabUserControl.cs b/CefSharp.WinForms.Example/BrowserTabUserControl.cs index 861c436d1f..a4808d7348 100644 --- a/CefSharp.WinForms.Example/BrowserTabUserControl.cs +++ b/CefSharp.WinForms.Example/BrowserTabUserControl.cs @@ -20,7 +20,7 @@ namespace CefSharp.WinForms.Example { public partial class BrowserTabUserControl : UserControl { - public IChromiumHostControl BrowserControl { get; private set; } + public IChromiumWebBrowserBase Browser { get; private set; } private ChromiumWidgetNativeWindow messageInterceptor; private bool multiThreadedMessageLoopEnabled; @@ -28,7 +28,7 @@ public BrowserTabUserControl(ChromiumHostControl chromiumHostControl) { InitializeComponent(); - BrowserControl = chromiumHostControl; + Browser = chromiumHostControl; browserPanel.Controls.Add(chromiumHostControl); @@ -52,7 +52,7 @@ public BrowserTabUserControl(Action openNewTab, string url, bool m browserPanel.Controls.Add(browser); - BrowserControl = browser; + Browser = browser; browser.MenuHandler = new MenuHandler(); browser.RequestHandler = new WinFormsRequestHandler(openNewTab); @@ -288,7 +288,7 @@ private void SetIsLoading(bool isLoading) private void OnIsBrowserInitializedChanged(object sender, EventArgs e) { //Get the underlying browser host wrapper - var browserHost = BrowserControl.BrowserCore.GetHost(); + var browserHost = Browser.BrowserCore.GetHost(); var requestContext = browserHost.RequestContext; string errorMessage; // Browser must be initialized before getting/setting preferences @@ -335,9 +335,9 @@ private void SetupMessageInterceptor() while (true) { IntPtr chromeWidgetHostHandle; - if (ChromiumRenderWidgetHandleFinder.TryFindHandle(BrowserControl.BrowserCore, out chromeWidgetHostHandle)) + if (ChromiumRenderWidgetHandleFinder.TryFindHandle(Browser.BrowserCore, out chromeWidgetHostHandle)) { - messageInterceptor = new ChromiumWidgetNativeWindow((Control)BrowserControl, chromeWidgetHostHandle); + messageInterceptor = new ChromiumWidgetNativeWindow((Control)Browser, chromeWidgetHostHandle); messageInterceptor.OnWndProc(message => { @@ -434,12 +434,12 @@ private void GoButtonClick(object sender, EventArgs e) private void BackButtonClick(object sender, EventArgs e) { - BrowserControl.GoBack(); + Browser.Back(); } private void ForwardButtonClick(object sender, EventArgs e) { - BrowserControl.GoForward(); + Browser.Forward(); } private void UrlTextBoxKeyUp(object sender, KeyEventArgs e) @@ -456,20 +456,20 @@ private void LoadUrl(string url) { if (Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute)) { - BrowserControl.LoadUrl(url); + Browser.LoadUrl(url); } else { var searchUrl = "https://www.google.com/search?q=" + Uri.EscapeDataString(url); - BrowserControl.LoadUrl(searchUrl); + Browser.LoadUrl(searchUrl); } } public async void CopySourceToClipBoardAsync() { - var htmlSource = await BrowserControl.GetSourceAsync(); + var htmlSource = await Browser.GetSourceAsync(); Clipboard.SetText(htmlSource); DisplayOutput("HTML Source copied to clipboard"); @@ -479,7 +479,7 @@ private void ToggleBottomToolStrip() { if (toolStrip2.Visible) { - BrowserControl.StopFinding(true); + Browser.StopFinding(true); toolStrip2.Visible = false; } else @@ -503,7 +503,7 @@ private void Find(bool next) { if (!string.IsNullOrEmpty(findTextBox.Text)) { - BrowserControl.Find(0, findTextBox.Text, next, false, false); + Browser.Find(0, findTextBox.Text, next, false, false); } } @@ -542,7 +542,7 @@ public void ShowDevToolsDocked() if (devToolsControl == null || devToolsControl.IsDisposed) { - devToolsControl = BrowserControl.ShowDevToolsDocked( + devToolsControl = Browser.ShowDevToolsDocked( parentControl: browserSplitContainer.Panel2, controlName: nameof(devToolsControl)); @@ -563,7 +563,7 @@ public Task CheckIfDevToolsIsOpenAsync() { return Cef.UIThreadTaskFactory.StartNew(() => { - return BrowserControl.GetBrowserHost().HasDevTools; + return Browser.GetBrowserHost().HasDevTools; }); } } diff --git a/CefSharp.WinForms/ChromiumWebBrowser.cs b/CefSharp.WinForms/ChromiumWebBrowser.cs index b432d31f4d..2180d207b0 100644 --- a/CefSharp.WinForms/ChromiumWebBrowser.cs +++ b/CefSharp.WinForms/ChromiumWebBrowser.cs @@ -23,7 +23,7 @@ namespace CefSharp.WinForms [Docking(DockingBehavior.AutoDock), DefaultEvent("LoadingStateChanged"), ToolboxBitmap(typeof(ChromiumWebBrowser)), Description("CefSharp ChromiumWebBrowser - Chromium Embedded Framework .Net wrapper. https://github.com/cefsharp/CefSharp"), Designer(typeof(ChromiumWebBrowserDesigner))] - public partial class ChromiumWebBrowser : ChromiumHostControlBase, IWebBrowserInternal, IWinFormsWebBrowser, IChromiumHostControl + public partial class ChromiumWebBrowser : ChromiumHostControlBase, IWebBrowserInternal, IWinFormsWebBrowser { //TODO: If we start adding more consts then extract them into a common class //Possibly in the CefSharp assembly and move the WPF ones into there as well. @@ -406,6 +406,7 @@ private void InternalDispose(bool disposing) FocusHandler = new NoFocusHandler(); browser = null; + BrowserCore = null; if (parentFormMessageInterceptor != null) { @@ -596,7 +597,6 @@ private void CreateBrowser() /// The browser. partial void OnAfterBrowserCreated(IBrowser browser) { - BrowserCore = browser; BrowserHwnd = browser.GetHost().GetWindowHandle(); // By the time this callback gets called, this control diff --git a/CefSharp.WinForms/Host/ChromiumHostControl.cs b/CefSharp.WinForms/Host/ChromiumHostControl.cs index 5f977aa107..38d5198049 100644 --- a/CefSharp.WinForms/Host/ChromiumHostControl.cs +++ b/CefSharp.WinForms/Host/ChromiumHostControl.cs @@ -5,6 +5,7 @@ using System; using System.ComponentModel; using System.Drawing; +using System.Threading.Tasks; using System.Windows.Forms; namespace CefSharp.WinForms.Host @@ -15,8 +16,17 @@ namespace CefSharp.WinForms.Host /// [Docking(DockingBehavior.AutoDock), ToolboxBitmap(typeof(ChromiumHostControl)), Designer(typeof(ChromiumWebBrowserDesigner))] - public class ChromiumHostControl : ChromiumHostControlBase, IChromiumHostControl + public class ChromiumHostControl : ChromiumHostControlBase, IChromiumWebBrowserBase { + /// + /// Get access to the core instance. + /// Maybe null if the underlying CEF Browser has not yet been + /// created or if this control has been disposed. Check + /// before accessing. + /// + [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), DefaultValue(null)] + public IBrowser BrowserCore { get; internal set; } + /// /// Event handler that will get called when the resource load for a navigation fails or is canceled. /// It's important to note this event is fired on a CEF UI thread, which by default is not the same as your application UI @@ -232,6 +242,12 @@ public void LoadUrl(string url) } } + public Task LoadUrlAsync(string url) + { + //LoadUrlAsync is actually a static method so that CefSharp.Wpf.HwndHost can reuse the code + return CefSharp.WebBrowserExtensions.LoadUrlAsync(this, url); + } + /// /// Returns the main (top-level) frame for the browser window. /// @@ -260,7 +276,7 @@ protected override void Dispose(bool disposing) LoadingStateChanged = null; StatusMessage = null; TitleChanged = null; - + BrowserCore = null; } base.Dispose(disposing); diff --git a/CefSharp.WinForms/Host/ChromiumHostControlBase.cs b/CefSharp.WinForms/Host/ChromiumHostControlBase.cs index 628fbb549b..c1d34135a0 100644 --- a/CefSharp.WinForms/Host/ChromiumHostControlBase.cs +++ b/CefSharp.WinForms/Host/ChromiumHostControlBase.cs @@ -17,12 +17,6 @@ namespace CefSharp.WinForms.Host /// public abstract class ChromiumHostControlBase : Control { - /// - /// Get access to the core instance. - /// Maybe null if the underlying CEF Browser has not yet been - /// created or if this control has been disposed. - /// - public IBrowser BrowserCore { get; internal set; } /// /// IntPtr that represents the CefBrowser Hwnd /// Used for sending messages to the browser @@ -173,7 +167,6 @@ protected override void Dispose(bool disposing) { if (disposing) { - BrowserCore = null; BrowserHwnd = IntPtr.Zero; IsBrowserInitializedChanged = null; } diff --git a/CefSharp.WinForms/Host/HostControlExtensions.cs b/CefSharp.WinForms/Host/HostControlExtensions.cs deleted file mode 100644 index 11ba92d40e..0000000000 --- a/CefSharp.WinForms/Host/HostControlExtensions.cs +++ /dev/null @@ -1,458 +0,0 @@ -// Copyright © 2021 The CefSharp Authors. All rights reserved. -// -// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. - -using System; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; -using CefSharp.Web; - -namespace CefSharp.WinForms.Host -{ - /// - /// and extensions - /// - public static class HostControlExtensions - { - /// - /// Open DevTools using as the parent control. If inspectElementAtX and/or inspectElementAtY are specified then - /// the element at the specified (x,y) location will be inspected. - /// For resize/moving to work correctly you will need to use the implementation. - /// (Set to an instance of ) - /// - /// instance - /// Control used as the parent for DevTools (a custom control will be added to the collection) - /// x coordinate (used for inspectElement) - /// y coordinate (used for inspectElement) - /// Returns the that hosts the DevTools instance if successful, otherwise returns null on error. - public static Control ShowDevToolsDocked(this IChromiumHostControl hostControl, Control parentControl, string controlName = nameof(ChromiumHostControl) + "DevTools", DockStyle dockStyle = DockStyle.Fill, int inspectElementAtX = 0, int inspectElementAtY = 0) - { - if (hostControl.IsDisposed || parentControl == null || parentControl.IsDisposed) - { - return null; - } - - return hostControl.ShowDevToolsDocked((ctrl) => { parentControl.Controls.Add(ctrl); }, controlName, dockStyle, inspectElementAtX, inspectElementAtY); - } - - /// - /// Open DevTools using your own Control as the parent. If inspectElementAtX and/or inspectElementAtY are specified then - /// the element at the specified (x,y) location will be inspected. - /// For resize/moving to work correctly you will need to use the implementation. - /// (Set to an instance of ) - /// - /// instance - /// - /// Action that is Invoked when the DevTools Host Control has been created and needs to be added to it's parent. - /// It's important the control is added to it's intended parent at this point so the - /// can be calculated to set the initial display size. - /// x coordinate (used for inspectElement) - /// y coordinate (used for inspectElement) - /// Returns the that hosts the DevTools instance if successful, otherwise returns null on error. - public static Control ShowDevToolsDocked(this IChromiumHostControl hostControl, Action addParentControl, string controlName = nameof(ChromiumHostControl) + "DevTools", DockStyle dockStyle = DockStyle.Fill, int inspectElementAtX = 0, int inspectElementAtY = 0) - { - if (hostControl.IsDisposed || addParentControl == null) - { - return null; - } - - var host = hostControl.GetBrowserHost(); - if (host == null) - { - return null; - } - - var control = new ChromiumHostControl() - { - Name = controlName, - Dock = dockStyle - }; - - control.CreateControl(); - - //It's now time for the user to add the control to it's parent - addParentControl(control); - - //Devtools will be a child of the ChromiumHostControl - var rect = control.ClientRectangle; - var windowInfo = new WindowInfo(); - var windowBounds = new CefSharp.Structs.Rect(rect.X, rect.Y, rect.Width, rect.Height); - windowInfo.SetAsChild(control.Handle, windowBounds); - host.ShowDevTools(windowInfo, inspectElementAtX, inspectElementAtY); - - return control; - } - - /// - /// Asynchronously gets the current Zoom Level. - /// - /// The ChromiumWebBrowser/ChromiumHostControl instance this method extends. - /// - /// An asynchronous result that yields the zoom level. - /// - public static Task GetZoomLevelAsync(this IChromiumHostControl hostControl) - { - return hostControl.BrowserCore.GetZoomLevelAsync(); - } - - /// - /// Change the ZoomLevel to the specified value. Can be set to 0.0 to clear the zoom level. - /// - /// - /// If called on the CEF UI thread the change will be applied immediately. Otherwise, the change will be applied asynchronously - /// on the CEF UI thread. The CEF UI thread is different to the WPF/WinForms UI Thread. - /// - /// The ChromiumWebBrowser/ChromiumHostControl instance this method extends. - /// zoom level. - public static void SetZoomLevel(this IChromiumHostControl hostControl, double zoomLevel) - { - hostControl.BrowserCore.SetZoomLevel(zoomLevel); - } - - /// - /// Search for text within the current page. - /// - /// The ChromiumWebBrowser/ChromiumHostControl instance this method extends. - /// Can be used in can conjunction with searchText to have multiple searches running simultaneously. - /// search text. - /// indicates whether to search forward or backward within the page. - /// indicates whether the search should be case-sensitive. - /// indicates whether this is the first request or a follow-up. - public static void Find(this IChromiumHostControl hostControl, int identifier, string searchText, bool forward, bool matchCase, bool findNext) - { - hostControl.BrowserCore.Find(identifier, searchText, forward, matchCase, findNext); - } - - /// - /// Cancel all searches that are currently going on. - /// - /// The ChromiumWebBrowser/ChromiumHostControl instance this method extends. - /// clear the current search selection. - public static void StopFinding(this IChromiumHostControl hostControl, bool clearSelection) - { - hostControl.BrowserCore.StopFinding(clearSelection); - } - - /// - /// Opens a Print Dialog which if used (can be user cancelled) will print the browser contents. - /// - /// The ChromiumWebBrowser/ChromiumHostControl instance this method extends. - public static void Print(this IChromiumHostControl hostControl) - { - hostControl.BrowserCore.Print(); - } - - /// - /// Stops loading the current page. - /// - /// The ChromiumWebBrowser/ChromiumHostControl instance this method extends. - public static void StopLoad(this IChromiumHostControl hostControl) - { - hostControl.BrowserCore.StopLoad(); - } - - /// - /// Navigates back, must check before calling this method. - /// - /// The ChromiumWebBrowser/ChromiumHostControl instance this method extends. - public static void GoBack(this IChromiumHostControl hostControl) - { - hostControl.BrowserCore.GoBack(); - } - - /// - /// Navigates forward, must check before calling this method. - /// - /// The ChromiumWebBrowser/ChromiumHostControl instance this method extends. - public static void GoForward(this IChromiumHostControl hostControl) - { - hostControl.BrowserCore.GoForward(); - } - - /// - /// Reloads the page being displayed, optionally ignoring the cache (which means the whole page including all .css, .js etc. - /// resources will be re-fetched). - /// - /// The ChromiumWebBrowser/ChromiumHostControl instance this method extends. - /// true A reload is performed ignoring browser cache; false A reload is performed using - /// files from the browser cache, if available. - public static void Reload(this IChromiumHostControl hostControl, bool ignoreCache = false) - { - hostControl.BrowserCore.Reload(ignoreCache); - } - - /// - /// Retrieve the main frame's HTML source using a . - /// - /// The ChromiumWebBrowser/ChromiumHostControl instance this method extends. - /// - /// that when executed returns the main frame source as a string. - /// - public static Task GetSourceAsync(this IChromiumHostControl hostControl) - { - return hostControl.BrowserCore.GetSourceAsync(); - } - - /// - /// Shortcut method to get the browser . - /// - /// The ChromiumWebBrowser/ChromiumHostControl instance this method extends. - /// IBrowserHost instance or null - public static IBrowserHost GetBrowserHost(this IChromiumHostControl hostControl) - { - var cefBrowser = hostControl.BrowserCore; - - return cefBrowser == null ? null : cefBrowser.GetHost(); - } - - /// - /// Loads the specified in the Main Frame. - /// If is true then the method call will be ignored. - /// - /// The ChromiumWebBrowser/ChromiumHostControl instance this method extends. - /// url to load in Main Frame. - public static void LoadUrl(this IChromiumHostControl hostControl, string url) - { - if (hostControl.IsDisposed) - { - return; - } - - var browser = hostControl.BrowserCore; - - if (browser == null) - { - throw new Exception(CefSharp.WebBrowserExtensions.BrowserNullExceptionString); - } - - using (var mainFrame = browser.MainFrame) - { - mainFrame.LoadUrl(url); - } - } - - /// - /// Returns the focused frame for the browser window. - /// - /// The ChromiumWebBrowser/ChromiumHostControl instance this method extends. - /// the focused frame. - public static IFrame GetFocusedFrame(this IChromiumHostControl hostControl) - { - var browser = hostControl.BrowserCore; - - if (browser == null) - { - throw new Exception(CefSharp.WebBrowserExtensions.BrowserNullExceptionString); - } - - return browser.FocusedFrame; - } - - /// - /// Returns the main (top-level) frame for the browser window. - /// - /// The ChromiumWebBrowser/ChromiumHostControl instance this method extends. - /// the main frame. - public static IFrame GetMainFrame(this IChromiumHostControl hostControl) - { - var browser = hostControl.BrowserCore; - - if (browser == null) - { - throw new Exception(CefSharp.WebBrowserExtensions.BrowserNullExceptionString); - } - - return browser.MainFrame; - } - - /// - /// Execute Undo on the focused frame. - /// - /// The ChromiumWebBrowser/ChromiumHostControl instance this method extends. - public static void Undo(this IChromiumHostControl hostControl) - { - using (var frame = hostControl.GetFocusedFrame()) - { - ThrowExceptionIfFrameNull(frame); - - frame.Undo(); - } - } - - /// - /// Execute Redo on the focused frame. - /// - /// The ChromiumWebBrowser/ChromiumHostControl instance this method extends. - public static void Redo(this IChromiumHostControl hostControl) - { - using (var frame = hostControl.GetFocusedFrame()) - { - ThrowExceptionIfFrameNull(frame); - - frame.Redo(); - } - } - - /// - /// Execute Cut on the focused frame. - /// - /// The ChromiumWebBrowser/ChromiumHostControl instance this method extends. - public static void Cut(this IChromiumHostControl hostControl) - { - using (var frame = hostControl.GetFocusedFrame()) - { - ThrowExceptionIfFrameNull(frame); - - frame.Cut(); - } - } - - /// - /// Execute Copy on the focused frame. - /// - /// The ChromiumWebBrowser/ChromiumHostControl instance this method extends. - public static void Copy(this IChromiumHostControl hostControl) - { - using (var frame = hostControl.GetFocusedFrame()) - { - ThrowExceptionIfFrameNull(frame); - - frame.Copy(); - } - } - - /// - /// Execute Paste on the focused frame. - /// - /// The ChromiumWebBrowser/ChromiumHostControl instance this method extends. - public static void Paste(this IChromiumHostControl hostControl) - { - using (var frame = hostControl.GetFocusedFrame()) - { - ThrowExceptionIfFrameNull(frame); - - frame.Paste(); - } - } - - /// - /// Execute Delete on the focused frame. - /// - /// The ChromiumWebBrowser/ChromiumHostControl instance this method extends. - public static void Delete(this IChromiumHostControl hostControl) - { - using (var frame = hostControl.GetFocusedFrame()) - { - ThrowExceptionIfFrameNull(frame); - - frame.Delete(); - } - } - - /// - /// Execute SelectAll on the focused frame. - /// - /// The ChromiumWebBrowser/ChromiumHostControl instance this method extends. - public static void SelectAll(this IChromiumHostControl hostControl) - { - using (var frame = hostControl.GetFocusedFrame()) - { - ThrowExceptionIfFrameNull(frame); - - frame.SelectAll(); - } - } - - /// - /// Opens up a new program window (using the default text editor) where the source code of the currently displayed web page is - /// shown. - /// - /// The ChromiumWebBrowser/ChromiumHostControl instance this method extends. - public static void ViewSource(this IChromiumHostControl hostControl) - { - using (var frame = hostControl.GetMainFrame()) - { - ThrowExceptionIfFrameNull(frame); - - frame.ViewSource(); - } - } - - /// - /// Open developer tools in its own window. - /// - /// The ChromiumWebBrowser/ChromiumHostControl instance this method extends. - /// (Optional) window info used for showing dev tools. - /// (Optional) x coordinate (used for inspectElement) - /// (Optional) y coordinate (used for inspectElement) - public static void ShowDevTools(this IChromiumHostControl hostControl, IWindowInfo windowInfo = null, int inspectElementAtX = 0, int inspectElementAtY = 0) - { - var host = hostControl.GetBrowserHost(); - CefSharp.WebBrowserExtensions.ThrowExceptionIfBrowserHostNull(host); - - host.ShowDevTools(windowInfo, inspectElementAtX, inspectElementAtY); - } - - /// - /// Explicitly close the developer tools window if one exists for this browser instance. - /// - /// The ChromiumWebBrowser/ChromiumHostControl instance this method extends. - public static void CloseDevTools(this IChromiumHostControl hostControl) - { - var host = hostControl.GetBrowserHost(); - CefSharp.WebBrowserExtensions.ThrowExceptionIfBrowserHostNull(host); - - host.CloseDevTools(); - } - - /// - /// Asynchronously prints the current browser contents to the PDF file specified. The caller is responsible for deleting the file - /// when done. - /// - /// The ChromiumWebBrowser/ChromiumHostControl instance this method extends. - /// Output file location. - /// (Optional) Print Settings. - /// - /// A task that represents the asynchronous print operation. The result is true on success or false on failure to generate the - /// Pdf. - /// - public static Task PrintToPdfAsync(this IChromiumHostControl hostControl, string path, PdfPrintSettings settings = null) - { - var host = hostControl.GetBrowserHost(); - CefSharp.WebBrowserExtensions.ThrowExceptionIfBrowserHostNull(host); - - var callback = new TaskPrintToPdfCallback(); - host.PrintToPdf(path, settings, callback); - - return callback.Task; - } - - /// - /// Loads html as Data Uri See https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs for details If - /// base64Encode is false then html will be Uri encoded. - /// - /// The ChromiumWebBrowser/ChromiumHostControl instance this method extends. - /// Html to load as data uri. - /// (Optional) if true the html string will be base64 encoded using UTF8 encoding. - public static void LoadHtml(this IChromiumHostControl hostControl, string html, bool base64Encode = false) - { - var htmlString = new HtmlString(html, base64Encode); - - hostControl.LoadUrl(htmlString.ToDataUriString()); - } - - /// - /// Throw exception if frame null. - /// - /// Thrown when an exception error condition occurs. - /// The instance this method extends. - private static void ThrowExceptionIfFrameNull(IFrame frame) - { - if (frame == null) - { - throw new Exception(CefSharp.WebBrowserExtensions.FrameNullExceptionString); - } - } - } -} diff --git a/CefSharp.WinForms/WebBrowserExtensions.cs b/CefSharp.WinForms/WebBrowserExtensions.cs index de2115592c..4616a6de70 100644 --- a/CefSharp.WinForms/WebBrowserExtensions.cs +++ b/CefSharp.WinForms/WebBrowserExtensions.cs @@ -71,7 +71,7 @@ public static bool DestroyWindow(this IWebBrowser chromiumWebBrowser) /// x coordinate (used for inspectElement) /// y coordinate (used for inspectElement) /// Returns the that hosts the DevTools instance if successful, otherwise returns null on error. - public static Control ShowDevToolsDocked(this IWebBrowser chromiumWebBrowser, Control parentControl, string controlName = nameof(ChromiumHostControl) + "DevTools", DockStyle dockStyle = DockStyle.Fill, int inspectElementAtX = 0, int inspectElementAtY = 0) + public static Control ShowDevToolsDocked(this IChromiumWebBrowserBase chromiumWebBrowser, Control parentControl, string controlName = nameof(ChromiumHostControl) + "DevTools", DockStyle dockStyle = DockStyle.Fill, int inspectElementAtX = 0, int inspectElementAtY = 0) { if (chromiumWebBrowser.IsDisposed || parentControl == null || parentControl.IsDisposed) { @@ -95,7 +95,7 @@ public static Control ShowDevToolsDocked(this IWebBrowser chromiumWebBrowser, Co /// x coordinate (used for inspectElement) /// y coordinate (used for inspectElement) /// Returns the that hosts the DevTools instance if successful, otherwise returns null on error. - public static Control ShowDevToolsDocked(this IWebBrowser chromiumWebBrowser, Action addParentControl, string controlName = nameof(ChromiumHostControl) + "DevTools", DockStyle dockStyle = DockStyle.Fill, int inspectElementAtX = 0, int inspectElementAtY = 0) + public static Control ShowDevToolsDocked(this IChromiumWebBrowserBase chromiumWebBrowser, Action addParentControl, string controlName = nameof(ChromiumHostControl) + "DevTools", DockStyle dockStyle = DockStyle.Fill, int inspectElementAtX = 0, int inspectElementAtY = 0) { if (chromiumWebBrowser.IsDisposed || addParentControl == null) { diff --git a/CefSharp.Wpf/ChromiumWebBrowser.cs b/CefSharp.Wpf/ChromiumWebBrowser.cs index 38dbd2fc4e..baf5a4f5fd 100644 --- a/CefSharp.Wpf/ChromiumWebBrowser.cs +++ b/CefSharp.Wpf/ChromiumWebBrowser.cs @@ -656,6 +656,7 @@ private void InternalDispose(bool disposing) FocusHandler = new NoFocusHandler(); browser = null; + BrowserCore = null; // In case we accidentally have a reference to the CEF drag data currentDragData?.Dispose(); diff --git a/CefSharp.WinForms/Host/IChromiumHostControl.cs b/CefSharp/IChromiumWebBrowserBase.cs similarity index 83% rename from CefSharp.WinForms/Host/IChromiumHostControl.cs rename to CefSharp/IChromiumWebBrowserBase.cs index 1e74f88ff7..51f8bc3d7b 100644 --- a/CefSharp.WinForms/Host/IChromiumHostControl.cs +++ b/CefSharp/IChromiumWebBrowserBase.cs @@ -3,14 +3,15 @@ // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. using System; +using System.Threading.Tasks; -namespace CefSharp.WinForms.Host +namespace CefSharp { /// - /// Chromium Host Control Interface used by WinForms as a common reference - /// for and + /// Interface for common events/methods/properties for and popup host implementations. /// - public interface IChromiumHostControl + /// + public interface IChromiumWebBrowserBase : IDisposable { /// /// Event handler for receiving Javascript console messages being sent from web pages. @@ -72,30 +73,6 @@ public interface IChromiumHostControl /// event EventHandler LoadingStateChanged; - /// - /// Occurs when the browser address changed. - /// It's important to note this event is fired on a CEF UI thread, which by default is not the same as your application UI - /// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang.. - /// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread. - /// - event EventHandler AddressChanged; - - /// - /// Occurs when the browser title changed. - /// It's important to note this event is fired on a CEF UI thread, which by default is not the same as your application UI - /// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang.. - /// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread. - /// - event EventHandler TitleChanged; - - /// - /// Event called after the underlying CEF browser instance has been created. - /// It's important to note this event is fired on a CEF UI thread, which by default is not the same as your application UI - /// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang.. - /// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread. - /// - event EventHandler IsBrowserInitializedChanged; - /// /// Loads the specified in the Main Frame. /// Same as calling @@ -108,6 +85,16 @@ public interface IChromiumHostControl /// void LoadUrl(string url); + /// + /// Load the in the main frame of the browser + /// + /// url to load + /// + /// A that can be awaited to load the and return the HttpStatusCode and . + /// A HttpStatusCode equal to 200 and is considered a success. + /// + Task LoadUrlAsync(string url); + /// /// A flag that indicates whether the WebBrowser is initialized (true) or not (false). /// diff --git a/CefSharp/IWebBrowser.cs b/CefSharp/IWebBrowser.cs index 56169138b7..fc9f9deb96 100644 --- a/CefSharp/IWebBrowser.cs +++ b/CefSharp/IWebBrowser.cs @@ -3,7 +3,6 @@ // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. using System; -using System.Threading; using System.Threading.Tasks; namespace CefSharp @@ -12,69 +11,8 @@ namespace CefSharp /// ChromiumWebBrowser implementations implement this interface. Can be cast to /// the concrete implementation to access UI specific features. /// - /// - public interface IWebBrowser : IDisposable + public interface IWebBrowser : IChromiumWebBrowserBase { - /// - /// Event handler for receiving Javascript console messages being sent from web pages. - /// It's important to note this event is fired on a CEF UI thread, which by default is not the same as your application UI - /// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang.. - /// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread. - /// (The exception to this is when you're running with settings.MultiThreadedMessageLoop = false, then they'll be the same thread). - /// - event EventHandler ConsoleMessage; - - /// - /// Event handler for changes to the status message. - /// It's important to note this event is fired on a CEF UI thread, which by default is not the same as your application UI - /// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang. - /// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread. - /// (The exception to this is when you're running with settings.MultiThreadedMessageLoop = false, then they'll be the same thread). - /// - event EventHandler StatusMessage; - - /// - /// Event handler that will get called when the browser begins loading a frame. Multiple frames may be loading at the same - /// time. Sub-frames may start or continue loading after the main frame load has ended. This method may not be called for a - /// particular frame if the load request for that frame fails. For notification of overall browser load status use - /// OnLoadingStateChange instead. - /// It's important to note this event is fired on a CEF UI thread, which by default is not the same as your application UI - /// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang.. - /// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread. - /// - /// Whilst this may seem like a logical place to execute js, it's called before the DOM has been loaded, implement - /// as it's called when the underlying V8Context is created - /// - event EventHandler FrameLoadStart; - - /// - /// Event handler that will get called when the browser is done loading a frame. Multiple frames may be loading at the same - /// time. Sub-frames may start or continue loading after the main frame load has ended. This method will always be called - /// for all frames irrespective of whether the request completes successfully. - /// It's important to note this event is fired on a CEF UI thread, which by default is not the same as your application UI - /// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang.. - /// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread. - /// - event EventHandler FrameLoadEnd; - - /// - /// Event handler that will get called when the resource load for a navigation fails or is canceled. - /// It's important to note this event is fired on a CEF UI thread, which by default is not the same as your application UI - /// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang.. - /// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread. - /// - event EventHandler LoadError; - - /// - /// Event handler that will get called when the Loading state has changed. - /// This event will be fired twice. Once when loading is initiated either programmatically or - /// by user action, and once when loading is terminated due to completion, cancellation of failure. - /// It's important to note this event is fired on a CEF UI thread, which by default is not the same as your application UI - /// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang.. - /// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread. - /// - event EventHandler LoadingStateChanged; - /// /// Event handler that will get called when the message that originates from CefSharp.PostMessage /// @@ -88,28 +26,6 @@ public interface IWebBrowser : IDisposable /// The URL to be loaded. void Load(string url); - /// - /// Loads the specified in the Main Frame. - /// Same as calling - /// - /// The URL to be loaded. - /// - /// This is exactly the same as calling Load(string), it was added - /// as the method name is more meaningful and easier to discover - /// via Intellisense. - /// - void LoadUrl(string url); - - /// - /// Load the in the main frame of the browser - /// - /// url to load - /// - /// A that can be awaited to load the and return the HttpStatusCode and . - /// A HttpStatusCode equal to 200 and is considered a success. - /// - Task LoadUrlAsync(string url); - /// /// Wait for the Browser to finish loading the initial web page. /// @@ -218,55 +134,6 @@ public interface IWebBrowser : IDisposable /// IFrameHandler FrameHandler { get; set; } - /// - /// A flag that indicates whether the WebBrowser is initialized (true) or not (false). - /// - /// true if this instance is browser initialized; otherwise, false. - /// In the WPF control there are two IsBrowserInitialized properties, the ChromiumWebBrowser.IsBrowserInitialized - /// property is implemented as a Dependency Property and fully supports data binding. This property - /// can only be called from the UI Thread. The explicit IWebBrowser.IsBrowserInitialized interface implementation that - /// can be called from any Thread. - bool IsBrowserInitialized { get; } - - /// - /// A flag that indicates whether the WebBrowser has been disposed () or not () - /// - /// if this instance is disposed; otherwise, - bool IsDisposed { get; } - - /// - /// A flag that indicates whether the control is currently loading one or more web pages (true) or not (false). - /// - /// true if this instance is loading; otherwise, false. - /// In the WPF control, this property is implemented as a Dependency Property and fully supports data - /// binding. - bool IsLoading { get; } - - /// - /// A flag that indicates whether the state of the control current supports the GoBack action (true) or not (false). - /// - /// true if this instance can go back; otherwise, false. - /// In the WPF control, this property is implemented as a Dependency Property and fully supports data - /// binding. - bool CanGoBack { get; } - - /// - /// A flag that indicates whether the state of the control currently supports the GoForward action (true) or not (false). - /// - /// true if this instance can go forward; otherwise, false. - /// In the WPF control, this property is implemented as a Dependency Property and fully supports data - /// binding. - bool CanGoForward { get; } - - /// - /// The address (URL) which the browser control is currently displaying. - /// Will automatically be updated as the user navigates to another page (e.g. by clicking on a link). - /// - /// The address. - /// In the WPF control, this property is implemented as a Dependency Property and fully supports data - /// binding. - string Address { get; } - /// /// The text that will be displayed as a ToolTip /// @@ -288,13 +155,6 @@ public interface IWebBrowser : IDisposable /// IRequestContext RequestContext { get; } - /// - /// Attempts to give focus to the IWebBrowser control. - /// - /// true if keyboard focus and logical focus were set to this element; false if only logical focus - /// was set to this element, or if the call to this method did not force the focus to change. - bool Focus(); - /// /// Returns the current CEF Browser Instance /// diff --git a/CefSharp/Internals/Partial/ChromiumWebBrowser.Partial.cs b/CefSharp/Internals/Partial/ChromiumWebBrowser.Partial.cs index 7824827016..c7a8be95f0 100644 --- a/CefSharp/Internals/Partial/ChromiumWebBrowser.Partial.cs +++ b/CefSharp/Internals/Partial/ChromiumWebBrowser.Partial.cs @@ -58,6 +58,15 @@ public partial class ChromiumWebBrowser /// private Action initialLoadAction; + /// + /// Get access to the core instance. + /// Maybe null if the underlying CEF Browser has not yet been + /// created or if this control has been disposed. Check + /// before accessing. + /// + [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), DefaultValue(null)] + public IBrowser BrowserCore { get; internal set; } + /// /// A flag that indicates if you can execute javascript in the main frame. /// Flag is set to true in IRenderProcessMessageHandler.OnContextCreated. @@ -228,7 +237,7 @@ public partial class ChromiumWebBrowser /// A flag that indicates whether the WebBrowser is initialized (true) or not (false). /// /// true if this instance is browser initialized; otherwise, false. - bool IWebBrowser.IsBrowserInitialized + bool IChromiumWebBrowserBase.IsBrowserInitialized { get { return InternalIsBrowserInitialized(); } } @@ -327,6 +336,7 @@ void IWebBrowserInternal.OnAfterBrowserCreated(IBrowser browser) } this.browser = browser; + BrowserCore = browser; initialLoadAction = InitialLoad; Interlocked.Exchange(ref browserInitialized, 1); diff --git a/CefSharp/WebBrowserExtensions.cs b/CefSharp/WebBrowserExtensions.cs index 0cdd7a4c2d..dc0a94127d 100644 --- a/CefSharp/WebBrowserExtensions.cs +++ b/CefSharp/WebBrowserExtensions.cs @@ -65,9 +65,9 @@ public static void RegisterAsyncJsObject(this IWebBrowser webBrowser, string nam /// /// the ChromiumWebBrowser instance. /// the main frame. - public static IFrame GetMainFrame(this IWebBrowser browser) + public static IFrame GetMainFrame(this IChromiumWebBrowserBase browser) { - var cefBrowser = browser.GetBrowser(); + var cefBrowser = browser.BrowserCore; cefBrowser.ThrowExceptionIfBrowserNull(); @@ -79,9 +79,9 @@ public static IFrame GetMainFrame(this IWebBrowser browser) /// /// the ChromiumWebBrowser instance. /// the focused frame. - public static IFrame GetFocusedFrame(this IWebBrowser browser) + public static IFrame GetFocusedFrame(this IChromiumWebBrowserBase browser) { - var cefBrowser = browser.GetBrowser(); + var cefBrowser = browser.BrowserCore; cefBrowser.ThrowExceptionIfBrowserNull(); @@ -92,14 +92,9 @@ public static IFrame GetFocusedFrame(this IWebBrowser browser) /// Execute Undo on the focused frame. /// /// The ChromiumWebBrowser instance this method extends. - public static void Undo(this IWebBrowser browser) + public static void Undo(this IChromiumWebBrowserBase browser) { - using (var frame = browser.GetFocusedFrame()) - { - ThrowExceptionIfFrameNull(frame); - - frame.Undo(); - } + browser.BrowserCore.Undo(); } /// @@ -122,14 +117,9 @@ public static void Undo(this IBrowser browser) /// Execute Redo on the focused frame. /// /// The ChromiumWebBrowser instance this method extends. - public static void Redo(this IWebBrowser browser) + public static void Redo(this IChromiumWebBrowserBase browser) { - using (var frame = browser.GetFocusedFrame()) - { - ThrowExceptionIfFrameNull(frame); - - frame.Redo(); - } + browser.BrowserCore.Redo(); } /// @@ -152,14 +142,9 @@ public static void Redo(this IBrowser browser) /// Execute Cut on the focused frame. /// /// The ChromiumWebBrowser instance this method extends. - public static void Cut(this IWebBrowser browser) + public static void Cut(this IChromiumWebBrowserBase browser) { - using (var frame = browser.GetFocusedFrame()) - { - ThrowExceptionIfFrameNull(frame); - - frame.Cut(); - } + browser.BrowserCore.Cut(); } /// @@ -182,14 +167,9 @@ public static void Cut(this IBrowser browser) /// Execute Copy on the focused frame. /// /// The ChromiumWebBrowser instance this method extends. - public static void Copy(this IWebBrowser browser) + public static void Copy(this IChromiumWebBrowserBase browser) { - using (var frame = browser.GetFocusedFrame()) - { - ThrowExceptionIfFrameNull(frame); - - frame.Copy(); - } + browser.BrowserCore.Copy(); } /// @@ -212,14 +192,9 @@ public static void Copy(this IBrowser browser) /// Execute Paste on the focused frame. /// /// The ChromiumWebBrowser instance this method extends. - public static void Paste(this IWebBrowser browser) + public static void Paste(this IChromiumWebBrowserBase browser) { - using (var frame = browser.GetFocusedFrame()) - { - ThrowExceptionIfFrameNull(frame); - - frame.Paste(); - } + browser.BrowserCore.Paste(); } /// @@ -242,14 +217,9 @@ public static void Paste(this IBrowser browser) /// Execute Delete on the focused frame. /// /// The ChromiumWebBrowser instance this method extends. - public static void Delete(this IWebBrowser browser) + public static void Delete(this IChromiumWebBrowserBase browser) { - using (var frame = browser.GetFocusedFrame()) - { - ThrowExceptionIfFrameNull(frame); - - frame.Delete(); - } + browser.BrowserCore.Delete(); } /// @@ -272,14 +242,9 @@ public static void Delete(this IBrowser browser) /// Execute SelectAll on the focused frame. /// /// The ChromiumWebBrowser instance this method extends. - public static void SelectAll(this IWebBrowser browser) + public static void SelectAll(this IChromiumWebBrowserBase browser) { - using (var frame = browser.GetFocusedFrame()) - { - ThrowExceptionIfFrameNull(frame); - - frame.SelectAll(); - } + browser.BrowserCore.SelectAll(); } /// @@ -303,14 +268,9 @@ public static void SelectAll(this IBrowser browser) /// shown. /// /// The ChromiumWebBrowser instance this method extends. - public static void ViewSource(this IWebBrowser browser) + public static void ViewSource(this IChromiumWebBrowserBase browser) { - using (var frame = browser.GetMainFrame()) - { - ThrowExceptionIfFrameNull(frame); - - frame.ViewSource(); - } + browser.BrowserCore.ViewSource(); } /// @@ -337,14 +297,9 @@ public static void ViewSource(this IBrowser browser) /// /// that when executed returns the main frame source as a string. /// - public static Task GetSourceAsync(this IWebBrowser browser) + public static Task GetSourceAsync(this IChromiumWebBrowserBase browser) { - using (var frame = browser.GetMainFrame()) - { - ThrowExceptionIfFrameNull(frame); - - return frame.GetSourceAsync(); - } + return browser.BrowserCore.GetSourceAsync(); } /// @@ -373,14 +328,9 @@ public static Task GetSourceAsync(this IBrowser browser) /// /// that when executed returns the main frame display text as a string. /// - public static Task GetTextAsync(this IWebBrowser browser) + public static Task GetTextAsync(this IChromiumWebBrowserBase browser) { - using (var frame = browser.GetMainFrame()) - { - ThrowExceptionIfFrameNull(frame); - - return frame.GetTextAsync(); - } + return browser.BrowserCore.GetTextAsync(); } /// @@ -407,13 +357,9 @@ public static Task GetTextAsync(this IBrowser browser) /// /// The ChromiumWebBrowser instance this method extends. /// url to download - public static void StartDownload(this IWebBrowser browser, string url) + public static void StartDownload(this IChromiumWebBrowserBase browser, string url) { - var host = browser.GetBrowserHost(); - - ThrowExceptionIfBrowserHostNull(host); - - host.StartDownload(url); + browser.BrowserCore.StartDownload(url); } /// @@ -441,7 +387,7 @@ public static void StartDownload(this IBrowser browser, string url) /// /// url to load /// See for details - public static Task LoadUrlAsync(IWebBrowser chromiumWebBrowser, string url) + public static Task LoadUrlAsync(IChromiumWebBrowserBase chromiumWebBrowser, string url) { if(string.IsNullOrEmpty(url)) { @@ -509,7 +455,7 @@ public static Task LoadUrlAsync(IWebBrowser chromiumWebBro chromiumWebBrowser.LoadError += loadErrorHandler; chromiumWebBrowser.LoadingStateChanged += loadingStateChangeHandler; - chromiumWebBrowser.Load(url); + chromiumWebBrowser.LoadUrl(url); return tcs.Task; } @@ -552,7 +498,7 @@ public static void ExecuteScriptAsync(this IBrowser browser, string methodName, /// /// The ChromiumWebBrowser instance this method extends. /// The Javascript code that should be executed. - public static void ExecuteScriptAsync(this IWebBrowser browser, string script) + public static void ExecuteScriptAsync(this IChromiumWebBrowserBase browser, string script) { using (var frame = browser.GetMainFrame()) { @@ -593,7 +539,7 @@ public static void ExecuteScriptAsync(this IBrowser browser, string script) /// The ChromiumWebBrowser instance this method extends. /// The Javascript code that should be executed. /// (Optional) The script will only be executed on first page load, subsequent page loads will be ignored. - public static void ExecuteScriptAsyncWhenPageLoaded(this IWebBrowser webBrowser, string script, bool oneTime = true) + public static void ExecuteScriptAsyncWhenPageLoaded(this IChromiumWebBrowserBase webBrowser, string script, bool oneTime = true) { var useLoadingStateChangedEventHandler = webBrowser.IsBrowserInitialized == false || oneTime == false; @@ -601,7 +547,7 @@ public static void ExecuteScriptAsyncWhenPageLoaded(this IWebBrowser webBrowser, if (webBrowser.IsBrowserInitialized) { //CefBrowser wrapper - var browser = webBrowser.GetBrowser(); + var browser = webBrowser.BrowserCore; if (browser.HasDocument && browser.IsLoading == false) { webBrowser.ExecuteScriptAsync(script); @@ -644,9 +590,9 @@ public static void ExecuteScriptAsyncWhenPageLoaded(this IWebBrowser webBrowser, /// url to load /// post data as byte array /// (Optional) if set the Content-Type header will be set - public static void LoadUrlWithPostData(this IWebBrowser browser, string url, byte[] postDataBytes, string contentType = null) + public static void LoadUrlWithPostData(this IChromiumWebBrowserBase browser, string url, byte[] postDataBytes, string contentType = null) { - browser.GetBrowser().LoadUrlWithPostData(url, postDataBytes, contentType); + browser.BrowserCore.LoadUrlWithPostData(url, postDataBytes, contentType); } /// @@ -714,11 +660,11 @@ public static bool LoadHtml(this IWebBrowser browser, string html, string url) /// The ChromiumWebBrowser instance this method extends. /// Html to load as data uri. /// (Optional) if true the html string will be base64 encoded using UTF8 encoding. - public static void LoadHtml(this IWebBrowser browser, string html, bool base64Encode = false) + public static void LoadHtml(this IChromiumWebBrowserBase browser, string html, bool base64Encode = false) { var htmlString = new HtmlString(html, base64Encode); - browser.Load(htmlString.ToDataUriString()); + browser.LoadUrl(htmlString.ToDataUriString()); } /// @@ -832,13 +778,9 @@ public static void UnRegisterResourceHandler(this IWebBrowser browser, string ur /// Stops loading the current page. /// /// The ChromiumWebBrowser instance this method extends. - public static void Stop(this IWebBrowser browser) + public static void Stop(this IChromiumWebBrowserBase browser) { - var cefBrowser = browser.GetBrowser(); - - cefBrowser.ThrowExceptionIfBrowserNull(); - - cefBrowser.StopLoad(); + browser.BrowserCore.Stop(); } /// @@ -856,13 +798,9 @@ public static void Stop(this IBrowser browser) /// Navigates back, must check before calling this method. /// /// The ChromiumWebBrowser instance this method extends. - public static void Back(this IWebBrowser browser) + public static void Back(this IChromiumWebBrowserBase browser) { - var cefBrowser = browser.GetBrowser(); - - cefBrowser.ThrowExceptionIfBrowserNull(); - - cefBrowser.GoBack(); + browser.BrowserCore.Back(); } /// @@ -880,13 +818,9 @@ public static void Back(this IBrowser browser) /// Navigates forward, must check before calling this method. /// /// The ChromiumWebBrowser instance this method extends. - public static void Forward(this IWebBrowser browser) + public static void Forward(this IChromiumWebBrowserBase browser) { - var cefBrowser = browser.GetBrowser(); - - cefBrowser.ThrowExceptionIfBrowserNull(); - - cefBrowser.GoForward(); + browser.BrowserCore.Forward(); } /// @@ -904,7 +838,7 @@ public static void Forward(this IBrowser browser) /// Reloads the page being displayed. This method will use data from the browser's cache, if available. /// /// The ChromiumWebBrowser instance this method extends. - public static void Reload(this IWebBrowser browser) + public static void Reload(this IChromiumWebBrowserBase browser) { browser.Reload(false); } @@ -916,13 +850,9 @@ public static void Reload(this IWebBrowser browser) /// The ChromiumWebBrowser instance this method extends. /// true A reload is performed ignoring browser cache; false A reload is performed using /// files from the browser cache, if available. - public static void Reload(this IWebBrowser browser, bool ignoreCache) + public static void Reload(this IChromiumWebBrowserBase browser, bool ignoreCache) { - var cefBrowser = browser.GetBrowser(); - - cefBrowser.ThrowExceptionIfBrowserNull(); - - cefBrowser.Reload(ignoreCache); + browser.BrowserCore.Reload(ignoreCache); } /// @@ -987,10 +917,9 @@ public static Task GetZoomLevelAsync(this IBrowser cefBrowser) /// /// An asynchronous result that yields the zoom level. /// - public static Task GetZoomLevelAsync(this IWebBrowser browser) + public static Task GetZoomLevelAsync(this IChromiumWebBrowserBase browser) { - var cefBrowser = browser.GetBrowser(); - return cefBrowser.GetZoomLevelAsync(); + return browser.BrowserCore.GetZoomLevelAsync(); } /// @@ -1021,10 +950,9 @@ public static void SetZoomLevel(this IBrowser cefBrowser, double zoomLevel) /// /// The ChromiumWebBrowser instance this method extends. /// zoom level. - public static void SetZoomLevel(this IWebBrowser browser, double zoomLevel) + public static void SetZoomLevel(this IChromiumWebBrowserBase browser, double zoomLevel) { - var cefBrowser = browser.GetBrowser(); - cefBrowser.SetZoomLevel(zoomLevel); + browser.BrowserCore.SetZoomLevel(zoomLevel); } /// @@ -1053,9 +981,9 @@ public static void Find(this IBrowser cefBrowser, int identifier, string searchT /// indicates whether to search forward or backward within the page. /// indicates whether the search should be case-sensitive. /// indicates whether this is the first request or a follow-up. - public static void Find(this IWebBrowser browser, int identifier, string searchText, bool forward, bool matchCase, bool findNext) + public static void Find(this IChromiumWebBrowserBase browser, int identifier, string searchText, bool forward, bool matchCase, bool findNext) { - var cefBrowser = browser.GetBrowser(); + var cefBrowser = browser.BrowserCore; cefBrowser.ThrowExceptionIfBrowserNull(); cefBrowser.Find(identifier, searchText, forward, matchCase, findNext); @@ -1081,12 +1009,9 @@ public static void StopFinding(this IBrowser cefBrowser, bool clearSelection) /// /// The ChromiumWebBrowser instance this method extends. /// clear the current search selection. - public static void StopFinding(this IWebBrowser browser, bool clearSelection) + public static void StopFinding(this IChromiumWebBrowserBase browser, bool clearSelection) { - var cefBrowser = browser.GetBrowser(); - cefBrowser.ThrowExceptionIfBrowserNull(); - - cefBrowser.StopFinding(clearSelection); + browser.BrowserCore.StopFinding(clearSelection); } /// @@ -1127,9 +1052,9 @@ public static Task PrintToPdfAsync(this IBrowser cefBrowser, string path, /// Opens a Print Dialog which if used (can be user cancelled) will print the browser contents. /// /// The ChromiumWebBrowser instance this method extends. - public static void Print(this IWebBrowser browser) + public static void Print(this IChromiumWebBrowserBase browser) { - var cefBrowser = browser.GetBrowser(); + var cefBrowser = browser.BrowserCore; cefBrowser.ThrowExceptionIfBrowserNull(); cefBrowser.Print(); @@ -1146,12 +1071,9 @@ public static void Print(this IWebBrowser browser) /// A task that represents the asynchronous print operation. The result is true on success or false on failure to generate the /// Pdf. /// - public static Task PrintToPdfAsync(this IWebBrowser browser, string path, PdfPrintSettings settings = null) + public static Task PrintToPdfAsync(this IChromiumWebBrowserBase browser, string path, PdfPrintSettings settings = null) { - var cefBrowser = browser.GetBrowser(); - cefBrowser.ThrowExceptionIfBrowserNull(); - - return cefBrowser.PrintToPdfAsync(path, settings); + return browser.BrowserCore.PrintToPdfAsync(path, settings); } /// @@ -1176,11 +1098,9 @@ public static void ShowDevTools(this IBrowser cefBrowser, IWindowInfo windowInfo /// (Optional) window info used for showing dev tools. /// (Optional) x coordinate (used for inspectElement) /// (Optional) y coordinate (used for inspectElement) - public static void ShowDevTools(this IWebBrowser browser, IWindowInfo windowInfo = null, int inspectElementAtX = 0, int inspectElementAtY = 0) + public static void ShowDevTools(this IChromiumWebBrowserBase browser, IWindowInfo windowInfo = null, int inspectElementAtX = 0, int inspectElementAtY = 0) { - var cefBrowser = browser.GetBrowser(); - cefBrowser.ThrowExceptionIfBrowserNull(); - cefBrowser.ShowDevTools(windowInfo, inspectElementAtX, inspectElementAtY); + browser.BrowserCore.ShowDevTools(windowInfo, inspectElementAtX, inspectElementAtY); } /// @@ -1199,11 +1119,9 @@ public static void CloseDevTools(this IBrowser cefBrowser) /// Explicitly close the developer tools window if one exists for this browser instance. /// /// The ChromiumWebBrowser instance this method extends. - public static void CloseDevTools(this IWebBrowser browser) + public static void CloseDevTools(this IChromiumWebBrowserBase browser) { - var cefBrowser = browser.GetBrowser(); - cefBrowser.ThrowExceptionIfBrowserNull(); - cefBrowser.CloseDevTools(); + browser.BrowserCore.CloseDevTools(); } /// @@ -1224,12 +1142,9 @@ public static void ReplaceMisspelling(this IBrowser cefBrowser, string word) /// /// The ChromiumWebBrowser instance this method extends. /// The new word that will replace the currently selected word. - public static void ReplaceMisspelling(this IWebBrowser browser, string word) + public static void ReplaceMisspelling(this IChromiumWebBrowserBase browser, string word) { - var cefBrowser = browser.GetBrowser(); - cefBrowser.ThrowExceptionIfBrowserNull(); - - cefBrowser.ReplaceMisspelling(word); + browser.BrowserCore.ReplaceMisspelling(word); } /// @@ -1252,9 +1167,9 @@ public static void AddWordToDictionary(this IBrowser cefBrowser, string word) /// /// browserHost or null. /// - public static IBrowserHost GetBrowserHost(this IWebBrowser browser) + public static IBrowserHost GetBrowserHost(this IChromiumWebBrowserBase browser) { - var cefBrowser = browser.GetBrowser(); + var cefBrowser = browser.BrowserCore; return cefBrowser == null ? null : cefBrowser.GetHost(); } @@ -1264,12 +1179,9 @@ public static IBrowserHost GetBrowserHost(this IWebBrowser browser) /// /// The ChromiumWebBrowser instance this method extends. /// The new word that will be added to the dictionary. - public static void AddWordToDictionary(this IWebBrowser browser, string word) + public static void AddWordToDictionary(this IChromiumWebBrowserBase browser, string word) { - var cefBrowser = browser.GetBrowser(); - cefBrowser.ThrowExceptionIfBrowserNull(); - - cefBrowser.AddWordToDictionary(word); + browser.BrowserCore.AddWordToDictionary(word); } /// @@ -1281,12 +1193,9 @@ public static void AddWordToDictionary(this IWebBrowser browser, string word) /// The delta x coordinate. /// The delta y coordinate. /// The modifiers. - public static void SendMouseWheelEvent(this IWebBrowser browser, int x, int y, int deltaX, int deltaY, CefEventFlags modifiers) + public static void SendMouseWheelEvent(this IChromiumWebBrowserBase browser, int x, int y, int deltaX, int deltaY, CefEventFlags modifiers) { - var cefBrowser = browser.GetBrowser(); - cefBrowser.ThrowExceptionIfBrowserNull(); - - cefBrowser.SendMouseWheelEvent(x, y, deltaX, deltaY, modifiers); + browser.BrowserCore.SendMouseWheelEvent(x, y, deltaX, deltaY, modifiers); } ///