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        #[unsafe(method(messageHandler))]
80        #[unsafe(method_family = none)]
81        pub unsafe fn messageHandler(
82            &self,
83        ) -> *mut block2::DynBlock<dyn Fn(*mut AnyObject, *mut NSError)>;
84
85        #[cfg(feature = "block2")]
86        /// Setter for [`messageHandler`][Self::messageHandler].
87        #[unsafe(method(setMessageHandler:))]
88        #[unsafe(method_family = none)]
89        pub unsafe fn setMessageHandler(
90            &self,
91            message_handler: Option<&block2::DynBlock<dyn Fn(*mut AnyObject, *mut NSError)>>,
92        );
93
94        #[cfg(feature = "block2")]
95        /// The block to be executed when the port disconnects.
96        ///
97        /// An optional block to be invoked when the port disconnects, taking an optional error that indicates if the disconnection was caused by an error.
98        #[unsafe(method(disconnectHandler))]
99        #[unsafe(method_family = none)]
100        pub unsafe fn disconnectHandler(&self) -> *mut block2::DynBlock<dyn Fn(*mut NSError)>;
101
102        #[cfg(feature = "block2")]
103        /// Setter for [`disconnectHandler`][Self::disconnectHandler].
104        #[unsafe(method(setDisconnectHandler:))]
105        #[unsafe(method_family = none)]
106        pub unsafe fn setDisconnectHandler(
107            &self,
108            disconnect_handler: Option<&block2::DynBlock<dyn Fn(*mut NSError)>>,
109        );
110
111        /// Indicates whether the message port is disconnected.
112        #[unsafe(method(isDisconnected))]
113        #[unsafe(method_family = none)]
114        pub unsafe fn isDisconnected(&self) -> bool;
115
116        #[cfg(feature = "block2")]
117        /// Sends a message to the connected web extension.
118        ///
119        /// Parameter `message`: The JSON-serializable message to be sent.
120        ///
121        /// Parameter `completionHandler`: An optional block to be invoked after the message is sent, taking an optional error.
122        ///
123        /// Note: The message must be JSON-serializable according to ``NSJSONSerialization``.
124        #[unsafe(method(sendMessage:completionHandler:))]
125        #[unsafe(method_family = none)]
126        pub unsafe fn sendMessage_completionHandler(
127            &self,
128            message: Option<&AnyObject>,
129            completion_handler: Option<&block2::DynBlock<dyn Fn(*mut NSError)>>,
130        );
131
132        /// Disconnects the port, terminating all further messages.
133        #[unsafe(method(disconnect))]
134        #[unsafe(method_family = none)]
135        pub unsafe fn disconnect(&self);
136
137        /// Disconnects the port, terminating all further messages with an optional error.
138        ///
139        /// Parameter `error`: An optional error indicating the reason for disconnection.
140        #[unsafe(method(disconnectWithError:))]
141        #[unsafe(method_family = none)]
142        pub unsafe fn disconnectWithError(&self, error: Option<&NSError>);
143    );
144}