@@ -35,14 +35,15 @@ use objc2::runtime::Bool;
35
35
use objc2:: {
36
36
rc:: Retained ,
37
37
runtime:: { AnyObject , NSObject , ProtocolObject } ,
38
- ClassType , DeclaredClass ,
38
+ AllocAnyThread , DeclaredClass , MainThreadOnly , Message ,
39
39
} ;
40
40
#[ cfg( target_os = "macos" ) ]
41
41
use objc2_app_kit:: { NSApplication , NSAutoresizingMaskOptions , NSTitlebarSeparatorStyle , NSView } ;
42
42
#[ cfg( target_os = "macos" ) ]
43
- use objc2_foundation:: CGSize ;
43
+ use objc2_core_foundation:: CGSize ;
44
+ use objc2_core_foundation:: { CGPoint , CGRect } ;
44
45
use objc2_foundation:: {
45
- ns_string, CGPoint , CGRect , MainThreadMarker , NSArray , NSBundle , NSDate , NSError , NSHTTPCookie ,
46
+ ns_string, MainThreadMarker , NSArray , NSBundle , NSDate , NSError , NSHTTPCookie ,
46
47
NSHTTPCookieSameSiteLax , NSHTTPCookieSameSiteStrict , NSJSONSerialization , NSMutableURLRequest ,
47
48
NSNumber , NSObjectNSKeyValueCoding , NSObjectProtocol , NSString , NSUTF8StringEncoding , NSURL ,
48
49
NSUUID ,
@@ -70,8 +71,8 @@ use once_cell::sync::Lazy;
70
71
use raw_window_handle:: { HasWindowHandle , RawWindowHandle } ;
71
72
72
73
use std:: {
73
- collections:: { HashMap , HashSet } ,
74
- ffi:: { c_void, CString } ,
74
+ collections:: HashSet ,
75
+ ffi:: { c_void, CStr , CString } ,
75
76
net:: Ipv4Addr ,
76
77
os:: raw:: c_char,
77
78
panic:: AssertUnwindSafe ,
@@ -114,6 +115,7 @@ pub struct PrintOptions {
114
115
115
116
pub ( crate ) struct InnerWebView {
116
117
id : String ,
118
+ mtm : MainThreadMarker ,
117
119
pub webview : Retained < WryWebView > ,
118
120
pub manager : Retained < WKUserContentController > ,
119
121
data_store : Retained < WKWebsiteDataStore > ,
@@ -194,7 +196,7 @@ impl InnerWebView {
194
196
195
197
// Safety: objc runtime calls are unsafe
196
198
unsafe {
197
- let config = WKWebViewConfiguration :: new ( ) ;
199
+ let config = WKWebViewConfiguration :: new ( mtm ) ;
198
200
199
201
// Incognito mode
200
202
let ( os_major_version, _, _) = util:: operating_system_version ( ) ;
@@ -208,14 +210,14 @@ impl InnerWebView {
208
210
custom_data_store_available,
209
211
pl_attrs. data_store_identifier ,
210
212
) {
211
- ( true , _, _) => WKWebsiteDataStore :: nonPersistentDataStore ( ) ,
213
+ ( true , _, _) => WKWebsiteDataStore :: nonPersistentDataStore ( mtm ) ,
212
214
// if data_store_identifier is given and custom data stores are available, use custom store
213
215
( false , true , Some ( data_store) ) => {
214
216
let identifier = NSUUID :: from_bytes ( data_store) ;
215
- WKWebsiteDataStore :: dataStoreForIdentifier ( & identifier)
217
+ WKWebsiteDataStore :: dataStoreForIdentifier ( & identifier, mtm )
216
218
}
217
219
// default data store
218
- _ => WKWebsiteDataStore :: defaultDataStore ( ) ,
220
+ _ => WKWebsiteDataStore :: defaultDataStore ( mtm ) ,
219
221
} ;
220
222
221
223
// Register Custom Protocols
@@ -226,11 +228,17 @@ impl InnerWebView {
226
228
let function = Box :: into_raw ( Box :: new ( function) ) ;
227
229
protocol_ptrs. push ( function) ;
228
230
229
- let ivar = ( * handler) . class ( ) . instance_variable ( "function" ) . unwrap ( ) ;
231
+ let ivar = ( * handler)
232
+ . class ( )
233
+ . instance_variable ( CStr :: from_bytes_with_nul ( b"function\0 " ) . unwrap ( ) )
234
+ . unwrap ( ) ;
230
235
let ivar_delegate = ivar. load_mut ( & mut * handler) ;
231
236
* ivar_delegate = function as * mut _ as * mut c_void ;
232
237
233
- let ivar = ( * handler) . class ( ) . instance_variable ( "webview_id" ) . unwrap ( ) ;
238
+ let ivar = ( * handler)
239
+ . class ( )
240
+ . instance_variable ( CStr :: from_bytes_with_nul ( b"webview_id\0 " ) . unwrap ( ) )
241
+ . unwrap ( ) ;
234
242
let ivar_delegate: & mut * mut c_char = ivar. load_mut ( & mut * handler) ;
235
243
* ivar_delegate = CString :: new ( webview_id. as_bytes ( ) ) . unwrap ( ) . into_raw ( ) ;
236
244
@@ -247,7 +255,7 @@ impl InnerWebView {
247
255
248
256
// WebView and manager
249
257
let manager = config. userContentController ( ) ;
250
- let webview = mtm . alloc :: < WryWebView > ( ) . set_ivars ( WryWebViewIvars {
258
+ let webview = WryWebView :: alloc ( mtm ) . set_ivars ( WryWebViewIvars {
251
259
is_child,
252
260
#[ cfg( target_os = "macos" ) ]
253
261
drag_drop_handler : match attributes. drag_drop_handler {
@@ -256,7 +264,7 @@ impl InnerWebView {
256
264
} ,
257
265
#[ cfg( target_os = "macos" ) ]
258
266
accept_first_mouse : Bool :: new ( attributes. accept_first_mouse ) ,
259
- custom_protocol_task_ids : HashMap :: new ( ) ,
267
+ custom_protocol_task_ids : Default :: default ( ) ,
260
268
} ) ;
261
269
262
270
config. setWebsiteDataStore ( & data_store) ;
@@ -289,9 +297,7 @@ impl InnerWebView {
289
297
config. setValue_forKey ( Some ( & _yes) , ns_string ! ( "allowsInlineMediaPlayback" ) ) ;
290
298
291
299
if attributes. autoplay {
292
- config. setMediaTypesRequiringUserActionForPlayback (
293
- WKAudiovisualMediaTypes :: WKAudiovisualMediaTypeNone ,
294
- ) ;
300
+ config. setMediaTypesRequiringUserActionForPlayback ( WKAudiovisualMediaTypes :: None ) ;
295
301
}
296
302
297
303
#[ cfg( feature = "transparent" ) ]
@@ -341,14 +347,14 @@ impl InnerWebView {
341
347
size : CGSize :: new ( w as f64 , h as f64 ) ,
342
348
} ;
343
349
let webview: Retained < WryWebView > =
344
- objc2:: msg_send_id ![ super ( webview) , initWithFrame: frame configuration: & * * config] ;
350
+ objc2:: msg_send ![ super ( webview) , initWithFrame: frame, configuration: & * * config] ;
345
351
webview
346
352
} ;
347
353
#[ cfg( target_os = "ios" ) ]
348
354
let webview = {
349
355
let frame = ns_view. frame ( ) ;
350
356
let webview: Retained < WryWebView > =
351
- objc2:: msg_send_id ![ super ( webview) , initWithFrame: frame configuration: & * * config] ;
357
+ objc2:: msg_send ![ super ( webview) , initWithFrame: frame, configuration: & * * config] ;
352
358
webview
353
359
} ;
354
360
@@ -382,12 +388,12 @@ impl InnerWebView {
382
388
{
383
389
if is_child {
384
390
// fixed element
385
- webview. setAutoresizingMask ( NSAutoresizingMaskOptions :: NSViewMinYMargin ) ;
391
+ webview. setAutoresizingMask ( NSAutoresizingMaskOptions :: ViewMinYMargin ) ;
386
392
} else {
387
393
// Auto-resize
388
394
webview. setAutoresizingMask (
389
- NSAutoresizingMaskOptions :: NSViewHeightSizable
390
- | NSAutoresizingMaskOptions :: NSViewWidthSizable ,
395
+ NSAutoresizingMaskOptions :: ViewHeightSizable
396
+ | NSAutoresizingMaskOptions :: ViewWidthSizable ,
391
397
) ;
392
398
}
393
399
@@ -406,7 +412,7 @@ impl InnerWebView {
406
412
// disable scroll bounce by default
407
413
// https://developer.apple.com/documentation/webkit/wkwebview/1614784-scrollview?language=objc
408
414
// But not exist in objc2-web-kit
409
- let scroll_view: Retained < UIScrollView > = objc2:: msg_send_id ![ & webview, scrollView] ;
415
+ let scroll_view: Retained < UIScrollView > = objc2:: msg_send ![ & webview, scrollView] ;
410
416
// let scroll_view: Retained<UIScrollView> = webview.ivars().scrollView; // FIXME: not test yet
411
417
scroll_view. setBounces ( false )
412
418
}
@@ -471,12 +477,11 @@ impl InnerWebView {
471
477
mtm,
472
478
) ;
473
479
474
- let proto_navigation_policy_delegate =
475
- ProtocolObject :: from_ref ( navigation_policy_delegate. as_ref ( ) ) ;
480
+ let proto_navigation_policy_delegate = ProtocolObject :: from_ref ( & * navigation_policy_delegate) ;
476
481
webview. setNavigationDelegate ( Some ( proto_navigation_policy_delegate) ) ;
477
482
478
483
let ui_delegate: Retained < WryWebViewUIDelegate > = WryWebViewUIDelegate :: new ( mtm) ;
479
- let proto_ui_delegate = ProtocolObject :: from_ref ( ui_delegate. as_ref ( ) ) ;
484
+ let proto_ui_delegate = ProtocolObject :: from_ref ( & * ui_delegate) ;
480
485
webview. setUIDelegate ( Some ( proto_ui_delegate) ) ;
481
486
482
487
// ns window is required for the print operation
@@ -492,6 +497,7 @@ impl InnerWebView {
492
497
493
498
let mut w = Self {
494
499
id : webview_id,
500
+ mtm,
495
501
webview : webview. clone ( ) ,
496
502
manager : manager. clone ( ) ,
497
503
ns_view : ns_view. retain ( ) ,
@@ -547,8 +553,8 @@ r#"Object.defineProperty(window, 'ipc', {
547
553
}
548
554
549
555
parent_view. setAutoresizingMask (
550
- NSAutoresizingMaskOptions :: NSViewHeightSizable
551
- | NSAutoresizingMaskOptions :: NSViewWidthSizable ,
556
+ NSAutoresizingMaskOptions :: ViewHeightSizable
557
+ | NSAutoresizingMaskOptions :: ViewWidthSizable ,
552
558
) ;
553
559
parent_view. addSubview ( & webview. clone ( ) ) ;
554
560
@@ -607,7 +613,7 @@ r#"Object.defineProperty(window, 'ipc', {
607
613
if !val. is_null ( ) {
608
614
let json_ns_data = NSJSONSerialization :: dataWithJSONObject_options_error (
609
615
& * val,
610
- objc2_foundation:: NSJSONWritingOptions :: NSJSONWritingFragmentsAllowed ,
616
+ objc2_foundation:: NSJSONWritingOptions :: FragmentsAllowed ,
611
617
)
612
618
. unwrap ( ) ;
613
619
let json_string = NSString :: alloc ( ) ;
@@ -647,7 +653,7 @@ r#"Object.defineProperty(window, 'ipc', {
647
653
fn init ( & self , js : & str , for_main_only : bool ) {
648
654
// Safety: objc runtime calls are unsafe
649
655
unsafe {
650
- let userscript = WKUserScript :: alloc ( ) ;
656
+ let userscript = WKUserScript :: alloc ( self . mtm ) ;
651
657
let script = WKUserScript :: initWithSource_injectionTime_forMainFrameOnly (
652
658
userscript,
653
659
& NSString :: from_str ( js) ,
@@ -675,7 +681,7 @@ r#"Object.defineProperty(window, 'ipc', {
675
681
unsafe {
676
682
let config = self . webview . configuration ( ) ;
677
683
let store = config. websiteDataStore ( ) ;
678
- let all_data_types = WKWebsiteDataStore :: allWebsiteDataTypes ( ) ;
684
+ let all_data_types = WKWebsiteDataStore :: allWebsiteDataTypes ( self . mtm ) ;
679
685
let date = NSDate :: dateWithTimeIntervalSince1970 ( 0.0 ) ;
680
686
let handler = block2:: RcBlock :: new ( || { } ) ;
681
687
store. removeDataOfTypes_modifiedSince_completionHandler ( & all_data_types, & date, & handler) ;
@@ -687,7 +693,7 @@ r#"Object.defineProperty(window, 'ipc', {
687
693
// Safety: objc runtime calls are unsafe
688
694
unsafe {
689
695
let url = NSURL :: URLWithString ( & NSString :: from_str ( url) ) . unwrap ( ) ;
690
- let mut request = NSMutableURLRequest :: requestWithURL ( & url) ;
696
+ let request = NSMutableURLRequest :: requestWithURL ( & url) ;
691
697
if let Some ( headers) = headers {
692
698
for ( name, value) in headers. iter ( ) {
693
699
let key = NSString :: from_str ( name. as_str ( ) ) ;
@@ -763,7 +769,7 @@ r#"Object.defineProperty(window, 'ipc', {
763
769
#[ cfg( target_os = "macos" ) ]
764
770
unsafe {
765
771
// taken from <https://github.com/WebKit/WebKit/blob/784f93cb80a386c29186c510bba910b67ce3adc1/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm#L1939>
766
- let tool: Retained < AnyObject > = objc2:: msg_send_id ![ & self . webview, _inspector] ;
772
+ let tool: Retained < AnyObject > = objc2:: msg_send ![ & self . webview, _inspector] ;
767
773
let ( ) = objc2:: msg_send![ & tool, show] ;
768
774
}
769
775
}
@@ -773,7 +779,7 @@ r#"Object.defineProperty(window, 'ipc', {
773
779
#[ cfg( target_os = "macos" ) ]
774
780
unsafe {
775
781
// taken from <https://github.com/WebKit/WebKit/blob/784f93cb80a386c29186c510bba910b67ce3adc1/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm#L1939>
776
- let tool: Retained < AnyObject > = objc2:: msg_send_id ![ & self . webview, _inspector] ;
782
+ let tool: Retained < AnyObject > = objc2:: msg_send ![ & self . webview, _inspector] ;
777
783
let ( ) = objc2:: msg_send![ & tool, close] ;
778
784
}
779
785
}
@@ -783,7 +789,7 @@ r#"Object.defineProperty(window, 'ipc', {
783
789
#[ cfg( target_os = "macos" ) ]
784
790
unsafe {
785
791
// taken from <https://github.com/WebKit/WebKit/blob/784f93cb80a386c29186c510bba910b67ce3adc1/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm#L1939>
786
- let tool: Retained < AnyObject > = objc2:: msg_send_id ![ & self . webview, _inspector] ;
792
+ let tool: Retained < AnyObject > = objc2:: msg_send ![ & self . webview, _inspector] ;
787
793
let is_visible: bool = objc2:: msg_send![ & tool, isVisible] ;
788
794
is_visible
789
795
}
@@ -889,8 +895,8 @@ r#"Object.defineProperty(window, 'ipc', {
889
895
890
896
let same_site = cookie. sameSitePolicy ( ) ;
891
897
let same_site = match same_site {
892
- Some ( policy) if policy. as_ref ( ) == NSHTTPCookieSameSiteLax => cookie:: SameSite :: Lax ,
893
- Some ( policy) if policy. as_ref ( ) == NSHTTPCookieSameSiteStrict => cookie:: SameSite :: Strict ,
898
+ Some ( policy) if & * policy == NSHTTPCookieSameSiteLax => cookie:: SameSite :: Lax ,
899
+ Some ( policy) if & * policy == NSHTTPCookieSameSiteStrict => cookie:: SameSite :: Strict ,
894
900
_ => cookie:: SameSite :: None ,
895
901
} ;
896
902
cookie_builder = cookie_builder. same_site ( same_site) ;
@@ -948,7 +954,7 @@ r#"Object.defineProperty(window, 'ipc', {
948
954
let cookies = cookies
949
955
. to_vec ( )
950
956
. into_iter ( )
951
- . map ( |cookie| Self :: cookie_from_wkwebview ( cookie) )
957
+ . map ( |cookie| Self :: cookie_from_wkwebview ( & cookie) )
952
958
. collect ( ) ;
953
959
let _ = tx. send ( cookies) ;
954
960
} ,
@@ -1003,7 +1009,7 @@ pub fn platform_webview_version() -> Result<String> {
1003
1009
let webkit_version = dict
1004
1010
. objectForKey ( & NSString :: from_str ( "CFBundleVersion" ) )
1005
1011
. unwrap ( ) ;
1006
- let webkit_version = Retained :: cast :: < NSString > ( webkit_version ) ;
1012
+ let webkit_version = webkit_version . downcast :: < NSString > ( ) . unwrap ( ) ;
1007
1013
1008
1014
bundle. unload ( ) ;
1009
1015
Ok ( webkit_version. to_string ( ) )
0 commit comments