CefInit.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.IO;
  3. using Cbdx.Tests.Resources;
  4. using CefSharp;
  5. using CefSharp.WinForms;
  6. namespace Cbdx.Tests.CefRelated
  7. {
  8. internal static class CefInit
  9. {
  10. public static readonly string ResourceSchemeName = $"cef-custom-scheme-{Guid.NewGuid()}".ToLower();
  11. public static readonly string ResourceDomainName = $"app-{Guid.NewGuid()}";
  12. public static readonly string ResourceRootUrl = $"{ResourceSchemeName}://{ResourceDomainName}/";
  13. public static void Init()
  14. {
  15. CefSharpSettings.LegacyJavascriptBindingEnabled = true;
  16. var settings = new CefSettings();
  17. settings.BrowserSubprocessPath = Path.Combine(Environment.Is64BitProcess ? "x64" : "x86", settings.BrowserSubprocessPath);
  18. settings.LogSeverity = LogSeverity.Disable;
  19. settings.CefCommandLineArgs.Add("-incognito", "");
  20. settings.CefCommandLineArgs.Add("--disable-application-cache", "");
  21. settings.CefCommandLineArgs.Add("--disable-cache", "");
  22. settings.CefCommandLineArgs.Add("--disable-gpu-program-cache", "");
  23. settings.CefCommandLineArgs.Add("--disable-gpu-shader-disk-cache", "");
  24. settings.CefCommandLineArgs.Add("--disable-local-storage", "");
  25. settings.RegisterScheme(new CefCustomScheme
  26. {
  27. SchemeName = ResourceSchemeName,
  28. DomainName = ResourceDomainName,
  29. SchemeHandlerFactory = new ResourceSchemeHandlerFactory(),
  30. });
  31. Cef.Initialize(settings);
  32. }
  33. public static void Quit()
  34. {
  35. Cef.Shutdown();
  36. }
  37. }
  38. internal class ResourceSchemeHandlerFactory : ISchemeHandlerFactory
  39. {
  40. IResourceHandler ISchemeHandlerFactory.Create(IBrowser browser, IFrame frame, string schemeName, IRequest request)
  41. {
  42. var path = new Uri(request.Url).LocalPath.TrimStart('/');
  43. var resource = ResourceLoader.GetBinResource(path);
  44. return null != resource
  45. ? new ResourceHandler(ResourceHandler.GetMimeType(Path.GetExtension(path)), new MemoryStream(resource, false), true)
  46. : null;
  47. }
  48. }
  49. }