-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathWebViewLoader.cs
More file actions
84 lines (68 loc) · 2.98 KB
/
WebViewLoader.cs
File metadata and controls
84 lines (68 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using Xilium.CefGlue;
using Xilium.CefGlue.Common;
using Xilium.CefGlue.Common.Shared;
namespace WebViewControl {
internal static class WebViewLoader {
private static string[] CustomSchemes { get; } = new[] {
ResourceUrl.LocalScheme,
ResourceUrl.EmbeddedScheme,
ResourceUrl.CustomScheme,
Uri.UriSchemeHttp,
Uri.UriSchemeHttps
};
private static GlobalSettings globalSettings;
[MethodImpl(MethodImplOptions.NoInlining)]
public static void Initialize(GlobalSettings settings) {
if (CefRuntimeLoader.IsLoaded) {
return;
}
globalSettings = settings;
var cefSettings = new CefSettings {
LogSeverity = string.IsNullOrWhiteSpace(settings.LogFile) ? CefLogSeverity.Disable : (settings.EnableErrorLogOnly ? CefLogSeverity.Error : CefLogSeverity.Verbose),
LogFile = settings.LogFile,
UncaughtExceptionStackSize = 100, // enable stack capture
CachePath = settings.CachePath, // enable cache for external resources to speedup loading
WindowlessRenderingEnabled = settings.OsrEnabled,
RemoteDebuggingPort = settings.GetRemoteDebuggingPort(),
UserAgent = settings.UserAgent,
BackgroundColor = new CefColor((uint)settings.BackgroundColor.ToArgb())
};
var customSchemes = CustomSchemes.Select(s => new CustomScheme() {
SchemeName = s,
SchemeHandlerFactory = new SchemeHandlerFactory()
}).ToArray();
settings.AddCommandLineSwitch("enable-experimental-web-platform-features", null);
if (settings.EnableVideoAutoplay) {
settings.AddCommandLineSwitch("autoplay-policy", "no-user-gesture-required");
}
CefRuntimeLoader.Initialize(settings: cefSettings, flags: settings.CommandLineSwitches.ToArray(), customSchemes: customSchemes);
AppDomain.CurrentDomain.ProcessExit += delegate { Cleanup(); };
}
/// <summary>
/// Release all resources and shutdown web view
/// </summary>
[DebuggerNonUserCode]
public static void Cleanup() {
CefRuntime.Shutdown(); // must shutdown cef to free cache files (so that cleanup is able to delete files)
if (globalSettings.PersistCache) {
return;
}
try {
var dirInfo = new DirectoryInfo(globalSettings.CachePath);
if (dirInfo.Exists) {
dirInfo.Delete(true);
}
} catch (UnauthorizedAccessException) {
// ignore
} catch (IOException) {
// ignore
}
}
}
}