// Copyright © 2013 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.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Automation.Peers; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Input; using System.Windows.Interop; using System.Windows.Threading; using CefSharp.Enums; using CefSharp.Internals; using CefSharp.Structs; using CefSharp.Wpf.Experimental; using CefSharp.Wpf.Handler; using CefSharp.Wpf.Internals; using CefSharp.Wpf.Rendering; using Microsoft.Win32.SafeHandles; using CursorType = CefSharp.Enums.CursorType; using Point = System.Windows.Point; using Range = CefSharp.Structs.Range; using Rect = CefSharp.Structs.Rect; using Size = System.Windows.Size; namespace CefSharp.Wpf { /// /// ChromiumWebBrowser is the WPF web browser control /// /// /// [TemplatePart(Name = PartImageName, Type = typeof(Image))] [TemplatePart(Name = PartPopupImageName, Type = typeof(Image))] public partial class ChromiumWebBrowser : Control, IRenderWebBrowser, IWpfWebBrowser { /// /// TemplatePart Name constant for the Image used to represent the browser /// public const string PartImageName = "PART_image"; /// /// TemplatePart Name constant for the Image used to represent the popup /// overlayed on the browser /// public const string PartPopupImageName = "PART_popupImage"; /// /// View Rectangle used by /// private Rect viewRect; /// /// Store the previous window state, used to determine if the /// Windows was previous /// and resume rendering /// private WindowState previousWindowState; /// /// The source /// private HwndSource source; /// /// The HwndSource RootVisual (Window) - We store a reference /// to unsubscribe event handlers /// private Window sourceWindow; /// /// The MonitorInfo based on the current hwnd /// private MonitorInfoEx monitorInfo; /// /// The tooltip timer /// private DispatcherTimer tooltipTimer; /// /// The tool tip /// private ToolTip toolTip; /// /// The managed cef browser adapter /// private IBrowserAdapter managedCefBrowserAdapter; /// /// The ignore URI change /// private bool ignoreUriChange; /// /// Initial address /// private string initialAddress; /// /// Used to stop multiple threads trying to load the initial Url multiple times. /// If the Address property is bound after the browser is initialized /// private bool initialLoadCalled; /// /// Has the underlying Cef Browser been created (slightly different to initialized in that /// the browser is initialized in an async fashion) /// private bool browserCreated; /// /// The image that represents this browser instances /// private Image image; /// /// The popup image /// private Image popupImage; /// /// Location of the control on the screen, relative to Top/Left /// Used to calculate GetScreenPoint /// We're unable to call PointToScreen directly due to treading restrictions /// and calling in a sync fashion on the UI thread was problematic. /// private Point browserScreenLocation; /// /// Browser initialization settings /// private IBrowserSettings browserSettings; /// /// The request context (we deliberately use a private variable so we can throw an exception if /// user attempts to set after browser created) /// private IRequestContext requestContext; /// /// Keep a short term copy of IDragData, so when calling DoDragDrop, DragEnter is called, /// we can reuse the drag data provided from CEF /// private IDragData currentDragData; /// /// Keep the current drag&drop effects to return the appropriate effects on drag over. /// private DragDropEffects currentDragDropEffects; /// /// A flag that indicates whether or not the designer is active /// NOTE: Needs to be static for OnApplicationExit /// private static bool DesignMode; // https://bitbucket.org/chromiumembedded/cef/issues/3427/osr-rendering-bug-when-minimizing-and private bool resizeHackIgnoreOnPaint; private Structs.Size? resizeHackSize; /// /// This flag is set when the browser gets focus before the underlying CEF browser /// has been initialized. /// private bool initialFocus; /// /// When enabled the browser will resize by 1px when it becomes visible to workaround /// the upstream issue /// Hack to work around upstream issue https://bitbucket.org/chromiumembedded/cef/issues/3427/osr-rendering-bug-when-minimizing-and /// Disabled by default /// public bool ResizeHackEnabled { get; set; } = false; /// /// Number of milliseconds to wait after resizing the browser when it first /// becomes visible. After the delay the browser will revert to it's /// original size. /// Hack to workaround upstream issue https://bitbucket.org/chromiumembedded/cef/issues/3427/osr-rendering-bug-when-minimizing-and /// public int ResizeHackDelayInMs { get; set; } = 50; /// /// Gets a value indicating whether this instance is disposed. /// /// if this instance is disposed; otherwise, . public bool IsDisposed { get { return Interlocked.CompareExchange(ref disposeSignaled, 1, 1) == 1; } } /// /// WPF Keyboard Handled forwards key events to the underlying browser /// public IWpfKeyboardHandler WpfKeyboardHandler { get; set; } /// /// Gets or sets the browser settings. /// /// The browser settings. public IBrowserSettings BrowserSettings { get { return browserSettings; } set { if (browserCreated) { throw new Exception("Browser has already been created. BrowserSettings must be " + "set before the underlying CEF browser is created."); } //New instance is created in the constructor, if you use //xaml to initialize browser settings then it will also create a new //instance, so we dispose of the old one if (browserSettings != null && browserSettings.AutoDispose) { browserSettings.Dispose(); } browserSettings = value; } } /// /// Gets or sets the request context. /// /// The request context. public IRequestContext RequestContext { get { return requestContext; } set { if (browserCreated) { throw new Exception("Browser has already been created. RequestContext must be " + "set before the underlying CEF browser is created."); } if (value != null && !Core.ObjectFactory.RequestContextType.IsAssignableFrom(value.UnWrap().GetType())) { throw new Exception(string.Format("RequestContext can only be of type {0} or null", Core.ObjectFactory.RequestContextType)); } requestContext = value; } } /// /// Implement and control how the control is rendered /// /// The render Handler. public IRenderHandler RenderHandler { get; set; } /// /// Implement to handle events related to accessibility. /// /// The accessibility handler. public IAccessibilityHandler AccessibilityHandler { get; set; } /// /// Raised every time is called. You can access the underlying buffer, though it's /// preferable to either override or implement your own as there is no outwardly /// accessible locking (locking is done within the default implementations). /// 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 /// public event EventHandler Paint; /// /// Raised every time is called. /// 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 /// public event EventHandler VirtualKeyboardRequested; /// /// Navigates to the previous page in the browser history. Will automatically be enabled/disabled depending on the /// browser state. /// /// The back command. public ICommand BackCommand { get; private set; } /// /// Navigates to the next page in the browser history. Will automatically be enabled/disabled depending on the /// browser state. /// /// The forward command. public ICommand ForwardCommand { get; private set; } /// /// Reloads the content of the current page. Will automatically be enabled/disabled depending on the browser state. /// /// The reload command. public ICommand ReloadCommand { get; private set; } /// /// Prints the current browser contents. /// /// The print command. public ICommand PrintCommand { get; private set; } /// /// Increases the zoom level. /// /// The zoom in command. public ICommand ZoomInCommand { get; private set; } /// /// Decreases the zoom level. /// /// The zoom out command. public ICommand ZoomOutCommand { get; private set; } /// /// Resets the zoom level to the default. (100%) /// /// The zoom reset command. public ICommand ZoomResetCommand { get; private set; } /// /// Opens up a new program window (using the default text editor) where the source code of the currently displayed web /// page is shown. /// /// The view source command. public ICommand ViewSourceCommand { get; private set; } /// /// Command which cleans up the Resources used by the ChromiumWebBrowser /// /// The cleanup command. public ICommand CleanupCommand { get; private set; } /// /// Stops loading the current page. /// /// The stop command. public ICommand StopCommand { get; private set; } /// /// Cut selected text to the clipboard. /// /// The cut command. public ICommand CutCommand { get; private set; } /// /// Copy selected text to the clipboard. /// /// The copy command. public ICommand CopyCommand { get; private set; } /// /// Paste text from the clipboard. /// /// The paste command. public ICommand PasteCommand { get; private set; } /// /// Select all text. /// /// The select all command. public ICommand SelectAllCommand { get; private set; } /// /// Undo last action. /// /// The undo command. public ICommand UndoCommand { get; private set; } /// /// Redo last action. /// /// The redo command. public ICommand RedoCommand { get; private set; } /// public ICommand ToggleAudioMuteCommand { get; private set; } /// /// The dpi scale factor, if the browser has already been initialized /// you must manually call IBrowserHost.NotifyScreenInfoChanged for the /// browser to be notified of the change. /// public float DpiScaleFactor { get; set; } /// /// Initializes static members of the class. /// //TODO: Currently only a single Dispatcher is supported, all controls will //be Shutdown on that dispatcher which may not actually be the correct Dispatcher. //We could potentially use a ThreadStatic variable to implement this behaviour //and support multiple dispatchers. static ChromiumWebBrowser() { DefaultStyleKeyProperty.OverrideMetadata( typeof(ChromiumWebBrowser), new FrameworkPropertyMetadata(typeof(ChromiumWebBrowser))); if (CefSharpSettings.ShutdownOnExit) { //Use Dispatcher.FromThread as it returns null if no dispatcher //is available for this thread. var dispatcher = Dispatcher.FromThread(Thread.CurrentThread); if (dispatcher == null) { //No dispatcher then we'll rely on Application.Exit var app = Application.Current; if (app != null) { app.Exit += OnApplicationExit; } } else { dispatcher.ShutdownStarted += DispatcherShutdownStarted; dispatcher.ShutdownFinished += DispatcherShutdownFinished; } } } /// /// Handles Dispatcher Shutdown /// /// sender /// eventargs private static void DispatcherShutdownStarted(object sender, EventArgs e) { var dispatcher = (Dispatcher)sender; dispatcher.ShutdownStarted -= DispatcherShutdownStarted; if (!DesignMode) { CefPreShutdown(); } } private static void DispatcherShutdownFinished(object sender, EventArgs e) { var dispatcher = (Dispatcher)sender; dispatcher.ShutdownFinished -= DispatcherShutdownFinished; if (!DesignMode) { CefShutdown(); } } /// /// Handles the event. /// /// The sender. /// The instance containing the event data. private static void OnApplicationExit(object sender, ExitEventArgs e) { if (!DesignMode) { CefShutdown(); } } /// /// Required for designer support - this method cannot be inlined as the designer /// will attempt to load libcef.dll and will subsequently throw an exception. /// [MethodImpl(MethodImplOptions.NoInlining)] private static void CefPreShutdown() { Cef.PreShutdown(); } /// /// Required for designer support - this method cannot be inlined as the designer /// will attempt to load libcef.dll and will subsequently throw an exception. /// [MethodImpl(MethodImplOptions.NoInlining)] private static void CefShutdown() { Cef.Shutdown(); } /// /// To control how is called, this method will /// unsubscribe from , /// and . /// public static void UnregisterShutdownHandler() { //Use Dispatcher.FromThread as it returns null if no dispatcher //is available for this thread. var dispatcher = Dispatcher.FromThread(Thread.CurrentThread); if (dispatcher != null) { dispatcher.ShutdownStarted -= DispatcherShutdownStarted; dispatcher.ShutdownFinished -= DispatcherShutdownFinished; } var app = Application.Current; if (app != null) { app.Exit -= OnApplicationExit; } } /// /// Initializes a new instance of the class. /// /// Cef::Initialize() failed public ChromiumWebBrowser() { DesignMode = System.ComponentModel.DesignerProperties.GetIsInDesignMode(this); if (!DesignMode) { NoInliningConstructor(); } } /// /// Initializes a new instance of the class. /// Use this constructor to load the browser before it's attached to the Visual Tree. /// The underlying CefBrowser will be created with the specified . /// CEF requires positive values for and , /// if values less than 1 are specified then the default value of 1 will be used instead. /// You can subscribe to the event and attach the browser /// to its parent control when Loading is complete ( is false). /// /// HwndSource for the Window that will host the browser. /// address to be loaded when the browser is created. /// size /// /// /// //Obtain Hwnd from parent window /// var hwndSource = (HwndSource)PresentationSource.FromVisual(this); /// var browser = new ChromiumWebBrowser(hwndSource, "github.com", 1024, 768); /// browser.LoadingStateChanged += OnBrowserLoadingStateChanged; /// /// private void OnBrowserLoadingStateChanged(object sender, LoadingStateChangedEventArgs e) /// { /// if (e.IsLoading == false) /// { /// var b = (ChromiumWebBrowser)sender; /// b.LoadingStateChanged -= OnBrowserLoadingStateChanged; /// Dispatcher.InvokeAsync(() => /// { /// //Attach to visual tree /// ParentControl.Child = b; /// }); /// } /// } /// /// public ChromiumWebBrowser(HwndSource parentWindowHwndSource, string initialAddress, Size size) : this(initialAddress) { CreateBrowser(parentWindowHwndSource, size); } /// /// Initializes a new instance of the class. /// The specified will be loaded initially. /// Use this constructor if you are loading a Chrome Extension. /// /// address to be loaded when the browser is created. public ChromiumWebBrowser(string initialAddress) { this.initialAddress = initialAddress; NoInliningConstructor(); } /// /// Constructor logic has been moved into this method /// Required for designer support - this method cannot be inlined as the designer /// will attempt to load libcef.dll and will subsequently throw an exception. /// [MethodImpl(MethodImplOptions.NoInlining)] private void NoInliningConstructor() { //Initialize CEF if it hasn't already been initialized if (!Cef.IsInitialized) { var settings = new CefSettings(); if (!Cef.Initialize(settings)) { throw new InvalidOperationException(CefInitializeFailedErrorMessage); } } //Add this ChromiumWebBrowser instance to a list of IDisposable objects // that if still alive at the time Cef.Shutdown is called will be disposed of // It's important all browser instances be freed before Cef.Shutdown is called. Cef.AddDisposable(this); Focusable = true; FocusVisualStyle = null; IsTabStop = true; Dispatcher.BeginInvoke((Action)(() => WebBrowser = this)); Loaded += OnLoaded; SizeChanged += OnActualSizeChanged; GotKeyboardFocus += OnGotKeyboardFocus; LostKeyboardFocus += OnLostKeyboardFocus; // Drag Drop events DragEnter += OnDragEnter; DragOver += OnDragOver; DragLeave += OnDragLeave; Drop += OnDrop; IsVisibleChanged += OnIsVisibleChanged; ToolTip = toolTip = new ToolTip(); toolTip.StaysOpen = true; toolTip.Visibility = Visibility.Collapsed; toolTip.Closed += OnTooltipClosed; BackCommand = new DelegateCommand(this.Back, () => CanGoBack); ForwardCommand = new DelegateCommand(this.Forward, () => CanGoForward); ReloadCommand = new DelegateCommand(this.Reload, () => !IsLoading); PrintCommand = new DelegateCommand(this.Print); ZoomInCommand = new DelegateCommand(ZoomIn); ZoomOutCommand = new DelegateCommand(ZoomOut); ZoomResetCommand = new DelegateCommand(ZoomReset); ViewSourceCommand = new DelegateCommand(this.ViewSource); CleanupCommand = new DelegateCommand(Dispose); StopCommand = new DelegateCommand(this.Stop); CutCommand = new DelegateCommand(this.Cut); CopyCommand = new DelegateCommand(this.Copy); PasteCommand = new DelegateCommand(this.Paste); SelectAllCommand = new DelegateCommand(this.SelectAll); UndoCommand = new DelegateCommand(this.Undo); RedoCommand = new DelegateCommand(this.Redo); ToggleAudioMuteCommand = new DelegateCommand(this.ToggleAudioMute); managedCefBrowserAdapter = ManagedCefBrowserAdapter.Create(this, true); browserSettings = Core.ObjectFactory.CreateBrowserSettings(autoDispose: true); WpfKeyboardHandler = new WpfKeyboardHandler(this); PresentationSource.AddSourceChangedHandler(this, PresentationSourceChangedHandler); MenuHandler = new ContextMenuHandler(); UseLayoutRounding = true; } /// /// Finalizes an instance of the class. /// ~ChromiumWebBrowser() { Dispose(false); } /// /// Releases all resources used by the object /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } /// /// If not in design mode; Releases unmanaged and - optionally - managed resources for the /// /// to release both managed and unmanaged resources; to release only unmanaged resources. protected virtual void Dispose(bool disposing) { // Attempt to move the disposeSignaled state from 0 to 1. If successful, we can be assured that // this thread is the first thread to do so, and can safely dispose of the object. if (Interlocked.CompareExchange(ref disposeSignaled, 1, 0) != 0) { return; } if (DesignMode) { return; } InternalDispose(disposing); } /// /// Releases unmanaged and - optionally - managed resources for the /// /// to release both managed and unmanaged resources; to release only unmanaged resources. /// /// This method cannot be inlined as the designer will attempt to load libcef.dll and will subsequently throw an exception. /// [MethodImpl(MethodImplOptions.NoInlining)] private void InternalDispose(bool disposing) { if (disposing) { CanExecuteJavascriptInMainFrame = false; Interlocked.Exchange(ref browserInitialized, 0); //Stop rendering immediately so later on when we dispose of the //RenderHandler no further OnPaint calls take place //Check browser not null as it's possible to call Dispose before it's created browser?.GetHost().WasHidden(true); UiThreadRunAsync(() => { SetCurrentValue(IsBrowserInitializedProperty, false); WebBrowser = null; }); // No longer reference event listeners: ConsoleMessage = null; FrameLoadEnd = null; FrameLoadStart = null; AddressChanged = null; IsBrowserInitializedChanged = null; LoadError = null; LoadingStateChanged = null; Paint = null; StatusMessage = null; TitleChanged = null; VirtualKeyboardRequested = null; JavascriptMessageReceived = null; // Release reference to handlers, except LifeSpanHandler which is done after Disposing // ManagedCefBrowserAdapter otherwise the ILifeSpanHandler.DoClose will not be invoked. // We also leave FocusHandler and override with a NoFocusHandler implementation as // it so we can block taking Focus (we're dispoing afterall). Issue #3715 FreeHandlersExceptLifeSpanAndFocus(); FocusHandler = new NoFocusHandler(); browser = null; BrowserCore = null; // In case we accidentally have a reference to the CEF drag data currentDragData?.Dispose(); currentDragData = null; PresentationSource.RemoveSourceChangedHandler(this, PresentationSourceChangedHandler); // Release window event listeners if PresentationSourceChangedHandler event wasn't // fired before Dispose if (sourceWindow != null) { sourceWindow.StateChanged -= OnWindowStateChanged; sourceWindow.LocationChanged -= OnWindowLocationChanged; sourceWindow = null; } // Release internal event listeners: Loaded -= OnLoaded; SizeChanged -= OnActualSizeChanged; GotKeyboardFocus -= OnGotKeyboardFocus; LostKeyboardFocus -= OnLostKeyboardFocus; // Release internal event listeners for Drag Drop events: DragEnter -= OnDragEnter; DragOver -= OnDragOver; DragLeave -= OnDragLeave; Drop -= OnDrop; IsVisibleChanged -= OnIsVisibleChanged; if (tooltipTimer != null) { tooltipTimer.Tick -= OnTooltipTimerTick; tooltipTimer.Stop(); tooltipTimer = null; } if (CleanupElement != null) { CleanupElement.Unloaded -= OnCleanupElementUnloaded; } managedCefBrowserAdapter?.Dispose(); managedCefBrowserAdapter = null; // LifeSpanHandler is set to null after managedCefBrowserAdapter.Dispose so ILifeSpanHandler.DoClose // is called. LifeSpanHandler = null; WpfKeyboardHandler?.Dispose(); WpfKeyboardHandler = null; //Take a copy of the RenderHandler then set to property to null //Before we dispose, reduces the changes of any OnPaint calls //using the RenderHandler after Dispose var renderHandler = RenderHandler; RenderHandler = null; renderHandler?.Dispose(); source = null; } Cef.RemoveDisposable(this); } /// /// Gets the ScreenInfo - currently used to get the DPI scale factor. /// /// ScreenInfo containing the current DPI scale factor ScreenInfo? IRenderWebBrowser.GetScreenInfo() { return GetScreenInfo(); } /// /// Gets the ScreenInfo - currently used to get the DPI scale factor. /// /// ScreenInfo containing the current DPI scale factor protected virtual ScreenInfo? GetScreenInfo() { Rect rect = monitorInfo.Monitor; Rect availableRect = monitorInfo.WorkArea; if (DpiScaleFactor > 1.0) { rect = rect.ScaleByDpi(DpiScaleFactor); availableRect = availableRect.ScaleByDpi(DpiScaleFactor); } var screenInfo = new ScreenInfo { DeviceScaleFactor = DpiScaleFactor, Rect = rect, AvailableRect = availableRect }; return screenInfo; } /// /// Called to retrieve the view rectangle which is relative to screen coordinates. /// This method must always provide a non-empty rectangle. /// /// View Rectangle Rect IRenderWebBrowser.GetViewRect() { return GetViewRect(); } /// /// Called to retrieve the view rectangle which is relative to screen coordinates. /// This method must always provide a non-empty rectangle. /// /// View Rectangle protected virtual Rect GetViewRect() { //Take a local copy as the value is set on a different thread, //Its possible the struct is set to null after our initial check. var resizeRect = resizeHackSize; if (resizeRect == null) { return viewRect; } var size = resizeRect.Value; return new Rect(0, 0, size.Width, size.Height); } /// bool IRenderWebBrowser.GetScreenPoint(int viewX, int viewY, out int screenX, out int screenY) { return GetScreenPoint(viewX, viewY, out screenX, out screenY); } /// /// Called to retrieve the translation from view coordinates to actual screen coordinates. /// /// x /// y /// screen x /// screen y /// Return true if the screen coordinates were provided. protected virtual bool GetScreenPoint(int viewX, int viewY, out int screenX, out int screenY) { screenX = 0; screenY = 0; //We manually calculate the screen point as calling PointToScreen can only be called on the UI thread // in a sync fashion and it's easy for users to get themselves into a deadlock. if (DpiScaleFactor > 1) { screenX = (int)(browserScreenLocation.X + (viewX * DpiScaleFactor)); screenY = (int)(browserScreenLocation.Y + (viewY * DpiScaleFactor)); } else { screenX = (int)(browserScreenLocation.X + viewX); screenY = (int)(browserScreenLocation.Y + viewY); } return true; } /// bool IRenderWebBrowser.StartDragging(IDragData dragData, DragOperationsMask allowedOps, int x, int y) { return StartDragging(dragData, allowedOps, x, y); } /// /// Called when the user starts dragging content in the web view. /// OS APIs that run a system message loop may be used within the StartDragging call. /// Don't call any of IBrowserHost::DragSource*Ended* methods after returning false. /// Call IBrowserHost.DragSourceEndedAt and DragSourceSystemDragEnded either synchronously or asynchronously to inform the web view that the drag operation has ended. /// /// Contextual information about the dragged content /// allowed operations /// is the drag start location in screen coordinates /// is the drag start location in screen coordinates /// Return true to handle the drag operation. protected virtual bool StartDragging(IDragData dragData, DragOperationsMask allowedOps, int x, int y) { var dataObject = new DataObject(); dataObject.SetText(dragData.FragmentText, TextDataFormat.Text); dataObject.SetText(dragData.FragmentText, TextDataFormat.UnicodeText); dataObject.SetText(dragData.FragmentHtml, TextDataFormat.Html); //Clone dragData for use in OnDragEnter event handler currentDragData = dragData.Clone(); currentDragData.ResetFileContents(); // TODO: The following code block *should* handle images, but GetFileContents is // not yet implemented. //if (dragData.IsFile) //{ // var bmi = new BitmapImage(); // bmi.BeginInit(); // bmi.StreamSource = dragData.GetFileContents(); // bmi.EndInit(); // dataObject.SetImage(bmi); //} UiThreadRunAsync(delegate { if (browser != null) { //DoDragDrop will fire DragEnter event var result = DragDrop.DoDragDrop(this, dataObject, allowedOps.GetDragEffects()); //DragData was stored so when DoDragDrop fires DragEnter we reuse a clone of the IDragData provided here currentDragData = null; //If result == DragDropEffects.None then we'll send DragOperationsMask.None //effectively cancelling the drag operation browser.GetHost().DragSourceEndedAt(x, y, result.GetDragOperationsMask()); browser.GetHost().DragSourceSystemDragEnded(); } }); return true; } /// void IRenderWebBrowser.UpdateDragCursor(DragOperationsMask operation) { UpdateDragCursor(operation); } /// /// Called when the web view wants to update the mouse cursor during a drag & drop operation. /// /// describes the allowed operation (none, move, copy, link). protected virtual void UpdateDragCursor(DragOperationsMask operation) { currentDragDropEffects = operation.GetDragEffects(); } /// void IRenderWebBrowser.OnAcceleratedPaint(PaintElementType type, Rect dirtyRect, IntPtr sharedHandle) { OnAcceleratedPaint(type == PaintElementType.Popup, dirtyRect, sharedHandle); } /// /// Called when an element has been rendered to the shared texture handle. /// This method is only called when is set to true /// /// indicates whether the element is the view or the popup widget. /// contains the set of rectangles in pixel coordinates that need to be repainted /// is the handle for a D3D11 Texture2D that can be accessed via ID3D11Device using the OpenSharedResource method. protected virtual void OnAcceleratedPaint(bool isPopup, Rect dirtyRect, IntPtr sharedHandle) { RenderHandler?.OnAcceleratedPaint(isPopup, dirtyRect, sharedHandle); } /// void IRenderWebBrowser.OnPaint(PaintElementType type, Rect dirtyRect, IntPtr buffer, int width, int height) { OnPaint(type == PaintElementType.Popup, dirtyRect, buffer, width, height); } /// /// Called when an element should be painted. Pixel values passed to this method are scaled relative to view coordinates based on the /// value of returned from . To override the default behaviour /// override this method or implement your own and assign to /// Called on the CEF UI Thread /// /// indicates whether the element is the view or the popup widget. /// contains the set of rectangles in pixel coordinates that need to be repainted /// The bitmap will be width * height *4 bytes in size and represents a BGRA image with an upper-left origin. /// The buffer should no be used outside the scope of this method, a copy should be taken. As the buffer is reused /// internally and potentially even freed. /// /// width /// height protected virtual void OnPaint(bool isPopup, Rect dirtyRect, IntPtr buffer, int width, int height) { if (resizeHackIgnoreOnPaint) { return; } var paint = Paint; if (paint != null) { var args = new PaintEventArgs(isPopup, dirtyRect, buffer, width, height); paint(this, args); if (args.Handled) { return; } } var img = isPopup ? popupImage : image; RenderHandler?.OnPaint(isPopup, dirtyRect, buffer, width, height, img); } /// void IRenderWebBrowser.OnPopupSize(Rect rect) { OnPopupSize(rect); } /// /// Sets the popup size and position. /// /// The popup rectangle (size and position). protected virtual void OnPopupSize(Rect rect) { UiThreadRunAsync(() => SetPopupSizeAndPositionImpl(rect)); } /// /// Sets the popup is open. /// /// if set to true [is open]. void IRenderWebBrowser.OnPopupShow(bool isOpen) { OnPopupShow(isOpen); } /// /// Sets the popup is open. /// /// if set to true [is open]. protected virtual void OnPopupShow(bool isOpen) { UiThreadRunAsync(() => { popupImage.Visibility = isOpen ? Visibility.Visible : Visibility.Hidden; }); } /// void IRenderWebBrowser.OnCursorChange(IntPtr handle, CursorType type, CursorInfo customCursorInfo) { OnCursorChange(handle, type, customCursorInfo); } /// /// Sets the cursor. /// /// The handle. /// The type. /// custom cursor information protected virtual void OnCursorChange(IntPtr handle, CursorType type, CursorInfo customCursorInfo) { //Custom cursors are handled differently, for now keep standard ones executing //in an async fashion if (type == CursorType.Custom) { //When using a custom it appears we need to update the cursor in a sync fashion //Likely the underlying handle/buffer is being released before the cursor // is created when executed in an async fashion. Doesn't seem to be a problem //for built in cursor types UiThreadRunSync(() => { Cursor = CursorInteropHelper.Create(new SafeFileHandle(handle, ownsHandle: false)); }); } else { UiThreadRunAsync(() => { Cursor = CursorInteropHelper.Create(new SafeFileHandle(handle, ownsHandle: false)); }); } } /// void IRenderWebBrowser.OnImeCompositionRangeChanged(Range selectedRange, Rect[] characterBounds) { OnImeCompositionRangeChanged(selectedRange, characterBounds); } /// /// Called when the IME composition range has changed. /// /// is the range of characters that have been selected /// is the bounds of each character in view coordinates. protected virtual void OnImeCompositionRangeChanged(Range selectedRange, Rect[] characterBounds) { var imeKeyboardHandler = WpfKeyboardHandler as WpfImeKeyboardHandler; if (imeKeyboardHandler != null) { imeKeyboardHandler.ChangeCompositionRange(selectedRange, characterBounds); } } /// void IRenderWebBrowser.OnVirtualKeyboardRequested(IBrowser browser, TextInputMode inputMode) { OnVirtualKeyboardRequested(browser, inputMode); } /// /// Called when an on-screen keyboard should be shown or hidden for the specified browser. /// /// the browser /// specifies what kind of keyboard should be opened. If , any existing keyboard for this browser should be hidden. protected virtual void OnVirtualKeyboardRequested(IBrowser browser, TextInputMode inputMode) { var args = new VirtualKeyboardRequestedEventArgs(browser, inputMode); VirtualKeyboardRequested?.Invoke(this, args); } /// /// Sets the address. /// /// The instance containing the event data. void IWebBrowserInternal.SetAddress(AddressChangedEventArgs args) { UiThreadRunAsync(() => { ignoreUriChange = true; SetCurrentValue(AddressProperty, args.Address); ignoreUriChange = false; // The tooltip should obviously also be reset (and hidden) when the address changes. SetCurrentValue(TooltipTextProperty, null); }); } /// /// Sets the loading state change. /// /// The instance containing the event data. partial void SetLoadingStateChange(LoadingStateChangedEventArgs args) { UiThreadRunAsync(() => { SetCurrentValue(CanGoBackProperty, args.CanGoBack); SetCurrentValue(CanGoForwardProperty, args.CanGoForward); SetCurrentValue(IsLoadingProperty, args.IsLoading); ((DelegateCommand)BackCommand).RaiseCanExecuteChanged(); ((DelegateCommand)ForwardCommand).RaiseCanExecuteChanged(); ((DelegateCommand)ReloadCommand).RaiseCanExecuteChanged(); }); } /// /// Sets the title. /// /// The instance containing the event data. void IWebBrowserInternal.SetTitle(TitleChangedEventArgs args) { UiThreadRunAsync(() => SetCurrentValue(TitleProperty, args.Title)); } /// /// Sets the tooltip text. /// /// The tooltip text. void IWebBrowserInternal.SetTooltipText(string tooltipText) { UiThreadRunAsync(() => SetCurrentValue(TooltipTextProperty, tooltipText)); } /// /// Called when [after browser created]. /// /// The browser. partial void OnAfterBrowserCreated(IBrowser browser) { UiThreadRunAsync(() => { if (!IsDisposed) { SetCurrentValue(IsBrowserInitializedProperty, true); // Only call Load if initialAddress is null and Address is not empty if (string.IsNullOrEmpty(initialAddress) && !string.IsNullOrEmpty(Address) && !initialLoadCalled) { Load(Address); } } }); if (initialFocus) { browser.GetHost()?.SetFocus(true); } } #region CanGoBack dependency property /// /// 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. public bool CanGoBack { get { return (bool)GetValue(CanGoBackProperty); } } /// /// The can go back property /// public static DependencyProperty CanGoBackProperty = DependencyProperty.Register(nameof(CanGoBack), typeof(bool), typeof(ChromiumWebBrowser)); #endregion #region CanGoForward dependency property /// /// 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. public bool CanGoForward { get { return (bool)GetValue(CanGoForwardProperty); } } /// /// The can go forward property /// public static DependencyProperty CanGoForwardProperty = DependencyProperty.Register(nameof(CanGoForward), typeof(bool), typeof(ChromiumWebBrowser)); #endregion #region Address dependency property /// /// 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. public string Address { get { return (string)GetValue(AddressProperty); } set { SetValue(AddressProperty, value); } } /// /// The address property /// public static readonly DependencyProperty AddressProperty = DependencyProperty.Register(nameof(Address), typeof(string), typeof(ChromiumWebBrowser), new UIPropertyMetadata(null, OnAddressChanged)); /// /// Event called when the browser address has changed /// public event DependencyPropertyChangedEventHandler AddressChanged; /// /// Handles the event. /// /// The sender. /// The instance containing the event data. private static void OnAddressChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) { var owner = (ChromiumWebBrowser)sender; var oldValue = (string)args.OldValue; var newValue = (string)args.NewValue; owner.OnAddressChanged(oldValue, newValue); owner.AddressChanged?.Invoke(owner, args); } /// /// Called when [address changed]. /// /// The old value. /// The new value. protected virtual void OnAddressChanged(string oldValue, string newValue) { if (ignoreUriChange || newValue == null || !InternalIsBrowserInitialized()) { return; } Load(newValue); } #endregion Address dependency property #region IsLoading dependency property /// /// 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. public bool IsLoading { get { return (bool)GetValue(IsLoadingProperty); } } /// /// The is loading property /// public static readonly DependencyProperty IsLoadingProperty = DependencyProperty.Register(nameof(IsLoading), typeof(bool), typeof(ChromiumWebBrowser), new PropertyMetadata(false)); #endregion IsLoading dependency property #region IsBrowserInitialized dependency property /// /// 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, this property is implemented as a Dependency Property and fully supports data /// binding. public bool IsBrowserInitialized { get { return (bool)GetValue(IsBrowserInitializedProperty); } } /// /// The is browser initialized property /// public static readonly DependencyProperty IsBrowserInitializedProperty = DependencyProperty.Register(nameof(IsBrowserInitialized), typeof(bool), typeof(ChromiumWebBrowser), new PropertyMetadata(false, OnIsBrowserInitializedChanged)); /// /// Event called after the underlying CEF browser instance has been created. /// public event DependencyPropertyChangedEventHandler IsBrowserInitializedChanged; /// /// Handles the event. /// /// The d. /// The instance containing the event data. private static void OnIsBrowserInitializedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var owner = (ChromiumWebBrowser)d; var oldValue = (bool)e.OldValue; var newValue = (bool)e.NewValue; owner.OnIsBrowserInitializedChanged(oldValue, newValue); owner.IsBrowserInitializedChanged?.Invoke(owner, e); } /// /// Called when [is browser initialized changed]. /// /// if set to true [old value]. /// if set to true [new value]. protected virtual void OnIsBrowserInitializedChanged(bool oldValue, bool newValue) { if (newValue && !IsDisposed) { var task = this.GetZoomLevelAsync(); task.ContinueWith(previous => { if (previous.Status == TaskStatus.RanToCompletion) { UiThreadRunAsync(() => { if (!IsDisposed) { SetCurrentValue(ZoomLevelProperty, previous.Result); } }); } else { throw new InvalidOperationException("Unexpected failure of calling CEF->GetZoomLevelAsync", previous.Exception); } }, TaskContinuationOptions.ExecuteSynchronously); } } #endregion IsInitialized dependency property #region Title dependency property /// /// The title of the web page being currently displayed. /// /// The title. /// This property is implemented as a Dependency Property and fully supports data binding. public string Title { get { return (string)GetValue(TitleProperty); } set { SetValue(TitleProperty, value); } } /// /// The title property /// public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(nameof(Title), typeof(string), typeof(ChromiumWebBrowser), new PropertyMetadata(null, OnTitleChanged)); /// /// Event handler that will get called when the browser title changes /// public event DependencyPropertyChangedEventHandler TitleChanged; /// /// Handles the event. /// /// The d. /// The instance containing the event data. private static void OnTitleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var owner = (ChromiumWebBrowser)d; owner.TitleChanged?.Invoke(owner, e); } #endregion Title dependency property #region ZoomLevel dependency property /// /// The zoom level at which the browser control is currently displaying. /// Can be set to 0 to clear the zoom level (resets to default zoom level). /// NOTE: For browsers that share the same render process (same origin) this /// property is only updated when the browser changes its visible state. /// If you have two browsers visible at the same time that share the same render /// process then zooming one will not update this property in the other (unless /// the control is hidden and then shown). You can isolate browser instances /// using a , they will then have their own render process /// regardless of the process policy. You can manually get the Zoom level using /// /// /// The zoom level. public double ZoomLevel { get { return (double)GetValue(ZoomLevelProperty); } set { SetValue(ZoomLevelProperty, value); } } /// /// The zoom level property /// public static readonly DependencyProperty ZoomLevelProperty = DependencyProperty.Register(nameof(ZoomLevel), typeof(double), typeof(ChromiumWebBrowser), new UIPropertyMetadata(0d, OnZoomLevelChanged)); /// /// Handles the event. /// /// The sender. /// The instance containing the event data. private static void OnZoomLevelChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) { var owner = (ChromiumWebBrowser)sender; var oldValue = (double)args.OldValue; var newValue = (double)args.NewValue; owner.OnZoomLevelChanged(oldValue, newValue); } /// /// Called when [zoom level changed]. /// /// The old value. /// The new value. protected virtual void OnZoomLevelChanged(double oldValue, double newValue) { this.SetZoomLevel(newValue); } #endregion ZoomLevel dependency property #region ZoomLevelIncrement dependency property /// /// Specifies the amount used to increase/decrease to ZoomLevel by /// By Default this value is 0.10 /// /// The zoom level increment. public double ZoomLevelIncrement { get { return (double)GetValue(ZoomLevelIncrementProperty); } set { SetValue(ZoomLevelIncrementProperty, value); } } /// /// The zoom level increment property /// public static readonly DependencyProperty ZoomLevelIncrementProperty = DependencyProperty.Register(nameof(ZoomLevelIncrement), typeof(double), typeof(ChromiumWebBrowser), new PropertyMetadata(0.10)); #endregion ZoomLevelIncrement dependency property #region CleanupElement dependency property /// /// The CleanupElement Controls when the BrowserResources will be cleaned up. /// The ChromiumWebBrowser will register on Unloaded of the provided Element and dispose all resources when that handler is called. /// By default the cleanup element is the Window that contains the ChromiumWebBrowser. /// if you want cleanup to happen earlier provide another FrameworkElement. /// Be aware that this Control is not usable anymore after cleanup is done. /// /// The cleanup element. public FrameworkElement CleanupElement { get { return (FrameworkElement)GetValue(CleanupElementProperty); } set { SetValue(CleanupElementProperty, value); } } /// /// The cleanup element property /// public static readonly DependencyProperty CleanupElementProperty = DependencyProperty.Register(nameof(CleanupElement), typeof(FrameworkElement), typeof(ChromiumWebBrowser), new PropertyMetadata(null, OnCleanupElementChanged)); /// /// Handles the event. /// /// The sender. /// The instance containing the event data. private static void OnCleanupElementChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) { var owner = (ChromiumWebBrowser)sender; var oldValue = (FrameworkElement)args.OldValue; var newValue = (FrameworkElement)args.NewValue; owner.OnCleanupElementChanged(oldValue, newValue); } /// /// Called when [cleanup element changed]. /// /// The old value. /// The new value. protected virtual void OnCleanupElementChanged(FrameworkElement oldValue, FrameworkElement newValue) { if (oldValue != null) { oldValue.Unloaded -= OnCleanupElementUnloaded; } if (newValue != null) { newValue.Unloaded -= OnCleanupElementUnloaded; newValue.Unloaded += OnCleanupElementUnloaded; } } /// /// Handles the event. /// /// The sender. /// The instance containing the event data. private void OnCleanupElementUnloaded(object sender, RoutedEventArgs e) { Dispose(); } #endregion CleanupElement dependency property #region TooltipText dependency property /// /// The text that will be displayed as a ToolTip /// /// The tooltip text. public string TooltipText { get { return (string)GetValue(TooltipTextProperty); } } /// /// The tooltip text property /// public static readonly DependencyProperty TooltipTextProperty = DependencyProperty.Register(nameof(TooltipText), typeof(string), typeof(ChromiumWebBrowser), new PropertyMetadata(null, OnTooltipTextChanged)); /// /// Handles the change. /// /// dependency object. /// The instance containing property change data. private static void OnTooltipTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var owner = (ChromiumWebBrowser)d; var oldValue = (string)e.OldValue; var newValue = (string)e.NewValue; owner.OnTooltipTextChanged(oldValue, newValue); } /// /// Called when tooltip text was changed changed. /// /// old value /// new value protected virtual void OnTooltipTextChanged(string oldValue, string newValue) { var timer = tooltipTimer; if (timer == null) { return; } // There are cases where oldValue is null and newValue is string.Empty // and vice versa, simply ignore when string.IsNullOrEmpty for both. if (string.IsNullOrEmpty(oldValue) && string.IsNullOrEmpty(newValue)) { return; } if (string.IsNullOrEmpty(newValue)) { if (timer.IsEnabled) { timer.Stop(); } OpenOrCloseToolTip(null); } else if (!timer.IsEnabled) { timer.Start(); } } #endregion #region WebBrowser dependency property /// /// Gets or sets the WebBrowser. /// /// The WebBrowser. public IWebBrowser WebBrowser { get { return (IWebBrowser)GetValue(WebBrowserProperty); } set { SetValue(WebBrowserProperty, value); } } /// /// The WebBrowser property /// public static readonly DependencyProperty WebBrowserProperty = DependencyProperty.Register(nameof(WebBrowser), typeof(IWebBrowser), typeof(ChromiumWebBrowser), new UIPropertyMetadata(defaultValue: null)); #endregion WebBrowser dependency property /// /// Handles the event. /// /// The sender. /// The instance containing the event data. private void OnDrop(object sender, DragEventArgs e) { if (browser != null) { var mouseEvent = GetMouseEvent(e); var effect = e.AllowedEffects.GetDragOperationsMask(); browser.GetHost().DragTargetDragOver(mouseEvent, effect); browser.GetHost().DragTargetDragDrop(mouseEvent); } } /// /// Handles the event. /// /// The sender. /// The instance containing the event data. private void OnDragLeave(object sender, DragEventArgs e) { if (browser != null) { browser.GetHost().DragTargetDragLeave(); } } /// /// Handles the event. /// /// The sender. /// The instance containing the event data. private void OnDragOver(object sender, DragEventArgs e) { if (browser != null) { browser.GetHost().DragTargetDragOver(GetMouseEvent(e), e.AllowedEffects.GetDragOperationsMask()); } e.Effects = currentDragDropEffects; e.Handled = true; } /// /// Handles the event. /// /// The sender. /// The instance containing the event data. private void OnDragEnter(object sender, DragEventArgs e) { if (browser != null) { var mouseEvent = GetMouseEvent(e); var effect = e.AllowedEffects.GetDragOperationsMask(); //DoDragDrop will fire this handler for internally sourced Drag/Drop operations //we use the existing IDragData (cloned copy) var dragData = currentDragData ?? e.GetDragData(); browser.GetHost().DragTargetDragEnter(dragData, mouseEvent, effect); browser.GetHost().DragTargetDragOver(mouseEvent, effect); } } /// /// PresentationSource changed handler. /// /// The sender. /// The instance containing the event data. private void PresentationSourceChangedHandler(object sender, SourceChangedEventArgs args) { if (args.NewSource != null) { source = (HwndSource)args.NewSource; WpfKeyboardHandler.Setup(source); var matrix = source.CompositionTarget.TransformToDevice; NotifyDpiChange((float)matrix.M11); var window = source.RootVisual as Window; if (window != null) { window.StateChanged += OnWindowStateChanged; window.LocationChanged += OnWindowLocationChanged; sourceWindow = window; if (CleanupElement == null) { CleanupElement = window; } else if(CleanupElement is Window parent) { //If the CleanupElement is a window then move it to the new Window if(parent != window) { CleanupElement = window; } } } browserScreenLocation = GetBrowserScreenLocation(); monitorInfo.Init(); MonitorInfo.GetMonitorInfoForWindowHandle(source.Handle, ref monitorInfo); } else if (args.OldSource != null) { WpfKeyboardHandler.Dispose(); var window = args.OldSource.RootVisual as Window; if (window != null) { window.StateChanged -= OnWindowStateChanged; window.LocationChanged -= OnWindowLocationChanged; sourceWindow = null; } } } #if NETCOREAPP || NET462 /// protected override void OnDpiChanged(DpiScale oldDpi, DpiScale newDpi) { NotifyDpiChange((float)newDpi.DpiScaleX); base.OnDpiChanged(oldDpi, newDpi); } #endif private void OnWindowStateChanged(object sender, EventArgs e) { var window = (Window)sender; switch (window.WindowState) { case WindowState.Normal: case WindowState.Maximized: { if (previousWindowState == WindowState.Minimized && IsVisible) { OnBrowserWasHidden(false); } break; } case WindowState.Minimized: { OnBrowserWasHidden(true); break; } } previousWindowState = window.WindowState; } /// /// Called when the underlying CefBrowser instance is hidden/shown /// Calls . /// This method can be overriden to keep the browser in a visible state /// even when it's not displayed on screen. /// /// if true the browser will be notified that it was hidden. protected virtual void OnBrowserWasHidden(bool hidden) { if (browser != null) { browser.GetHost().WasHidden(hidden); if (ResizeHackEnabled) { if (hidden) { resizeHackIgnoreOnPaint = true; } else { _ = CefUiThreadRunAsync(async () => { await ResizeHackRun(); }); } } } } /// /// Called when the Window Location Changes, the PresentationSource changes /// and the page loads. We manually track the position as CEF makes calls /// on a non-UI thread and calling Invoke in IRenderWebBrowser.GetScreenPoint /// makes it very easy to deadlock the browser. /// /// Returns screen coordinates of the browsers location protected virtual Point GetBrowserScreenLocation() { if (source != null && PresentationSource.FromVisual(this) != null) { return PointToScreen(new Point()); } else { return new Point(); } } private void OnWindowLocationChanged(object sender, EventArgs e) { //We maintain a manual reference to the controls screen location //(relative to top/left of the screen) browserScreenLocation = GetBrowserScreenLocation(); } /// /// Create the underlying CefBrowser instance with the specified . /// This method should only be used in instances where you need the browser /// to load before it's attached to the Visual Tree. /// /// HwndSource for the Window that will host the browser. /// initial size /// Returns false if browser already created, otherwise true. public bool CreateBrowser(HwndSource parentWindowHwndSource, Size initialSize) { if (initialSize.IsEmpty || initialSize.Equals(new Size(0, 0))) { throw new Exception("Invalid size, must be greater than 0,0"); } source = parentWindowHwndSource; if (!CreateOffscreenBrowser(initialSize)) { return false; } RoutedEventHandler handler = null; handler = (sender, args) => { Loaded -= handler; //If the browser has finished rendering before we attach to the Visual Tree //then ask for a new frame to be generated var host = this.GetBrowserHost(); if (host != null) { host.Invalidate(PaintElementType.View); } }; Loaded += handler; return true; } /// /// Create the underlying Browser instance, can be overriden to defer control creation /// The browser will only be created when size > Size(0,0). If you specify a positive /// size then the browser will be created, if the ActualWidth and ActualHeight /// properties are in reality still 0 then you'll likely end up with a browser that /// won't render. /// /// size of the current control, must be greater than Size(0, 0) /// bool to indicate if browser was created. If the browser has already been created then this will return false. protected virtual bool CreateOffscreenBrowser(Size size) { if (browserCreated || size.IsEmpty || size.Equals(new Size(0, 0))) { return false; } var webBrowserInternal = this as IWebBrowserInternal; if (!webBrowserInternal.HasParent) { var windowInfo = CreateOffscreenBrowserWindowInfo(source == null ? IntPtr.Zero : source.Handle); //If initialAddress is set then we use that value, later in OnAfterBrowserCreated then we will //call Load(url) if initial address was empty. //Issue https://github.com/cefsharp/CefSharp/issues/2300 managedCefBrowserAdapter.CreateBrowser(windowInfo, browserSettings, requestContext, address: initialAddress); //Dispose of BrowserSettings if we created it, if user created then they're responsible if (browserSettings.AutoDispose) { browserSettings.Dispose(); } browserSettings = null; } browserCreated = true; return true; } /// /// Override this method to handle creation of WindowInfo. This method can be used to customise aspects of /// browser creation including configuration of settings such as /// (used for D3D11 shared texture rendering). /// /// Window handle for the HwndSource /// Window Info protected virtual IWindowInfo CreateOffscreenBrowserWindowInfo(IntPtr handle) { var windowInfo = Core.ObjectFactory.CreateWindowInfo(); windowInfo.SetAsWindowless(handle); return windowInfo; } /// /// Runs the specific Action on the Dispatcher in an async fashion /// /// The action. /// The priority. internal void UiThreadRunAsync(Action action, DispatcherPriority priority = DispatcherPriority.DataBind) { if (Dispatcher.CheckAccess()) { action(); } else if (!Dispatcher.HasShutdownStarted) { Dispatcher.BeginInvoke(action, priority); } } /// /// Runs the specific Action on the Dispatcher in an sync fashion /// /// The action. /// The priority. private void UiThreadRunSync(Action action, DispatcherPriority priority = DispatcherPriority.DataBind) { if (Dispatcher.CheckAccess()) { action(); } else if (!Dispatcher.HasShutdownStarted) { Dispatcher.Invoke(action, priority); } } /// /// Handles the event. /// /// The sender. /// The instance containing the event data. private void OnActualSizeChanged(object sender, SizeChangedEventArgs e) { // Initialize RenderClientAdapter when WPF has calculated the actual size of current content. CreateOffscreenBrowser(e.NewSize); //NOTE: Previous we used Math.Ceiling to round the sizing up, we //now set UseLayoutRounding = true; on the control so the sizes are //already rounded to a whole number for us. viewRect = new Rect(0, 0, (int)e.NewSize.Width, (int)e.NewSize.Height); if (browser != null) { browser.GetHost().WasResized(); } } /// /// Handles the event. /// /// The sender. /// The instance containing the event data. private void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs args) { var isVisible = (bool)args.NewValue; if (browser != null) { OnBrowserWasHidden(!isVisible); if (isVisible) { //Fix for #1778 - When browser becomes visible we update the zoom level //browsers of the same origin will share the same zoomlevel and //we need to track the update, so our ZoomLevelProperty works //properly var host = browser.GetHost(); host.GetZoomLevelAsync().ContinueWith(t => { if (!IsDisposed) { SetCurrentValue(ZoomLevelProperty, t.Result); } }, CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.FromCurrentSynchronizationContext()); } } } /// /// Handles the event. /// /// The sender. /// The instance containing the event data. private void OnLoaded(object sender, RoutedEventArgs routedEventArgs) { // TODO: Consider making the delay here configurable. tooltipTimer = new DispatcherTimer( TimeSpan.FromSeconds(0.5), DispatcherPriority.Render, OnTooltipTimerTick, Dispatcher ); tooltipTimer.IsEnabled = false; //Initial value for screen location browserScreenLocation = GetBrowserScreenLocation(); } /// /// When overridden in a derived class, is invoked whenever application code or internal processes call /// . /// public override void OnApplyTemplate() { base.OnApplyTemplate(); if (image == null) { // Create main window image = (Image)GetTemplateChild(PartImageName); } if (popupImage == null) { popupImage = (Image)GetTemplateChild(PartPopupImageName); } } /// /// Converts a .NET Drag event to a CefSharp MouseEvent /// /// The instance containing the event data. /// MouseEvent. private MouseEvent GetMouseEvent(DragEventArgs e) { var point = e.GetPosition(this); return new MouseEvent((int)point.X, (int)point.Y, e.GetModifiers()); } /// /// Sets the popup size and position implementation. /// /// The popup rectangle (size and position). private void SetPopupSizeAndPositionImpl(Rect rect) { popupImage.Width = rect.Width; popupImage.Height = rect.Height; Canvas.SetLeft(popupImage, rect.X); Canvas.SetTop(popupImage, rect.Y); } /// /// Handles the event. /// /// The sender. /// The instance containing the event data. private void OnTooltipTimerTick(object sender, EventArgs e) { // Checks to see if the control is disposed/disposing if (!IsDisposed) { tooltipTimer.Stop(); OpenOrCloseToolTip(TooltipText); } } /// /// Handles the event. /// /// The sender. /// The instance containing the event data. private void OnTooltipClosed(object sender, RoutedEventArgs e) { toolTip.Visibility = Visibility.Collapsed; // Set Placement to something other than PlacementMode.Mouse, so that when we re-show the tooltip in // UpdateTooltip(), the tooltip will be repositioned to the new mouse point. toolTip.Placement = PlacementMode.Absolute; } /// /// Open or Close the tooltip at the current mouse position. /// /// ToolTip text, if null or empty tooltip will be closed. protected virtual void OpenOrCloseToolTip(string text) { if (string.IsNullOrEmpty(text)) { if (toolTip.IsOpen) { toolTip.IsOpen = false; } } else { // hide old tooltip before showing the new one to update the position if (toolTip.IsOpen) { toolTip.IsOpen = false; } //If no ToolTip style is defined then we'll //use a TextBlock to ensure the Text is wrapped //If a style is applied leave to the user to apply relevant wrapping //Issue https://github.com/cefsharp/CefSharp/issues/2488 if (toolTip.Style == null) { toolTip.Content = new TextBlock { Text = text, TextWrapping = TextWrapping.Wrap }; } else { toolTip.SetCurrentValue(ContentControl.ContentProperty, text); } toolTip.Placement = PlacementMode.Mouse; toolTip.Visibility = Visibility.Visible; toolTip.IsOpen = true; } } /// /// Handles the event. /// /// The sender. /// The instance containing the event data. private void OnGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) { if (browser == null) { initialFocus = true; } else { browser.GetHost().SetFocus(true); } } /// /// Handles the event. /// /// The sender. /// The instance containing the event data. private void OnLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) { if (browser == null) { initialFocus = false; } else { browser.GetHost().SetFocus(false); } } /// /// Invoked when an unhandled  attached event reaches an /// element in its route that is derived from this class. Implement this method to add class handling for this event. /// /// The that contains the event data. protected override void OnPreviewKeyDown(KeyEventArgs e) { if (!e.Handled && browser != null) { WpfKeyboardHandler.HandleKeyPress(e); } base.OnPreviewKeyDown(e); } /// /// Invoked when an unhandled  attached event reaches an /// element in its route that is derived from this class. Implement this method to add class handling for this event. /// /// The that contains the event data. protected override void OnPreviewKeyUp(KeyEventArgs e) { if (!e.Handled && browser != null) { WpfKeyboardHandler.HandleKeyPress(e); } base.OnPreviewKeyUp(e); } /// /// Handles the event. /// /// The instance containing the event data. protected override void OnPreviewTextInput(TextCompositionEventArgs e) { if (!e.Handled && browser != null) { WpfKeyboardHandler.HandleTextInput(e); } base.OnPreviewTextInput(e); } /// /// Invoked when an unhandled  attached event reaches an element in its route that is derived from this class. Implement this method to add class handling for this event. /// /// The that contains the event data. protected override void OnMouseMove(MouseEventArgs e) { //Mouse, touch, and stylus will raise mouse event. //For mouse events from an actual mouse, e.StylusDevice will be null. //For mouse events from touch and stylus, e.StylusDevice will not be null. //We only handle event from mouse here. //If not, touch will cause duplicate events (mousemove and touchmove) and so does stylus. //Use e.StylusDevice == null to ensure only mouse. if (!e.Handled && browser != null && e.StylusDevice == null) { var point = e.GetPosition(this); var modifiers = e.GetModifiers(); browser.GetHost().SendMouseMoveEvent((int)point.X, (int)point.Y, false, modifiers); } base.OnMouseMove(e); } /// /// Invoked when an unhandled  attached event reaches an element in its route that is derived from this class. Implement this method to add class handling for this event. /// /// The that contains the event data. protected override void OnMouseWheel(MouseWheelEventArgs e) { if (!e.Handled && browser != null) { var point = e.GetPosition(this); var modifiers = e.GetModifiers(); var isShiftKeyDown = Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift); var pointX = (int)point.X; var pointY = (int)point.Y; browser.SendMouseWheelEvent( pointX, pointY, deltaX: isShiftKeyDown ? e.Delta : 0, deltaY: !isShiftKeyDown ? e.Delta : 0, modifiers: modifiers); e.Handled = true; } base.OnMouseWheel(e); } /// /// Invoked when an unhandled  attached event reaches an /// element in its route that is derived from this class. Implement this method to add class handling for this event. /// /// The that contains the event data. /// This event data reports details about the mouse button that was pressed and the handled state. protected override void OnMouseDown(MouseButtonEventArgs e) { //Mouse, touch, and stylus will raise mouse event. //For mouse events from an actual mouse, e.StylusDevice will be null. //For mouse events from touch and stylus, e.StylusDevice will not be null. //We only handle event from mouse here. //If not, touch will cause duplicate events (mouseup and touchup) and so does stylus. //Use e.StylusDevice == null to ensure only mouse. if (e.StylusDevice == null) { Focus(); OnMouseButton(e); //We should only need to capture the left button exiting the browser if (e.ChangedButton == MouseButton.Left && e.LeftButton == MouseButtonState.Pressed) { //Capture/Release Mouse to allow for scrolling outside bounds of browser control (#2258). //Known issue when capturing and the device has a touch screen, to workaround this issue //disable WPF StylusAndTouchSupport see for details https://github.com/dotnet/wpf/issues/1323#issuecomment-513870984 CaptureMouse(); } } base.OnMouseDown(e); } /// /// Invoked when an unhandled  routed event reaches an element in its route that is derived from this class. Implement this method to add class handling for this event. /// /// The that contains the event data. The event data reports that the mouse button was released. protected override void OnMouseUp(MouseButtonEventArgs e) { //Mouse, touch, and stylus will raise mouse event. //For mouse events from an actual mouse, e.StylusDevice will be null. //For mouse events from touch and stylus, e.StylusDevice will not be null. //We only handle event from mouse here. //If not, touch will cause duplicate events (mouseup and touchup) and so does stylus. //Use e.StylusDevice == null to ensure only mouse. if (e.StylusDevice == null) { OnMouseButton(e); if (e.ChangedButton == MouseButton.Left && e.LeftButton == MouseButtonState.Released) { //Release the mouse capture that we grabbed on mouse down. //We won't get here if e.g. the right mouse button is pressed and released //while the left is still held, but in that case the left mouse capture seems //to be released implicitly (even without the left mouse SendMouseClickEvent in leave below) //Use ReleaseMouseCapture over Mouse.Capture(null); as it has additional Mouse.Captured == this check ReleaseMouseCapture(); } } base.OnMouseUp(e); } /// /// Invoked when an unhandled  attached event is raised on this element. Implement this method to add class handling for this event. /// /// The that contains the event data. protected override void OnMouseLeave(MouseEventArgs e) { //Mouse, touch, and stylus will raise mouse event. //For mouse events from an actual mouse, e.StylusDevice will be null. //For mouse events from touch and stylus, e.StylusDevice will not be null. //We only handle event from mouse here. //OnMouseLeave event from touch or stylus needn't to be handled. //Use e.StylusDevice == null to ensure only mouse. if (!e.Handled && browser != null && e.StylusDevice == null) { var modifiers = e.GetModifiers(); var point = e.GetPosition(this); browser.GetHost().SendMouseMoveEvent((int)point.X, (int)point.Y, true, modifiers); ((IWebBrowserInternal)this).SetTooltipText(null); } base.OnMouseLeave(e); } /// /// Invoked when an unhandled  attached event reaches an element in /// its route that is derived from this class. Implement this method to add class handling for this event. /// /// The that contains event data. protected override void OnLostMouseCapture(MouseEventArgs e) { if (!e.Handled && browser != null) { browser.GetHost().SendCaptureLostEvent(); } base.OnLostMouseCapture(e); } /// /// Handles the event. /// /// The instance containing the event data. private void OnMouseButton(MouseButtonEventArgs e) { if (!e.Handled && browser != null) { var modifiers = e.GetModifiers(); var mouseUp = (e.ButtonState == MouseButtonState.Released); var point = e.GetPosition(this); if (e.ChangedButton == MouseButton.XButton1) { if (CanGoBack && mouseUp) { this.Back(); } } else if (e.ChangedButton == MouseButton.XButton2) { if (CanGoForward && mouseUp) { this.Forward(); } } else { //Chromium only supports values of 1, 2 or 3. //https://github.com/cefsharp/CefSharp/issues/3940 //Anything greater than 3 then we send click count of 1 var clickCount = e.ClickCount; if(clickCount > 3) { clickCount = 1; } browser.GetHost().SendMouseClickEvent((int)point.X, (int)point.Y, (MouseButtonType)e.ChangedButton, mouseUp, clickCount, modifiers); } e.Handled = true; } } /// /// Provides class handling for the routed event that occurs when a touch presses inside this element. /// /// The that contains the event data. protected override void OnTouchDown(TouchEventArgs e) { Focus(); // Capture touch so touch events are still pushed to CEF even if the touch leaves the control before a TouchUp. // This behavior is similar to how other browsers handle touch input. CaptureTouch(e.TouchDevice); OnTouch(e); base.OnTouchDown(e); } /// /// Provides class handling for the routed event that occurs when a touch moves while inside this element. /// /// The that contains the event data. protected override void OnTouchMove(TouchEventArgs e) { OnTouch(e); base.OnTouchMove(e); } /// /// Provides class handling for the routed event that occurs when a touch is released inside this element. /// /// The that contains the event data. protected override void OnTouchUp(TouchEventArgs e) { ReleaseTouchCapture(e.TouchDevice); OnTouch(e); base.OnTouchUp(e); } /// /// Handles a event. /// /// The instance containing the event data. private void OnTouch(TouchEventArgs e) { if (!e.Handled && browser != null) { var modifiers = WpfExtensions.GetModifierKeys(); var touchPoint = e.GetTouchPoint(this); var touchEventType = TouchEventType.Cancelled; switch (touchPoint.Action) { case TouchAction.Down: { touchEventType = TouchEventType.Pressed; break; } case TouchAction.Move: { touchEventType = TouchEventType.Moved; break; } case TouchAction.Up: { touchEventType = TouchEventType.Released; break; } default: { touchEventType = TouchEventType.Cancelled; break; } } var touchEvent = new TouchEvent() { Id = e.TouchDevice.Id, X = (float)touchPoint.Position.X, Y = (float)touchPoint.Position.Y, PointerType = PointerType.Touch, Type = touchEventType, Modifiers = modifiers, }; browser.GetHost().SendTouchEvent(touchEvent); e.Handled = true; } } protected override AutomationPeer OnCreateAutomationPeer() { var handler = AccessibilityHandler as Experimental.Accessibility.AccessibilityHandler; if (handler == null) { return base.OnCreateAutomationPeer(); } return handler.AutomationPeer; } /// public void Load(string url) { if(IsDisposed) { return; } //If the browser is already initialized then we can call LoadUrl directly if (InternalIsBrowserInitialized()) { // Added null check -> binding-triggered changes of Address will lead to a nullref after Dispose has been called // or before OnApplyTemplate has been called if (browser != null) { initialLoadCalled = true; using (var frame = browser.MainFrame) { frame.LoadUrl(url); } } } //If CreateBrowser was called and InternalIsBrowserInitialized() == false then we need to set the Address //property so in OnAfterBrowserCreated the Url is loaded. If initialAddress was //set then the Url set here will be ignored. If we called Load(url) then historically //an aborted error would be raised as per https://github.com/cefsharp/CefSharp/issues/2300 //So we ignore the call for now. else if (browserCreated) { UiThreadRunAsync(() => { Address = url; }); } //Before browser created, set the intialAddress else { initialAddress = url; } } /// /// Zooms the browser in. /// private void ZoomIn() { UiThreadRunAsync(() => { ZoomLevel = ZoomLevel + ZoomLevelIncrement; }); } /// /// Zooms the browser out. /// private void ZoomOut() { UiThreadRunAsync(() => { ZoomLevel = ZoomLevel - ZoomLevelIncrement; }); } /// /// Reset the browser's zoom level to default. /// private void ZoomReset() { UiThreadRunAsync(() => { ZoomLevel = 0; }); } /// /// Manually notify the browser the DPI of the parent window has changed. /// The tested/expected values for are 1.0, 1.25, 1.5, 2.0 as these /// correspond to 96, 120, 144, 192 DPI (referred to as 100%, 125%, 150%, 200% in the Windows GUI). /// /// new DPI /// .Net 4.6.2 adds HwndSource.DpiChanged which could be used to automatically /// handle DPI change, unfortunately we still target .Net 4.5.2 public virtual void NotifyDpiChange(float newDpi) { //Do nothing if (DpiScaleFactor.Equals(newDpi)) { return; } var notifyDpiChanged = DpiScaleFactor > 0 && !DpiScaleFactor.Equals(newDpi); DpiScaleFactor = newDpi; if (notifyDpiChanged && browser != null) { browser.GetHost().NotifyScreenInfoChanged(); } //If the user has specified a custom RenderHandler then we'll skip this part //as to not override their implementation. //TODO: Add support for RenderHandler changing the DPI rather //than creating a new instance (allow users to be notified of DPI change). if (RenderHandler == null || RenderHandler is WritableBitmapRenderHandler || RenderHandler is DirectWritableBitmapRenderHandler) { const int DefaultDpi = 96; var scale = DefaultDpi * DpiScaleFactor; var oldRenderHandler = RenderHandler; if (Cef.CurrentlyOnThread(CefThreadIds.TID_UI)) { RenderHandler = new DirectWritableBitmapRenderHandler(scale, scale, invalidateDirtyRect: true); } else { RenderHandler = new WritableBitmapRenderHandler(scale, scale); } oldRenderHandler?.Dispose(); } } #if NETCOREAPP || NET462 /// /// Waits for the page rendering to be idle for . /// Rendering is considered to be idle when no events have occured /// for . /// This is useful for scenarios like taking a screen shot /// /// optional idleTime in miliseconds, default to 500ms /// optional timeout, if not specified defaults to thirty(30) seconds. /// optional CancellationToken /// Task that resolves when page rendering has been idle for public async Task WaitForRenderIdleAsync(int idleTimeInMs = 500, TimeSpan? timeout = null, CancellationToken cancellationToken = default) { var renderIdleTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var idleTimer = new System.Timers.Timer { Interval = idleTimeInMs, AutoReset = false }; EventHandler handler = null; idleTimer.Elapsed += (sender, args) => { Paint -= handler; idleTimer.Stop(); idleTimer.Dispose(); renderIdleTcs.TrySetResult(true); }; //Every time Paint is called we reset our timer handler = (s, args) => { idleTimer.Stop(); idleTimer.Start(); }; idleTimer.Start(); Paint += handler; try { await TaskTimeoutExtensions.WaitAsync(renderIdleTcs.Task, timeout ?? TimeSpan.FromSeconds(30), cancellationToken).ConfigureAwait(false); } catch (Exception) { Paint -= handler; idleTimer?.Stop(); idleTimer?.Dispose(); throw; } } #endif /// /// Legacy keyboard handler uses WindowProc callback interceptor to forward keypress events /// the the browser. Use this method to revert to the previous keyboard handling behaviour /// public void UseLegacyKeyboardHandler() { if (!(WpfKeyboardHandler is WpfLegacyKeyboardHandler)) { WpfKeyboardHandler.Dispose(); WpfKeyboardHandler = new WpfLegacyKeyboardHandler(this); WpfKeyboardHandler.Setup(source); } } /// /// The javascript object repository, one repository per ChromiumWebBrowser instance. /// public IJavascriptObjectRepository JavascriptObjectRepository { get { return managedCefBrowserAdapter?.JavascriptObjectRepository; } } /// /// Returns the current IBrowser Instance /// /// browser instance or null public IBrowser GetBrowser() { ThrowExceptionIfDisposed(); ThrowExceptionIfBrowserNotInitialized(); return browser; } private async Task CefUiThreadRunAsync(Action action) { if (!IsDisposed && InternalIsBrowserInitialized()) { if (Cef.CurrentlyOnThread(CefThreadIds.TID_UI)) { action(); } else { await Cef.UIThreadTaskFactory.StartNew(delegate { action(); }); } } } /// /// Resize hack for https://bitbucket.org/chromiumembedded/cef/issues/3427/osr-rendering-bug-when-minimizing-and /// /// Task private async Task ResizeHackRun() { var host = browser?.GetHost(); if (host != null && !host.IsDisposed) { resizeHackSize = new Structs.Size(viewRect.Width + 1, viewRect.Height + 1); host.WasResized(); await Task.Delay(ResizeHackDelayInMs); if (!host.IsDisposed) { resizeHackSize = null; host.WasResized(); resizeHackIgnoreOnPaint = false; host.Invalidate(PaintElementType.View); } } } } }