Skip to content

Commit 6aa5854

Browse files
authored
fix(macos): webview_version crash if com.apple.WebKit is not found (#1609)
1 parent cb36e77 commit 6aa5854

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wry": patch
3+
---
4+
5+
Enhance error handling of the `webview_version` function on macOS.

src/wkwebview/mod.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,12 +1260,33 @@ pub fn url_from_webview(webview: &WKWebView) -> Result<String> {
12601260

12611261
pub fn platform_webview_version() -> Result<String> {
12621262
unsafe {
1263-
let bundle = NSBundle::bundleWithIdentifier(&NSString::from_str("com.apple.WebKit")).unwrap();
1264-
let dict = bundle.infoDictionary().unwrap();
1265-
let webkit_version = dict
1266-
.objectForKey(&NSString::from_str("CFBundleVersion"))
1267-
.unwrap();
1268-
let webkit_version = webkit_version.downcast::<NSString>().unwrap();
1263+
let Some(bundle) = NSBundle::bundleWithIdentifier(&NSString::from_str("com.apple.WebKit"))
1264+
else {
1265+
return Err(Error::Io(std::io::Error::new(
1266+
std::io::ErrorKind::Other,
1267+
"failed to locate com.apple.WebKit bundle",
1268+
)));
1269+
};
1270+
let Some(dict) = bundle.infoDictionary() else {
1271+
return Err(Error::Io(std::io::Error::new(
1272+
std::io::ErrorKind::Other,
1273+
"failed to get WebKit info dictionary",
1274+
)));
1275+
};
1276+
1277+
let Some(webkit_version) = dict.objectForKey(&NSString::from_str("CFBundleVersion")) else {
1278+
return Err(Error::Io(std::io::Error::new(
1279+
std::io::ErrorKind::Other,
1280+
"failed to get WebKit version",
1281+
)));
1282+
};
1283+
1284+
let Ok(webkit_version) = webkit_version.downcast::<NSString>() else {
1285+
return Err(Error::Io(std::io::Error::new(
1286+
std::io::ErrorKind::Other,
1287+
"failed to parse WebKit version",
1288+
)));
1289+
};
12691290

12701291
bundle.unload();
12711292
Ok(webkit_version.to_string())

0 commit comments

Comments
 (0)