From 9e6a975690abb715904bbb61f8bb5ee67c06ac12 Mon Sep 17 00:00:00 2001 From: martinekvili Date: Mon, 9 Oct 2017 17:48:11 +0200 Subject: [PATCH 1/4] Added default implementation of IRequestHandler --- CefSharp/CefSharp.csproj | 1 + CefSharp/DefaultRequestHandler.cs | 96 +++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 CefSharp/DefaultRequestHandler.cs diff --git a/CefSharp/CefSharp.csproj b/CefSharp/CefSharp.csproj index 9a99b466d9..e002638927 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -98,6 +98,7 @@ + diff --git a/CefSharp/DefaultRequestHandler.cs b/CefSharp/DefaultRequestHandler.cs new file mode 100644 index 0000000000..52b21dbc1b --- /dev/null +++ b/CefSharp/DefaultRequestHandler.cs @@ -0,0 +1,96 @@ +// Copyright © 2010-2017 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.Security.Cryptography.X509Certificates; + +namespace CefSharp +{ + /// + /// Default implementation of . + /// This class provides default implementations of the methods from , + /// therefore providing a convenience base class for any custom request handler. + /// + public class DefaultRequestHandler : IRequestHandler + { + public bool GetAuthCredentials(IWebBrowser browserControl, IBrowser browser, IFrame frame, bool isProxy, string host, + int port, string realm, string scheme, IAuthCallback callback) + { + callback.Dispose(); + return false; + } + + public IResponseFilter GetResourceResponseFilter(IWebBrowser browserControl, IBrowser browser, IFrame frame, + IRequest request, IResponse response) + { + return null; + } + + public bool OnBeforeBrowse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, bool isRedirect) + { + return false; + } + + public CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, + IRequestCallback callback) + { + callback.Dispose(); + return CefReturnValue.Continue; + } + + public bool OnCertificateError(IWebBrowser browserControl, IBrowser browser, CefErrorCode errorCode, string requestUrl, + ISslInfo sslInfo, IRequestCallback callback) + { + callback.Dispose(); + return false; + } + + public bool OnOpenUrlFromTab(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, + WindowOpenDisposition targetDisposition, bool userGesture) + { + return false; + } + + public void OnPluginCrashed(IWebBrowser browserControl, IBrowser browser, string pluginPath) + { } + + public bool OnProtocolExecution(IWebBrowser browserControl, IBrowser browser, string url) + { + return false; + } + + public bool OnQuotaRequest(IWebBrowser browserControl, IBrowser browser, string originUrl, long newSize, + IRequestCallback callback) + { + callback.Dispose(); + return false; + } + + public void OnRenderProcessTerminated(IWebBrowser browserControl, IBrowser browser, CefTerminationStatus status) + { } + + public void OnRenderViewReady(IWebBrowser browserControl, IBrowser browser) + { } + + public void OnResourceLoadComplete(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, + IResponse response, UrlRequestStatus status, long receivedContentLength) + { } + + public void OnResourceRedirect(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, + IResponse response, ref string newUrl) + { } + + public bool OnResourceResponse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, + IResponse response) + { + return false; + } + + public bool OnSelectClientCertificate(IWebBrowser browserControl, IBrowser browser, bool isProxy, string host, int port, + X509Certificate2Collection certificates, ISelectClientCertificateCallback callback) + { + callback.Dispose(); + return false; + } + } +} From 9dbf53ea99beae441d22200d8833d4351c4b9d9a Mon Sep 17 00:00:00 2001 From: martinekvili Date: Mon, 9 Oct 2017 18:37:55 +0200 Subject: [PATCH 2/4] Functions made virtual --- CefSharp/DefaultRequestHandler.cs | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/CefSharp/DefaultRequestHandler.cs b/CefSharp/DefaultRequestHandler.cs index 52b21dbc1b..ad9747b28e 100644 --- a/CefSharp/DefaultRequestHandler.cs +++ b/CefSharp/DefaultRequestHandler.cs @@ -13,80 +13,80 @@ namespace CefSharp /// public class DefaultRequestHandler : IRequestHandler { - public bool GetAuthCredentials(IWebBrowser browserControl, IBrowser browser, IFrame frame, bool isProxy, string host, + public virtual bool GetAuthCredentials(IWebBrowser browserControl, IBrowser browser, IFrame frame, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback) { callback.Dispose(); return false; } - public IResponseFilter GetResourceResponseFilter(IWebBrowser browserControl, IBrowser browser, IFrame frame, + public virtual IResponseFilter GetResourceResponseFilter(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response) { return null; } - public bool OnBeforeBrowse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, bool isRedirect) + public virtual bool OnBeforeBrowse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, bool isRedirect) { return false; } - public CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, + public virtual CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback) { callback.Dispose(); return CefReturnValue.Continue; } - public bool OnCertificateError(IWebBrowser browserControl, IBrowser browser, CefErrorCode errorCode, string requestUrl, + public virtual bool OnCertificateError(IWebBrowser browserControl, IBrowser browser, CefErrorCode errorCode, string requestUrl, ISslInfo sslInfo, IRequestCallback callback) { callback.Dispose(); return false; } - public bool OnOpenUrlFromTab(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, + public virtual bool OnOpenUrlFromTab(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, WindowOpenDisposition targetDisposition, bool userGesture) { return false; } - public void OnPluginCrashed(IWebBrowser browserControl, IBrowser browser, string pluginPath) + public virtual void OnPluginCrashed(IWebBrowser browserControl, IBrowser browser, string pluginPath) { } - public bool OnProtocolExecution(IWebBrowser browserControl, IBrowser browser, string url) + public virtual bool OnProtocolExecution(IWebBrowser browserControl, IBrowser browser, string url) { return false; } - public bool OnQuotaRequest(IWebBrowser browserControl, IBrowser browser, string originUrl, long newSize, + public virtual bool OnQuotaRequest(IWebBrowser browserControl, IBrowser browser, string originUrl, long newSize, IRequestCallback callback) { callback.Dispose(); return false; } - public void OnRenderProcessTerminated(IWebBrowser browserControl, IBrowser browser, CefTerminationStatus status) + public virtual void OnRenderProcessTerminated(IWebBrowser browserControl, IBrowser browser, CefTerminationStatus status) { } - public void OnRenderViewReady(IWebBrowser browserControl, IBrowser browser) + public virtual void OnRenderViewReady(IWebBrowser browserControl, IBrowser browser) { } - public void OnResourceLoadComplete(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, + public virtual void OnResourceLoadComplete(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength) { } - public void OnResourceRedirect(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, + public virtual void OnResourceRedirect(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response, ref string newUrl) { } - public bool OnResourceResponse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, + public virtual bool OnResourceResponse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response) { return false; } - public bool OnSelectClientCertificate(IWebBrowser browserControl, IBrowser browser, bool isProxy, string host, int port, + public virtual bool OnSelectClientCertificate(IWebBrowser browserControl, IBrowser browser, bool isProxy, string host, int port, X509Certificate2Collection certificates, ISelectClientCertificateCallback callback) { callback.Dispose(); From 027906b12f1fe742f33f9fc2be78737313cb4493 Mon Sep 17 00:00:00 2001 From: martinekvili Date: Tue, 12 Dec 2017 20:32:10 +0100 Subject: [PATCH 3/4] Include DefaultRequestHandler in csproj (merge fix) --- CefSharp/CefSharp.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/CefSharp/CefSharp.csproj b/CefSharp/CefSharp.csproj index fbf8e83cde..36a7649be8 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -98,6 +98,7 @@ + From 09fb77a0520337ffb891cc3f7a310bfc2eaf0493 Mon Sep 17 00:00:00 2001 From: martinekvili Date: Wed, 13 Dec 2017 14:11:59 +0100 Subject: [PATCH 4/4] Moved DefaultRequestHandler to CefSharp.Handler namespace; added comments --- CefSharp/CefSharp.csproj | 2 +- CefSharp/DefaultRequestHandler.cs | 96 -------- CefSharp/Handler/DefaultRequestHandler.cs | 263 ++++++++++++++++++++++ 3 files changed, 264 insertions(+), 97 deletions(-) delete mode 100644 CefSharp/DefaultRequestHandler.cs create mode 100644 CefSharp/Handler/DefaultRequestHandler.cs diff --git a/CefSharp/CefSharp.csproj b/CefSharp/CefSharp.csproj index 36a7649be8..182a4948a6 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -98,7 +98,7 @@ - + diff --git a/CefSharp/DefaultRequestHandler.cs b/CefSharp/DefaultRequestHandler.cs deleted file mode 100644 index ad9747b28e..0000000000 --- a/CefSharp/DefaultRequestHandler.cs +++ /dev/null @@ -1,96 +0,0 @@ -// Copyright © 2010-2017 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.Security.Cryptography.X509Certificates; - -namespace CefSharp -{ - /// - /// Default implementation of . - /// This class provides default implementations of the methods from , - /// therefore providing a convenience base class for any custom request handler. - /// - public class DefaultRequestHandler : IRequestHandler - { - public virtual bool GetAuthCredentials(IWebBrowser browserControl, IBrowser browser, IFrame frame, bool isProxy, string host, - int port, string realm, string scheme, IAuthCallback callback) - { - callback.Dispose(); - return false; - } - - public virtual IResponseFilter GetResourceResponseFilter(IWebBrowser browserControl, IBrowser browser, IFrame frame, - IRequest request, IResponse response) - { - return null; - } - - public virtual bool OnBeforeBrowse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, bool isRedirect) - { - return false; - } - - public virtual CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, - IRequestCallback callback) - { - callback.Dispose(); - return CefReturnValue.Continue; - } - - public virtual bool OnCertificateError(IWebBrowser browserControl, IBrowser browser, CefErrorCode errorCode, string requestUrl, - ISslInfo sslInfo, IRequestCallback callback) - { - callback.Dispose(); - return false; - } - - public virtual bool OnOpenUrlFromTab(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, - WindowOpenDisposition targetDisposition, bool userGesture) - { - return false; - } - - public virtual void OnPluginCrashed(IWebBrowser browserControl, IBrowser browser, string pluginPath) - { } - - public virtual bool OnProtocolExecution(IWebBrowser browserControl, IBrowser browser, string url) - { - return false; - } - - public virtual bool OnQuotaRequest(IWebBrowser browserControl, IBrowser browser, string originUrl, long newSize, - IRequestCallback callback) - { - callback.Dispose(); - return false; - } - - public virtual void OnRenderProcessTerminated(IWebBrowser browserControl, IBrowser browser, CefTerminationStatus status) - { } - - public virtual void OnRenderViewReady(IWebBrowser browserControl, IBrowser browser) - { } - - public virtual void OnResourceLoadComplete(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, - IResponse response, UrlRequestStatus status, long receivedContentLength) - { } - - public virtual void OnResourceRedirect(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, - IResponse response, ref string newUrl) - { } - - public virtual bool OnResourceResponse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, - IResponse response) - { - return false; - } - - public virtual bool OnSelectClientCertificate(IWebBrowser browserControl, IBrowser browser, bool isProxy, string host, int port, - X509Certificate2Collection certificates, ISelectClientCertificateCallback callback) - { - callback.Dispose(); - return false; - } - } -} diff --git a/CefSharp/Handler/DefaultRequestHandler.cs b/CefSharp/Handler/DefaultRequestHandler.cs new file mode 100644 index 0000000000..8095d84cd1 --- /dev/null +++ b/CefSharp/Handler/DefaultRequestHandler.cs @@ -0,0 +1,263 @@ +// Copyright © 2010-2017 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.Security.Cryptography.X509Certificates; + +namespace CefSharp.Handler +{ + /// + /// Default implementation of . + /// This class provides default implementations of the methods from , + /// therefore providing a convenience base class for any custom request handler. + /// + public class DefaultRequestHandler : IRequestHandler + { + /// + /// Called when the browser needs credentials from the user. + /// + /// The ChromiumWebBrowser control + /// the browser object + /// The frame object that needs credentials (This will contain the URL that is being requested.) + /// indicates whether the host is a proxy server + /// hostname + /// port number + /// realm + /// scheme + /// Callback interface used for asynchronous continuation of authentication requests. + /// Return true to continue the request and call CefAuthCallback::Continue() when the authentication information is available. Return false to cancel the request. + public virtual bool GetAuthCredentials(IWebBrowser browserControl, IBrowser browser, IFrame frame, bool isProxy, string host, + int port, string realm, string scheme, IAuthCallback callback) + { + callback.Dispose(); + return false; + } + + /// + /// Called on the CEF IO thread to optionally filter resource response content. + /// + /// The ChromiumWebBrowser control + /// the browser object + /// The frame that is being redirected. + /// 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 + public virtual IResponseFilter GetResourceResponseFilter(IWebBrowser browserControl, IBrowser browser, IFrame frame, + IRequest request, IResponse response) + { + return null; + } + + /// + /// Called before browser navigation. + /// If the navigation is allowed and + /// will be called. If the navigation is canceled will be called with an ErrorCode + /// value of . + /// + /// the ChromiumWebBrowser control + /// the browser object + /// The frame the request is coming from + /// the request object - cannot be modified in this callback + /// has the request been redirected + /// Return true to cancel the navigation or false to allow the navigation to proceed. + public virtual bool OnBeforeBrowse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, bool isRedirect) + { + return false; + } + + /// + /// Called before a resource request is loaded. For async processing return + /// and execute or + /// + /// The ChromiumWebBrowser control + /// the browser object + /// The frame object + /// the request object - can be modified in this callback. + /// Callback interface used for asynchronous continuation of url requests. + /// To cancel loading of the resource return + /// or to allow the resource to load normally. For async + /// return + public virtual CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, + IRequestCallback callback) + { + callback.Dispose(); + return CefReturnValue.Continue; + } + + /// + /// Called to handle requests for URLs with an invalid SSL certificate. + /// Return true and call either + /// in this method or at a later time to continue or cancel the request. + /// If is set all invalid certificates + /// will be accepted without calling this method. + /// + /// the ChromiumWebBrowser control + /// the browser object + /// the error code for this invalid certificate + /// the url of the request for the invalid certificate + /// ssl certificate information + /// Callback interface used for asynchronous continuation of url requests. + /// If empty the error cannot be recovered from and the request will be canceled automatically. + /// Return false to cancel the request immediately. Return true and use to + /// execute in an async fashion. + public virtual bool OnCertificateError(IWebBrowser browserControl, IBrowser browser, CefErrorCode errorCode, string requestUrl, + ISslInfo sslInfo, IRequestCallback callback) + { + callback.Dispose(); + return false; + } + + /// + /// Called on the UI thread before OnBeforeBrowse in certain limited cases + /// where navigating a new or different browser might be desirable. This + /// includes user-initiated navigation that might open in a special way (e.g. + /// links clicked via middle-click or ctrl + left-click) and certain types of + /// cross-origin navigation initiated from the renderer process (e.g. + /// navigating the top-level frame to/from a file URL). + /// + /// the ChromiumWebBrowser control + /// the browser object + /// The frame object + /// target url + /// The value indicates where the user intended to navigate the browser based + /// on standard Chromium behaviors (e.g. current tab, new tab, etc). + /// The value will be true if the browser navigated via explicit user gesture + /// (e.g. clicking a link) or false if it navigated automatically (e.g. via the DomContentLoaded event). + /// Return true to cancel the navigation or false to allow the navigation + /// to proceed in the source browser's top-level frame. + public virtual bool OnOpenUrlFromTab(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, + WindowOpenDisposition targetDisposition, bool userGesture) + { + return false; + } + + /// + /// Called when a plugin has crashed + /// + /// the ChromiumWebBrowser control + /// the browser object + /// path of the plugin that crashed + public virtual void OnPluginCrashed(IWebBrowser browserControl, IBrowser browser, string pluginPath) + { } + + /// + /// Called on the 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 + /// the request url + /// return to true to attempt execution via the registered OS protocol handler, if any. Otherwise return false. + public virtual bool OnProtocolExecution(IWebBrowser browserControl, IBrowser browser, string url) + { + return false; + } + + /// + /// Called when JavaScript requests a specific storage quota size via the webkitStorageInfo.requestQuota function. + /// For async processing return true and execute at a later time to + /// grant or deny the request or to cancel. + /// + /// The ChromiumWebBrowser control + /// the browser object + /// the origin of the page making the request + /// is the requested quota size in bytes + /// Callback interface used for asynchronous continuation of url requests. + /// Return false to cancel the request immediately. Return true to continue the request + /// and call either in this method or at a later time to + /// grant or deny the request. + public virtual bool OnQuotaRequest(IWebBrowser browserControl, IBrowser browser, string originUrl, long newSize, + IRequestCallback callback) + { + callback.Dispose(); + return false; + } + + /// + /// Called when the render process terminates unexpectedly. + /// + /// The ChromiumWebBrowser control + /// the browser object + /// indicates how the process terminated. + public virtual void OnRenderProcessTerminated(IWebBrowser browserControl, IBrowser browser, CefTerminationStatus status) + { } + + /// + /// Called on the CEF UI thread when the render view associated + /// with browser is ready to receive/handle IPC messages in the render + /// process. + /// + /// The ChromiumWebBrowser control + /// the browser object + public virtual void OnRenderViewReady(IWebBrowser browserControl, IBrowser browser) + { } + + /// + /// Called on the CEF IO thread when a resource load has completed. + /// + /// The ChromiumWebBrowser control + /// the browser object + /// The frame that is being redirected. + /// 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. + public virtual void OnResourceLoadComplete(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, + IResponse response, UrlRequestStatus status, long receivedContentLength) + { } + + /// + /// Called on the IO thread when a resource load is redirected. The + /// parameter will contain the old URL and other request-related information. + /// + /// The ChromiumWebBrowser control + /// the browser object + /// The frame that is being redirected. + /// the request object - cannot be modified in this callback + /// the response object + /// the new URL and can be changed if desired + public virtual void OnResourceRedirect(IWebBrowser browserControl, 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 to load normally return false. + /// To redirect or retry the resource modify request (url, headers or post body) and return true. + /// The response object cannot be modified in this callback. + /// + /// The ChromiumWebBrowser control + /// the browser object + /// The frame that is being redirected. + /// the request object + /// the response object - cannot be modified in this callback + /// + /// To allow the resource to load normally return false. + /// To redirect or retry the resource modify request (url, headers or post body) and return true. + /// + public virtual bool OnResourceResponse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, + IResponse response) + { + return false; + } + + /// + /// Called when the browser needs user to select Client Certificate for authentication requests (eg. PKI authentication). + /// + /// The ChromiumWebBrowser control + /// the browser object + /// indicates whether the host is a proxy server + /// hostname + /// port number + /// List of Client certificates for selection + /// Callback interface used for asynchronous continuation of client certificate selection for authentication requests. + /// Return true to continue the request and call ISelectClientCertificateCallback.Select() with the selected certificate for authentication. + /// Return false to use the default behavior where the browser selects the first certificate from the list. + public virtual bool OnSelectClientCertificate(IWebBrowser browserControl, IBrowser browser, bool isProxy, string host, int port, + X509Certificate2Collection certificates, ISelectClientCertificateCallback callback) + { + callback.Dispose(); + return false; + } + } +}