Skip to content

Commit

Permalink
chore(deps): Use objc2 v0.6 (#1468)
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm authored Jan 26, 2025
1 parent a92baaa commit c27b4ff
Show file tree
Hide file tree
Showing 14 changed files with 360 additions and 412 deletions.
7 changes: 7 additions & 0 deletions .changes/objc2-v6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
wry: patch
---

Update to `objc2` v0.6.

This bumps MSRV on macOS/iOS to 1.71.
31 changes: 22 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,13 @@ features = [
"Win32_UI_Input_KeyboardAndMouse",
]

[target."cfg(any(target_os = \"ios\", target_os = \"macos\"))".dependencies]
[target.'cfg(target_vendor = "apple")'.dependencies]
url = "2.5"
block2 = "0.5"
objc2 = { version = "0.5", features = ["exception"] }
objc2-web-kit = { version = "0.2.0", features = [
block2 = "0.6"
objc2 = { version = "0.6", features = ["exception"] }
objc2-web-kit = { version = "0.3.0", default-features = false, features = [
"std",
"objc2-core-foundation",
"objc2-app-kit",
"block2",
"WKWebView",
Expand All @@ -122,7 +124,13 @@ objc2-web-kit = { version = "0.2.0", features = [
"WKUserScript",
"WKHTTPCookieStore",
] }
objc2-foundation = { version = "0.2.0", features = [
objc2-core-foundation = { version = "0.3.0", default-features = false, features = [
"std",
"CFCGTypes",
] }
objc2-foundation = { version = "0.3.0", default-features = false, features = [
"std",
"objc2-core-foundation",
"NSURLRequest",
"NSURL",
"NSString",
Expand All @@ -131,6 +139,7 @@ objc2-foundation = { version = "0.2.0", features = [
"NSDictionary",
"NSObject",
"NSData",
"NSEnumerator",
"NSKeyValueObserving",
"NSThread",
"NSJSONSerialization",
Expand All @@ -142,8 +151,10 @@ objc2-foundation = { version = "0.2.0", features = [
"NSRunLoop",
] }

[target."cfg(target_os = \"ios\")".dependencies]
objc2-ui-kit = { version = "0.2.2", features = [
[target.'cfg(target_os = "ios")'.dependencies]
objc2-ui-kit = { version = "0.3.0", default-features = false, features = [
"std",
"objc2-core-foundation",
"UIResponder",
"UIScrollView",
"UIView",
Expand All @@ -152,8 +163,10 @@ objc2-ui-kit = { version = "0.2.2", features = [
"UIEvent",
] }

[target."cfg(target_os = \"macos\")".dependencies]
objc2-app-kit = { version = "0.2.0", features = [
[target.'cfg(target_os = "macos")'.dependencies]
objc2-app-kit = { version = "0.3.0", default-features = false, features = [
"std",
"objc2-core-foundation",
"NSApplication",
"NSButton",
"NSControl",
Expand Down
33 changes: 13 additions & 20 deletions src/wkwebview/class/document_title_changed_observer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
use std::{ffi::c_void, ptr::null_mut};

use objc2::{
declare_class, msg_send, msg_send_id,
mutability::InteriorMutable,
define_class, msg_send,
rc::Retained,
runtime::{AnyObject, NSObject},
ClassType, DeclaredClass,
AllocAnyThread, DefinedClass,
};
use objc2_foundation::{
NSDictionary, NSKeyValueChangeKey, NSKeyValueObservingOptions,
Expand All @@ -22,21 +21,15 @@ pub struct DocumentTitleChangedObserverIvars {
pub handler: Box<dyn Fn(String)>,
}

declare_class!(
define_class!(
#[unsafe(super(NSObject))]
#[name = "DocumentTitleChangedObserver"]
#[ivars = DocumentTitleChangedObserverIvars]
pub struct DocumentTitleChangedObserver;

unsafe impl ClassType for DocumentTitleChangedObserver {
type Super = NSObject;
type Mutability = InteriorMutable;
const NAME: &'static str = "DocumentTitleChangedObserver";
}

impl DeclaredClass for DocumentTitleChangedObserver {
type Ivars = DocumentTitleChangedObserverIvars;
}

unsafe impl DocumentTitleChangedObserver {
#[method(observeValueForKeyPath:ofObject:change:context:)]
/// NSKeyValueObserving.
impl DocumentTitleChangedObserver {
#[unsafe(method(observeValueForKeyPath:ofObject:change:context:))]
fn observe_value_for_key_path(
&self,
key_path: Option<&NSString>,
Expand All @@ -49,8 +42,8 @@ declare_class!(
unsafe {
let handler = &self.ivars().handler;
// if !handler.is_null() {
let title: *const NSString = msg_send![object, title];
handler((*title).to_string());
let title: *const NSString = msg_send![object, title];
handler((*title).to_string());
// }
}
}
Expand All @@ -68,7 +61,7 @@ impl DocumentTitleChangedObserver {
handler,
});

let observer: Retained<Self> = unsafe { msg_send_id![super(observer), init] };
let observer: Retained<Self> = unsafe { msg_send![super(observer), init] };

unsafe {
observer
Expand All @@ -77,7 +70,7 @@ impl DocumentTitleChangedObserver {
.addObserver_forKeyPath_options_context(
&observer,
&NSString::from_str("title"),
NSKeyValueObservingOptions::NSKeyValueObservingOptionNew,
NSKeyValueObservingOptions::New,
null_mut(),
);
}
Expand Down
52 changes: 28 additions & 24 deletions src/wkwebview/class/url_scheme_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::{
ffi::{c_char, c_void, CStr},
panic::AssertUnwindSafe,
ptr::NonNull,
slice,
};

use http::{
Expand All @@ -17,7 +16,7 @@ use http::{
use objc2::{
rc::Retained,
runtime::{AnyClass, AnyObject, ClassBuilder, ProtocolObject},
ClassType,
AllocAnyThread, ClassType,
};
use objc2_foundation::{
NSData, NSHTTPURLResponse, NSMutableDictionary, NSObject, NSObjectProtocol, NSString, NSURL,
Expand All @@ -29,12 +28,13 @@ use crate::{wkwebview::WEBVIEW_IDS, RequestAsyncResponder, WryWebView};

pub fn create(name: &str) -> &AnyClass {
unsafe {
let scheme_name = format!("{}URLSchemeHandler", name);
let scheme_name = format!("{}URLSchemeHandler\0", name);
let scheme_name = CStr::from_bytes_with_nul(scheme_name.as_bytes()).unwrap();
let cls = ClassBuilder::new(&scheme_name, NSObject::class());
match cls {
Some(mut cls) => {
cls.add_ivar::<*mut c_void>("function");
cls.add_ivar::<*mut c_char>("webview_id");
cls.add_ivar::<*mut c_void>(CStr::from_bytes_with_nul(b"function\0").unwrap());
cls.add_ivar::<*mut c_char>(CStr::from_bytes_with_nul(b"webview_id\0").unwrap());
cls.add_method(
objc2::sel!(webView:startURLSchemeTask:),
start_task as extern "C" fn(_, _, _, _),
Expand All @@ -54,7 +54,7 @@ pub fn create(name: &str) -> &AnyClass {
extern "C" fn start_task(
this: &AnyObject,
_sel: objc2::runtime::Sel,
webview: &'static mut WryWebView,
webview: &'static WryWebView,
task: &'static ProtocolObject<dyn WKURLSchemeTask>,
) {
unsafe {
Expand All @@ -65,14 +65,20 @@ extern "C" fn start_task(
let task_key = task.hash(); // hash by task object address
let task_uuid = webview.add_custom_task_key(task_key);

let ivar = this.class().instance_variable("webview_id").unwrap();
let ivar = this
.class()
.instance_variable(CStr::from_bytes_with_nul(b"webview_id\0").unwrap())
.unwrap();
let webview_id_ptr: *mut c_char = *ivar.load(this);
let webview_id = CStr::from_ptr(webview_id_ptr)
.to_str()
.ok()
.unwrap_or_default();

let ivar = this.class().instance_variable("function").unwrap();
let ivar = this
.class()
.instance_variable(CStr::from_bytes_with_nul(b"function\0").unwrap())
.unwrap();
let function: &*mut c_void = ivar.load(this);
if !function.is_null() {
let function = &mut *(*function
Expand All @@ -98,9 +104,7 @@ extern "C" fn start_task(
let body = request.HTTPBody();
let body_stream = request.HTTPBodyStream();
if let Some(body) = body {
let length = body.length();
let data_bytes = body.bytes();
sent_form_body = slice::from_raw_parts(data_bytes.as_ptr(), length).to_vec();
sent_form_body = body.to_vec();
} else if let Some(body_stream) = body_stream {
body_stream.open();

Expand All @@ -121,7 +125,7 @@ extern "C" fn start_task(
// get all our headers values and inject them in our request
if let Some(all_headers) = all_headers {
for current_header in all_headers.allKeys().to_vec() {
let header_value = all_headers.valueForKey(current_header).unwrap();
let header_value = all_headers.valueForKey(&current_header).unwrap();
// inject the header into the request
http_request = http_request.header(current_header.to_string(), header_value.to_string());
}
Expand Down Expand Up @@ -196,7 +200,7 @@ extern "C" fn start_task(
// FIXME: though we give it a static lifetime, it's not guaranteed to be valid.
task: &'static ProtocolObject<dyn WKURLSchemeTask>,
// FIXME: though we give it a static lifetime, it's not guaranteed to be valid.
webview: &'static mut WryWebView,
webview: &'static WryWebView,
task_key: usize,
task_uuid: Retained<NSUUID>,
webview_id: &str,
Expand All @@ -215,24 +219,24 @@ extern "C" fn start_task(
// default to HTTP/1.1
let wanted_version = format!("{:#?}", sent_response.version());

let mut headers = NSMutableDictionary::new();
let headers = NSMutableDictionary::new();
if let Some(mime) = wanted_mime {
headers.insert_id(
NSString::from_str(CONTENT_TYPE.as_str()).as_ref(),
NSString::from_str(mime.to_str().unwrap()),
headers.insert(
&*NSString::from_str(CONTENT_TYPE.as_str()),
&*NSString::from_str(mime.to_str().unwrap()),
);
}
headers.insert_id(
NSString::from_str(CONTENT_LENGTH.as_str()).as_ref(),
NSString::from_str(&content.len().to_string()),
headers.insert(
&*NSString::from_str(CONTENT_LENGTH.as_str()),
&*NSString::from_str(&content.len().to_string()),
);

// add headers
for (name, value) in sent_response.headers().iter() {
if let Ok(value) = value.to_str() {
headers.insert_id(
NSString::from_str(name.as_str()).as_ref(),
NSString::from_str(value),
headers.insert(
&*NSString::from_str(name.as_str()),
&*NSString::from_str(value),
);
}
}
Expand Down Expand Up @@ -334,7 +338,7 @@ extern "C" fn start_task(
extern "C" fn stop_task(
_this: &ProtocolObject<dyn WKURLSchemeHandler>,
_sel: objc2::runtime::Sel,
webview: &mut WryWebView,
webview: &WryWebView,
task: &ProtocolObject<dyn WKURLSchemeTask>,
) {
webview.remove_custom_task_key(task.hash());
Expand Down
36 changes: 11 additions & 25 deletions src/wkwebview/class/wry_download_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

use std::{cell::RefCell, path::PathBuf, rc::Rc};

use objc2::{
declare_class, msg_send_id, mutability::MainThreadOnly, rc::Retained, runtime::NSObject,
ClassType, DeclaredClass,
};
use objc2::{define_class, msg_send, rc::Retained, runtime::NSObject, MainThreadOnly};
use objc2_foundation::{
MainThreadMarker, NSData, NSError, NSObjectProtocol, NSString, NSURLResponse, NSURL,
};
Expand All @@ -20,23 +17,17 @@ pub struct WryDownloadDelegateIvars {
pub completed: Option<Rc<dyn Fn(String, Option<PathBuf>, bool) + 'static>>,
}

declare_class!(
define_class!(
#[unsafe(super(NSObject))]
#[name = "WryDownloadDelegate"]
#[thread_kind = MainThreadOnly]
#[ivars = WryDownloadDelegateIvars]
pub struct WryDownloadDelegate;

unsafe impl ClassType for WryDownloadDelegate {
type Super = NSObject;
type Mutability = MainThreadOnly;
const NAME: &'static str = "WryDownloadDelegate";
}

impl DeclaredClass for WryDownloadDelegate {
type Ivars = WryDownloadDelegateIvars;
}

unsafe impl NSObjectProtocol for WryDownloadDelegate {}

unsafe impl WKDownloadDelegate for WryDownloadDelegate {
#[method(download:decideDestinationUsingResponse:suggestedFilename:completionHandler:)]
#[unsafe(method(download:decideDestinationUsingResponse:suggestedFilename:completionHandler:))]
fn download_policy(
&self,
download: &WKDownload,
Expand All @@ -47,18 +38,13 @@ declare_class!(
download_policy(self, download, response, suggested_path, handler);
}

#[method(downloadDidFinish:)]
#[unsafe(method(downloadDidFinish:))]
fn download_did_finish(&self, download: &WKDownload) {
download_did_finish(self, download);
}

#[method(download:didFailWithError:resumeData:)]
fn download_did_fail(
&self,
download: &WKDownload,
error: &NSError,
resume_data: &NSData,
) {
#[unsafe(method(download:didFailWithError:resumeData:))]
fn download_did_fail(&self, download: &WKDownload, error: &NSError, resume_data: &NSData) {
download_did_fail(self, download, error, resume_data);
}
}
Expand All @@ -77,6 +63,6 @@ impl WryDownloadDelegate {
completed: download_completed_handler,
});

unsafe { msg_send_id![super(delegate), init] }
unsafe { msg_send![super(delegate), init] }
}
}
Loading

0 comments on commit c27b4ff

Please sign in to comment.