// Copyright © 2020 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
using System.Collections.Generic;
namespace CefSharp.Handler
{
///
/// Implement this interface to provide handler implementations. The handler
/// instance will not be released until all objects related to the context have
/// been destroyed. Implement this interface to cancel loading of specific plugins
///
public class RequestContextHandler : IRequestContextHandler
{
private readonly IList> preferences = new List>();
private bool requestContextInitialized = false;
private Action onContextInitialziedAction;
///
/// The is executed when the RequestContext has been initialized, after the
/// preferences/proxy preferences have been set, before OnRequestContextInitialized.
///
/// action to perform on context initialize
/// A instance allowing you to chain multiple AddPreference calls together
/// Only a single action reference is maintained, multiple calls will result in the
/// previous action reference being overriden.
public RequestContextHandler OnInitialize(Action onContextInitialziedAction)
{
this.onContextInitialziedAction = onContextInitialziedAction;
return this;
}
///
/// Sets the preferences when the
/// method is called. If is null the preference will be restored
/// to its default value. Preferences set via the command-line usually cannot be modified.
///
/// preference name
/// preference value
/// A instance allowing you to chain multiple AddPreference calls together
public RequestContextHandler SetPreferenceOnContextInitialized(string name, object value)
{
if (requestContextInitialized)
{
throw new System.Exception("RequestContext has already been initialized, ");
}
preferences.Add(new KeyValuePair(name, value));
return this;
}
///
/// Sets the proxy preferences when the
/// method is called. Proxy set via the command-line usually cannot be modified.
///
/// proxy host
/// proxy port
/// A instance allowing you to chain multiple AddPreference calls together
public RequestContextHandler SetProxyOnContextInitialized(string host, int? port = null)
{
return SetProxyOnContextInitialized(null, host, port);
}
///
/// Sets the proxy preferences when the
/// method is called. Proxy set via the command-line usually cannot be modified.
///
/// is the protocol of the proxy server, and is one of: 'http', 'socks', 'socks4', 'socks5'. Also note that 'socks' is equivalent to 'socks5'.
/// proxy host
/// proxy port
/// A instance allowing you to chain multiple AddPreference calls together
public RequestContextHandler SetProxyOnContextInitialized(string scheme, string host, int? port)
{
if (requestContextInitialized)
{
throw new System.Exception("RequestContext has already been initialized, ");
}
var value = RequestContextExtensions.GetProxyDictionary(scheme, host, port);
preferences.Add(new KeyValuePair("proxy", value));
return this;
}
IResourceRequestHandler IRequestContextHandler.GetResourceRequestHandler(IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)
{
return GetResourceRequestHandler(browser, frame, request, isNavigation, isDownload, requestInitiator, ref disableDefaultHandling);
}
///
/// Called on the CEF IO thread before a resource request is initiated.
/// This method will not be called if the client associated with returns a non-NULL value
/// from for the same request (identified by ).
///
/// represent the source browser of the request, and may be null for requests originating from service workers.
/// represent the source frame of the request, and may be null for requests originating from service workers.
/// represents the request contents and cannot be modified in this callback
/// will be true if the resource request is a navigation
/// will be true if the resource request is a download
/// is the origin (scheme + domain) of the page that initiated the request
/// Set to true to disable default handling of the request, in which case it will need to be handled via or it will be canceled
/// To allow the resource load to proceed with default handling return null. To specify a handler for the resource return a object.
protected virtual IResourceRequestHandler GetResourceRequestHandler(IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)
{
return null;
}
bool IRequestContextHandler.OnBeforePluginLoad(string mimeType, string url, bool isMainFrame, string topOriginUrl, WebPluginInfo pluginInfo, ref PluginPolicy pluginPolicy)
{
return OnBeforePluginLoad(mimeType, url, isMainFrame, topOriginUrl, pluginInfo, ref pluginPolicy);
}
///
/// Called on the CEF IO thread before a plugin instance is loaded.
/// The default plugin policy can be set at runtime using the `--plugin-policy=[allow|detect|block]` command-line flag.
///
/// is the mime type of the plugin that will be loaded
/// is the content URL that the plugin will load and may be empty
/// will be true if the plugin is being loaded in the main (top-level) frame
/// is the URL for the top-level frame that contains the plugin
/// includes additional information about the plugin that will be loaded
/// Modify and return true to change the policy.
/// Return false to use the recommended policy. Modify and return true to change the policy.
protected virtual bool OnBeforePluginLoad(string mimeType, string url, bool isMainFrame, string topOriginUrl, WebPluginInfo pluginInfo, ref PluginPolicy pluginPolicy)
{
return false;
}
void IRequestContextHandler.OnRequestContextInitialized(IRequestContext requestContext)
{
requestContextInitialized = true;
string errorMessage;
foreach (var pref in preferences)
{
if (!requestContext.SetPreference(pref.Key, pref.Value, out errorMessage))
{
//TODO: Do something if there's an error
}
}
onContextInitialziedAction?.Invoke(requestContext);
OnRequestContextInitialized(requestContext);
}
///
/// Called immediately after the request context has been initialized.
/// It's important to note this event is fired on a CEF UI thread, which by default is not the same as your application UI
/// thread.
///
/// the request context
protected virtual void OnRequestContextInitialized(IRequestContext requestContext)
{
}
}
}