Dreamine.Hybrid.Wpf 1.0.3
`AddDreamineHybridWpf()` 내부에서 `AddWpfBlazorWebView()` 호출 및
로딩중...
검색중...
일치하는것 없음
WebView2Initializer.cs
이 파일의 문서화 페이지로 가기
1// \file WebView2Initializer.cs
2// WebView2 초기화/캐시 경로/진단 유틸리티.
3// \details 다국어·특수문자 경로 문제를 회피하기 위해 ASCII 전용 LocalAppData 하위 경로를 사용.
4// \author Dreamine
5// \version 1.0.0
6
7using Microsoft.Web.WebView2.Core;
8using Microsoft.Web.WebView2.Wpf;
9using System;
10using System.Diagnostics;
11using System.IO;
12using System.Security.Principal;
13using System.Threading.Tasks;
14
16{
25 internal static class WebView2Initializer
26 {
35 private const string LowResourceModeEnvironmentVariable = "DREAMINE_WEBVIEW2_LOW_RESOURCE_MODE";
36
53 public static string GetSafeUserDataFolder()
54 {
55 var basePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
56 var processName = Process.GetCurrentProcess().ProcessName;
57 var integrity = IsAdministrator() ? "Admin" : "User";
58 var path = Path.Combine(basePath, "Dreamine", "WebView2Cache", processName, integrity);
59 Directory.CreateDirectory(path);
60 return path;
61 }
62
79 private static string GetAdditionalBrowserArguments()
80 {
81 var lowResourceMode = Environment.GetEnvironmentVariable(LowResourceModeEnvironmentVariable);
82 if (string.Equals(lowResourceMode, "0", StringComparison.OrdinalIgnoreCase) ||
83 string.Equals(lowResourceMode, "false", StringComparison.OrdinalIgnoreCase))
84 {
85 return string.Empty;
86 }
87
88 return string.Join(" ", new[]
89 {
90 "--disable-gpu",
91 "--disable-gpu-compositing",
92 "--disable-accelerated-2d-canvas",
93 "--disable-accelerated-video-decode",
94 "--disable-smooth-scrolling",
95 "--disable-features=CalculateNativeWinOcclusion,msWebOOUI,msPdfOOUI"
96 });
97 }
98
115 private static bool IsAdministrator()
116 {
117 try
118 {
119 using WindowsIdentity identity = WindowsIdentity.GetCurrent();
120 return new WindowsPrincipal(identity).IsInRole(WindowsBuiltInRole.Administrator);
121 }
122 catch
123 {
124 return false;
125 }
126 }
127
144 public static WebView2 CreateConfiguredWebView2()
145 {
146 var cachePath = GetSafeUserDataFolder();
147 var browserArguments = GetAdditionalBrowserArguments();
148 Debug.WriteLine($"[WebView2.CachePath] {cachePath}");
149 Debug.WriteLine($"[WebView2.Args] {(string.IsNullOrWhiteSpace(browserArguments) ? "(none)" : browserArguments)}");
150
151 var webView = new WebView2
152 {
153 CreationProperties = new CoreWebView2CreationProperties
154 {
155 UserDataFolder = cachePath,
156 AdditionalBrowserArguments = browserArguments
157 }
158 };
159
160 webView.CoreWebView2InitializationCompleted += (s, a) =>
161 {
162 if (!a.IsSuccess)
163 Debug.WriteLine($"[WebView2.InitFailed] {a.InitializationException}");
164 else
165 Debug.WriteLine($"[WebView2.InitOK] {webView.CoreWebView2?.Environment?.BrowserVersionString}");
166 };
167
168 webView.NavigationStarting += (s, e) => Debug.WriteLine($"[WebView2.NavStarting] {e.Uri}");
169 webView.NavigationCompleted += (s, e) =>
170 {
171 if (!e.IsSuccess)
172 Debug.WriteLine($"[WebView2.NavFailed] {e.WebErrorStatus}");
173 else
174 Debug.WriteLine("[WebView2.NavOK]");
175 };
176
177 return webView;
178 }
179
220 public static async Task ShowOfflineMessageAsync(WebView2 webView, string url)
221 {
222 try
223 {
224 await webView.EnsureCoreWebView2Async();
225 webView.NavigateToString($@"
226<!doctype html>
227<html>
228<head><meta charset='utf-8'><title>Server Offline</title></head>
229<body style='font-family:Segoe UI; background:#222; color:#ddd; padding:24px;'>
230 <h2>Blazor Server에 연결할 수 없습니다.</h2>
231 <p>대상: <b>{System.Net.WebUtility.HtmlEncode(url)}</b></p>
232 <ul>
233 <li>Kestrel이 지정 포트에서 기동 중인지 확인(UseUrls)</li>
234 <li>다른 프로세스가 포트를 점유하지 않는지 확인</li>
235 <li>로컬 방화벽/보안 솔루션 차단 해제</li>
236 </ul>
237</body>
238</html>");
239 }
240 catch (Exception ex)
241 {
242 Debug.WriteLine($"[WebView2.OfflineMessageFailed] {ex}");
243 }
244 }
245 }
246}