Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(macOS): race conditions around WKURLSchemeTask #1484

Merged
merged 1 commit into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/objc-exceptions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
wry: minor
---

Removed `obj-exception` feature.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ rustc-args = ["--cfg", "docsrs"]
rustdoc-args = ["--cfg", "docsrs"]

[features]
default = ["drag-drop", "objc-exception", "protocol", "os-webview"]
default = ["drag-drop", "protocol", "os-webview"]
serde = ["dpi/serde"]
objc-exception = ["objc2/catch-all"]
drag-drop = []
protocol = []
devtools = []
Expand Down
22 changes: 12 additions & 10 deletions src/wkwebview/class/url_scheme_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use http::{
use objc2::{
rc::Retained,
runtime::{AnyClass, AnyObject, ClassBuilder, ProtocolObject},
AllocAnyThread, ClassType,
AllocAnyThread, ClassType, Message,
};
use objc2_foundation::{
NSData, NSHTTPURLResponse, NSMutableDictionary, NSObject, NSObjectProtocol, NSString, NSURL,
Expand Down Expand Up @@ -54,8 +54,8 @@ pub fn create(name: &str) -> &AnyClass {
extern "C" fn start_task(
this: &AnyObject,
_sel: objc2::runtime::Sel,
webview: &'static WryWebView,
task: &'static ProtocolObject<dyn WKURLSchemeTask>,
webview: &WryWebView,
task: &ProtocolObject<dyn WKURLSchemeTask>,
) {
unsafe {
#[cfg(feature = "tracing")]
Expand Down Expand Up @@ -180,12 +180,14 @@ extern "C" fn start_task(
// send response
match http_request.body(sent_form_body) {
Ok(final_request) => {
let webview = webview.retain();
let task = task.retain();
let responder: Box<dyn FnOnce(HttpResponse<Cow<'static, [u8]>>)> =
Box::new(move |sent_response| {
// Consolidate checks before calling into `did*` methods.
let validate = || -> crate::Result<()> {
check_webview_id_valid(webview_id)?;
check_task_is_valid(webview, task_key, task_uuid.clone())?;
check_task_is_valid(&webview, task_key, task_uuid.clone())?;
Ok(())
};

Expand All @@ -198,9 +200,9 @@ extern "C" fn start_task(

unsafe fn response(
// FIXME: though we give it a static lifetime, it's not guaranteed to be valid.
task: &'static ProtocolObject<dyn WKURLSchemeTask>,
task: Retained<ProtocolObject<dyn WKURLSchemeTask>>,
// FIXME: though we give it a static lifetime, it's not guaranteed to be valid.
webview: &'static WryWebView,
webview: Retained<WryWebView>,
task_key: usize,
task_uuid: Retained<NSUUID>,
webview_id: &str,
Expand All @@ -209,7 +211,7 @@ extern "C" fn start_task(
) -> crate::Result<()> {
// Validate
check_webview_id_valid(webview_id)?;
check_task_is_valid(webview, task_key, task_uuid.clone())?;
check_task_is_valid(&webview, task_key, task_uuid.clone())?;

let content = sent_response.body();
// default: application/octet-stream, but should be provided by the client
Expand Down Expand Up @@ -253,7 +255,7 @@ extern "C" fn start_task(

// Re-validate before calling didReceiveResponse
check_webview_id_valid(webview_id)?;
check_task_is_valid(webview, task_key, task_uuid.clone())?;
check_task_is_valid(&webview, task_key, task_uuid.clone())?;

// Use map_err to convert Option<Retained<Exception>> to crate::Error
objc2::exception::catch(AssertUnwindSafe(|| {
Expand All @@ -273,15 +275,15 @@ extern "C" fn start_task(

// Check validity again
check_webview_id_valid(webview_id)?;
check_task_is_valid(webview, task_key, task_uuid.clone())?;
check_task_is_valid(&webview, task_key, task_uuid.clone())?;

objc2::exception::catch(AssertUnwindSafe(|| {
task.didReceiveData(&data);
}))
.map_err(|_e| crate::Error::CustomProtocolTaskInvalid)?;

check_webview_id_valid(webview_id)?;
check_task_is_valid(webview, task_key, task_uuid.clone())?;
check_task_is_valid(&webview, task_key, task_uuid.clone())?;

objc2::exception::catch(AssertUnwindSafe(|| {
task.didFinish();
Expand Down
13 changes: 8 additions & 5 deletions src/wkwebview/class/wry_web_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use std::{cell::RefCell, collections::HashMap};
use std::{cell::RefCell, collections::HashMap, sync::Mutex};

#[cfg(target_os = "macos")]
use objc2::runtime::ProtocolObject;
Expand All @@ -29,7 +29,7 @@ pub struct WryWebViewIvars {
pub(crate) drag_drop_handler: Box<dyn Fn(DragDropEvent) -> bool>,
#[cfg(target_os = "macos")]
pub(crate) accept_first_mouse: objc2::runtime::Bool,
pub(crate) custom_protocol_task_ids: RefCell<HashMap<usize, Retained<NSUUID>>>,
pub(crate) custom_protocol_task_ids: Mutex<HashMap<usize, Retained<NSUUID>>>,
}

define_class!(
Expand Down Expand Up @@ -117,22 +117,25 @@ impl WryWebView {
self
.ivars()
.custom_protocol_task_ids
.borrow_mut()
.lock()
.unwrap()
.insert(task_id, task_uuid.clone());
task_uuid
}
pub(crate) fn remove_custom_task_key(&self, task_id: usize) {
self
.ivars()
.custom_protocol_task_ids
.borrow_mut()
.lock()
.unwrap()
.remove(&task_id);
}
pub(crate) fn get_custom_task_uuid(&self, task_id: usize) -> Option<Retained<NSUUID>> {
self
.ivars()
.custom_protocol_task_ids
.borrow()
.lock()
.unwrap()
.get(&task_id)
.cloned()
}
Expand Down
Loading