-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwebview2_runtime_probe.cpp
More file actions
182 lines (151 loc) · 5.84 KB
/
Copy pathwebview2_runtime_probe.cpp
File metadata and controls
182 lines (151 loc) · 5.84 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// WebView2 Runtime 探测实现。设计 + 调用时机见 webview2_runtime_probe.hpp。
#include "webview2_runtime_probe.hpp"
#include "../utils/logger.hpp"
#include "../utils/utf8_path.hpp"
#include <array>
#include <cstdint>
#include <string>
#include <system_error>
#ifdef _WIN32
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# ifndef NOMINMAX
# define NOMINMAX
# endif
# include <windows.h>
# include <shlobj.h>
# include <knownfolders.h>
#endif
namespace fs = std::filesystem;
namespace acecode::desktop {
namespace {
constexpr const char* kEdgeWebViewExecutableName = "msedgewebview2.exe";
// 把版本号字符串("100.0.1234.56")拆 4 段非负整数。失败返回 std::nullopt
// (整段不是 4 位、含非数字字符、任何一段超过 uint32 上限都视为非版本)。
std::optional<std::array<std::uint32_t, 4>> parse_version_4(const std::string& s) {
std::array<std::uint32_t, 4> out{0, 0, 0, 0};
std::size_t segment = 0;
std::uint64_t acc = 0;
bool any_digit_in_segment = false;
for (std::size_t i = 0; i <= s.size(); ++i) {
const bool at_end = (i == s.size());
const char c = at_end ? '.' : s[i];
if (c == '.') {
if (!any_digit_in_segment) return std::nullopt;
if (segment >= 4) return std::nullopt;
if (acc > 0xFFFFFFFFu) return std::nullopt;
out[segment] = static_cast<std::uint32_t>(acc);
++segment;
acc = 0;
any_digit_in_segment = false;
continue;
}
if (c < '0' || c > '9') return std::nullopt;
acc = acc * 10 + static_cast<std::uint64_t>(c - '0');
if (acc > 0xFFFFFFFFu) return std::nullopt;
any_digit_in_segment = true;
}
if (segment != 4) return std::nullopt;
return out;
}
// Edge 浏览器与 Evergreen WebView2 Runtime 的标准布局。Edge Beta/Dev/Canary
// 各有自己的 root,这里仍只扫稳定通道,避免误用快速变化通道。
std::array<fs::path, 2> webview_application_dirs(const fs::path& root) {
return {
root / "Microsoft" / "Edge" / "Application",
root / "Microsoft" / "EdgeWebView" / "Application",
};
}
// 在一个 Application 目录下扫所有合法版本子目录,挑最大版本 + 验证
// msedgewebview2.exe 存在。返回 (version, folder),无候选返回 nullopt。
std::optional<std::pair<std::array<std::uint32_t, 4>, fs::path>>
pick_latest_in_application_dir(const fs::path& application_dir) {
std::error_code ec;
if (!fs::is_directory(application_dir, ec) || ec) {
return std::nullopt;
}
std::optional<std::array<std::uint32_t, 4>> best_version;
fs::path best_folder;
fs::directory_iterator it(application_dir, fs::directory_options::skip_permission_denied, ec);
if (ec) return std::nullopt;
for (const auto& entry : it) {
std::error_code ec_entry;
if (!entry.is_directory(ec_entry) || ec_entry) continue;
const std::string name = acecode::path_to_utf8(entry.path().filename());
auto parsed = parse_version_4(name);
if (!parsed.has_value()) continue;
const fs::path candidate_exe = entry.path() / kEdgeWebViewExecutableName;
std::error_code exe_ec;
if (!fs::is_regular_file(candidate_exe, exe_ec) || exe_ec) {
// Edge 浏览器某些 channel 切换时会保留只剩 Resources 的旧版本号
// 目录,但没了 msedgewebview2.exe — 直接跳过,免得后面 set env
// 指向死路径。
continue;
}
if (!best_version.has_value() || *parsed > *best_version) {
best_version = parsed;
best_folder = entry.path();
}
}
if (!best_version.has_value()) return std::nullopt;
return std::make_pair(*best_version, best_folder);
}
} // namespace
std::optional<fs::path> find_edge_browser_folder_in(
const std::vector<fs::path>& roots) {
std::optional<std::array<std::uint32_t, 4>> best_version;
fs::path best_folder;
for (const auto& root : roots) {
if (root.empty()) continue;
for (const auto& application_dir : webview_application_dirs(root)) {
auto pick = pick_latest_in_application_dir(application_dir);
if (!pick.has_value()) continue;
if (!best_version.has_value() || pick->first > *best_version) {
best_version = pick->first;
best_folder = pick->second;
}
}
}
if (!best_version.has_value()) return std::nullopt;
return best_folder;
}
#ifdef _WIN32
namespace {
// SHGetKnownFolderPath 拿 KNOWNFOLDERID 对应路径。失败返回空 path。COM
// 不需要 init —— SHGetKnownFolderPath 走的是 NTDLL 路径不依赖 apartment。
fs::path known_folder_path(REFKNOWNFOLDERID id) {
PWSTR raw = nullptr;
HRESULT hr = ::SHGetKnownFolderPath(id, KF_FLAG_DEFAULT, nullptr, &raw);
if (FAILED(hr) || !raw) {
if (raw) ::CoTaskMemFree(raw);
return {};
}
fs::path result(raw);
::CoTaskMemFree(raw);
return result;
}
} // namespace
std::optional<fs::path> find_edge_browser_folder() {
std::vector<fs::path> roots;
if (auto pf = known_folder_path(FOLDERID_ProgramFiles); !pf.empty()) {
roots.push_back(std::move(pf));
}
if (auto pfx86 = known_folder_path(FOLDERID_ProgramFilesX86); !pfx86.empty()) {
roots.push_back(std::move(pfx86));
}
if (auto local = known_folder_path(FOLDERID_LocalAppData); !local.empty()) {
roots.push_back(std::move(local));
}
if (roots.empty()) {
LOG_WARN("[webview2_probe] SHGetKnownFolderPath returned no ProgramFiles paths");
return std::nullopt;
}
return find_edge_browser_folder_in(roots);
}
#else // _WIN32
std::optional<fs::path> find_edge_browser_folder() {
return std::nullopt;
}
#endif // _WIN32
} // namespace acecode::desktop