static int Main(string[] args) { var startUrl = Environment.GetEnvironmentVariable("ASPNETCORE_URLS") ?? "http://localhost:5000/"; var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseStartup <Startup>() .UseUrls(startUrl) .Build(); host.StartAsync(); var config = new CefConfig(Debug) { Args = args, StartUrl = startUrl, HideConsoleWindow = false, Width = 1200, Height = 1024, // Kiosk = true, // FullScreen = true, }; return(CefPlatformWindows.Start(config)); }
static void Main() { //CefConfig.Init(); API = new PocketApi(); GameInstance = new Game1(); GameInstance.Services.AddService(typeof(API), API); GameInstance.Run(); CefConfig.Stop(); }
public void Can_use_PopulateInstance_to_populate_enum() { var map = new Dictionary <string, object> { ["LogSeverity"] = "Verbose" }; var config = new CefConfig { CefSettings = new CefSettings { LogSeverity = CefLogSeverity.Info } }; map.PopulateInstance(config.CefSettings); Assert.That(config.CefSettings.LogSeverity, Is.EqualTo(CefLogSeverity.Verbose)); }
protected override void Initialize() { // Modify the title of the window Window.Title = "Game"; Window.IsMouseVisible = true; ((System.Windows.Forms.Form)Window.NativeWindow).ClientSize = new System.Drawing.Size(800, 600); Window.ClientSizeChanged += (s, ea) => { CalculateResize(); }; base.Initialize(); CefConfig.Initialize(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + "\\Cef"); }
/// <summary> /// The Initialize. /// </summary> public static void Initialize() { if (!Cef.IsInitialized) { System.AppContext.SetSwitch("Switch.System.Windows.Input.Stylus.EnablePointerSupport", true); const bool multiThreadedMessageLoop = true; var browserProcessHandler = new BrowserProcessHandler(); var settings = new CefSettings { //UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0", MultiThreadedMessageLoop = multiThreadedMessageLoop, ExternalMessagePump = !multiThreadedMessageLoop }; settings.PersistSessionCookies = true; settings.SetOffScreenRenderingBestPerformanceArgs(); settings.CefCommandLineArgs.Remove("disable-gpu-compositing"); CefConfig.Init(settings, browserProcessHandler: browserProcessHandler); } }
public CefGlueHost(CefConfig context) => this.context = context;
public App() { CefConfig.SupportAnyCpu(); }
public static async Task <int> Main(string[] cmdArgs) { try { Startup.AppVersion = $"Chromium: {CefRuntime.ChromeVersion}"; Startup.GetAppHostInstructions = _ => new AppHostInstructions { ImportParams = DesktopConfig.Instance.ImportParams, }; DesktopState.AppDebug = Environment.GetEnvironmentVariable("APP_DEBUG") == "1"; DesktopState.OriginalCommandArgs = cmdArgs; DesktopState.Tool = "app"; DesktopState.ToolVersion = Startup.GetVersion(); DesktopState.ChromeVersion = CefRuntime.ChromeVersion; DesktopConfig.WindowFactory = scope => DesktopState.BrowserHandle; Startup.ConfigureScript = feature => feature.ScriptMethods.Add(new DesktopScripts()); var firstArg = cmdArgs.FirstOrDefault(); if (firstArg?.StartsWith("app:") == true || firstArg?.StartsWith("sharp:") == true || firstArg?.StartsWith("xapp:") == true) { DesktopState.FromScheme = true; var cmds = firstArg.ConvertUrlSchemeToCommands(); if (cmds.Any(x => Startup.DebugArgs.Contains(x) && !cmdArgs.Any(x => Startup.VerboseArgs.Contains(x)))) { cmds.Add("-verbose"); } cmdArgs = cmds.ToArray(); if (DesktopState.AppDebug) { NativeWin.MessageBox(0, cmdArgs.Join(","), "cmdArgs", 0); } } else { CreateRegistryEntryFor("app"); CreateRegistryEntryFor("sharp"); CreateRegistryEntryFor("xapp", "x.exe"); } var cefDebug = DesktopState.AppDebug = DesktopState.AppDebug || cmdArgs.Any(x => Startup.DebugArgs.Contains(x)); if (DesktopState.AppDebug) { Startup.DebugMode = true; } var args = DesktopState.CommandArgs = cmdArgs; var kiosk = args.Contains("-kiosk"); if (kiosk) { args = args.Where(x => x != "-kiosk").ToArray(); } string startUrl = null; string favIcon = Startup.ToolFavIcon; var startPos = Array.IndexOf(args, "start"); if (startPos >= 0) { if (startPos == args.Length - 1) { Console.WriteLine(@"Usage: app start {url}"); return(-1); } startUrl = args[startPos + 1]; if (startUrl.IndexOf("://", StringComparison.Ordinal) == -1) { Console.WriteLine(@$ "Not a valid URL: '{startUrl}'"); Console.WriteLine(@"Usage: app start {url}"); return(-2); } } if (DesktopState.FromScheme && !DesktopState.AppDebug) { var hWnd = CefPlatformWindows.GetConsoleHandle(); if (hWnd != IntPtr.Zero) { User32.ShowWindow(hWnd, User32.WindowShowStyle.SW_HIDE); } } var cts = new CancellationTokenSource(); Process process = null; CefPlatformWindows.OnExit = () => { if (Startup.Verbose) { $"OnExit".Print(); } DesktopConfig.Instance.OnExit?.Invoke(); cts?.Cancel(); process?.Close(); }; if (startUrl == null) { var host = await Startup.CreateWebHost("app", args, new WebAppEvents { CreateShortcut = Shortcut.Create, HandleUnknownCommand = ctx => Startup.PrintUsage("app"), OpenBrowser = url => CefPlatformWindows.Start(new CefConfig { StartUrl = url, Width = 1040, DevTools = false, Icon = Startup.ToolFavIcon, HideConsoleWindow = !DesktopState.AppDebug, }), RunNetCoreProcess = ctx => { var url = Environment.GetEnvironmentVariable("ASPNETCORE_URLS")?.LeftPart(';') ?? "http://localhost:5000"; var target = ctx.RunProcess; var fileName = ctx.RunProcess; var arguments = ""; if (target.EndsWith(".dll")) { fileName = "dotnet"; arguments = ctx.RunProcess; } process = Startup.PipeProcess(fileName, arguments, fn: () => CefPlatformWindows.Start(new CefConfig { StartUrl = url, Icon = ctx.FavIcon, HideConsoleWindow = !DesktopState.AppDebug })); }, }); if (host == null) { return(0); } startUrl = host.StartUrl; favIcon = host.FavIcon; cefDebug = host.DebugMode || DesktopState.AppDebug; #pragma warning disable 4014 host.Build().StartAsync(cts.Token); #pragma warning restore 4014 } var config = new CefConfig(cefDebug) { Args = args, StartUrl = DesktopState.StartUrl = startUrl, Icon = favIcon, CefSettings = { PersistSessionCookies = true, }, CefBrowserSettings = new CefBrowserSettings { DefaultEncoding = "UTF-8", FileAccessFromFileUrls = CefState.Enabled, UniversalAccessFromFileUrls = CefState.Enabled, JavaScriptCloseWindows = CefState.Enabled, JavaScriptAccessClipboard = CefState.Enabled, JavaScriptDomPaste = CefState.Enabled, JavaScript = CefState.Enabled, }, }; if ("name".TryGetAppSetting(out var name)) { config.WindowTitle = name; } if ("CefConfig".TryGetAppSetting(out var cefConfigString)) { var cefConfig = JS.eval(cefConfigString); if (cefConfig is Dictionary <string, object> objDictionary) { objDictionary.PopulateInstance(config); } } if (kiosk) { config.Kiosk = true; } if ("CefConfig.CefSettings".TryGetAppSetting(out var cefSettingsString)) { var cefSettings = JS.eval(cefSettingsString); if (cefSettings is Dictionary <string, object> objDictionary) { objDictionary.PopulateInstance(config.CefSettings); } } void allowCors(ProxyScheme proxyScheme, string origin) { proxyScheme.IgnoreHeaders.AddIfNotExists("Content-Security-Policy"); proxyScheme.AddHeaders[HttpHeaders.AllowMethods] = "GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD"; proxyScheme.AddHeaders[HttpHeaders.AllowHeaders] = "Content-Type"; proxyScheme.AddHeaders[HttpHeaders.AllowCredentials] = "true"; proxyScheme.OnResponseHeaders = headers => headers[HttpHeaders.AllowOrigin] = origin; } var i = 0; while ($"CefConfig.Schemes[{i++}]".TryGetAppSetting(out var proxyConfigString)) { var proxyScheme = new ProxyScheme(); var objDictionary = (Dictionary <string, object>)JS.eval(proxyConfigString); objDictionary.PopulateInstance(proxyScheme); if (proxyScheme.AllowCors) { allowCors(proxyScheme, startUrl); } if (objDictionary.ContainsKey("allowIFrames")) { proxyScheme.IgnoreHeaders.AddIfNotExists("Content-Security-Policy"); proxyScheme.IgnoreHeaders.Add("X-Frame-Options"); } config.Schemes.Add(proxyScheme); } foreach (var proxyConfig in DesktopConfig.Instance.ProxyConfigs) { var proxyScheme = proxyConfig.ConvertTo <ProxyScheme>(); if (proxyScheme.AllowCors) { allowCors(proxyScheme, startUrl); } config.Schemes.Add(proxyScheme); } if (config.EnableCorsScheme) { var corsScheme = CreateCorsProxy(); allowCors(corsScheme, config.StartUrl); config.SchemeFactories.Add( new SchemeFactory("cors", new CefProxySchemeHandlerFactory(corsScheme))); } var appHost = AppLoader.TryGetAppHost(); config.SchemeFactories.Add(appHost != null ? new SchemeFactory("host", new CefAppHostSchemeHandlerFactory(appHost)) : new SchemeFactory("host", new CefProxySchemeHandlerFactory(CreateCorsProxy("host", startUrl)) { RequestFilter = (webReq, request) => { webReq.Headers["X-Window-Handle"] = DesktopState.BrowserHandle.ToString(); webReq.Headers["X-Desktop-Info"] = NativeWin.GetDesktopInfo().ToJsv(); } })); if (DesktopState.AppDebug) { config.HideConsoleWindow = false; config.Verbose = true; } return(CefPlatformWindows.Start(config)); } catch (Exception ex) { DesktopConfig.Instance.OnError?.Invoke(ex); if (DesktopState.AppDebug) { NativeWin.MessageBox(0, ex.Message, "Exception", 0); } ex.HandleProgramExceptions(); return(-1); } finally { CefPlatformWindows.Provider?.ShowConsoleWindow(); } }
protected override void OnExiting(object sender, EventArgs args) { CefConfig.Shutdown(); base.OnExiting(sender, args); }
public static int Main(string[] args) { try { var host = Startup.CreateWebHost("win", args, new WebAppEvents { CreateShortcut = Shortcut.Create, HandleUnknownCommand = ctx => Startup.PrintUsage("win"), OpenBrowser = url => CefPlatformWindows.Start(new CefConfig { StartUrl = url, Width = 1040, DevTools = false, Icon = Startup.ToolFavIcon, HideConsoleWindow = false }), RunNetCoreProcess = ctx => { var url = Environment.GetEnvironmentVariable("ASPNETCORE_URLS")?.LeftPart(';') ?? "http://localhost:5000"; var target = ctx.RunProcess; var fileName = ctx.RunProcess; var arguments = ""; if (target.EndsWith(".dll")) { fileName = "dotnet"; arguments = ctx.RunProcess; } using (var process = new Process { StartInfo = { FileName = fileName, Arguments = arguments, UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true, } }) { process.OutputDataReceived += (sender, data) => { Console.WriteLine(data.Data); }; process.StartInfo.RedirectStandardError = true; process.ErrorDataReceived += (sender, data) => { Console.WriteLine(data.Data); }; process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); CefPlatformWindows.Start(new CefConfig { StartUrl = url, Icon = ctx.FavIcon }); process.Kill(); process.Close(); } } }); if (host == null) { return(0); } host.Build().StartAsync(); var config = new CefConfig(host.DebugMode) { Args = args, StartUrl = host.StartUrl, Icon = host.FavIcon, }; if ("name".TryGetAppSetting(out var name)) { config.WindowTitle = name; } if ("CefConfig".TryGetAppSetting(out var cefConfigString)) { var cefConfig = JS.eval(cefConfigString); if (cefConfig is Dictionary <string, object> objDictionary) { objDictionary.PopulateInstance(config); } } if ("CefConfig.CefSettings".TryGetAppSetting(out var cefSettingsString)) { var cefSettings = JS.eval(cefSettingsString); if (cefSettings is Dictionary <string, object> objDictionary) { objDictionary.PopulateInstance(config.CefSettings); } } return(CefPlatformWindows.Start(config)); } catch (Exception ex) { Console.WriteLine(Startup.Verbose ? ex.ToString() : ex.Message); return(-1); } }
public static async Task <int> Main(string[] args) { try { var host = await Startup.CreateWebHost("app", args, new WebAppEvents { CreateShortcut = Shortcut.Create, HandleUnknownCommand = ctx => Startup.PrintUsage("app"), OpenBrowser = url => CefPlatformWindows.Start(new CefConfig { StartUrl = url, Width = 1040, DevTools = false, Icon = Startup.ToolFavIcon }), RunNetCoreProcess = ctx => { var url = Environment.GetEnvironmentVariable("ASPNETCORE_URLS")?.LeftPart(';') ?? "http://localhost:5000"; var target = ctx.RunProcess; var fileName = ctx.RunProcess; var arguments = ""; if (target.EndsWith(".dll")) { fileName = "dotnet"; arguments = ctx.RunProcess; } Startup.PipeProcess(fileName, arguments, fn: () => CefPlatformWindows.Start(new CefConfig { StartUrl = url, Icon = ctx.FavIcon })); }, SelectFolder = options => { var dlgArgs = new OpenFileName { hwndOwner = Process.GetCurrentProcess().MainWindowHandle, Flags = options.Flags.GetValueOrDefault(), lpstrTitle = options.Title, lpstrFilter = options.Filter, lpstrInitialDir = options.InitialDir, lpstrDefExt = options.DefaultExt, }; if (options.IsFolderPicker) { //HACK http://unafaltadecomprension.blogspot.com/2013/04/browsing-for-files-and-folders-c.html dlgArgs.Flags |= (int)(FileOpenOptions.NoValidate | FileOpenOptions.PathMustExist); dlgArgs.lpstrFile = "Folder Selection."; } if (GetOpenFileName(dlgArgs)) { //var fileName = Marshal.PtrToStringAuto(dlgArgs.lpstrFile); var fileName = dlgArgs.lpstrFile.Replace("Folder Selection", ""); var ret = new DialogResult { FolderPath = fileName, Ok = true, }; return(ret); } return(new DialogResult()); } }); if (host == null) { return(0); } #pragma warning disable 4014 host.Build().StartAsync(); #pragma warning restore 4014 var config = new CefConfig(host.DebugMode) { Args = args, StartUrl = host.StartUrl, Icon = host.FavIcon, }; if ("name".TryGetAppSetting(out var name)) { config.WindowTitle = name; } if ("CefConfig".TryGetAppSetting(out var cefConfigString)) { var cefConfig = JS.eval(cefConfigString); if (cefConfig is Dictionary <string, object> objDictionary) { objDictionary.PopulateInstance(config); } } if ("CefConfig.CefSettings".TryGetAppSetting(out var cefSettingsString)) { var cefSettings = JS.eval(cefSettingsString); if (cefSettings is Dictionary <string, object> objDictionary) { objDictionary.PopulateInstance(config.CefSettings); } } return(CefPlatformWindows.Start(config)); } catch (Exception ex) { ex.HandleProgramExceptions(); return(-1); } }
public WebCefApp(CefConfig context) { this.context = context; }
public WebCefRenderProcessHandler(CefConfig context) => this.context = context;
public WebCefApp(CefConfig context) { this.context = context; this.args = context.Args; }
public App() { this.DispatcherUnhandledException += App_DispatcherUnhandledException; CefConfig.SupportAnyCpu(); }
public App() { CefConfig.SupportAnyCpu(); this.Startup += App_Startup; }