123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using Xilium.CefGlue.Common.Handlers;
- using Xilium.CefGlue.Common.Shared;
- namespace Xilium.CefGlue.Common
- {
- public static class CefRuntimeLoader
- {
- private static Action<BrowserProcessHandler> _delayedInitialization;
- public static void Initialize(CefSettings settings = null, KeyValuePair<string, string>[] flags = null, CustomScheme[] customSchemes = null)
- {
- _delayedInitialization = (browserProcessHandler) => InternalInitialize(settings, flags, customSchemes, browserProcessHandler);
- }
- private static void InternalInitialize(CefSettings settings = null, KeyValuePair<string, string>[] flags = null, CustomScheme[] customSchemes = null, BrowserProcessHandler browserProcessHandler = null)
- {
- CefRuntime.Load();
- if (settings == null)
- {
- settings = new CefSettings();
- }
- settings.UncaughtExceptionStackSize = 100; // for uncaught exception event work properly
- var path = AppDomain.CurrentDomain.BaseDirectory;
- var subprocessPath = Path.Combine(path, BrowserProcessFileName);
- if (!File.Exists(subprocessPath))
- {
- subprocessPath = Path.Combine(path, "CefGlueBrowserProcess", BrowserProcessFileName);
-
- if (!File.Exists(subprocessPath))
- {
- throw new FileNotFoundException($"Unable to find \"{subprocessPath}\"");
- }
- }
-
- settings.BrowserSubprocessPath = subprocessPath;
- switch (CefRuntime.Platform)
- {
- case CefRuntimePlatform.Windows:
- settings.MultiThreadedMessageLoop = true;
- break;
- case CefRuntimePlatform.MacOSX:
- var resourcesPath = Path.Combine(path, "Resources");
- if (!Directory.Exists(resourcesPath))
- {
- throw new FileNotFoundException($"Unable to find Resources folder");
- }
- settings.NoSandbox = true;
- settings.MultiThreadedMessageLoop = false;
- settings.ExternalMessagePump = true;
- settings.MainBundlePath = path;
- settings.FrameworkDirPath = path;
- settings.ResourcesDirPath = resourcesPath;
- break;
- }
- AppDomain.CurrentDomain.ProcessExit += delegate { CefRuntime.Shutdown(); };
- IsOSREnabled = settings.WindowlessRenderingEnabled;
- CefRuntime.Initialize(new CefMainArgs(new string[0]), settings, new BrowserCefApp(customSchemes, flags, browserProcessHandler), IntPtr.Zero);
- if (customSchemes != null)
- {
- foreach (var scheme in customSchemes)
- {
- CefRuntime.RegisterSchemeHandlerFactory(scheme.SchemeName, scheme.DomainName, scheme.SchemeHandlerFactory);
- }
- }
- }
- internal static void Load(BrowserProcessHandler browserProcessHandler = null)
- {
- if (_delayedInitialization != null)
- {
- _delayedInitialization.Invoke(browserProcessHandler);
- _delayedInitialization = null;
- }
- else
- {
- InternalInitialize(browserProcessHandler: browserProcessHandler);
- }
- }
- public static bool IsLoaded => CefRuntime.IsInitialized;
- internal static bool IsOSREnabled { get; private set; }
- private static string BrowserProcessFileName {
- get {
- const string Filename = "Xilium.CefGlue.BrowserProcess";
- switch (CefRuntime.Platform)
- {
- case CefRuntimePlatform.Windows:
- return Filename + ".exe";
- default:
- return Filename;
- }
- }
- }
- }
- }
|