Skip to content

Commit 3ccb0f8

Browse files
committed
Core - Reduce ChromiumWebBrowser Code Duplication
- Move events to partial class (where possible) - Move other explicit interface implementation methods Issue #3082
1 parent 6d62d18 commit 3ccb0f8

File tree

4 files changed

+159
-492
lines changed

4 files changed

+159
-492
lines changed

CefSharp.OffScreen/ChromiumWebBrowser.cs

Lines changed: 3 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,6 @@ public partial class ChromiumWebBrowser : IRenderWebBrowser
5151
/// </summary>
5252
private int disposeSignaled;
5353

54-
/// <summary>
55-
/// Used as workaround for issue https://github.com/cefsharp/CefSharp/issues/3021
56-
/// </summary>
57-
private long canExecuteJavascriptInMainFrameId;
58-
5954
/// <summary>
6055
/// Gets a value indicating whether this instance is disposed.
6156
/// </summary>
@@ -123,43 +118,6 @@ public bool IsDisposed
123118
/// <value>The accessibility handler.</value>
124119
public IAccessibilityHandler AccessibilityHandler { get; set; }
125120
/// <summary>
126-
/// Event handler that will get called when the resource load for a navigation fails or is canceled.
127-
/// 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
128-
/// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang..
129-
/// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread.
130-
/// </summary>
131-
public event EventHandler<LoadErrorEventArgs> LoadError;
132-
/// <summary>
133-
/// Event handler that will get called when the browser begins loading a frame. Multiple frames may be loading at the same
134-
/// time. Sub-frames may start or continue loading after the main frame load has ended. This method may not be called for a
135-
/// particular frame if the load request for that frame fails. For notification of overall browser load status use
136-
/// OnLoadingStateChange instead.
137-
/// 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
138-
/// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang..
139-
/// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread.
140-
/// </summary>
141-
/// <remarks>Whilst this may seem like a logical place to execute js, it's called before the DOM has been loaded, implement
142-
/// <see cref="IRenderProcessMessageHandler.OnContextCreated" /> as it's called when the underlying V8Context is created
143-
/// </remarks>
144-
public event EventHandler<FrameLoadStartEventArgs> FrameLoadStart;
145-
/// <summary>
146-
/// Event handler that will get called when the browser is done loading a frame. Multiple frames may be loading at the same
147-
/// time. Sub-frames may start or continue loading after the main frame load has ended. This method will always be called
148-
/// for all frames irrespective of whether the request completes successfully.
149-
/// 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
150-
/// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang..
151-
/// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread.
152-
/// </summary>
153-
public event EventHandler<FrameLoadEndEventArgs> FrameLoadEnd;
154-
/// <summary>
155-
/// Event handler for receiving Javascript console messages being sent from web pages.
156-
/// 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
157-
/// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang..
158-
/// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread.
159-
/// (The exception to this is when your running with settings.MultiThreadedMessageLoop = false, then they'll be the same thread).
160-
/// </summary>
161-
public event EventHandler<ConsoleMessageEventArgs> ConsoleMessage;
162-
/// <summary>
163121
/// Event called after the underlying CEF browser instance has been created.
164122
/// 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
165123
/// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang..
@@ -168,24 +126,7 @@ public bool IsDisposed
168126
/// </summary>
169127
public event EventHandler BrowserInitialized;
170128
/// <summary>
171-
/// Event handler for changes to the status message.
172-
/// 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
173-
/// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang.
174-
/// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread.
175-
/// (The exception to this is when your running with settings.MultiThreadedMessageLoop = false, then they'll be the same thread).
176-
/// </summary>
177-
public event EventHandler<StatusMessageEventArgs> StatusMessage;
178-
/// <summary>
179-
/// Event handler that will get called when the Loading state has changed.
180-
/// This event will be fired twice. Once when loading is initiated either programmatically or
181-
/// by user action, and once when loading is terminated due to completion, cancellation of failure.
182-
/// 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
183-
/// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang..
184-
/// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread.
185-
/// </summary>
186-
public event EventHandler<LoadingStateChangedEventArgs> LoadingStateChanged;
187-
/// <summary>
188-
/// Occurs when [address changed].
129+
/// Occurs when the browser address changed.
189130
/// 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
190131
/// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang..
191132
/// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread.
@@ -216,19 +157,7 @@ public bool IsDisposed
216157
private event EventHandler<OnPaintEventArgs> AfterPaint;
217158

218159
/// <summary>
219-
/// Event handler that will get called when the message that originates from CefSharp.PostMessage
220-
/// </summary>
221-
public event EventHandler<JavascriptMessageReceivedEventArgs> JavascriptMessageReceived;
222-
223-
/// <summary>
224-
/// A flag that indicates if you can execute javascript in the main frame.
225-
/// Flag is set to true in IRenderProcessMessageHandler.OnContextCreated.
226-
/// and false in IRenderProcessMessageHandler.OnContextReleased
227-
/// </summary>
228-
public bool CanExecuteJavascriptInMainFrame { get; private set; }
229-
230-
/// <summary>
231-
/// Create a new OffScreen Chromium Browser. If you use <see cref="JavascriptBindingSettings.LegacyBindingEnabled"/> = true then you must
160+
/// Create a new OffScreen Chromium Browser. If you use <see cref="CefSharp.JavascriptBinding.JavascriptBindingSettings.LegacyBindingEnabled"/> = true then you must
232161
/// set <paramref name="automaticallyCreateBrowser"/> to false and call <see cref="CreateBrowser"/> after the objects are registered.
233162
/// </summary>
234163
/// <param name="html">html string to be initially loaded in the browser.</param>
@@ -242,7 +171,7 @@ public ChromiumWebBrowser(HtmlString html, BrowserSettings browserSettings = nul
242171
}
243172

244173
/// <summary>
245-
/// Create a new OffScreen Chromium Browser. If you use <see cref="JavascriptBindingSettings.LegacyBindingEnabled"/> = true then you must
174+
/// Create a new OffScreen Chromium Browser. If you use <see cref="CefSharp.JavascriptBinding.JavascriptBindingSettings.LegacyBindingEnabled"/> = true then you must
246175
/// set <paramref name="automaticallyCreateBrowser"/> to false and call <see cref="CreateBrowser"/> after the objects are registered.
247176
/// </summary>
248177
/// <param name="address">Initial address (url) to load</param>
@@ -725,33 +654,6 @@ void IRenderWebBrowser.OnVirtualKeyboardRequested(IBrowser browser, TextInputMod
725654
RenderHandler?.OnVirtualKeyboardRequested(browser, inputMode);
726655
}
727656

728-
/// <summary>
729-
/// Handles the <see cref="E:ConsoleMessage" /> event.
730-
/// </summary>
731-
/// <param name="args">The <see cref="ConsoleMessageEventArgs"/> instance containing the event data.</param>
732-
void IWebBrowserInternal.OnConsoleMessage(ConsoleMessageEventArgs args)
733-
{
734-
ConsoleMessage?.Invoke(this, args);
735-
}
736-
737-
/// <summary>
738-
/// Handles the <see cref="E:FrameLoadStart" /> event.
739-
/// </summary>
740-
/// <param name="args">The <see cref="FrameLoadStartEventArgs"/> instance containing the event data.</param>
741-
void IWebBrowserInternal.OnFrameLoadStart(FrameLoadStartEventArgs args)
742-
{
743-
FrameLoadStart?.Invoke(this, args);
744-
}
745-
746-
/// <summary>
747-
/// Handles the <see cref="E:FrameLoadEnd" /> event.
748-
/// </summary>
749-
/// <param name="args">The <see cref="FrameLoadEndEventArgs"/> instance containing the event data.</param>
750-
void IWebBrowserInternal.OnFrameLoadEnd(FrameLoadEndEventArgs args)
751-
{
752-
FrameLoadEnd?.Invoke(this, args);
753-
}
754-
755657
/// <summary>
756658
/// Called when [after browser created].
757659
/// </summary>
@@ -765,39 +667,6 @@ void IWebBrowserInternal.OnAfterBrowserCreated(IBrowser browser)
765667
BrowserInitialized?.Invoke(this, EventArgs.Empty);
766668
}
767669

768-
/// <summary>
769-
/// Handles the <see cref="E:LoadError" /> event.
770-
/// </summary>
771-
/// <param name="args">The <see cref="LoadErrorEventArgs"/> instance containing the event data.</param>
772-
void IWebBrowserInternal.OnLoadError(LoadErrorEventArgs args)
773-
{
774-
LoadError?.Invoke(this, args);
775-
}
776-
777-
/// <summary>
778-
/// Gets the browser adapter.
779-
/// </summary>
780-
/// <value>The browser adapter.</value>
781-
IBrowserAdapter IWebBrowserInternal.BrowserAdapter
782-
{
783-
get { return managedCefBrowserAdapter; }
784-
}
785-
786-
/// <summary>
787-
/// Gets or sets a value indicating whether this instance has parent.
788-
/// </summary>
789-
/// <value><c>true</c> if this instance has parent; otherwise, <c>false</c>.</value>
790-
bool IWebBrowserInternal.HasParent { get; set; }
791-
792-
/// <summary>
793-
/// Handles the <see cref="E:StatusMessage" /> event.
794-
/// </summary>
795-
/// <param name="args">The <see cref="StatusMessageEventArgs"/> instance containing the event data.</param>
796-
void IWebBrowserInternal.OnStatusMessage(StatusMessageEventArgs args)
797-
{
798-
StatusMessage?.Invoke(this, args);
799-
}
800-
801670
/// <summary>
802671
/// Sets the address.
803672
/// </summary>
@@ -840,29 +709,6 @@ void IWebBrowserInternal.SetTooltipText(string tooltipText)
840709
TooltipText = tooltipText;
841710
}
842711

843-
void IWebBrowserInternal.SetCanExecuteJavascriptOnMainFrame(long frameId, bool canExecute)
844-
{
845-
//When loading pages of a different origin the frameId changes
846-
//For the first loading of a new origin the messages from the render process
847-
//Arrive in a different order than expected, the OnContextCreated message
848-
//arrives before the OnContextReleased, then the message for OnContextReleased
849-
//incorrectly overrides the value
850-
//https://github.com/cefsharp/CefSharp/issues/3021
851-
852-
if (frameId > canExecuteJavascriptInMainFrameId && !canExecute)
853-
{
854-
return;
855-
}
856-
857-
canExecuteJavascriptInMainFrameId = frameId;
858-
CanExecuteJavascriptInMainFrame = canExecute;
859-
}
860-
861-
void IWebBrowserInternal.SetJavascriptMessageReceived(JavascriptMessageReceivedEventArgs args)
862-
{
863-
JavascriptMessageReceived?.Invoke(this, args);
864-
}
865-
866712
/// <summary>
867713
/// Creates a new bitmap with the dimensions of firstBitmap, then
868714
/// draws the firstBitmap, then overlays the secondBitmap

0 commit comments

Comments
 (0)