CefRuntimeLoader.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Xilium.CefGlue.Common.Handlers;
  5. using Xilium.CefGlue.Common.Shared;
  6. namespace Xilium.CefGlue.Common
  7. {
  8. public static class CefRuntimeLoader
  9. {
  10. private static Action<BrowserProcessHandler> _delayedInitialization;
  11. public static void Initialize(CefSettings settings = null, KeyValuePair<string, string>[] flags = null, CustomScheme[] customSchemes = null)
  12. {
  13. _delayedInitialization = (browserProcessHandler) => InternalInitialize(settings, flags, customSchemes, browserProcessHandler);
  14. }
  15. private static void InternalInitialize(CefSettings settings = null, KeyValuePair<string, string>[] flags = null, CustomScheme[] customSchemes = null, BrowserProcessHandler browserProcessHandler = null)
  16. {
  17. CefRuntime.Load();
  18. if (settings == null)
  19. {
  20. settings = new CefSettings();
  21. }
  22. settings.UncaughtExceptionStackSize = 100; // for uncaught exception event work properly
  23. var path = AppDomain.CurrentDomain.BaseDirectory;
  24. var subprocessPath = Path.Combine(path, BrowserProcessFileName);
  25. if (!File.Exists(subprocessPath))
  26. {
  27. subprocessPath = Path.Combine(path, "CefGlueBrowserProcess", BrowserProcessFileName);
  28. if (!File.Exists(subprocessPath))
  29. {
  30. throw new FileNotFoundException($"Unable to find \"{subprocessPath}\"");
  31. }
  32. }
  33. settings.BrowserSubprocessPath = subprocessPath;
  34. switch (CefRuntime.Platform)
  35. {
  36. case CefRuntimePlatform.Windows:
  37. settings.MultiThreadedMessageLoop = true;
  38. break;
  39. case CefRuntimePlatform.MacOSX:
  40. var resourcesPath = Path.Combine(path, "Resources");
  41. if (!Directory.Exists(resourcesPath))
  42. {
  43. throw new FileNotFoundException($"Unable to find Resources folder");
  44. }
  45. settings.NoSandbox = true;
  46. settings.MultiThreadedMessageLoop = false;
  47. settings.ExternalMessagePump = true;
  48. settings.MainBundlePath = path;
  49. settings.FrameworkDirPath = path;
  50. settings.ResourcesDirPath = resourcesPath;
  51. break;
  52. }
  53. AppDomain.CurrentDomain.ProcessExit += delegate { CefRuntime.Shutdown(); };
  54. IsOSREnabled = settings.WindowlessRenderingEnabled;
  55. CefRuntime.Initialize(new CefMainArgs(new string[0]), settings, new BrowserCefApp(customSchemes, flags, browserProcessHandler), IntPtr.Zero);
  56. if (customSchemes != null)
  57. {
  58. foreach (var scheme in customSchemes)
  59. {
  60. CefRuntime.RegisterSchemeHandlerFactory(scheme.SchemeName, scheme.DomainName, scheme.SchemeHandlerFactory);
  61. }
  62. }
  63. }
  64. internal static void Load(BrowserProcessHandler browserProcessHandler = null)
  65. {
  66. if (_delayedInitialization != null)
  67. {
  68. _delayedInitialization.Invoke(browserProcessHandler);
  69. _delayedInitialization = null;
  70. }
  71. else
  72. {
  73. InternalInitialize(browserProcessHandler: browserProcessHandler);
  74. }
  75. }
  76. public static bool IsLoaded => CefRuntime.IsInitialized;
  77. internal static bool IsOSREnabled { get; private set; }
  78. private static string BrowserProcessFileName {
  79. get {
  80. const string Filename = "Xilium.CefGlue.BrowserProcess";
  81. switch (CefRuntime.Platform)
  82. {
  83. case CefRuntimePlatform.Windows:
  84. return Filename + ".exe";
  85. default:
  86. return Filename;
  87. }
  88. }
  89. }
  90. }
  91. }