objc2_web_kit/generated/
WKScriptMessageHandlerWithReply.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_protocol!(
11    /// A class conforming to  the WKScriptMessageHandlerWithReply protocol provides a
12    /// method for receiving messages from JavaScript running in a webpage and replying to them asynchronously.
13    ///
14    /// See also [Apple's documentation](https://developer.apple.com/documentation/webkit/wkscriptmessagehandlerwithreply?language=objc)
15    pub unsafe trait WKScriptMessageHandlerWithReply:
16        NSObjectProtocol + MainThreadOnly
17    {
18        #[cfg(all(
19            feature = "WKScriptMessage",
20            feature = "WKUserContentController",
21            feature = "block2"
22        ))]
23        /// Invoked when a script message is received from a webpage.
24        ///
25        /// Parameter `userContentController`: The user content controller invoking the delegate method.
26        ///
27        /// Parameter `message`: The script message received.
28        ///
29        /// Parameter `replyHandler`: A block to be called with the result of processing the message.
30        ///
31        /// When the JavaScript running in your application's web content called window.webkit.messageHandlers.
32        /// <name
33        /// >.postMessage(
34        /// <messageBody
35        /// >),
36        /// a JavaScript Promise object was returned. The values passed to the replyHandler are used to resolve that Promise.
37        ///
38        /// Passing a non-nil NSString value to the second parameter of the replyHandler signals an error.
39        /// No matter what value you pass to the first parameter of the replyHandler,
40        /// the Promise will be rejected with a JavaScript error object whose message property is set to that errorMessage string.
41        ///
42        /// If the second parameter to the replyHandler is nil, the first argument will be serialized into its JavaScript equivalent and the Promise will be fulfilled with the resulting value.
43        /// If the first argument is nil then the Promise will be resolved with a JavaScript value of "undefined"
44        ///
45        /// Allowed non-nil result types are:
46        /// NSNumber, NSNull, NSString, NSDate, NSArray, and NSDictionary.
47        /// Any NSArray or NSDictionary containers can only contain objects of those types.
48        ///
49        /// The replyHandler can be called at most once.
50        /// If the replyHandler is deallocated before it is called, the Promise will be rejected with a JavaScript Error object
51        /// with an appropriate message indicating the handler was never called.
52        ///
53        /// Example:
54        ///
55        /// With a WKScriptMessageHandlerWithReply object installed with the name "testHandler", consider the following JavaScript:
56        ///
57        /// ```text
58        ///      var promise = window.webkit.messageHandlers.testHandler.postMessage("Fulfill me with 42");
59        ///      await p;
60        ///      return p;
61        /// ```
62        ///
63        /// And consider the following WKScriptMessageHandlerWithReply implementation:
64        ///
65        /// ```text
66        ///      - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message replyHandler:(void (^)(id, NSString *))replyHandler
67        ///      {
68        ///         if ([message.body isEqual:@"Fulfill me with 42"])
69        ///             replyHandler(@42, nil);
70        ///         else
71        ///             replyHandler(nil, @"Unexpected message received");
72        ///      }
73        /// ```
74        ///
75        /// In this example:
76        /// - The JavaScript code sends a message to your application code with the body
77        /// "
78        /// Fulfill me with 42"
79        /// - JavaScript execution is suspended while waiting for the resulting promise to resolve.
80        /// - Your message handler is invoked with that message and a block to call with the reply when ready.
81        /// - Your message handler sends the value
82        /// @
83        /// 42 as a reply.
84        /// - The JavaScript promise is fulfilled with the value 42.
85        /// - JavaScript execution continues and the value 42 is returned.
86        #[unsafe(method(userContentController:didReceiveScriptMessage:replyHandler:))]
87        #[unsafe(method_family = none)]
88        unsafe fn userContentController_didReceiveScriptMessage_replyHandler(
89            &self,
90            user_content_controller: &WKUserContentController,
91            message: &WKScriptMessage,
92            reply_handler: &block2::DynBlock<dyn Fn(*mut AnyObject, *mut NSString)>,
93        );
94    }
95);