Core - Add Default Handler implementations

Inherit and override only the required methods rather than implement the whole interface
This commit is contained in:
Alex Maitland
2021-05-11 20:24:48 +10:00
parent fd1afdd8b4
commit e035c39d2b
22 changed files with 1854 additions and 47 deletions
@@ -9,7 +9,7 @@ using CefSharp.SchemeHandler;
namespace CefSharp.Example.Handlers
{
public class BrowserProcessHandler : IBrowserProcessHandler
public class BrowserProcessHandler : CefSharp.Handler.BrowserProcessHandler
{
/// <summary>
/// The interval between calls to Cef.DoMessageLoopWork
@@ -20,7 +20,7 @@ namespace CefSharp.Example.Handlers
/// </summary>
protected const int ThirtyTimesPerSecond = 1000 / 30; //30fps
void IBrowserProcessHandler.OnContextInitialized()
protected override void OnContextInitialized()
{
//The Global CookieManager has been initialized, you can now set cookies
var cookieManager = Cef.GetGlobalCookieManager();
@@ -106,26 +106,5 @@ namespace CefSharp.Example.Handlers
context.RegisterSchemeHandlerFactory("https", "cefsharp.example", folderSchemeHandlerExample);
}
}
void IBrowserProcessHandler.OnScheduleMessagePumpWork(long delay)
{
//If the delay is greater than the Maximum then use ThirtyTimesPerSecond
//instead - we do this to achieve a minimum number of FPS
if (delay > ThirtyTimesPerSecond)
{
delay = ThirtyTimesPerSecond;
}
OnScheduleMessagePumpWork((int)delay);
}
protected virtual void OnScheduleMessagePumpWork(int delay)
{
//TODO: Schedule work on the UI thread - call Cef.DoMessageLoopWork
}
public virtual void Dispose()
{
}
}
}
@@ -39,8 +39,15 @@ namespace CefSharp.WinForms.Example.Handlers
factory.StartNew(() => Cef.DoMessageLoopWork());
}
protected override void OnScheduleMessagePumpWork(int delay)
protected override void OnScheduleMessagePumpWork(long delay)
{
//If the delay is greater than the Maximum then use ThirtyTimesPerSecond
//instead - we do this to achieve a minimum number of FPS
if (delay > ThirtyTimesPerSecond)
{
delay = ThirtyTimesPerSecond;
}
//when delay <= 0 queue the Task up for execution on the UI thread.
if (delay <= 0)
{
@@ -49,14 +56,19 @@ namespace CefSharp.WinForms.Example.Handlers
}
}
public override void Dispose()
protected override void Dispose(bool disposing)
{
if (timer != null)
if(disposing)
{
timer.Stop();
timer.Dispose();
timer = null;
if (timer != null)
{
timer.Stop();
timer.Dispose();
timer = null;
}
}
base.Dispose(disposing);
}
}
}
@@ -33,19 +33,24 @@ namespace CefSharp.WinForms.Example.Handlers
Cef.DoMessageLoopWork();
}
protected override void OnScheduleMessagePumpWork(int delay)
protected override void OnScheduleMessagePumpWork(long delay)
{
//NOOP - Only enabled when CefSettings.ExternalMessagePump
}
public override void Dispose()
protected override void Dispose(bool disposing)
{
if (timer != null)
if(disposing)
{
timer.Stop();
timer.Dispose();
timer = null;
if (timer != null)
{
timer.Stop();
timer.Dispose();
timer = null;
}
}
base.Dispose(disposing);
}
}
}
@@ -44,8 +44,15 @@ namespace CefSharp.Wpf.Example.Handlers
dispatcher.BeginInvoke((Action)(() => Cef.DoMessageLoopWork()), DispatcherPriority.Render);
}
protected override void OnScheduleMessagePumpWork(int delay)
protected override void OnScheduleMessagePumpWork(long delay)
{
//If the delay is greater than the Maximum then use ThirtyTimesPerSecond
//instead - we do this to achieve a minimum number of FPS
if (delay > ThirtyTimesPerSecond)
{
delay = ThirtyTimesPerSecond;
}
//When delay <= 0 we'll execute Cef.DoMessageLoopWork immediately
// if it's greater than we'll just let the Timer which fires 30 times per second
// care of the call
@@ -55,20 +62,25 @@ namespace CefSharp.Wpf.Example.Handlers
}
}
public override void Dispose()
protected override void Dispose(bool disposing)
{
if (dispatcher != null)
if(disposing)
{
dispatcher.ShutdownStarted -= DispatcherShutdownStarted;
dispatcher = null;
if (dispatcher != null)
{
dispatcher.ShutdownStarted -= DispatcherShutdownStarted;
dispatcher = null;
}
if (timer != null)
{
timer.Stop();
timer.Dispose();
timer = null;
}
}
if (timer != null)
{
timer.Stop();
timer.Dispose();
timer = null;
}
base.Dispose(disposing);
}
}
}
+16
View File
@@ -104,6 +104,22 @@
<Compile Include="Enums\CookiePriority.cs" />
<Compile Include="Enums\CookieSameSite.cs" />
<Compile Include="Enums\SchemeOptions.cs" />
<Compile Include="Handler\AccessibilityHandler.cs" />
<Compile Include="Handler\AudioHandler.cs" />
<Compile Include="Handler\BrowserProcessHandler.cs" />
<Compile Include="Handler\ContextMenuHandler.cs" />
<Compile Include="Handler\CookieAccessFilter.cs" />
<Compile Include="Handler\DialogHandler.cs" />
<Compile Include="Handler\DisplayHandler.cs" />
<Compile Include="Handler\DownloadHandler.cs" />
<Compile Include="Handler\DragHandler .cs" />
<Compile Include="Handler\ExtensionHandler .cs" />
<Compile Include="Handler\FindHandler.cs" />
<Compile Include="Handler\FocusHandler.cs" />
<Compile Include="Handler\LoadHandler.cs" />
<Compile Include="Handler\KeyboardHandler.cs" />
<Compile Include="Handler\JsDialogHandler.cs" />
<Compile Include="Handler\LifeSpanHandler.cs" />
<Compile Include="Handler\RequestContextHandler.cs" />
<Compile Include="Internals\CefThread.cs" />
<Compile Include="Internals\FreezableBase.cs" />
+30 -1
View File
@@ -2,6 +2,7 @@
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
using System.Collections.Generic;
namespace CefSharp
@@ -10,8 +11,10 @@ namespace CefSharp
/// Default implementation of <see cref="IApp"/> which represents the CefApp class.
/// </summary>
/// <seealso cref="T:CefSharp.IApp"/>
public class DefaultApp : IApp
public class DefaultApp : IApp, IDisposable
{
private bool isDisposed;
/// <summary>
/// Return the handler for functionality specific to the browser process. This method is called on multiple threads.
/// </summary>
@@ -88,5 +91,31 @@ namespace CefSharp
}
}
}
/// <summary>
/// Releases unmanaged and managed resources
/// </summary>
/// <param name="disposing"><see langword="true" /> to release both managed and unmanaged resources; <see langword="false" /> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (!isDisposed)
{
if (disposing)
{
BrowserProcessHandler?.Dispose();
BrowserProcessHandler = null;
}
isDisposed = true;
}
}
/// <inheritdoc/>
void IDisposable.Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}
+44
View File
@@ -0,0 +1,44 @@
// 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.
namespace CefSharp.Handler
{
/// <summary>
/// Inherit from this class to receive accessibility notification when accessibility events have been registered.
/// It's important to note that the methods of this interface are called on a CEF UI thread,
/// which by default is not the same as your application UI thread.
/// </summary>
public class AccessibilityHandler : IAccessibilityHandler
{
/// <inheritdoc/>
void IAccessibilityHandler.OnAccessibilityLocationChange(IValue value)
{
OnAccessibilityLocationChange(value);
}
/// <summary>
/// Called after renderer process sends accessibility location changes to the browser process.
/// </summary>
/// <param name="value">Updated location info.</param>
protected virtual void OnAccessibilityLocationChange(IValue value)
{
}
/// <inheritdoc/>
void IAccessibilityHandler.OnAccessibilityTreeChange(IValue value)
{
OnAccessibilityTreeChange(value);
}
/// <summary>
/// Called after renderer process sends accessibility tree changes to the browser process.
/// </summary>
/// <param name="value">Updated tree info.</param>
protected virtual void OnAccessibilityTreeChange(IValue value)
{
}
}
}
+121
View File
@@ -0,0 +1,121 @@
// 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 CefSharp.Structs;
namespace CefSharp.Handler
{
/// <summary>
/// Inherit from this class to handle audio events
/// All methods will be called on the CEF UI thread
/// </summary>
public class AudioHandler : IAudioHandler
{
/// <inheritdoc/>
bool IAudioHandler.GetAudioParameters(IWebBrowser chromiumWebBrowser, IBrowser browser, ref AudioParameters parameters)
{
return GetAudioParameters(chromiumWebBrowser, browser, ref parameters);
}
/// <summary>
/// Called on the CEF UI thread to allow configuration of audio stream parameters.
/// Audio stream paramaters can optionally be configured by modifying <paramref name="parameters"/>
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
/// <param name="parameters">audio stream parameters can optionally be configured here, they are
/// pre-filled with some sensible defaults.</param>
/// <returns>Return true to proceed with audio stream capture, or false to cancel it</returns>
protected virtual bool GetAudioParameters(IWebBrowser chromiumWebBrowser, IBrowser browser, ref AudioParameters parameters)
{
return false;
}
/// <inheritdoc/>
void IAudioHandler.OnAudioStreamStarted(IWebBrowser chromiumWebBrowser,
IBrowser browser,
AudioParameters parameters,
int channels)
{
OnAudioStreamStarted(chromiumWebBrowser, browser, parameters, channels);
}
/// <summary>
/// Called on a browser audio capture thread when the browser starts streaming audio.
/// OnAudioSteamStopped will always be called after OnAudioStreamStarted; both methods may be called multiple
/// times for the same browser.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
/// <param name="parameters">contains the audio parameters like sample rate and channel layout.
/// Changing the param values will have no effect here.</param>
/// <param name="channels">is the number of channels</param>
protected virtual void OnAudioStreamStarted(IWebBrowser chromiumWebBrowser,
IBrowser browser,
AudioParameters parameters,
int channels)
{
}
/// <inheritdoc/>
void IAudioHandler.OnAudioStreamPacket(IWebBrowser chromiumWebBrowser, IBrowser browser, IntPtr data, int noOfFrames, long pts)
{
OnAudioStreamPacket(chromiumWebBrowser, browser, data, noOfFrames, pts);
}
/// <summary>
/// Called on the audio stream thread when a PCM packet is received for the stream.
/// Based on and the <see cref="AudioParameters.ChannelLayout"/> value passed to <see cref="OnAudioStreamStarted"/>
/// you can calculate the size of the <paramref name="data"/> array in bytes.
/// </summary>
/// <param name="chromiumWebBrowser"></param>
/// <param name="data">is an array representing the raw PCM data as a floating point type, i.e. 4-byte value(s).</param>
/// <param name="noOfFrames">is the number of frames in the PCM packet</param>
/// <param name="pts">is the presentation timestamp (in milliseconds since the Unix Epoch)
/// and represents the time at which the decompressed packet should be presented to the user</param>
protected virtual void OnAudioStreamPacket(IWebBrowser chromiumWebBrowser, IBrowser browser, IntPtr data, int noOfFrames, long pts)
{
}
/// <inheritdoc/>
void IAudioHandler.OnAudioStreamStopped(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
OnAudioStreamStopped(chromiumWebBrowser, browser);
}
/// <summary>
/// Called on the CEF UI thread when the stream has stopped. OnAudioSteamStopped will always be called after <see cref="OnAudioStreamStarted"/>;
/// both methods may be called multiple times for the same stream.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
protected virtual void OnAudioStreamStopped(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
}
/// <inheritdoc/>
void IAudioHandler.OnAudioStreamError(IWebBrowser chromiumWebBrowser, IBrowser browser, string errorMessage)
{
OnAudioStreamError(chromiumWebBrowser, browser, errorMessage);
}
/// <summary>
/// Called on the CEF UI thread or audio stream thread when an error occurred. During the
/// stream creation phase this callback will be called on the UI thread while
/// in the capturing phase it will be called on the audio stream thread. The
/// stream will be stopped immediately.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
/// <param name="errorMessage">error message</param>
protected virtual void OnAudioStreamError(IWebBrowser chromiumWebBrowser, IBrowser browser, string errorMessage)
{
}
}
}
+78
View File
@@ -0,0 +1,78 @@
// 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;
namespace CefSharp.Handler
{
/// <summary>
/// Inherit from this class to handle events related to browser process callbacks.
/// The methods of this class will be called on the CEF UI thread unless otherwise indicated. .
/// </summary>
public class BrowserProcessHandler : IBrowserProcessHandler
{
private bool isDisposed;
/// <inheritdoc/>
void IBrowserProcessHandler.OnContextInitialized()
{
OnContextInitialized();
}
/// <summary>
/// Called on the CEF UI thread immediately after the CEF context has been initialized.
/// You can now access the Global RequestContext through Cef.GetGlobalRequestContext() - this is the
/// first place you can set Preferences (e.g. proxy settings, spell check dictionaries).
/// </summary>
protected virtual void OnContextInitialized()
{
}
/// <inheritdoc/>
void IBrowserProcessHandler.OnScheduleMessagePumpWork(long delay)
{
OnScheduleMessagePumpWork(delay);
}
/// <summary>
/// Called from any thread when work has been scheduled for the browser process
/// main (UI) thread. This callback is used in combination with CefSettings.
/// ExternalMessagePump and Cef.DoMessageLoopWork() in cases where the CEF
/// message loop must be integrated into an existing application message loop
/// (see additional comments and warnings on Cef.DoMessageLoopWork). This
/// callback should schedule a Cef.DoMessageLoopWork() call to happen on the
/// main (UI) thread.
/// </summary>
/// <param name="delay">is the requested delay in milliseconds. If
/// delay is less than or equal to 0 then the call should happen reasonably soon. If
/// delay is greater than 0 then the call should be scheduled to happen after the
/// specified delay and any currently pending scheduled call should be
/// cancelled.</param>
protected virtual void OnScheduleMessagePumpWork(long delay)
{
}
/// <summary>
/// IsDisposed
/// </summary>
public bool IsDisposed
{
get { return isDisposed; }
}
protected virtual void Dispose(bool disposing)
{
isDisposed = true;
}
/// <inheritdoc/>
void IDisposable.Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}
+100
View File
@@ -0,0 +1,100 @@
// 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.
namespace CefSharp.Handler
{
/// Inherit from this class to handle context menu events.
/// </summary>
public class ContextMenuHandler : IContextMenuHandler
{
/// <inheritdoc/>
void IContextMenuHandler.OnBeforeContextMenu(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters,
IMenuModel model)
{
OnBeforeContextMenu(chromiumWebBrowser, browser, frame, parameters, model);
}
/// <summary>
/// Called before a context menu is displayed. The model can be cleared to show no context menu or
/// modified to show a custom menu.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
/// <param name="frame">The frame the request is coming from</param>
/// <param name="parameters">provides information about the context menu state</param>
/// <param name="model">initially contains the default context menu</param>
protected virtual void OnBeforeContextMenu(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters,
IMenuModel model)
{
}
/// <inheritdoc/>
bool IContextMenuHandler.OnContextMenuCommand(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters,
CefMenuCommand commandId, CefEventFlags eventFlags)
{
return OnContextMenuCommand(chromiumWebBrowser, browser, frame, parameters, commandId, eventFlags);
}
/// <summary>
/// Called to execute a command selected from the context menu. See
/// cef_menu_id_t for the command ids that have default implementations. All
/// user-defined command ids should be between MENU_ID_USER_FIRST and
/// MENU_ID_USER_LAST.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
/// <param name="frame">The frame the request is coming from</param>
/// <param name="parameters">will have the same values as what was passed to</param>
/// <param name="commandId">menu command id</param>
/// <param name="eventFlags">event flags</param>
/// <returns>Return true if the command was handled or false for the default implementation.</returns>
protected virtual bool OnContextMenuCommand(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters,
CefMenuCommand commandId, CefEventFlags eventFlags)
{
return false;
}
/// <inheritdoc/>
void IContextMenuHandler.OnContextMenuDismissed(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame)
{
OnContextMenuDismissed(chromiumWebBrowser, browser, frame);
}
/// <summary>
/// Called when the context menu is dismissed irregardless of whether the menu
/// was empty or a command was selected.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
/// <param name="frame">The frame the request is coming from</param>
protected virtual void OnContextMenuDismissed(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame)
{
}
/// <inheritdoc/>
bool IContextMenuHandler.RunContextMenu(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback)
{
return RunContextMenu(chromiumWebBrowser, browser, frame, parameters, model, callback);
}
/// <summary>
/// Called to allow custom display of the context menu.
/// For custom display return true and execute callback either synchronously or asynchronously with the selected command Id.
/// For default display return false. Do not keep references to parameters or model outside of this callback.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
/// <param name="frame">The frame the request is coming from</param>
/// <param name="parameters">provides information about the context menu state</param>
/// <param name="model">contains the context menu model resulting from OnBeforeContextMenu</param>
/// <param name="callback">the callback to execute for custom display</param>
/// <returns>For custom display return true and execute callback either synchronously or asynchronously with the selected command ID.</returns>
protected virtual bool RunContextMenu(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback)
{
return false;
}
}
}
+55
View File
@@ -0,0 +1,55 @@
// 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.
namespace CefSharp.Handler
{
/// <summary>
/// Inherit from this class to filter cookies that may be sent or received from
/// resource requests. The methods of this class will be called on the CEF IO thread
/// unless otherwise indicated.
/// </summary>
public class CookieAccessFilter : ICookieAccessFilter
{
/// <inheritdoc/>
bool ICookieAccessFilter.CanSendCookie(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, Cookie cookie)
{
return CanSendCookie(chromiumWebBrowser, browser, frame, request, cookie);
}
/// <summary>
/// Called on the CEF IO thread before a resource request is sent.
/// </summary>
/// <param name="chromiumWebBrowser">The ChromiumWebBrowser control</param>
/// <param name="browser">the browser object - may be null if originating from ServiceWorker or CefURLRequest</param>
/// <param name="frame">the frame object - may be null if originating from ServiceWorker or CefURLRequest</param>
/// <param name="request">the request object - cannot be modified in this callback</param>
/// <param name="cookie">the cookie object</param>
/// <returns>Return true if the specified cookie can be sent with the request or false otherwise.</returns>
protected virtual bool CanSendCookie(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, Cookie cookie)
{
return false;
}
/// <inheritdoc/>
bool ICookieAccessFilter.CanSaveCookie(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response, Cookie cookie)
{
return CanSaveCookie(chromiumWebBrowser, browser, frame, request, response, cookie);
}
/// <summary>
/// Called on the CEF IO thread after a resource response is received.
/// </summary>
/// <param name="chromiumWebBrowser">The ChromiumWebBrowser control</param>
/// <param name="browser">the browser object - may be null if originating from ServiceWorker or CefURLRequest</param>
/// <param name="frame">the frame object - may be null if originating from ServiceWorker or CefURLRequest</param>
/// <param name="request">the request object - cannot be modified in this callback</param>
/// <param name="response">the response object - cannot be modified in this callback</param>
/// <param name="cookie">the cookie object</param>
/// <returns>Return true if the specified cookie returned with the response can be saved or false otherwise.</returns>
protected virtual bool CanSaveCookie(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response, Cookie cookie)
{
return false;
}
}
}
+69
View File
@@ -0,0 +1,69 @@
// 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.Collections.Generic;
namespace CefSharp.Handler
{
/// <summary>
/// Implement this interface to handle dialog events. The methods of this class will be called on the CEF UI thread.
/// </summary>
public class DialogHandler : IDialogHandler
{
/// <inheritdoc/>
bool IDialogHandler.OnFileDialog(
IWebBrowser chromiumWebBrowser,
IBrowser browser,
CefFileDialogMode mode,
CefFileDialogFlags flags,
string title,
string defaultFilePath,
List<string> acceptFilters,
int selectedAcceptFilter,
IFileDialogCallback callback)
{
return OnFileDialog(chromiumWebBrowser, browser, mode, flags, title, defaultFilePath, acceptFilters, selectedAcceptFilter, callback);
}
/// <summary>
/// Runs a file chooser dialog.
/// </summary>
/// <example>
/// To test assign something like TempFileDialogHandler (from CefSharp.Example) to DialogHandler e.g.
/// <code>
/// browser.DialogHandler = new TempFileDialogHandler();
/// </code>
/// Example URL to use for file browsing http://www.cs.tut.fi/~jkorpela/forms/file.html#example
/// Simply click browse, the space next to the browse button should be populated with a randomly generated filename.
/// </example>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
/// <param name="mode">represents the type of dialog to display</param>
/// <param name="flags">further specifies behavior dialog should exhibit</param>
/// <param name="title">the title to be used for the dialog. It may be empty to show the default title ("Open" or "Save"
/// depending on the mode).</param>
/// <param name="defaultFilePath">is the path with optional directory and/or file name component that
/// should be initially selected in the dialog.</param>
/// <param name="acceptFilters">are used to restrict the selectable file types and may any combination of
/// (a) valid lower-cased MIME types (e.g. "text/*" or "image/*"),
/// (b) individual file extensions (e.g. ".txt" or ".png"),
/// (c) combined description and file extension delimited using "|" and ";" (e.g. "Image Types|.png;.gif;.jpg").</param>
/// <param name="selectedAcceptFilter">is the 0-based index of the filter that should be selected by default.</param>
/// <param name="callback">Callback interface for asynchronous continuation of file dialog requests.</param>
/// <returns>To display a custom dialog return true. To display the default dialog return false.</returns>
protected virtual bool OnFileDialog(
IWebBrowser chromiumWebBrowser,
IBrowser browser,
CefFileDialogMode mode,
CefFileDialogFlags flags,
string title,
string defaultFilePath,
List<string> acceptFilters,
int selectedAcceptFilter,
IFileDialogCallback callback)
{
return false;
}
}
}
+198
View File
@@ -0,0 +1,198 @@
// 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 CefSharp.Enums;
using CefSharp.Structs;
using System;
using System.Collections.Generic;
using Size = CefSharp.Structs.Size;
namespace CefSharp.Handler
{
/// <summary>
/// Handle events related to browser display state.
/// </summary>
public class DisplayHandler : IDisplayHandler
{
/// <inheritdoc/>
void IDisplayHandler.OnAddressChanged(IWebBrowser chromiumWebBrowser, AddressChangedEventArgs addressChangedArgs)
{
OnAddressChanged(chromiumWebBrowser, addressChangedArgs);
}
/// <summary>
/// Called when a frame's address has changed.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="addressChangedArgs">args</param>
protected virtual void OnAddressChanged(IWebBrowser chromiumWebBrowser, AddressChangedEventArgs addressChangedArgs)
{
}
/// <inheritdoc/>
bool IDisplayHandler.OnAutoResize(IWebBrowser chromiumWebBrowser, IBrowser browser, Size newSize)
{
return OnAutoResize(chromiumWebBrowser, browser, newSize);
}
/// <summary>
/// Called when auto-resize is enabled via IBrowserHost.SetAutoResizeEnabled and the contents have auto-resized.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
/// <param name="newSize">will be the desired size in view coordinates</param>
/// <returns>Return true if the resize was handled or false for default handling. </returns>
protected virtual bool OnAutoResize(IWebBrowser chromiumWebBrowser, IBrowser browser, Size newSize)
{
return false;
}
/// <inheritdoc/>
bool IDisplayHandler.OnCursorChange(IWebBrowser chromiumWebBrowser, IBrowser browser, IntPtr cursor, CursorType type, CursorInfo customCursorInfo)
{
return OnCursorChange(chromiumWebBrowser, browser, cursor, type, customCursorInfo);
}
/// <summary>
/// Called when the browser's cursor has changed.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
/// <param name="cursor">If type is Custom then customCursorInfo will be populated with the custom cursor information</param>
/// <param name="type">cursor type</param>
/// <param name="customCursorInfo">custom cursor Information</param>
/// <returns>Return true if the cursor change was handled or false for default handling.</returns>
protected virtual bool OnCursorChange(IWebBrowser chromiumWebBrowser, IBrowser browser, IntPtr cursor, CursorType type, CursorInfo customCursorInfo)
{
return false;
}
/// <inheritdoc/>
void IDisplayHandler.OnTitleChanged(IWebBrowser chromiumWebBrowser, TitleChangedEventArgs titleChangedArgs)
{
OnTitleChanged(chromiumWebBrowser, titleChangedArgs);
}
/// <summary>
/// Called when the page title changes.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="titleChangedArgs">args</param>
protected virtual void OnTitleChanged(IWebBrowser chromiumWebBrowser, TitleChangedEventArgs titleChangedArgs)
{
}
/// <inheritdoc/>
void IDisplayHandler.OnFaviconUrlChange(IWebBrowser chromiumWebBrowser, IBrowser browser, IList<string> urls)
{
OnFaviconUrlChange(chromiumWebBrowser, browser, urls);
}
/// <summary>
/// Called when the page icon changes.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
/// <param name="urls">list of urls where the favicons can be downloaded</param>
protected virtual void OnFaviconUrlChange(IWebBrowser chromiumWebBrowser, IBrowser browser, IList<string> urls)
{
}
/// <inheritdoc/>
void IDisplayHandler.OnFullscreenModeChange(IWebBrowser chromiumWebBrowser, IBrowser browser, bool fullscreen)
{
OnFullscreenModeChange(chromiumWebBrowser, browser, fullscreen);
}
/// <summary>
/// Called when web content in the page has toggled fullscreen mode. The client is
/// responsible for resizing the browser if desired.
/// </summary>
/// <param name="chromiumWebBrowser">The ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
/// <param name="fullscreen">If true the content will automatically be sized to fill the browser content area.
/// If false the content will automatically return to its original size and position.</param>
protected virtual void OnFullscreenModeChange(IWebBrowser chromiumWebBrowser, IBrowser browser, bool fullscreen)
{
}
/// <inheritdoc/>
void IDisplayHandler.OnLoadingProgressChange(IWebBrowser chromiumWebBrowser, IBrowser browser, double progress)
{
OnLoadingProgressChange(chromiumWebBrowser, browser, progress);
}
/// <summary>
/// Called when the overall page loading progress has changed
/// </summary>
/// <param name="chromiumWebBrowser">The ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
/// <param name="progress">ranges from 0.0 to 1.0.</param>
protected virtual void OnLoadingProgressChange(IWebBrowser chromiumWebBrowser, IBrowser browser, double progress)
{
}
/// <inheritdoc/>
bool IDisplayHandler.OnTooltipChanged(IWebBrowser chromiumWebBrowser, ref string text)
{
return OnTooltipChanged(chromiumWebBrowser, ref text);
}
/// <summary>
/// Called when the browser is about to display a tooltip. text contains the
/// text that will be displayed in the tooltip. You can optionally modify text
/// and then return false to allow the browser to display the tooltip.
/// When window rendering is disabled the application is responsible for
/// drawing tooltips and the return value is ignored.
/// </summary>
/// <param name="chromiumWebBrowser">The ChromiumWebBrowser control</param>
/// <param name="text">the text that will be displayed in the tooltip</param>
/// <returns>To handle the display of the tooltip yourself return true otherwise return false
/// to allow the browser to display the tooltip.</returns>
/// <remarks>Only called when using Off-screen rendering (WPF and OffScreen)</remarks>
protected virtual bool OnTooltipChanged(IWebBrowser chromiumWebBrowser, ref string text)
{
return false;
}
/// <inheritdoc/>
void IDisplayHandler.OnStatusMessage(IWebBrowser chromiumWebBrowser, StatusMessageEventArgs statusMessageArgs)
{
}
/// <summary>
/// Called when the browser receives a status message.
/// </summary>
/// <param name="chromiumWebBrowser">The <see cref="IWebBrowser"/> control this popup is related to.</param>
/// <param name="statusMessageArgs">args</param>
protected virtual void OnStatusMessage(IWebBrowser chromiumWebBrowser, StatusMessageEventArgs statusMessageArgs)
{
}
/// <inheritdoc/>
bool IDisplayHandler.OnConsoleMessage(IWebBrowser chromiumWebBrowser, ConsoleMessageEventArgs consoleMessageArgs)
{
return OnConsoleMessage(chromiumWebBrowser, consoleMessageArgs);
}
/// <summary>
/// Called to display a console message.
/// </summary>
/// <param name="chromiumWebBrowser">The ChromiumWebBrowser control</param>
/// <param name="consoleMessageArgs">args</param>
/// <returns>Return true to stop the message from being output to the console.</returns>
protected virtual bool OnConsoleMessage(IWebBrowser chromiumWebBrowser, ConsoleMessageEventArgs consoleMessageArgs)
{
return false;
}
}
}
+61
View File
@@ -0,0 +1,61 @@
// 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.
namespace CefSharp.Handler
{
/// <summary>
/// Class used to handle file downloads.
/// The methods of this class will called on the CEF UI thread.
/// </summary>
public class DownloadHandler : IDownloadHandler
{
/// <summary>
/// Called before a download begins.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">The browser instance</param>
/// <param name="downloadItem">Represents the file being downloaded.</param>
/// <param name="callback">Callback interface used to asynchronously continue a download.</param>
void IDownloadHandler.OnBeforeDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)
{
OnBeforeDownload(chromiumWebBrowser, browser, downloadItem, callback);
}
/// <summary>
/// Called before a download begins.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">The browser instance</param>
/// <param name="downloadItem">Represents the file being downloaded.</param>
/// <param name="callback">Callback interface used to asynchronously continue a download.</param>
protected virtual void OnBeforeDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)
{
}
/// <summary>
/// Called when a download's status or progress information has been updated. This may be called multiple times before and after <see cref="OnBeforeDownload"/>.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">The browser instance</param>
/// <param name="downloadItem">Represents the file being downloaded.</param>
/// <param name="callback">The callback used to Cancel/Pause/Resume the process</param>
void IDownloadHandler.OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)
{
OnDownloadUpdated(chromiumWebBrowser, browser, downloadItem, callback);
}
/// <summary>
/// Called when a download's status or progress information has been updated. This may be called multiple times before and after <see cref="OnBeforeDownload"/>.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">The browser instance</param>
/// <param name="downloadItem">Represents the file being downloaded.</param>
/// <param name="callback">The callback used to Cancel/Pause/Resume the process</param>
protected virtual void OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)
{
}
}
}
+72
View File
@@ -0,0 +1,72 @@
// 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.Collections.Generic;
using CefSharp.Enums;
namespace CefSharp.Handler
{
/// <summary>
/// Inherit from this class to handle events related to dragging.
/// The methods of this class will be called on the UI thread.
/// </summary>
public class DragHandler : IDragHandler
{
/// <summary>
/// Called when an external drag event enters the browser window.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
/// <param name="dragData">contains the drag event data</param>
/// <param name="mask">represents the type of drag operation</param>
/// <returns>Return false for default drag handling behavior or true to cancel the drag event. </returns>
bool IDragHandler.OnDragEnter(IWebBrowser chromiumWebBrowser, IBrowser browser, IDragData dragData, DragOperationsMask mask)
{
return OnDragEnter(chromiumWebBrowser, browser, dragData, mask);
}
/// <summary>
/// Called when an external drag event enters the browser window.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
/// <param name="dragData">contains the drag event data</param>
/// <param name="mask">represents the type of drag operation</param>
/// <returns>Return false for default drag handling behavior or true to cancel the drag event. </returns>
protected virtual bool OnDragEnter(IWebBrowser chromiumWebBrowser, IBrowser browser, IDragData dragData, DragOperationsMask mask)
{
return false;
}
/// <summary>
/// Called whenever draggable regions for the browser window change.
/// These can be specified using the '-webkit-app-region: drag/no-drag' CSS-property.
/// If draggable regions are never defined in a document this method will also never be called.
/// If the last draggable region is removed from a document this method will be called with an empty IList.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
/// <param name="frame">The frame</param>
/// <param name="regions">List of <see cref="DraggableRegion"/> objects or null if last region was removed.</param>
void IDragHandler.OnDraggableRegionsChanged(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IList<DraggableRegion> regions)
{
OnDraggableRegionsChanged(chromiumWebBrowser, browser, frame, regions);
}
/// <summary>
/// Called whenever draggable regions for the browser window change.
/// These can be specified using the '-webkit-app-region: drag/no-drag' CSS-property.
/// If draggable regions are never defined in a document this method will also never be called.
/// If the last draggable region is removed from a document this method will be called with an empty IList.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
/// <param name="frame">The frame</param>
/// <param name="regions">List of <see cref="DraggableRegion"/> objects or null if last region was removed.</param>
protected virtual void OnDraggableRegionsChanged(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IList<DraggableRegion> regions)
{
}
}
}
+261
View File
@@ -0,0 +1,261 @@
// 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;
namespace CefSharp.Handler
{
/// <summary>
/// Implement this interface to handle events related to browser extensions.
/// The methods of this class will be called on the CEF UI thread.
/// See <see cref="IRequestContext.LoadExtension"/> for information about extension loading.
/// </summary>
public class ExtensionHandler : IExtensionHandler
{
private bool isDisposed;
/// <summary>
/// Called if the <see cref="IRequestContext.LoadExtension"/> request fails.
/// </summary>
/// <param name="errorCode">error code</param>
void IExtensionHandler.OnExtensionLoadFailed(CefErrorCode errorCode)
{
OnExtensionLoadFailed(errorCode);
}
/// <summary>
/// Called if the <see cref="IRequestContext.LoadExtension"/> request fails.
/// </summary>
/// <param name="errorCode">error code</param>
protected virtual void OnExtensionLoadFailed(CefErrorCode errorCode)
{
}
/// <summary>
/// Called if the <see cref="IRequestContext.LoadExtension"/> request succeeds.
/// </summary>
/// <param name="extension">is the loaded extension.</param>
void IExtensionHandler.OnExtensionLoaded(IExtension extension)
{
OnExtensionLoaded(extension);
}
/// <summary>
/// Called if the <see cref="IRequestContext.LoadExtension"/> request succeeds.
/// </summary>
/// <param name="extension">is the loaded extension.</param>
protected virtual void OnExtensionLoaded(IExtension extension)
{
}
/// <summary>
/// Called after the IExtension.Unload request has completed.
/// </summary>
/// <param name="extension">is the unloaded extension</param>
void IExtensionHandler.OnExtensionUnloaded(IExtension extension)
{
OnExtensionUnloaded(extension);
}
/// <summary>
/// Called after the IExtension.Unload request has completed.
/// </summary>
/// <param name="extension">is the unloaded extension</param>
protected virtual void OnExtensionUnloaded(IExtension extension)
{
}
/// <summary>
/// Called when an extension needs a browser to host a background script specified via the "background" manifest key.
/// The browser will have no visible window and cannot be displayed. To allow creation of the browser optionally
/// modify newBrowser and settings and return false. To cancel creation of the browser
/// (and consequently cancel load of the background script) return true. Successful creation will be indicated by a call to
/// ILifeSpanHandler.OnAfterCreated, and IBrowserHost.IsBackgroundHost
/// will return true for the resulting browser. See https://developer.chrome.com/extensions/event_pages for more information
/// about extension background script usage.
/// </summary>
/// <param name="extension">is the extension that is loading the background script</param>
/// <param name="url">is an internally generated reference to an HTML page that will be used to
/// load the background script via a script src attribute</param>
/// <param name="settings">browser settings</param>
/// <returns>To cancel creation of the browser (and consequently cancel load of the background script) return true, otherwise return false.</returns>
bool IExtensionHandler.OnBeforeBackgroundBrowser(IExtension extension, string url, IBrowserSettings settings)
{
return OnBeforeBackgroundBrowser(extension, url, settings);
}
/// <summary>
/// Called when an extension needs a browser to host a background script specified via the "background" manifest key.
/// The browser will have no visible window and cannot be displayed. To allow creation of the browser optionally
/// modify newBrowser and settings and return false. To cancel creation of the browser
/// (and consequently cancel load of the background script) return true. Successful creation will be indicated by a call to
/// ILifeSpanHandler.OnAfterCreated, and IBrowserHost.IsBackgroundHost
/// will return true for the resulting browser. See https://developer.chrome.com/extensions/event_pages for more information
/// about extension background script usage.
/// </summary>
/// <param name="extension">is the extension that is loading the background script</param>
/// <param name="url">is an internally generated reference to an HTML page that will be used to
/// load the background script via a script src attribute</param>
/// <param name="settings">browser settings</param>
/// <returns>To cancel creation of the browser (and consequently cancel load of the background script) return true, otherwise return false.</returns>
protected virtual bool OnBeforeBackgroundBrowser(IExtension extension, string url, IBrowserSettings settings)
{
return false;
}
/// <summary>
/// Called when an extension API (e.g. chrome.tabs.create) requests creation of a new browser.
/// Successful creation will be indicated by a call to <see cref="ILifeSpanHandler.OnAfterCreated"/>.
/// </summary>
/// <param name="extension">the source of the API call</param>
/// <param name="browser">the source of the API call</param>
/// <param name="activeBrowser">may optionally be specified via the windowId property or
/// returned via the GetActiveBrowser() callback and provides the default for the new browser</param>
/// <param name="index">is the position value optionally specified via the index property</param>
/// <param name="url">is the URL that will be loaded in the browser</param>
/// <param name="active">is true if the new browser should be active when opened</param>
/// <param name="windowInfo">optionally modify if you are going to allow creation of the browser</param>
/// <param name="settings">optionally modify browser settings</param>
/// <returns>To cancel creation of the browser return true. To allow creation return false and optionally modify windowInfo and settings</returns>
bool IExtensionHandler.OnBeforeBrowser(IExtension extension, IBrowser browser, IBrowser activeBrowser, int index, string url, bool active, IWindowInfo windowInfo, IBrowserSettings settings)
{
return OnBeforeBrowser(extension, browser, activeBrowser, index, url, active, windowInfo, settings);
}
/// <summary>
/// Called when an extension API (e.g. chrome.tabs.create) requests creation of a new browser.
/// Successful creation will be indicated by a call to <see cref="ILifeSpanHandler.OnAfterCreated"/>.
/// </summary>
/// <param name="extension">the source of the API call</param>
/// <param name="browser">the source of the API call</param>
/// <param name="activeBrowser">may optionally be specified via the windowId property or
/// returned via the GetActiveBrowser() callback and provides the default for the new browser</param>
/// <param name="index">is the position value optionally specified via the index property</param>
/// <param name="url">is the URL that will be loaded in the browser</param>
/// <param name="active">is true if the new browser should be active when opened</param>
/// <param name="windowInfo">optionally modify if you are going to allow creation of the browser</param>
/// <param name="settings">optionally modify browser settings</param>
/// <returns>To cancel creation of the browser return true. To allow creation return false and optionally modify windowInfo and settings</returns>
protected virtual bool OnBeforeBrowser(IExtension extension, IBrowser browser, IBrowser activeBrowser, int index, string url, bool active, IWindowInfo windowInfo, IBrowserSettings settings)
{
return false;
}
/// <summary>
/// Called when no tabId is specified to an extension API call that accepts a tabId parameter (e.g. chrome.tabs.*).
/// </summary>
/// <param name="extension">extension the call originates from</param>
/// <param name="browser">browser the call originates from</param>
/// <param name="includeIncognito">Incognito browsers should not be considered unless the source extension has incognito
/// access enabled, inwhich case this will be true</param>
/// <returns>Return the browser that will be acted on by the API call or return null to act on <paramref name="browser"/>.
/// The returned browser must share the same IRequestContext as <paramref name="browser"/></returns>
IBrowser IExtensionHandler.GetActiveBrowser(IExtension extension, IBrowser browser, bool includeIncognito)
{
return GetActiveBrowser(extension, browser, includeIncognito);
}
/// <summary>
/// Called when no tabId is specified to an extension API call that accepts a tabId parameter (e.g. chrome.tabs.*).
/// </summary>
/// <param name="extension">extension the call originates from</param>
/// <param name="browser">browser the call originates from</param>
/// <param name="includeIncognito">Incognito browsers should not be considered unless the source extension has incognito
/// access enabled, inwhich case this will be true</param>
/// <returns>Return the browser that will be acted on by the API call or return null to act on <paramref name="browser"/>.
/// The returned browser must share the same IRequestContext as <paramref name="browser"/></returns>
protected virtual IBrowser GetActiveBrowser(IExtension extension, IBrowser browser, bool includeIncognito)
{
return null;
}
/// <summary>
/// Called when the tabId associated with <paramref name="targetBrowser"/> is specified to an extension API call that accepts a tabId
/// parameter (e.g. chrome.tabs.*).
/// </summary>
/// <param name="extension">extension the call originates from</param>
/// <param name="browser">browser the call originates from</param>
/// <param name="includeIncognito">Access to incognito browsers should not be allowed unless the source extension has
/// incognito access
/// enabled, in which case this will be true.</param>
/// <param name="targetBrowser"></param>
/// <returns>Return true to allow access of false to deny access.</returns>
bool IExtensionHandler.CanAccessBrowser(IExtension extension, IBrowser browser, bool includeIncognito, IBrowser targetBrowser)
{
return CanAccessBrowser(extension, browser, includeIncognito, targetBrowser);
}
/// <summary>
/// Called when the tabId associated with <paramref name="targetBrowser"/> is specified to an extension API call that accepts a tabId
/// parameter (e.g. chrome.tabs.*).
/// </summary>
/// <param name="extension">extension the call originates from</param>
/// <param name="browser">browser the call originates from</param>
/// <param name="includeIncognito">Access to incognito browsers should not be allowed unless the source extension has
/// incognito access
/// enabled, in which case this will be true.</param>
/// <param name="targetBrowser"></param>
/// <returns>Return true to allow access of false to deny access.</returns>
protected virtual bool CanAccessBrowser(IExtension extension, IBrowser browser, bool includeIncognito, IBrowser targetBrowser)
{
return false;
}
/// <summary>
/// Called to retrieve an extension resource that would normally be loaded from disk
/// (e.g. if a file parameter is specified to chrome.tabs.executeScript).
/// Localization substitutions will not be applied to resources handled via this method.
/// </summary>
/// <param name="extension">extension the call originates from</param>
/// <param name="browser">browser the call originates from</param>
/// <param name="file">is the requested relative file path.</param>
/// <param name="callback">callback used to handle custom resource requests</param>
/// <returns>To handle the resource request return true and execute <paramref name="callback"/> either synchronously or asynchronously.
/// For the default behavior which reads the resource from the extension directory on disk return false</returns>
bool IExtensionHandler.GetExtensionResource(IExtension extension, IBrowser browser, string file, IGetExtensionResourceCallback callback)
{
return GetExtensionResource(extension, browser, file, callback);
}
/// <summary>
/// Called to retrieve an extension resource that would normally be loaded from disk
/// (e.g. if a file parameter is specified to chrome.tabs.executeScript).
/// Localization substitutions will not be applied to resources handled via this method.
/// </summary>
/// <param name="extension">extension the call originates from</param>
/// <param name="browser">browser the call originates from</param>
/// <param name="file">is the requested relative file path.</param>
/// <param name="callback">callback used to handle custom resource requests</param>
/// <returns>To handle the resource request return true and execute <paramref name="callback"/> either synchronously or asynchronously.
/// For the default behavior which reads the resource from the extension directory on disk return false</returns>
protected virtual bool GetExtensionResource(IExtension extension, IBrowser browser, string file, IGetExtensionResourceCallback callback)
{
return false;
}
/// <summary>
/// IsDisposed
/// </summary>
public bool IsDisposed
{
get { return isDisposed; }
}
protected virtual void Dispose(bool disposing)
{
isDisposed = true;
}
/// <inheritdoc/>
void IDisposable.Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}
+45
View File
@@ -0,0 +1,45 @@
// 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 CefSharp.Structs;
namespace CefSharp.Handler
{
/// <summary>
/// Implement this interface to handle events related to find results.
/// The methods of this class will be called on the CEF UI thread.
/// </summary>
public class FindHandler : IFindHandler
{
/// <summary>
/// Called to report find results returned by <see cref="IBrowserHost.Find"/>
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
/// <param name="identifier">is the identifier passed to Find()</param>
/// <param name="count">is the number of matches currently identified</param>
/// <param name="selectionRect">is the location of where the match was found (in window coordinates)</param>
/// <param name="activeMatchOrdinal">is the current position in the search results</param>
/// <param name="finalUpdate">is true if this is the last find notification.</param>
void IFindHandler.OnFindResult(IWebBrowser chromiumWebBrowser, IBrowser browser, int identifier, int count, Rect selectionRect, int activeMatchOrdinal, bool finalUpdate)
{
OnFindResult(chromiumWebBrowser, browser, identifier, count, selectionRect, activeMatchOrdinal, finalUpdate);
}
/// <summary>
/// Called to report find results returned by <see cref="IBrowserHost.Find"/>
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
/// <param name="identifier">is the identifier passed to Find()</param>
/// <param name="count">is the number of matches currently identified</param>
/// <param name="selectionRect">is the location of where the match was found (in window coordinates)</param>
/// <param name="activeMatchOrdinal">is the current position in the search results</param>
/// <param name="finalUpdate">is true if this is the last find notification.</param>
protected virtual void OnFindResult(IWebBrowser chromiumWebBrowser, IBrowser browser, int identifier, int count, Rect selectionRect, int activeMatchOrdinal, bool finalUpdate)
{
}
}
}
+83
View File
@@ -0,0 +1,83 @@
// 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.
namespace CefSharp.Handler
{
/// <summary>
/// Implement this interface to handle events related to focus.
/// The methods of this class will be called on the CEF UI thread.
/// </summary>
public class FocusHandler : IFocusHandler
{
/// <summary>
/// Called when the browser component has received focus.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
void IFocusHandler.OnGotFocus(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
OnGotFocus(chromiumWebBrowser, browser);
}
/// <summary>
/// Called when the browser component has received focus.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
protected virtual void OnGotFocus(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
}
/// <summary>
/// Called when the browser component is requesting focus.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object, do not keep a reference to this object outside of this method</param>
/// <param name="source">Indicates where the focus request is originating from.</param>
/// <returns>Return false to allow the focus to be set or true to cancel setting the focus.</returns>
bool IFocusHandler.OnSetFocus(IWebBrowser chromiumWebBrowser, IBrowser browser, CefFocusSource source)
{
return OnSetFocus(chromiumWebBrowser, browser, source);
}
/// <summary>
/// Called when the browser component is requesting focus.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object, do not keep a reference to this object outside of this method</param>
/// <param name="source">Indicates where the focus request is originating from.</param>
/// <returns>Return false to allow the focus to be set or true to cancel setting the focus.</returns>
protected virtual bool OnSetFocus(IWebBrowser chromiumWebBrowser, IBrowser browser, CefFocusSource source)
{
return false;
}
/// <summary>
/// Called when the browser component is about to lose focus.
/// For instance, if focus was on the last HTML element and the user pressed the TAB key.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
/// <param name="next">Will be true if the browser is giving focus to the next component
/// and false if the browser is giving focus to the previous component.</param>
void IFocusHandler.OnTakeFocus(IWebBrowser chromiumWebBrowser, IBrowser browser, bool next)
{
OnTakeFocus(chromiumWebBrowser, browser, next);
}
/// <summary>
/// Called when the browser component is about to lose focus.
/// For instance, if focus was on the last HTML element and the user pressed the TAB key.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
/// <param name="next">Will be true if the browser is giving focus to the next component
/// and false if the browser is giving focus to the previous component.</param>
protected virtual void OnTakeFocus(IWebBrowser chromiumWebBrowser, IBrowser browser, bool next)
{
}
}
}
+125
View File
@@ -0,0 +1,125 @@
// 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.
namespace CefSharp.Handler
{
/// <summary>
/// Inherit from this class to handle events related to JavaScript dialogs.
/// The methods of this class will be called on the CEF UI thread.
/// </summary>
public class JsDialogHandler : IJsDialogHandler
{
/// <summary>
/// Called to run a JavaScript dialog.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
/// <param name="originUrl">originating url</param>
/// <param name="dialogType">Dialog Type</param>
/// <param name="messageText">Message Text</param>
/// <param name="defaultPromptText">value will be specified for prompt dialogs only</param>
/// <param name="callback">Callback can be executed inline or in an async fashion</param>
/// <param name="suppressMessage">Set suppressMessage to true and return false to suppress the message (suppressing messages is preferable to immediately executing the callback as this is used to detect presumably malicious behavior like spamming alert messages in onbeforeunload). Set suppressMessage to false and return false to use the default implementation (the default implementation will show one modal dialog at a time and suppress any additional dialog requests until the displayed dialog is dismissed).</param>
/// <returns>Return true if the application will use a custom dialog or if the callback has been executed immediately. Custom dialogs may be either modal or modeless. If a custom dialog is used the application must execute |callback| once the custom dialog is dismissed.</returns>
bool IJsDialogHandler.OnJSDialog(IWebBrowser chromiumWebBrowser, IBrowser browser, string originUrl, CefJsDialogType dialogType, string messageText, string defaultPromptText, IJsDialogCallback callback, ref bool suppressMessage)
{
return OnJSDialog(chromiumWebBrowser, browser, originUrl, dialogType, messageText, defaultPromptText, callback, ref suppressMessage);
}
/// <summary>
/// Called to run a JavaScript dialog.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
/// <param name="originUrl">originating url</param>
/// <param name="dialogType">Dialog Type</param>
/// <param name="messageText">Message Text</param>
/// <param name="defaultPromptText">value will be specified for prompt dialogs only</param>
/// <param name="callback">Callback can be executed inline or in an async fashion</param>
/// <param name="suppressMessage">Set suppressMessage to true and return false to suppress the message (suppressing messages is preferable to immediately executing the callback as this is used to detect presumably malicious behavior like spamming alert messages in onbeforeunload). Set suppressMessage to false and return false to use the default implementation (the default implementation will show one modal dialog at a time and suppress any additional dialog requests until the displayed dialog is dismissed).</param>
/// <returns>Return true if the application will use a custom dialog or if the callback has been executed immediately. Custom dialogs may be either modal or modeless. If a custom dialog is used the application must execute |callback| once the custom dialog is dismissed.</returns>
protected virtual bool OnJSDialog(IWebBrowser chromiumWebBrowser, IBrowser browser, string originUrl, CefJsDialogType dialogType, string messageText, string defaultPromptText, IJsDialogCallback callback, ref bool suppressMessage)
{
return false;
}
/// <summary>
/// Called to run a dialog asking the user if they want to leave a page. Return false to use the default dialog implementation.
/// Return true if the application will use a custom dialog or if the callback has been executed immediately.
/// Custom dialogs may be either modal or modeless. If a custom dialog is used the application must execute <paramref name="callback"/>
/// once the custom dialog is dismissed.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
/// <param name="messageText">message text (optional)</param>
/// <param name="isReload">indicates a page reload</param>
/// <param name="callback">Callback can be executed inline or in an async fashion</param>
/// <returns>Return false to use the default dialog implementation otherwise return true to handle with your own custom implementation.</returns>
bool IJsDialogHandler.OnBeforeUnloadDialog(IWebBrowser chromiumWebBrowser, IBrowser browser, string messageText, bool isReload, IJsDialogCallback callback)
{
return OnBeforeUnloadDialog(chromiumWebBrowser, browser, messageText, isReload, callback);
}
/// <summary>
/// Called to run a dialog asking the user if they want to leave a page. Return false to use the default dialog implementation.
/// Return true if the application will use a custom dialog or if the callback has been executed immediately.
/// Custom dialogs may be either modal or modeless. If a custom dialog is used the application must execute <paramref name="callback"/>
/// once the custom dialog is dismissed.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
/// <param name="messageText">message text (optional)</param>
/// <param name="isReload">indicates a page reload</param>
/// <param name="callback">Callback can be executed inline or in an async fashion</param>
/// <returns>Return false to use the default dialog implementation otherwise return true to handle with your own custom implementation.</returns>
protected virtual bool OnBeforeUnloadDialog(IWebBrowser chromiumWebBrowser, IBrowser browser, string messageText, bool isReload, IJsDialogCallback callback)
{
return false;
}
/// <summary>
/// Called to cancel any pending dialogs and reset any saved dialog state. Will
/// be called due to events like page navigation irregardless of whether any
/// dialogs are currently pending.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
void IJsDialogHandler.OnResetDialogState(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
OnResetDialogState(chromiumWebBrowser, browser);
}
/// <summary>
/// Called to cancel any pending dialogs and reset any saved dialog state. Will
/// be called due to events like page navigation irregardless of whether any
/// dialogs are currently pending.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
protected virtual void OnResetDialogState(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
}
/// <summary>
/// Called when the default implementation dialog is closed.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
void IJsDialogHandler.OnDialogClosed(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
OnDialogClosed(chromiumWebBrowser, browser);
}
/// <summary>
/// Called when the default implementation dialog is closed.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
protected virtual void OnDialogClosed(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
}
}
}
+118
View File
@@ -0,0 +1,118 @@
// 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.
namespace CefSharp.Handler
{
/// <summary>
/// Inherit from this class to handle events related to keyboard input.
/// </summary>
public class KeyboardHandler : IKeyboardHandler
{
/// <summary>
/// Called before a keyboard event is sent to the renderer.
/// Return true if the event was handled or false
/// otherwise. If the event will be handled in <see cref="OnKeyEvent"/> as a keyboard
/// shortcut set isKeyboardShortcut to true and return false.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">The browser instance.</param>
/// <param name="type">Whether this was a key up/down/raw/etc...</param>
/// <param name="windowsKeyCode">
/// The Windows key code for the key event. This value is used by the DOM
/// specification. Sometimes it comes directly from the event (i.e. on
/// Windows) and sometimes it's determined using a mapping function. See
/// WebCore/platform/chromium/KeyboardCodes.h for the list of values.
/// </param>
/// <param name="nativeKeyCode">The native key code. On Windows this appears to be in the format of WM_KEYDOWN/WM_KEYUP/etc... lParam data.</param>
/// <param name="modifiers">What other modifier keys are currently down: Shift/Control/Alt/OS X Command/etc...</param>
/// <param name="isSystemKey">
/// Indicates whether the event is considered a "system key" event (see
/// http://msdn.microsoft.com/en-us/library/ms646286(VS.85).aspx for details).
/// </param>
/// <param name="isKeyboardShortcut">See the summary for an explanation of when to set this to true.</param>
/// <returns>Returns true if the event was handled or false otherwise.</returns>
bool IKeyboardHandler.OnPreKeyEvent(IWebBrowser chromiumWebBrowser, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey, ref bool isKeyboardShortcut)
{
return OnPreKeyEvent(chromiumWebBrowser, browser, type, windowsKeyCode, nativeKeyCode, modifiers, isSystemKey, ref isKeyboardShortcut);
}
/// <summary>
/// Called before a keyboard event is sent to the renderer.
/// Return true if the event was handled or false
/// otherwise. If the event will be handled in <see cref="OnKeyEvent"/> as a keyboard
/// shortcut set isKeyboardShortcut to true and return false.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">The browser instance.</param>
/// <param name="type">Whether this was a key up/down/raw/etc...</param>
/// <param name="windowsKeyCode">
/// The Windows key code for the key event. This value is used by the DOM
/// specification. Sometimes it comes directly from the event (i.e. on
/// Windows) and sometimes it's determined using a mapping function. See
/// WebCore/platform/chromium/KeyboardCodes.h for the list of values.
/// </param>
/// <param name="nativeKeyCode">The native key code. On Windows this appears to be in the format of WM_KEYDOWN/WM_KEYUP/etc... lParam data.</param>
/// <param name="modifiers">What other modifier keys are currently down: Shift/Control/Alt/OS X Command/etc...</param>
/// <param name="isSystemKey">
/// Indicates whether the event is considered a "system key" event (see
/// http://msdn.microsoft.com/en-us/library/ms646286(VS.85).aspx for details).
/// </param>
/// <param name="isKeyboardShortcut">See the summary for an explanation of when to set this to true.</param>
/// <returns>Returns true if the event was handled or false otherwise.</returns>
protected virtual bool OnPreKeyEvent(IWebBrowser chromiumWebBrowser, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey, ref bool isKeyboardShortcut)
{
return false;
}
/// <summary>
/// Called after the renderer and JavaScript in the page has had a chance to
/// handle the event. Return true if the keyboard event was handled or false otherwise.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">The browser instance.</param>
/// <param name="type">Whether this was a key up/down/raw/etc...</param>
/// <param name="windowsKeyCode">
/// The Windows key code for the key event. This value is used by the DOM
/// specification. Sometimes it comes directly from the event (i.e. on
/// Windows) and sometimes it's determined using a mapping function. See
/// WebCore/platform/chromium/KeyboardCodes.h for the list of values.
/// </param>
/// <param name="nativeKeyCode">The native key code. On Windows this appears to be in the format of WM_KEYDOWN/WM_KEYUP/etc... lParam data.</param>
/// <param name="modifiers">What other modifier keys are currently down: Shift/Control/Alt/OS X Command/etc...</param>
/// <param name="isSystemKey">
/// Indicates whether the event is considered a "system key" event (see
/// http://msdn.microsoft.com/en-us/library/ms646286(VS.85).aspx for details).
/// </param>
/// <returns>Return true if the keyboard event was handled or false otherwise.</returns>
bool IKeyboardHandler.OnKeyEvent(IWebBrowser chromiumWebBrowser, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey)
{
return OnKeyEvent(chromiumWebBrowser, browser, type, windowsKeyCode, nativeKeyCode, modifiers, isSystemKey);
}
/// <summary>
/// Called after the renderer and JavaScript in the page has had a chance to
/// handle the event. Return true if the keyboard event was handled or false otherwise.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">The browser instance.</param>
/// <param name="type">Whether this was a key up/down/raw/etc...</param>
/// <param name="windowsKeyCode">
/// The Windows key code for the key event. This value is used by the DOM
/// specification. Sometimes it comes directly from the event (i.e. on
/// Windows) and sometimes it's determined using a mapping function. See
/// WebCore/platform/chromium/KeyboardCodes.h for the list of values.
/// </param>
/// <param name="nativeKeyCode">The native key code. On Windows this appears to be in the format of WM_KEYDOWN/WM_KEYUP/etc... lParam data.</param>
/// <param name="modifiers">What other modifier keys are currently down: Shift/Control/Alt/OS X Command/etc...</param>
/// <param name="isSystemKey">
/// Indicates whether the event is considered a "system key" event (see
/// http://msdn.microsoft.com/en-us/library/ms646286(VS.85).aspx for details).
/// </param>
/// <returns>Return true if the keyboard event was handled or false otherwise.</returns>
protected virtual bool OnKeyEvent(IWebBrowser chromiumWebBrowser, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey)
{
return false;
}
}
}
+180
View File
@@ -0,0 +1,180 @@
// 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.
namespace CefSharp.Handler
{
/// <summary>
/// Default implementation of <see cref="ILifeSpanHandler"/>. This class provides default implementations of the methods from
/// <see cref="ILifeSpanHandler"/>, therefore providing a convenience base class for a custom implementation.
/// You need only override the methods you require.
/// IMPORTANT: <see cref="ILifeSpanHandler.DoClose(IWebBrowser, IBrowser)"/> behaviour of this implementation differs
/// from the default, the WM_CLOSE message is only sent by default for popups (return false), for the main browser
/// we return true to cancel this behaviour.
/// </summary>
/// <seealso cref="T:CefSharp.ILifeSpanHandler"/>
public class LifeSpanHandler : ILifeSpanHandler
{
/// <inheritdoc/>
bool ILifeSpanHandler.DoClose(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
return DoClose(chromiumWebBrowser, browser);
}
/// <summary>
/// Called when a browser has recieved a request to close. This may result
/// directly from a call to CefBrowserHost::CloseBrowser() or indirectly if the
/// browser is a top-level OS window created by CEF and the user attempts to
/// close the window. This method will be called after the JavaScript
/// 'onunload' event has been fired. It will not be called for browsers after
/// the associated OS window has been destroyed (for those browsers it is no
/// longer possible to cancel the close).
///
/// If CEF created an OS window for the browser returning false will send an OS
/// close notification to the browser window's top-level owner (e.g. WM_CLOSE
/// on Windows). If no OS window exists (window rendering disabled) returning false
/// will cause the browser object to be destroyed immediately. Return true if
/// the browser is parented to another window and that other window needs to
/// receive close notification via some non-standard technique.
///
/// !!IMPORTANT!!: Behaviour of this implementation differs from the default,
/// the WM_CLOSE message is only sent by default for popups (return false),
/// for the main browser we return true to cancel this behaviour.
///
/// If an application provides its own top-level window it should handle OS
/// close notifications by calling CefBrowserHost::CloseBrowser(false) instead
/// of immediately closing (see the example below). This gives CEF an
/// opportunity to process the 'onbeforeunload' event and optionally cancel the
/// close before DoClose() is called.
///
/// The CefLifeSpanHandler::OnBeforeClose() method will be called immediately
/// before the browser object is destroyed. The application should only exit
/// after OnBeforeClose() has been called for all existing browsers.
///
/// If the browser represents a modal window and a custom modal loop
/// implementation was provided in CefLifeSpanHandler::RunModal() this callback
/// should be used to restore the opener window to a usable state.
///
/// By way of example consider what should happen during window close when the
/// browser is parented to an application-provided top-level OS window.
/// 1. User clicks the window close button which sends an OS close
/// notification (e.g. WM_CLOSE on Windows, performClose: on OS-X and
/// "delete_event" on Linux).
/// 2. Application's top-level window receives the close notification and:
/// A. Calls CefBrowserHost::CloseBrowser(false).
/// B. Cancels the window close.
/// 3. JavaScript 'onbeforeunload' handler executes and shows the close
/// confirmation dialog (which can be overridden via
/// CefJSDialogHandler::OnBeforeUnloadDialog()).
/// 4. User approves the close.
/// 5. JavaScript 'onunload' handler executes.
/// 6. Application's DoClose() handler is called. Application will:
/// A. Set a flag to indicate that the next close attempt will be allowed.
/// B. Return false.
/// 7. CEF sends an OS close notification.
/// 8. Application's top-level window receives the OS close notification and
/// allows the window to close based on the flag from #6B.
/// 9. Browser OS window is destroyed.
/// 10. Application's CefLifeSpanHandler::OnBeforeClose() handler is called and
/// the browser object is destroyed.
/// 11. Application exits by calling CefQuitMessageLoop() if no other browsers
/// exist.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">The browser instance - check if IsDisposed as it's possible when the browser is disposing</param>
/// <returns>For default behaviour return false</returns>
protected virtual bool DoClose(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
//For popups by default are created by CEF and need to be closed via the default behaviour
//(Sending a WM_CLOSE message)
if (browser.IsPopup)
{
return false;
}
//For the main browser NO WM_CLOSE message will be sent.
return true;
}
/// <inheritdoc/>
void ILifeSpanHandler.OnAfterCreated(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
OnAfterCreated(chromiumWebBrowser, browser);
}
/// <summary>
/// Called after a new browser is created.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">The browser instance</param>
protected virtual void OnAfterCreated(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
}
/// <inheritdoc/>
void ILifeSpanHandler.OnBeforeClose(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
OnBeforeClose(chromiumWebBrowser, browser);
}
/// <summary>
/// Called before a CefBrowser window (either the main browser for <see cref="IWebBrowser"/>,
/// or one of its children)
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">The browser instance</param>
protected virtual void OnBeforeClose(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
}
/// <inheritdoc/>
bool ILifeSpanHandler.OnBeforePopup(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser)
{
return OnBeforePopup(chromiumWebBrowser, browser, frame, targetUrl, targetFrameName, targetDisposition, userGesture, popupFeatures, windowInfo, browserSettings, ref noJavascriptAccess, out newBrowser);
}
/// <summary>
/// Called before a popup window is created.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">The browser instance that launched this popup.</param>
/// <param name="frame">The HTML frame that launched this popup.</param>
/// <param name="targetUrl">The URL of the popup content. (This may be empty/null)</param>
/// <param name="targetFrameName">The name of the popup. (This may be empty/null)</param>
/// <param name="targetDisposition">The value indicates where the user intended to
/// open the popup (e.g. current tab, new tab, etc)</param>
/// <param name="userGesture">The value will be true if the popup was opened via explicit user gesture
/// (e.g. clicking a link) or false if the popup opened automatically (e.g. via the DomContentLoaded event).</param>
/// <param name="popupFeatures"> structure contains additional information about the requested popup window</param>
/// <param name="windowInfo">window information</param>
/// <param name="browserSettings">browser settings, defaults to source browsers</param>
/// <param name="noJavascriptAccess">value indicates whether the new browser window should be scriptable
/// and in the same process as the source browser.</param>
/// <param name="newBrowser">EXPERIMENTAL - A newly created browser that will host the popup. Set to null
/// for default behaviour.</param>
/// <returns>To cancel creation of the popup window return true otherwise return false.</returns>
/// <remarks>
/// CEF documentation:
///
/// Called on the IO thread before a new popup window is created. The |browser|
/// and |frame| parameters represent the source of the popup request. The
/// |target_url| and |target_frame_name| values may be empty if none were
/// specified with the request. The |popupFeatures| structure contains
/// information about the requested popup window. To allow creation of the
/// popup window optionally modify |windowInfo|, |client|, |settings| and
/// |no_javascript_access| and return false. To cancel creation of the popup
/// window return true. The |client| and |settings| values will default to the
/// source browser's values. The |no_javascript_access| value indicates whether
/// the new browser window should be scriptable and in the same process as the
/// source browser.
/// </remarks>
protected virtual bool OnBeforePopup(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser)
{
newBrowser = null;
return false;
}
}
}
+144
View File
@@ -0,0 +1,144 @@
// 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.
namespace CefSharp.Handler
{
/// <summary>
/// Implement this interface to handle events related to browser load status.
/// The methods of this interface will be called on the CEF UI thread. Blocking in these methods
/// will likely cause your UI to become unresponsive and/or hang.
/// </summary>
public class LoadHandler : ILoadHandler
{
/// <summary>
/// Called when the loading state has changed. This callback will be executed twice
/// once when loading is initiated either programmatically or by user action,
/// and once when loading is terminated due to completion, cancellation of failure.
/// This method will be called on the CEF UI thread.
/// Blocking this thread will likely cause your UI to become unresponsive and/or hang.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="loadingStateChangedArgs">args</param>
void ILoadHandler.OnLoadingStateChange(IWebBrowser chromiumWebBrowser, LoadingStateChangedEventArgs loadingStateChangedArgs)
{
OnLoadingStateChange(chromiumWebBrowser, loadingStateChangedArgs);
}
/// <summary>
/// Called when the loading state has changed. This callback will be executed twice
/// once when loading is initiated either programmatically or by user action,
/// and once when loading is terminated due to completion, cancellation of failure.
/// This method will be called on the CEF UI thread.
/// Blocking this thread will likely cause your UI to become unresponsive and/or hang.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="loadingStateChangedArgs">args</param>
protected virtual void OnLoadingStateChange(IWebBrowser chromiumWebBrowser, LoadingStateChangedEventArgs loadingStateChangedArgs)
{
}
/// <summary>
/// Called when the browser begins loading a frame.
/// The <see cref="FrameLoadEndEventArgs.Frame"/> value will never be empty
/// Check the <see cref="IFrame.IsMain"/> method to see if this frame is the main 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 <see cref="OnLoadingStateChange"/> instead.
/// This method will be called on the CEF UI thread.
/// Blocking this thread will likely cause your UI to become unresponsive and/or hang.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="frameLoadStartArgs">args</param>
/// <remarks>Whilst thist may seem like a logical place to execute js, it's called before the DOM has been loaded, implement
/// <see cref="IRenderProcessMessageHandler.OnContextCreated"/> as it's called when the underlying V8Context is created
/// (Only called for the main frame at this stage)</remarks>
void ILoadHandler.OnFrameLoadStart(IWebBrowser chromiumWebBrowser, FrameLoadStartEventArgs frameLoadStartArgs)
{
OnFrameLoadStart(chromiumWebBrowser, frameLoadStartArgs);
}
/// <summary>
/// Called when the browser begins loading a frame.
/// The <see cref="FrameLoadEndEventArgs.Frame"/> value will never be empty
/// Check the <see cref="IFrame.IsMain"/> method to see if this frame is the main 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 <see cref="OnLoadingStateChange"/> instead.
/// This method will be called on the CEF UI thread.
/// Blocking this thread will likely cause your UI to become unresponsive and/or hang.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="frameLoadStartArgs">args</param>
/// <remarks>Whilst thist may seem like a logical place to execute js, it's called before the DOM has been loaded, implement
/// <see cref="IRenderProcessMessageHandler.OnContextCreated"/> as it's called when the underlying V8Context is created
/// (Only called for the main frame at this stage)</remarks>
protected virtual void OnFrameLoadStart(IWebBrowser chromiumWebBrowser, FrameLoadStartEventArgs frameLoadStartArgs)
{
}
/// <summary>
/// Called when the browser is done loading a frame.
/// The <see cref="FrameLoadEndEventArgs.Frame"/> value will never be empty
/// Check the <see cref="IFrame.IsMain"/> method to see if this frame is the main 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.
/// This method will be called on the CEF UI thread.
/// Blocking this thread will likely cause your UI to become unresponsive and/or hang.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="frameLoadEndArgs">args</param>
void ILoadHandler.OnFrameLoadEnd(IWebBrowser chromiumWebBrowser, FrameLoadEndEventArgs frameLoadEndArgs)
{
OnFrameLoadEnd(chromiumWebBrowser, frameLoadEndArgs);
}
/// <summary>
/// Called when the browser is done loading a frame.
/// The <see cref="FrameLoadEndEventArgs.Frame"/> value will never be empty
/// Check the <see cref="IFrame.IsMain"/> method to see if this frame is the main 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.
/// This method will be called on the CEF UI thread.
/// Blocking this thread will likely cause your UI to become unresponsive and/or hang.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="frameLoadEndArgs">args</param>
protected virtual void OnFrameLoadEnd(IWebBrowser chromiumWebBrowser, FrameLoadEndEventArgs frameLoadEndArgs)
{
}
/// <summary>
/// Called when the resource load for a navigation fails or is canceled.
/// <see cref="LoadErrorEventArgs.ErrorCode"/> is the error code number, <see cref="LoadErrorEventArgs.ErrorText"/> is the error text and
/// <see cref="LoadErrorEventArgs.FailedUrl"/> is the URL that failed to load. See net\base\net_error_list.h
/// for complete descriptions of the error codes.
/// This method will be called on the CEF UI thread.
/// Blocking this thread will likely cause your UI to become unresponsive and/or hang.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="loadErrorArgs">args</param>
void ILoadHandler.OnLoadError(IWebBrowser chromiumWebBrowser, LoadErrorEventArgs loadErrorArgs)
{
OnLoadError(chromiumWebBrowser, loadErrorArgs);
}
/// <summary>
/// Called when the resource load for a navigation fails or is canceled.
/// <see cref="LoadErrorEventArgs.ErrorCode"/> is the error code number, <see cref="LoadErrorEventArgs.ErrorText"/> is the error text and
/// <see cref="LoadErrorEventArgs.FailedUrl"/> is the URL that failed to load. See net\base\net_error_list.h
/// for complete descriptions of the error codes.
/// This method will be called on the CEF UI thread.
/// Blocking this thread will likely cause your UI to become unresponsive and/or hang.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="loadErrorArgs">args</param>
protected virtual void OnLoadError(IWebBrowser chromiumWebBrowser, LoadErrorEventArgs loadErrorArgs)
{
}
}
}