ExternalRequestBlocker.cs 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. using CefSharp;
  2. using CefSharp.Handler;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace CefSharpWrap
  6. {
  7. internal class ExternalRequestBlocker : DefaultRequestHandler
  8. {
  9. private readonly HashSet<string> _whiteList;
  10. private readonly Action<string> _nonWhiteListAccess;
  11. public ExternalRequestBlocker(Action<string> nonWhiteListAccess = null, params string[] whiteListScheme) : this(whiteListScheme)
  12. {
  13. _nonWhiteListAccess = nonWhiteListAccess;
  14. }
  15. public ExternalRequestBlocker(params string[] whiteListScheme)
  16. {
  17. _whiteList = new HashSet<string>(whiteListScheme) { "chrome-devtools", CefSharpWrapFactory.SchemeName };
  18. }
  19. public override bool OnBeforeBrowse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, bool userGesture, bool isRedirect)
  20. {
  21. var uri = new Uri(request.Url);
  22. if (_whiteList.Contains(uri.Scheme)) return false;
  23. _nonWhiteListAccess?.Invoke(request.Url);
  24. return true;
  25. }
  26. }
  27. }