Skip to content

Commit c27b4ff

Browse files
authored
chore(deps): Use objc2 v0.6 (#1468)
1 parent a92baaa commit c27b4ff

14 files changed

+360
-412
lines changed

.changes/objc2-v6.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
wry: patch
3+
---
4+
5+
Update to `objc2` v0.6.
6+
7+
This bumps MSRV on macOS/iOS to 1.71.

Cargo.toml

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,13 @@ features = [
9292
"Win32_UI_Input_KeyboardAndMouse",
9393
]
9494

95-
[target."cfg(any(target_os = \"ios\", target_os = \"macos\"))".dependencies]
95+
[target.'cfg(target_vendor = "apple")'.dependencies]
9696
url = "2.5"
97-
block2 = "0.5"
98-
objc2 = { version = "0.5", features = ["exception"] }
99-
objc2-web-kit = { version = "0.2.0", features = [
97+
block2 = "0.6"
98+
objc2 = { version = "0.6", features = ["exception"] }
99+
objc2-web-kit = { version = "0.3.0", default-features = false, features = [
100+
"std",
101+
"objc2-core-foundation",
100102
"objc2-app-kit",
101103
"block2",
102104
"WKWebView",
@@ -122,7 +124,13 @@ objc2-web-kit = { version = "0.2.0", features = [
122124
"WKUserScript",
123125
"WKHTTPCookieStore",
124126
] }
125-
objc2-foundation = { version = "0.2.0", features = [
127+
objc2-core-foundation = { version = "0.3.0", default-features = false, features = [
128+
"std",
129+
"CFCGTypes",
130+
] }
131+
objc2-foundation = { version = "0.3.0", default-features = false, features = [
132+
"std",
133+
"objc2-core-foundation",
126134
"NSURLRequest",
127135
"NSURL",
128136
"NSString",
@@ -131,6 +139,7 @@ objc2-foundation = { version = "0.2.0", features = [
131139
"NSDictionary",
132140
"NSObject",
133141
"NSData",
142+
"NSEnumerator",
134143
"NSKeyValueObserving",
135144
"NSThread",
136145
"NSJSONSerialization",
@@ -142,8 +151,10 @@ objc2-foundation = { version = "0.2.0", features = [
142151
"NSRunLoop",
143152
] }
144153

145-
[target."cfg(target_os = \"ios\")".dependencies]
146-
objc2-ui-kit = { version = "0.2.2", features = [
154+
[target.'cfg(target_os = "ios")'.dependencies]
155+
objc2-ui-kit = { version = "0.3.0", default-features = false, features = [
156+
"std",
157+
"objc2-core-foundation",
147158
"UIResponder",
148159
"UIScrollView",
149160
"UIView",
@@ -152,8 +163,10 @@ objc2-ui-kit = { version = "0.2.2", features = [
152163
"UIEvent",
153164
] }
154165

155-
[target."cfg(target_os = \"macos\")".dependencies]
156-
objc2-app-kit = { version = "0.2.0", features = [
166+
[target.'cfg(target_os = "macos")'.dependencies]
167+
objc2-app-kit = { version = "0.3.0", default-features = false, features = [
168+
"std",
169+
"objc2-core-foundation",
157170
"NSApplication",
158171
"NSButton",
159172
"NSControl",

src/wkwebview/class/document_title_changed_observer.rs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
use std::{ffi::c_void, ptr::null_mut};
66

77
use objc2::{
8-
declare_class, msg_send, msg_send_id,
9-
mutability::InteriorMutable,
8+
define_class, msg_send,
109
rc::Retained,
1110
runtime::{AnyObject, NSObject},
12-
ClassType, DeclaredClass,
11+
AllocAnyThread, DefinedClass,
1312
};
1413
use objc2_foundation::{
1514
NSDictionary, NSKeyValueChangeKey, NSKeyValueObservingOptions,
@@ -22,21 +21,15 @@ pub struct DocumentTitleChangedObserverIvars {
2221
pub handler: Box<dyn Fn(String)>,
2322
}
2423

25-
declare_class!(
24+
define_class!(
25+
#[unsafe(super(NSObject))]
26+
#[name = "DocumentTitleChangedObserver"]
27+
#[ivars = DocumentTitleChangedObserverIvars]
2628
pub struct DocumentTitleChangedObserver;
2729

28-
unsafe impl ClassType for DocumentTitleChangedObserver {
29-
type Super = NSObject;
30-
type Mutability = InteriorMutable;
31-
const NAME: &'static str = "DocumentTitleChangedObserver";
32-
}
33-
34-
impl DeclaredClass for DocumentTitleChangedObserver {
35-
type Ivars = DocumentTitleChangedObserverIvars;
36-
}
37-
38-
unsafe impl DocumentTitleChangedObserver {
39-
#[method(observeValueForKeyPath:ofObject:change:context:)]
30+
/// NSKeyValueObserving.
31+
impl DocumentTitleChangedObserver {
32+
#[unsafe(method(observeValueForKeyPath:ofObject:change:context:))]
4033
fn observe_value_for_key_path(
4134
&self,
4235
key_path: Option<&NSString>,
@@ -49,8 +42,8 @@ declare_class!(
4942
unsafe {
5043
let handler = &self.ivars().handler;
5144
// if !handler.is_null() {
52-
let title: *const NSString = msg_send![object, title];
53-
handler((*title).to_string());
45+
let title: *const NSString = msg_send![object, title];
46+
handler((*title).to_string());
5447
// }
5548
}
5649
}
@@ -68,7 +61,7 @@ impl DocumentTitleChangedObserver {
6861
handler,
6962
});
7063

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

7366
unsafe {
7467
observer
@@ -77,7 +70,7 @@ impl DocumentTitleChangedObserver {
7770
.addObserver_forKeyPath_options_context(
7871
&observer,
7972
&NSString::from_str("title"),
80-
NSKeyValueObservingOptions::NSKeyValueObservingOptionNew,
73+
NSKeyValueObservingOptions::New,
8174
null_mut(),
8275
);
8376
}

src/wkwebview/class/url_scheme_handler.rs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::{
77
ffi::{c_char, c_void, CStr},
88
panic::AssertUnwindSafe,
99
ptr::NonNull,
10-
slice,
1110
};
1211

1312
use http::{
@@ -17,7 +16,7 @@ use http::{
1716
use objc2::{
1817
rc::Retained,
1918
runtime::{AnyClass, AnyObject, ClassBuilder, ProtocolObject},
20-
ClassType,
19+
AllocAnyThread, ClassType,
2120
};
2221
use objc2_foundation::{
2322
NSData, NSHTTPURLResponse, NSMutableDictionary, NSObject, NSObjectProtocol, NSString, NSURL,
@@ -29,12 +28,13 @@ use crate::{wkwebview::WEBVIEW_IDS, RequestAsyncResponder, WryWebView};
2928

3029
pub fn create(name: &str) -> &AnyClass {
3130
unsafe {
32-
let scheme_name = format!("{}URLSchemeHandler", name);
31+
let scheme_name = format!("{}URLSchemeHandler\0", name);
32+
let scheme_name = CStr::from_bytes_with_nul(scheme_name.as_bytes()).unwrap();
3333
let cls = ClassBuilder::new(&scheme_name, NSObject::class());
3434
match cls {
3535
Some(mut cls) => {
36-
cls.add_ivar::<*mut c_void>("function");
37-
cls.add_ivar::<*mut c_char>("webview_id");
36+
cls.add_ivar::<*mut c_void>(CStr::from_bytes_with_nul(b"function\0").unwrap());
37+
cls.add_ivar::<*mut c_char>(CStr::from_bytes_with_nul(b"webview_id\0").unwrap());
3838
cls.add_method(
3939
objc2::sel!(webView:startURLSchemeTask:),
4040
start_task as extern "C" fn(_, _, _, _),
@@ -54,7 +54,7 @@ pub fn create(name: &str) -> &AnyClass {
5454
extern "C" fn start_task(
5555
this: &AnyObject,
5656
_sel: objc2::runtime::Sel,
57-
webview: &'static mut WryWebView,
57+
webview: &'static WryWebView,
5858
task: &'static ProtocolObject<dyn WKURLSchemeTask>,
5959
) {
6060
unsafe {
@@ -65,14 +65,20 @@ extern "C" fn start_task(
6565
let task_key = task.hash(); // hash by task object address
6666
let task_uuid = webview.add_custom_task_key(task_key);
6767

68-
let ivar = this.class().instance_variable("webview_id").unwrap();
68+
let ivar = this
69+
.class()
70+
.instance_variable(CStr::from_bytes_with_nul(b"webview_id\0").unwrap())
71+
.unwrap();
6972
let webview_id_ptr: *mut c_char = *ivar.load(this);
7073
let webview_id = CStr::from_ptr(webview_id_ptr)
7174
.to_str()
7275
.ok()
7376
.unwrap_or_default();
7477

75-
let ivar = this.class().instance_variable("function").unwrap();
78+
let ivar = this
79+
.class()
80+
.instance_variable(CStr::from_bytes_with_nul(b"function\0").unwrap())
81+
.unwrap();
7682
let function: &*mut c_void = ivar.load(this);
7783
if !function.is_null() {
7884
let function = &mut *(*function
@@ -98,9 +104,7 @@ extern "C" fn start_task(
98104
let body = request.HTTPBody();
99105
let body_stream = request.HTTPBodyStream();
100106
if let Some(body) = body {
101-
let length = body.length();
102-
let data_bytes = body.bytes();
103-
sent_form_body = slice::from_raw_parts(data_bytes.as_ptr(), length).to_vec();
107+
sent_form_body = body.to_vec();
104108
} else if let Some(body_stream) = body_stream {
105109
body_stream.open();
106110

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

218-
let mut headers = NSMutableDictionary::new();
222+
let headers = NSMutableDictionary::new();
219223
if let Some(mime) = wanted_mime {
220-
headers.insert_id(
221-
NSString::from_str(CONTENT_TYPE.as_str()).as_ref(),
222-
NSString::from_str(mime.to_str().unwrap()),
224+
headers.insert(
225+
&*NSString::from_str(CONTENT_TYPE.as_str()),
226+
&*NSString::from_str(mime.to_str().unwrap()),
223227
);
224228
}
225-
headers.insert_id(
226-
NSString::from_str(CONTENT_LENGTH.as_str()).as_ref(),
227-
NSString::from_str(&content.len().to_string()),
229+
headers.insert(
230+
&*NSString::from_str(CONTENT_LENGTH.as_str()),
231+
&*NSString::from_str(&content.len().to_string()),
228232
);
229233

230234
// add headers
231235
for (name, value) in sent_response.headers().iter() {
232236
if let Ok(value) = value.to_str() {
233-
headers.insert_id(
234-
NSString::from_str(name.as_str()).as_ref(),
235-
NSString::from_str(value),
237+
headers.insert(
238+
&*NSString::from_str(name.as_str()),
239+
&*NSString::from_str(value),
236240
);
237241
}
238242
}
@@ -334,7 +338,7 @@ extern "C" fn start_task(
334338
extern "C" fn stop_task(
335339
_this: &ProtocolObject<dyn WKURLSchemeHandler>,
336340
_sel: objc2::runtime::Sel,
337-
webview: &mut WryWebView,
341+
webview: &WryWebView,
338342
task: &ProtocolObject<dyn WKURLSchemeTask>,
339343
) {
340344
webview.remove_custom_task_key(task.hash());

src/wkwebview/class/wry_download_delegate.rs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44

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

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

23-
declare_class!(
20+
define_class!(
21+
#[unsafe(super(NSObject))]
22+
#[name = "WryDownloadDelegate"]
23+
#[thread_kind = MainThreadOnly]
24+
#[ivars = WryDownloadDelegateIvars]
2425
pub struct WryDownloadDelegate;
2526

26-
unsafe impl ClassType for WryDownloadDelegate {
27-
type Super = NSObject;
28-
type Mutability = MainThreadOnly;
29-
const NAME: &'static str = "WryDownloadDelegate";
30-
}
31-
32-
impl DeclaredClass for WryDownloadDelegate {
33-
type Ivars = WryDownloadDelegateIvars;
34-
}
35-
3627
unsafe impl NSObjectProtocol for WryDownloadDelegate {}
3728

3829
unsafe impl WKDownloadDelegate for WryDownloadDelegate {
39-
#[method(download:decideDestinationUsingResponse:suggestedFilename:completionHandler:)]
30+
#[unsafe(method(download:decideDestinationUsingResponse:suggestedFilename:completionHandler:))]
4031
fn download_policy(
4132
&self,
4233
download: &WKDownload,
@@ -47,18 +38,13 @@ declare_class!(
4738
download_policy(self, download, response, suggested_path, handler);
4839
}
4940

50-
#[method(downloadDidFinish:)]
41+
#[unsafe(method(downloadDidFinish:))]
5142
fn download_did_finish(&self, download: &WKDownload) {
5243
download_did_finish(self, download);
5344
}
5445

55-
#[method(download:didFailWithError:resumeData:)]
56-
fn download_did_fail(
57-
&self,
58-
download: &WKDownload,
59-
error: &NSError,
60-
resume_data: &NSData,
61-
) {
46+
#[unsafe(method(download:didFailWithError:resumeData:))]
47+
fn download_did_fail(&self, download: &WKDownload, error: &NSError, resume_data: &NSData) {
6248
download_did_fail(self, download, error, resume_data);
6349
}
6450
}
@@ -77,6 +63,6 @@ impl WryDownloadDelegate {
7763
completed: download_completed_handler,
7864
});
7965

80-
unsafe { msg_send_id![super(delegate), init] }
66+
unsafe { msg_send![super(delegate), init] }
8167
}
8268
}

0 commit comments

Comments
 (0)