// Copyright © 2019 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
{
///
/// Default implementation of . This class provides default implementations of the methods
/// from , therefore providing a convenience base class for any custom resource request
/// handler.
///
///
public class ResourceRequestHandler : IResourceRequestHandler
{
///
/// Called on the CEF IO thread before a resource request is loaded. To optionally filter cookies for the request return a
/// object.
///
/// The ChromiumWebBrowser control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object - can be modified in this callback.
/// To optionally filter cookies for the request return a ICookieAccessFilter instance otherwise return null.
ICookieAccessFilter IResourceRequestHandler.GetCookieAccessFilter(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request)
{
return GetCookieAccessFilter(chromiumWebBrowser, browser, frame, request);
}
///
/// Called on the CEF IO thread before a resource request is loaded. To optionally filter cookies for the request return a
/// object.
///
/// The ChromiumWebBrowser control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object - can be modified in this callback.
/// To optionally filter cookies for the request return a ICookieAccessFilter instance otherwise return null.
protected virtual ICookieAccessFilter GetCookieAccessFilter(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request)
{
return null;
}
///
/// Called on the CEF IO thread before a resource is loaded. To specify a handler for the resource return a
/// object.
///
/// The browser UI control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object - cannot be modified in this callback.
///
/// To allow the resource to load using the default network loader return null otherwise return an instance of
/// with a valid stream.
///
IResourceHandler IResourceRequestHandler.GetResourceHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request)
{
return GetResourceHandler(chromiumWebBrowser, browser, frame, request);
}
///
/// Called on the CEF IO thread before a resource is loaded. To specify a handler for the resource return a
/// object.
///
/// The browser UI control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object - cannot be modified in this callback.
///
/// To allow the resource to load using the default network loader return null otherwise return an instance of
/// with a valid stream.
///
protected virtual IResourceHandler GetResourceHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request)
{
return null;
}
/// Called on the CEF IO thread to optionally filter resource response content.
/// The ChromiumWebBrowser control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object - cannot be modified in this callback.
/// the response object - cannot be modified in this callback.
/// Return an IResponseFilter to intercept this response, otherwise return null.
IResponseFilter IResourceRequestHandler.GetResourceResponseFilter(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
return GetResourceResponseFilter(chromiumWebBrowser, browser, frame, request, response);
}
/// Called on the CEF IO thread to optionally filter resource response content.
/// The ChromiumWebBrowser control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object - cannot be modified in this callback.
/// the response object - cannot be modified in this callback.
/// Return an IResponseFilter to intercept this response, otherwise return null.
protected virtual IResponseFilter GetResourceResponseFilter(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
return null;
}
///
/// Called on the CEF IO thread before a resource request is loaded. To redirect or change the resource load optionally modify
/// . Modification of the request URL will be treated as a redirect.
///
/// The ChromiumWebBrowser control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object - can be modified in this callback.
/// Callback interface used for asynchronous continuation of url requests.
///
/// Return to continue the request immediately. Return
/// and call or
/// at a later time to continue or the cancel the request asynchronously. Return
/// to cancel the request immediately.
///
CefReturnValue IResourceRequestHandler.OnBeforeResourceLoad(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
{
return OnBeforeResourceLoad(chromiumWebBrowser, browser, frame, request, callback);
}
///
/// Called on the CEF IO thread before a resource request is loaded. To redirect or change the resource load optionally modify
/// . Modification of the request URL will be treated as a redirect.
///
/// The ChromiumWebBrowser control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object - can be modified in this callback.
/// Callback interface used for asynchronous continuation of url requests.
///
/// Return to continue the request immediately. Return
/// and call or
/// at a later time to continue or the cancel the request asynchronously. Return
/// to cancel the request immediately.
///
protected virtual CefReturnValue OnBeforeResourceLoad(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
{
return CefReturnValue.Continue;
}
///
/// Called on the CEF UI thread to handle requests for URLs with an unknown protocol component. SECURITY WARNING: YOU SHOULD USE
/// THIS METHOD TO ENFORCE RESTRICTIONS BASED ON SCHEME, HOST OR OTHER URL ANALYSIS BEFORE ALLOWING OS EXECUTION.
///
/// The ChromiumWebBrowser control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object - cannot be modified in this callback.
///
/// return to true to attempt execution via the registered OS protocol handler, if any. Otherwise return false.
///
bool IResourceRequestHandler.OnProtocolExecution(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request)
{
return OnProtocolExecution(chromiumWebBrowser, browser, frame, request);
}
///
/// Called on the CEF UI thread to handle requests for URLs with an unknown protocol component. SECURITY WARNING: YOU SHOULD USE
/// THIS METHOD TO ENFORCE RESTRICTIONS BASED ON SCHEME, HOST OR OTHER URL ANALYSIS BEFORE ALLOWING OS EXECUTION.
///
/// The ChromiumWebBrowser control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object - cannot be modified in this callback.
///
/// return to true to attempt execution via the registered OS protocol handler, if any. Otherwise return false.
///
protected virtual bool OnProtocolExecution(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request)
{
return false;
}
///
/// Called on the CEF IO thread when a resource load has completed. This method will be called for all requests, including
/// requests that are aborted due to CEF shutdown or destruction of the associated browser. In cases where the associated browser
/// is destroyed this callback may arrive after the callback for that browser. The
/// method can be used to test for this situation, and care
/// should be taken not to call or methods that modify state (like LoadURL,
/// SendProcessMessage, etc.) if the frame is invalid.
///
/// The ChromiumWebBrowser control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object - cannot be modified in this callback.
/// the response object - cannot be modified in this callback.
/// indicates the load completion status.
/// is the number of response bytes actually read.
void IResourceRequestHandler.OnResourceLoadComplete(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
{
OnResourceLoadComplete(chromiumWebBrowser, browser, frame, request, response, status, receivedContentLength);
}
///
/// Called on the CEF IO thread when a resource load has completed. This method will be called for all requests, including
/// requests that are aborted due to CEF shutdown or destruction of the associated browser. In cases where the associated browser
/// is destroyed this callback may arrive after the callback for that browser. The
/// method can be used to test for this situation, and care
/// should be taken not to call or methods that modify state (like LoadURL,
/// SendProcessMessage, etc.) if the frame is invalid.
///
/// The ChromiumWebBrowser control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object - cannot be modified in this callback.
/// the response object - cannot be modified in this callback.
/// indicates the load completion status.
/// is the number of response bytes actually read.
protected virtual void OnResourceLoadComplete(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
{
}
///
/// Called on the CEF IO thread when a resource load is redirected. The parameter will contain the old
/// URL and other request-related information. The parameter will contain the response that resulted
/// in the redirect. The parameter will contain the new URL and can be changed if desired.
///
/// The ChromiumWebBrowser control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object - cannot be modified in this callback.
/// the response object - cannot be modified in this callback.
/// [in,out] the new URL and can be changed if desired.
void IResourceRequestHandler.OnResourceRedirect(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response, ref string newUrl)
{
OnResourceRedirect(chromiumWebBrowser, browser, frame, request, response, ref newUrl);
}
///
/// Called on the CEF IO thread when a resource load is redirected. The parameter will contain the old
/// URL and other request-related information. The parameter will contain the response that resulted
/// in the redirect. The parameter will contain the new URL and can be changed if desired.
///
/// The ChromiumWebBrowser control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object - cannot be modified in this callback.
/// the response object - cannot be modified in this callback.
/// [in,out] the new URL and can be changed if desired.
protected virtual void OnResourceRedirect(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response, ref string newUrl)
{
}
///
/// Called on the CEF IO thread when a resource response is received. To allow the resource load to proceed without modification
/// return false. To redirect or retry the resource load optionally modify and return true.
/// Modification of the request URL will be treated as a redirect. Requests handled using the default network loader cannot be
/// redirected in this callback.
///
/// WARNING: Redirecting using this method is deprecated. Use OnBeforeResourceLoad or GetResourceHandler to perform redirects.
///
/// The ChromiumWebBrowser control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object.
/// the response object - cannot be modified in this callback.
///
/// To allow the resource load to proceed without modification return false. To redirect or retry the resource load optionally
/// modify and return true. Modification of the request URL will be treated as a redirect. Requests
/// handled using the default network loader cannot be redirected in this callback.
///
bool IResourceRequestHandler.OnResourceResponse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
return OnResourceResponse(chromiumWebBrowser, browser, frame, request, response);
}
///
/// Called on the CEF IO thread when a resource response is received. To allow the resource load to proceed without modification
/// return false. To redirect or retry the resource load optionally modify and return true.
/// Modification of the request URL will be treated as a redirect. Requests handled using the default network loader cannot be
/// redirected in this callback.
///
/// WARNING: Redirecting using this method is deprecated. Use OnBeforeResourceLoad or GetResourceHandler to perform redirects.
///
/// The ChromiumWebBrowser control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object.
/// the response object - cannot be modified in this callback.
///
/// To allow the resource load to proceed without modification return false. To redirect or retry the resource load optionally
/// modify and return true. Modification of the request URL will be treated as a redirect. Requests
/// handled using the default network loader cannot be redirected in this callback.
///
protected virtual bool OnResourceResponse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
return false;
}
///
/// Called when the unamanged resource is freed.
/// Unmanaged resources are ref counted and freed when
/// the last reference is released, this works differently
/// to .Net garbage collection.
///
protected virtual void Dispose()
{
}
void IDisposable.Dispose()
{
Dispose();
}
}
}