Skip to content

Commit d786109

Browse files
authored
refactor(wkwebview): add OS API call which requires higher than minimum version support (#1679)
1 parent 40a7032 commit d786109

2 files changed

Lines changed: 56 additions & 10 deletions

File tree

src/wkwebview/mod.rs

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,10 @@ impl InnerWebView {
236236
// if data_store_identifier is given and custom data stores are available, use custom store
237237
(false, true, Some(data_store)) => {
238238
let identifier = NSUUID::from_bytes(data_store);
239+
// <https://developer.apple.com/documentation/webkit/wkwebsitedatastore/init(foridentifier:)>
240+
// Available: macOS 14+, iOS 17+
239241
WKWebsiteDataStore::dataStoreForIdentifier(&identifier, mtm)
240242
}
241-
// default data store
242243
_ => WKWebsiteDataStore::defaultDataStore(mtm),
243244
};
244245
config.setWebsiteDataStore(&data_store);
@@ -248,6 +249,8 @@ impl InnerWebView {
248249
// Register Custom Protocols
249250
let mut protocol_ptrs = Vec::new();
250251
for (name, function) in attributes.custom_protocols {
252+
// <https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/urlschemehandler(forurlscheme:)>
253+
// Available: macOS 10.13+, iOS 11+
251254
let already_registered = using_existing_config
252255
&& config
253256
.urlSchemeHandlerForURLScheme(&NSString::from_str(&name))
@@ -276,6 +279,8 @@ impl InnerWebView {
276279
*ivar_delegate = CString::new(webview_id.as_bytes()).unwrap().into_raw();
277280

278281
let set_result = objc2::exception::catch(AssertUnwindSafe(|| {
282+
// <https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/seturlschemehandler(_:forurlscheme:)>
283+
// Available: macOS 10.13+, iOS 11+
279284
config.setURLSchemeHandler_forURLScheme(
280285
Some(&*(handler.cast::<ProtocolObject<dyn WKURLSchemeHandler>>())),
281286
&NSString::from_str(&name),
@@ -312,6 +317,8 @@ impl InnerWebView {
312317

313318
#[cfg(target_os = "ios")]
314319
{
320+
// <https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/limitsnavigationstoappbounddomains>
321+
// Available: macOS 11+, iOS 14+
315322
if pl_attrs.limit_navigations_to_app_bound_domains && operating_system_version().0 >= 14 {
316323
config.setLimitsNavigationsToAppBoundDomains(true);
317324
}
@@ -333,45 +340,51 @@ impl InnerWebView {
333340
data_store.setValue_forKey(Some(&proxies), ns_string!("proxyConfigurations"));
334341
}
335342

343+
// NOTE: Private API — `allowsPictureInPictureMediaPlayback` is a private KVC key on WKPreferences.
336344
_preference.setValue_forKey(
337345
Some(&_yes),
338346
ns_string!("allowsPictureInPictureMediaPlayback"),
339347
);
340348

341349
if attributes.javascript_disabled {
350+
// <https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/defaultwebpagepreferences>
351+
// Available: macOS 10.15+, iOS 13+
342352
let web_page_preferences = config.defaultWebpagePreferences();
353+
// <https://developer.apple.com/documentation/webkit/wkwebpagepreferences/allowscontentjavascript>
354+
// Available: macOS 10.15+, iOS 13+
343355
web_page_preferences.setAllowsContentJavaScript(false);
344356
}
345357

346358
#[cfg(target_os = "ios")]
347359
config.setValue_forKey(Some(&_yes), ns_string!("allowsInlineMediaPlayback"));
348360

349361
if attributes.autoplay {
362+
// <https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/mediatypesrequiringuseractionforplayback>
363+
// Available: macOS 10.12+, iOS 10+
350364
config.setMediaTypesRequiringUserActionForPlayback(WKAudiovisualMediaTypes::None);
351365
}
352366

353367
#[cfg(feature = "transparent")]
354368
if attributes.transparent || attributes.background_color.is_some() {
355369
let no = NSNumber::numberWithBool(false);
356-
// Equivalent Obj-C:
357-
// drawsBackground is only available on macOS 10.14+
358370
#[cfg(target_os = "macos")]
359371
{
360372
let version = util::operating_system_version();
361373
if version.0 > 10 || (version.0 == 10 && version.1 >= 14) {
362-
// Equivalent Obj-C:
374+
// NOTE: Private API — `drawsBackground`.
375+
// Available: macOS 10.14+ (no public doc).
363376
config.setValue_forKey(Some(&no), ns_string!("drawsBackground"));
364377
}
365378
}
366379
#[cfg(target_os = "ios")]
367380
{
368-
// Equivalent Obj-C:
381+
// NOTE: Private API — `drawsBackground`.
369382
config.setValue_forKey(Some(&no), ns_string!("drawsBackground"));
370383
}
371384
}
372385

373386
#[cfg(feature = "fullscreen")]
374-
// Equivalent Obj-C:
387+
// NOTE: Private API — `fullScreenEnabled` is a private KVC key on WKPreferences.
375388
_preference.setValue_forKey(Some(&_yes), ns_string!("fullScreenEnabled"));
376389

377390
#[cfg(target_os = "macos")]
@@ -423,6 +436,8 @@ impl InnerWebView {
423436
blue as f64 / 255.0,
424437
alpha as f64 / 255.0,
425438
);
439+
// <https://developer.apple.com/documentation/webkit/wkwebview/underpagebackgroundcolor>
440+
// Available: macOS 12+, iOS 15+
426441
webview.setUnderPageBackgroundColor(Some(&color));
427442
}
428443
}
@@ -470,8 +485,9 @@ impl InnerWebView {
470485
BackgroundThrottlingPolicy::Throttle => WKInactiveSchedulingPolicy::Throttle.0,
471486
};
472487

473-
// Convert and set the value
474488
if let Ok(policy_number) = policy_value.try_into() {
489+
// <https://developer.apple.com/documentation/webkit/wkpreferences/inactiveschedulingpolicy>
490+
// Available: macOS 14+, iOS 17+
475491
_preference.setValue_forKey(
476492
Some(&NSNumber::numberWithInt(policy_number)),
477493
ns_string!("inactiveSchedulingPolicy"),
@@ -494,10 +510,10 @@ impl InnerWebView {
494510
);
495511
}
496512

497-
// allowsBackForwardNavigation
498513
webview.setAllowsBackForwardNavigationGestures(attributes.back_forward_navigation_gestures);
499514

500-
// tabFocusesLinks
515+
// <https://developer.apple.com/documentation/webkit/wkpreferences/tabfocuseslinks>
516+
// Available: macOS 12+
501517
_preference.setValue_forKey(Some(&_yes), ns_string!("tabFocusesLinks"));
502518
}
503519
#[cfg(target_os = "ios")]
@@ -520,11 +536,14 @@ impl InnerWebView {
520536

521537
#[cfg(any(debug_assertions, feature = "devtools"))]
522538
if attributes.devtools {
539+
// <https://developer.apple.com/documentation/webkit/wkwebview/isinspectable>
540+
// Available: macOS 13.3+, iOS 16.4+
523541
let has_inspectable_property: bool =
524542
NSObject::respondsToSelector(&webview, objc2::sel!(setInspectable:));
525543
if has_inspectable_property {
526544
webview.setInspectable(true);
527545
}
546+
// NOTE: Private API — `developerExtrasEnabled` is a private KVC key on WKPreferences.
528547
// this cannot be on an `else` statement, it does not work on macOS :(
529548
let dev = ns_string!("developerExtrasEnabled");
530549
_preference.setValue_forKey(Some(&_yes), dev);
@@ -586,6 +605,8 @@ impl InnerWebView {
586605
#[cfg(target_os = "macos")]
587606
{
588607
let ns_window = ns_view.window().unwrap();
608+
// <https://developer.apple.com/documentation/appkit/nswindow/titlebarseparatorstyle>
609+
// Available: macOS 11+
589610
let can_set_titlebar_style =
590611
ns_window.respondsToSelector(objc2::sel!(setTitlebarSeparatorStyle:));
591612
if can_set_titlebar_style {
@@ -670,6 +691,8 @@ r#"Object.defineProperty(window, 'ipc', {
670691
// make sure the window is always on top when we create a new webview
671692
let app = NSApplication::sharedApplication(mtm);
672693
if os_major_version >= 14 {
694+
// <https://developer.apple.com/documentation/appkit/nsapplication/activate()>
695+
// Available: macOS 14+
673696
NSApplication::activate(&app);
674697
} else {
675698
#[allow(deprecated)]
@@ -840,6 +863,8 @@ r#"Object.defineProperty(window, 'ipc', {
840863
// Safety: objc runtime calls are unsafe
841864
#[cfg(target_os = "macos")]
842865
unsafe {
866+
// <https://developer.apple.com/documentation/webkit/wkwebview/printoperation(with:)>
867+
// Available: macOS 11+
843868
let can_print = self
844869
.webview
845870
.respondsToSelector(objc2::sel!(printOperationWithPrintInfo:));
@@ -907,6 +932,8 @@ r#"Object.defineProperty(window, 'ipc', {
907932

908933
pub fn zoom(&self, scale_factor: f64) -> crate::Result<()> {
909934
unsafe {
935+
// <https://developer.apple.com/documentation/webkit/wkwebview/pagezoom>
936+
// Available: macOS 11+, iOS 14+
910937
self.webview.setPageZoom(scale_factor);
911938
}
912939

@@ -939,6 +966,7 @@ r#"Object.defineProperty(window, 'ipc', {
939966

940967
// Disable the default white background using the same drawsBackground KVC key
941968
// as the `transparent` feature. On the webview instance (vs config) for runtime changes.
969+
// NOTE: Private API — `drawsBackground` is a private KVC key on WKWebView instance.
942970
let no = NSNumber::numberWithBool(false);
943971
self
944972
.webview
@@ -952,6 +980,8 @@ r#"Object.defineProperty(window, 'ipc', {
952980
blue as f64 / 255.0,
953981
alpha as f64 / 255.0,
954982
);
983+
// <https://developer.apple.com/documentation/webkit/wkwebview/underpagebackgroundcolor>
984+
// Available: macOS 12+, iOS 15+
955985
self.webview.setUnderPageBackgroundColor(Some(&color));
956986
}
957987
}
@@ -1046,6 +1076,8 @@ r#"Object.defineProperty(window, 'ipc', {
10461076
// Using string comparison because of https://github.com/tauri-apps/wry/issues/1616
10471077
let (major, minor, _) = util::operating_system_version();
10481078
if major > 10 || (major == 10 && minor >= 15) {
1079+
// <https://developer.apple.com/documentation/foundation/httpcookie/samesitepolicy>
1080+
// Available: macOS 10.15+, iOS 13+
10491081
let same_site = cookie.sameSitePolicy();
10501082
let same_site = match same_site {
10511083
Some(policy) if policy.to_string() == "lax" => cookie::SameSite::Lax,
@@ -1170,6 +1202,9 @@ r#"Object.defineProperty(window, 'ipc', {
11701202
let (tx, rx) = std::sync::mpsc::channel();
11711203

11721204
unsafe {
1205+
// <https://developer.apple.com/documentation/webkit/wkwebsitedatastore/httpcookiestore>
1206+
// <https://developer.apple.com/documentation/webkit/wkhttpcookiestore/getallcookies(_:)>
1207+
// Available: macOS 10.13+, iOS 11+
11731208
self
11741209
.data_store
11751210
.httpCookieStore()
@@ -1194,6 +1229,9 @@ r#"Object.defineProperty(window, 'ipc', {
11941229

11951230
unsafe {
11961231
let wkwebview_cookie = Self::cookie_into_wkwebview(cookie);
1232+
// <https://developer.apple.com/documentation/webkit/wkwebsitedatastore/httpcookiestore>
1233+
// <https://developer.apple.com/documentation/webkit/wkhttpcookiestore/setcookie(_:completionhandler:)>
1234+
// Available: macOS 10.13+, iOS 11+
11971235
self
11981236
.data_store
11991237
.httpCookieStore()
@@ -1212,6 +1250,9 @@ r#"Object.defineProperty(window, 'ipc', {
12121250

12131251
unsafe {
12141252
let wkwebview_cookie = Self::cookie_into_wkwebview(cookie);
1253+
// <https://developer.apple.com/documentation/webkit/wkwebsitedatastore/httpcookiestore>
1254+
// <https://developer.apple.com/documentation/webkit/wkhttpcookiestore/deletecookie(_:completionhandler:)>
1255+
// Available: macOS 10.13+, iOS 11+
12151256
self
12161257
.data_store
12171258
.httpCookieStore()
@@ -1265,6 +1306,8 @@ r#"Object.defineProperty(window, 'ipc', {
12651306

12661307
match MainThreadMarker::new() {
12671308
Some(mtn) => unsafe {
1309+
// <https://developer.apple.com/documentation/webkit/wkwebsitedatastore/fetchalldatastoreidentifiers(_:)>
1310+
// Available: macOS 14+, iOS 17+
12681311
WKWebsiteDataStore::fetchAllDataStoreIdentifiers(&block, mtn);
12691312
Ok(())
12701313
},
@@ -1295,6 +1338,8 @@ r#"Object.defineProperty(window, 'ipc', {
12951338
});
12961339

12971340
unsafe {
1341+
// <https://developer.apple.com/documentation/webkit/wkwebsitedatastore/remove(foridentifier:completionhandler:)>
1342+
// Available: macOS 14+, iOS 17+
12981343
WKWebsiteDataStore::removeDataStoreForIdentifier_completionHandler(&identifier, &block, mtm);
12991344
}
13001345
}

src/wkwebview/navigation.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ pub(crate) fn navigation_policy(
5454
handler: &block2::Block<dyn Fn(WKNavigationActionPolicy)>,
5555
) {
5656
unsafe {
57-
// shouldPerformDownload is only available on macOS 11.3+
57+
// <https://developer.apple.com/documentation/webkit/wknavigationaction/shouldperformdownload>
58+
// Available: macOS 11.3+, iOS 14.5+
5859
let can_download = action.respondsToSelector(objc2::sel!(shouldPerformDownload));
5960
let should_download: bool = if can_download {
6061
action.shouldPerformDownload()

0 commit comments

Comments
 (0)