Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianLars committed Mar 20, 2024
1 parent 90a5447 commit d1c82e5
Showing 1 changed file with 87 additions and 2 deletions.
89 changes: 87 additions & 2 deletions src/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,12 +795,69 @@ impl InnerWebView {
decision_handler: id,
) {
unsafe {
println!("request_media_capture_permission");
let ty: NSInteger = msg_send![_type, integerValue];
dbg!(ty);

let decision_handler = decision_handler as *mut block::Block<(NSInteger,), c_void>;
//https://developer.apple.com/documentation/webkit/wkpermissiondecision?language=objc
(*decision_handler).call((0,)); // should be 1 for mic/cam
}
}

extern "C" fn request_user_media_authorization(
_this: &Object,
_: Sel,
_webview: id,
_devices: id,
_url: id,
_main_frame_url: id,
decision_handler: id,
) {
unsafe {
println!("request_user_media_authorization");

let decision_handler = decision_handler as *mut block::Block<(BOOL,), c_void>;
//https://developer.apple.com/documentation/webkit/wkpermissiondecision?language=objc
(*decision_handler).call((BOOL::from(true),));
}
}

extern "C" fn request_display_capture_permission(
_this: &Object,
_: Sel,
_webview: id,
_origin: id,
_frame: id,
_type: id,
decision_handler: id,
) {
unsafe {
println!("request_display_capture_permission");

let decision_handler = decision_handler as *mut block::Block<(NSInteger,), c_void>;
//https://developer.apple.com/documentation/webkit/wkpermissiondecision?language=objc
(*decision_handler).call((1,));
}
}

extern "C" fn query_permission_for_name(
_this: &Object,
_: Sel,
_webview: id,
_name: id,
_origin: id,
handler: id,
) {
unsafe {
println!("query_permission_for_name");

let handler = handler as *mut block::Block<(NSInteger,), c_void>;
//https://developer.apple.com/documentation/webkit/wkpermissiondecision?language=objc
(*handler).call((1,)); // should be 1 for mic/cam
}
}

let ui_delegate = match ClassDecl::new("WebViewUIDelegate", class!(NSObject)) {
Some(mut ctl) => {
ctl.add_method(
Expand All @@ -809,16 +866,44 @@ impl InnerWebView {
);

#[cfg(target_os = "macos")]
// TODO: remove this later
#[allow(clippy::match_single_binding)]
match operating_system_version() {
(14, 0, _) => {
// TODO: Test on older versions
/* (14, 0, _) => {
// Do not suppress media request on 14.0:
// https://github.com/tauri-apps/wry/issues/1101
}
} */
_ => {
dbg!("register");

// This probably isn't working but we somehow need an equivalent to:
// @protocol WKUIDelegatePrivate <NSObject>
let p = objc::runtime::Protocol::get("WKUIDelegatePrivate");
ctl.add_protocol(p.unwrap());

ctl.add_method(
sel!(webView:requestMediaCapturePermissionForOrigin:initiatedByFrame:type:decisionHandler:),
request_media_capture_permission as extern "C" fn(&Object, Sel, id, id, id, id, id),
);

// I tried webView _webView and webView_ and none of them worked but this is probably not the root cause
// Also, they seem to do _something_ because i have to comment out all of them (and requestMediaCapturePermission above) for getDisplayMedia to work.
// If i leave requestMediaCapturePermission commented out but add those 3 methods below it does seem to do something because
// https://webrtc.github.io/samples/src/content/getusermedia/getdisplaymedia/ stops printing NotAllowed.
ctl
.add_method(sel!(_webView:requestUserMediaAuthorizationForDevices:url:mainFrameURL:decisionHandler:),
request_user_media_authorization as extern "C" fn(&Object, Sel, id, id, id, id, id));

ctl.add_method(
sel!(_webView:requestDisplayCapturePermissionForSecurityOrigin:initiatedByFrame:withSystemAudio:decisionHandler:),
request_display_capture_permission as extern "C" fn(&Object, Sel, id, id, id, id, id),
);

ctl.add_method(
sel!(_webView:queryPermissionName:forOrigin:completionHandler:),
query_permission_for_name as extern "C" fn(&Object, Sel, id, id, id, id),
);
}
}

Expand Down

0 comments on commit d1c82e5

Please sign in to comment.