Skip to content

Commit 640672d

Browse files
committed
Core - Add IChromiumWebBrowserBase.GetRequestContext extension method
Simplifies getting the RequestContext for a ChromiumWebBrowser instance
1 parent 8a6241c commit 640672d

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

CefSharp.WinForms.Example/BrowserForm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ private void LoadExtensionsToolStripMenuItemClick(object sender, EventArgs e)
570570
//The sample extension only works for http(s) schemes
571571
if (control.Browser.GetMainFrame().Url.StartsWith("http"))
572572
{
573-
var requestContext = control.Browser.GetBrowserHost().RequestContext;
573+
var requestContext = control.Browser.GetRequestContext();
574574

575575
const string cefSharpExampleResourcesFolder =
576576
#if !NETCOREAPP

CefSharp.WinForms.Example/BrowserTabUserControl.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,7 @@ private void SetIsLoading(bool isLoading)
309309

310310
private void OnIsBrowserInitializedChanged(object sender, EventArgs e)
311311
{
312-
//Get the underlying browser host wrapper
313-
var browserHost = Browser.BrowserCore.GetHost();
314-
var requestContext = browserHost.RequestContext;
312+
var requestContext = Browser.GetRequestContext();
315313
string errorMessage;
316314
// Browser must be initialized before getting/setting preferences
317315
var success = requestContext.SetPreference("enable_do_not_track", true, out errorMessage);

CefSharp/WebBrowserExtensions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,23 @@ public static ICookieManager GetCookieManager(this IChromiumWebBrowserBase brows
893893
return requestContext.GetCookieManager(callback);
894894
}
895895

896+
/// <summary>
897+
/// Gets the RequestContext associated with the <see cref="IChromiumWebBrowserBase"/> instance.
898+
/// </summary>
899+
/// <exception cref="Exception">Thrown when an exception error condition occurs.</exception>
900+
/// <param name="browser">The ChromiumWebBrowser instance this method extends.</param>
901+
/// <returns>
902+
/// RequestContext
903+
/// </returns>
904+
public static IRequestContext GetRequestContext(this IChromiumWebBrowserBase browser)
905+
{
906+
var host = browser.GetBrowserHost();
907+
908+
ThrowExceptionIfBrowserHostNull(host);
909+
910+
return host.RequestContext;
911+
}
912+
896913
/// <summary>
897914
/// Asynchronously gets the current Zoom Level.
898915
/// </summary>

0 commit comments

Comments
 (0)