objc2_web_kit/generated/
WKWebExtensionMessagePort.rs

1//! This file has been automatically generated by `objc2`'s `header-translator`.
2//! DO NOT EDIT
3use core::ffi::*;
4use core::ptr::NonNull;
5use objc2::__framework_prelude::*;
6use objc2_foundation::*;
7
8use crate::*;
9
10extern "C" {
11    /// Indicates a ``WKWebExtensionMessagePort`` error.
12    ///
13    /// See also [Apple's documentation](https://developer.apple.com/documentation/webkit/wkwebextensionmessageporterrordomain?language=objc)
14    pub static WKWebExtensionMessagePortErrorDomain: &'static NSErrorDomain;
15}
16
17/// Constants used by ``NSError`` to indicate errors in the ``WKWebExtensionMessagePort`` domain.
18///
19/// See also [Apple's documentation](https://developer.apple.com/documentation/webkit/wkwebextensionmessageporterror?language=objc)
20// NS_ERROR_ENUM
21#[repr(transparent)]
22#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
23pub struct WKWebExtensionMessagePortError(pub NSInteger);
24impl WKWebExtensionMessagePortError {
25    #[doc(alias = "WKWebExtensionMessagePortErrorUnknown")]
26    pub const Unknown: Self = Self(1);
27    #[doc(alias = "WKWebExtensionMessagePortErrorNotConnected")]
28    pub const NotConnected: Self = Self(2);
29    #[doc(alias = "WKWebExtensionMessagePortErrorMessageInvalid")]
30    pub const MessageInvalid: Self = Self(3);
31}
32
33unsafe impl Encode for WKWebExtensionMessagePortError {
34    const ENCODING: Encoding = NSInteger::ENCODING;
35}
36
37unsafe impl RefEncode for WKWebExtensionMessagePortError {
38    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
39}
40
41extern_class!(
42    /// A ``WKWebExtensionMessagePort`` object manages message-based communication with a web extension.
43    ///
44    /// Contains properties and methods to handle message exchanges with a web extension.
45    ///
46    /// See also [Apple's documentation](https://developer.apple.com/documentation/webkit/wkwebextensionmessageport?language=objc)
47    #[unsafe(super(NSObject))]
48    #[thread_kind = MainThreadOnly]
49    #[derive(Debug, PartialEq, Eq, Hash)]
50    pub struct WKWebExtensionMessagePort;
51);
52
53extern_conformance!(
54    unsafe impl NSObjectProtocol for WKWebExtensionMessagePort {}
55);
56
57impl WKWebExtensionMessagePort {
58    extern_methods!(
59        #[unsafe(method(new))]
60        #[unsafe(method_family = new)]
61        pub unsafe fn new(mtm: MainThreadMarker) -> Retained<Self>;
62
63        #[unsafe(method(init))]
64        #[unsafe(method_family = init)]
65        pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>;
66
67        /// The unique identifier for the app to which this port should be connected.
68        ///
69        /// This identifier is provided by the web extension and may or may not be used by the app.
70        /// It's up to the app to decide how to interpret this identifier.
71        #[unsafe(method(applicationIdentifier))]
72        #[unsafe(method_family = none)]
73        pub unsafe fn applicationIdentifier(&self) -> Option<Retained<NSString>>;
74
75        #[cfg(feature = "block2")]
76        /// The block to be executed when a message is received from the web extension.
77        ///
78        /// An optional block to be invoked when a message is received, taking two parameters: the message and an optional error.
79        ///
80        /// # Safety
81        ///
82        /// - The returned block's argument 1 must be a valid pointer or null.
83        /// - The returned block's argument 2 must be a valid pointer or null.
84        #[unsafe(method(messageHandler))]
85        #[unsafe(method_family = none)]
86        pub unsafe fn messageHandler(
87            &self,
88        ) -> *mut block2::DynBlock<dyn Fn(*mut AnyObject, *mut NSError)>;
89
90        #[cfg(feature = "block2")]
91        /// Setter for [`messageHandler`][Self::messageHandler].
92        ///
93        /// This is [copied][objc2_foundation::NSCopying::copy] when set.
94        #[unsafe(method(setMessageHandler:))]
95        #[unsafe(method_family = none)]
96        pub unsafe fn setMessageHandler(
97            &self,
98            message_handler: Option<&block2::DynBlock<dyn Fn(*mut AnyObject, *mut NSError)>>,
99        );
100
101        #[cfg(feature = "block2")]
102        /// The block to be executed when the port disconnects.
103        ///
104        /// An optional block to be invoked when the port disconnects, taking an optional error that indicates if the disconnection was caused by an error.
105        ///
106        /// # Safety
107        ///
108        /// The returned block's argument must be a valid pointer or null.
109        #[unsafe(method(disconnectHandler))]
110        #[unsafe(method_family = none)]
111        pub unsafe fn disconnectHandler(&self) -> *mut block2::DynBlock<dyn Fn(*mut NSError)>;
112
113        #[cfg(feature = "block2")]
114        /// Setter for [`disconnectHandler`][Self::disconnectHandler].
115        ///
116        /// This is [copied][objc2_foundation::NSCopying::copy] when set.
117        #[unsafe(method(setDisconnectHandler:))]
118        #[unsafe(method_family = none)]
119        pub unsafe fn setDisconnectHandler(
120            &self,
121            disconnect_handler: Option<&block2::DynBlock<dyn Fn(*mut NSError)>>,
122        );
123
124        /// Indicates whether the message port is disconnected.
125        #[unsafe(method(isDisconnected))]
126        #[unsafe(method_family = none)]
127        pub unsafe fn isDisconnected(&self) -> bool;
128
129        #[cfg(feature = "block2")]
130        /// Sends a message to the connected web extension.
131        ///
132        /// Parameter `message`: The JSON-serializable message to be sent.
133        ///
134        /// Parameter `completionHandler`: An optional block to be invoked after the message is sent, taking an optional error.
135        ///
136        /// Note: The message must be JSON-serializable according to ``NSJSONSerialization``.
137        ///
138        /// # Safety
139        ///
140        /// `message` should be of the correct type.
141        #[unsafe(method(sendMessage:completionHandler:))]
142        #[unsafe(method_family = none)]
143        pub unsafe fn sendMessage_completionHandler(
144            &self,
145            message: Option<&AnyObject>,
146            completion_handler: Option<&block2::DynBlock<dyn Fn(*mut NSError)>>,
147        );
148
149        /// Disconnects the port, terminating all further messages.
150        #[unsafe(method(disconnect))]
151        #[unsafe(method_family = none)]
152        pub unsafe fn disconnect(&self);
153
154        /// Disconnects the port, terminating all further messages with an optional error.
155        ///
156        /// Parameter `error`: An optional error indicating the reason for disconnection.
157        #[unsafe(method(disconnectWithError:))]
158        #[unsafe(method_family = none)]
159        pub unsafe fn disconnectWithError(&self, error: Option<&NSError>);
160    );
161}