Repository navigation
desktop: fall back to Edge browser folder when WebView2 Runtime is missing - #10
Merged
Merged
Conversation
…ssing
很多企业 / 信创环境的机器只装了 Microsoft Edge 浏览器,而没有单独安装
"Microsoft Edge WebView2 Runtime"。现状下 acecode-desktop 走 WebView2Loader
默认 Runtime 发现路径直接抛 webview::exception,被 wWinMain 一路裸抛触发
Windows "未经处理的异常" 调试器对话框 — 普通用户既看不懂也无从下手。
WebView2Loader 公开了 WEBVIEW2_BROWSER_EXECUTABLE_FOLDER 环境变量(等价于
CreateCoreWebView2EnvironmentWithOptions 的 browserExecutableFolder 参数),
而 Edge 100+ 浏览器目录里都自带同版本的 msedgewebview2.exe,可以直接复用
避免分发 ~180MB 的 Fixed Version Runtime。
新增 src/desktop/webview2_runtime_probe.{hpp,cpp},暴露纯函数
find_edge_browser_folder_in(roots) + Win32 系统调用版
find_edge_browser_folder()。Impl ctor 被改成三段式:默认 Loader → Edge
浏览器目录 fallback → 终态 MessageBox + ExitProcess(1)。wWinMain 同时包顶层
try/catch 兜底任何其他启动期异常,避免再有"无声秒退"。
测试覆盖纯函数版本(版本号字典序陷阱、缺 exe 跳过、跨 root global latest 等)。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
很多企业 / 信创环境的机器只装了 Microsoft Edge 浏览器,而没有单独安装 "Microsoft Edge WebView2 Runtime"(独立 Evergreen Runtime,注册路径
HKLM\...\EdgeUpdate\Clients\{F3017226-...})。这种状态下:webview::webviewctor → 内部CreateCoreWebView2EnvironmentWithOptions找不到 Runtime → 抛webview::exceptionWebHost::Impl仅对"offscreen → nullptr"做了一层 fallback,nullptr 路径再失败时直接裸抛wWinMain没有顶层 try/catch →std::terminate→ Windows 弹"未经处理的异常"调试器对话框普通用户既看不懂也无从下手。同事 / IT 反馈"已经装了 webview2 / Edge 也没用"时,这就是根因。
方案
WebView2Loader公开了WEBVIEW2_BROWSER_EXECUTABLE_FOLDER环境变量(等价于CreateCoreWebView2EnvironmentWithOptions的browserExecutableFolder参数),而 Edge 100+ 浏览器目录里都自带同版本的msedgewebview2.exe+ 配套 dll,可以直接复用 —— 零打包代价(对比 Fixed Version Runtime 要 +180MB)。策略:正常路径优先,失败再 fallback Edge 目录,对装了 Evergreen Runtime 的正常用户零行为变化。
改动
新增
src/desktop/webview2_runtime_probe.{hpp,cpp}— Edge 浏览器目录探测器find_edge_browser_folder_in(roots):纯函数,接受根目录列表 → 扫<root>/Microsoft/Edge/Application/<4 段版本号>/msedgewebview2.exe→ 返回最新版本所在目录(按数值比较,跨 root 全局最大)find_edge_browser_folder():Win32 系统调用版,用SHGetKnownFolderPath拿 ProgramFiles + ProgramFiles(x86) 调上面纯函数;POSIX 上 stub 返回nulloptSetupMetrics/Installer/ 非 4 段版本号目录;跳过版本目录在但msedgewebview2.exe缺失的情况(Edge channel 切换残留)tests/desktop/webview2_runtime_probe_test.cpp— 8 个 GoogleTest 用例,覆盖纯函数:nullopt"99.0.1.1"vs"100.0.1.1")修改
src/desktop/web_host.cpp::WebHost::Impl::Impl— 三段式构造:find_edge_browser_folder()→SetEnvironmentVariableW(WEBVIEW2_BROWSER_EXECUTABLE_FOLDER)→ 重试构造show_webview2_failure_message_box弹中文 MessageBox 给用户可执行的修复指引(装 Runtime / 删 EBWebView / 杀软白名单),LOG_ERROR落盘后ExitProcess(1)src/desktop/main.cpp::wWinMain— IIFE lambda 包整个 body + 顶层try / catch (std::exception&) / catch (...),兜底任何其他启动期未捕获异常,落LOG_ERROR+ 中文 MessageBox +return 1,避免再有"无声秒退"。CMake 集成
零变更:
src/desktop/webview2_runtime_probe.cpp不在ACECODE_DESKTOP_BINARY_ONLY_SOURCES列表 → 自动进acecode_testable→acecode-desktop通过target_link_libraries(acecode-desktop PRIVATE acecode_testable)拿到acecode_testable在 Win32 已 linkshell32(folder_picker 用),SHGetKnownFolderPath不需要额外 linktests/desktop/webview2_runtime_probe_test.cpp由tests/CMakeLists.txt的GLOB_RECURSE *_test.cpp自动拾取验证
webview2_runtime_probe.cppg++ -std=c++17编译通过MessageBoxW调用 /SetEnvironmentVariableWUTF-16 / lambda captureTest plan
acecode-desktop正常通过(-DACECODE_BUILD_DESKTOP=ON)acecode_unit_tests跑Webview2RuntimeProbe.*全绿[desktop] retrying WebView2 with Edge browser folder: ...,窗口正常起来Generated by Claude Code