Skip to main content

qualia_client_core/
prerequisites.rs

1//! Windows desktop runtime prerequisites (WebView2 + VC++ redistributable).
2//!
3//! WebView2: prefer a **Fixed Version** runtime shipped next to the executable
4//! (`WebView2Runtime/` or `WebView2/`). If absent, fall back to the system
5//! Evergreen runtime (registry). Sets `WEBVIEW2_BROWSER_EXECUTABLE_FOLDER` when
6//! a bundled runtime is found.
7//!
8//! VC++ 2015–2022 x64: not redistributable inside QualiaDB — user installs
9//! Microsoft's installer; we detect via registry and re-check after launch.
10
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct PrerequisiteStatus {
13    /// True on Windows when the prerequisite gate should run.
14    pub platform_requires_check: bool,
15    pub webview2_ready: bool,
16    pub webview2_bundled: bool,
17    pub webview2_evergreen: bool,
18    pub vc_redist_ready: bool,
19    pub all_ready: bool,
20    /// Folder containing `msedgewebview2.exe` when bundled; empty if none.
21    pub bundled_webview2_dir: String,
22}
23
24#[cfg(windows)]
25const VC_REDIST_URL: &str = "https://aka.ms/vs/17/release/vc_redist.x64.exe";
26#[cfg(windows)]
27const WEBVIEW2_BOOTSTRAPPER_URL: &str = "https://go.microsoft.com/fwlink/p/?LinkId=2124703";
28
29#[cfg(windows)]
30mod win {
31    use super::{PrerequisiteStatus, VC_REDIST_URL, WEBVIEW2_BOOTSTRAPPER_URL};
32    use std::path::{Path, PathBuf};
33    use winreg::enums::{HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE};
34    use winreg::RegKey;
35
36    const WEBVIEW2_CLIENT_GUID: &str = r"{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}";
37
38    fn exe_dir() -> Option<PathBuf> {
39        std::env::current_exe()
40            .ok()
41            .and_then(|p| p.parent().map(|d| d.to_path_buf()))
42    }
43
44    fn folder_with_msedge(base: &Path) -> Option<PathBuf> {
45        let direct = base.join("msedgewebview2.exe");
46        if direct.is_file() {
47            return Some(base.to_path_buf());
48        }
49        let nested = base.join("x64").join("msedgewebview2.exe");
50        if nested.is_file() {
51            return nested.parent().map(|p| p.to_path_buf());
52        }
53        None
54    }
55
56    fn find_bundled_webview2() -> Option<PathBuf> {
57        let root = exe_dir()?;
58        for name in [
59            "WebView2Runtime",
60            "WebView2",
61            "Microsoft.WebView2.FixedVersionRuntime",
62            "webview2",
63        ] {
64            let candidate = root.join(name);
65            if let Some(found) = folder_with_msedge(&candidate) {
66                return Some(found);
67            }
68        }
69        None
70    }
71
72    fn evergreen_webview2_installed() -> bool {
73        let hives = [HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER];
74        let bases = [
75            r"SOFTWARE\WOW6432Node\Microsoft\EdgeUpdate\Clients",
76            r"SOFTWARE\Microsoft\EdgeUpdate\Clients",
77        ];
78        for hive in hives {
79            for base in bases {
80                let path = format!("{base}\\{WEBVIEW2_CLIENT_GUID}");
81                if let Ok(key) = RegKey::predef(hive).open_subkey(path) {
82                    if key.get_value::<String, _>("pv").is_ok() {
83                        return true;
84                    }
85                }
86            }
87        }
88        false
89    }
90
91    fn vc_redist_x64_installed() -> bool {
92        let paths = [
93            r"SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\x64",
94            r"SOFTWARE\WOW6432Node\Microsoft\VisualStudio\14.0\VC\Runtimes\x64",
95        ];
96        for path in paths {
97            if let Ok(key) = RegKey::predef(HKEY_LOCAL_MACHINE).open_subkey(path) {
98                if key.get_value::<u32, _>("Installed").ok() == Some(1) {
99                    return true;
100                }
101            }
102        }
103        false
104    }
105
106    /// Checks.
107    pub fn check() -> PrerequisiteStatus {
108        let bundled = find_bundled_webview2();
109        let webview2_bundled = bundled.is_some();
110        let webview2_evergreen = evergreen_webview2_installed();
111        let webview2_ready = webview2_bundled || webview2_evergreen;
112        let vc_redist_ready = vc_redist_x64_installed();
113        PrerequisiteStatus {
114            platform_requires_check: true,
115            webview2_ready,
116            webview2_bundled,
117            webview2_evergreen,
118            vc_redist_ready,
119            all_ready: webview2_ready && vc_redist_ready,
120            bundled_webview2_dir: bundled
121                .map(|p| p.to_string_lossy().into_owned())
122                .unwrap_or_default(),
123        }
124    }
125
126    /// Configures the WebView2 runtime.
127    pub fn configure_webview2_runtime() -> bool {
128        let Some(dir) = find_bundled_webview2() else {
129            return evergreen_webview2_installed();
130        };
131        std::env::set_var(
132            "WEBVIEW2_BROWSER_EXECUTABLE_FOLDER",
133            dir.to_string_lossy().as_ref(),
134        );
135        true
136    }
137
138    fn launch_downloaded(path: &Path) -> Result<(), String> {
139        std::process::Command::new("cmd")
140            .args(["/C", "start", "", &path.to_string_lossy()])
141            .spawn()
142            .map_err(|e| format!("Launch installer: {e}"))?;
143        Ok(())
144    }
145
146    async fn download_to_temp(url: &str, file_name: &str) -> Result<PathBuf, String> {
147        let response = reqwest::get(url)
148            .await
149            .map_err(|e| format!("Download failed: {e}"))?;
150        if !response.status().is_success() {
151            return Err(format!("Download HTTP {}", response.status()));
152        }
153        let bytes = response
154            .bytes()
155            .await
156            .map_err(|e| format!("Read body: {e}"))?;
157        let path = std::env::temp_dir().join(file_name);
158        std::fs::write(&path, &bytes).map_err(|e| format!("Write installer: {e}"))?;
159        Ok(path)
160    }
161
162    /// Installs a prerequisite.
163    pub async fn install_prerequisite(kind: &str) -> Result<(), String> {
164        let (url, file_name) = match kind {
165            "vc_redist" => (VC_REDIST_URL, "vc_redist.x64.exe"),
166            "webview2" => (WEBVIEW2_BOOTSTRAPPER_URL, "MicrosoftEdgeWebview2Setup.exe"),
167            _ => return Err(format!("Unknown prerequisite kind: {kind}")),
168        };
169        let path = download_to_temp(url, file_name).await?;
170        launch_downloaded(&path)
171    }
172}
173
174#[cfg(not(windows))]
175mod win {
176    use super::PrerequisiteStatus;
177
178    /// Checks.
179    pub fn check() -> PrerequisiteStatus {
180        PrerequisiteStatus {
181            platform_requires_check: false,
182            webview2_ready: true,
183            webview2_bundled: false,
184            webview2_evergreen: false,
185            vc_redist_ready: true,
186            all_ready: true,
187            bundled_webview2_dir: String::new(),
188        }
189    }
190
191    /// Configures the WebView2 runtime.
192    pub fn configure_webview2_runtime() -> bool {
193        true
194    }
195
196    /// Installs a prerequisite.
197    pub async fn install_prerequisite(_kind: &str) -> Result<(), String> {
198        Ok(())
199    }
200}
201
202/// Checks prerequisites.
203pub fn check_prerequisites() -> PrerequisiteStatus {
204    win::check()
205}
206
207/// Configures the WebView2 runtime.
208pub fn configure_webview2_runtime() -> bool {
209    win::configure_webview2_runtime()
210}
211
212/// Installs a prerequisite.
213pub async fn install_prerequisite(kind: String) -> Result<(), String> {
214    win::install_prerequisite(kind.as_str()).await
215}
216
217#[cfg(test)]
218mod tests {
219    #[allow(unused_imports)]
220    use super::*;
221
222    #[test]
223    fn non_windows_all_ready() {
224        #[cfg(not(windows))]
225        {
226            let s = check_prerequisites();
227            assert!(s.all_ready);
228            assert!(!s.platform_requires_check);
229        }
230    }
231}