// 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.
///
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;
}
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)
{
}
}
}