1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// from webkit-gir-files
// DO NOT EDIT

use crate::{ffi, ScriptMessageReply, UserContentFilter, UserScript, UserStyleSheet};
use glib::{
    prelude::*,
    signal::{connect_raw, SignalHandlerId},
    translate::*,
};
use std::boxed::Box as Box_;

glib::wrapper! {
    /// Manages user-defined content which affects web pages.
    ///
    /// Using a #WebKitUserContentManager user CSS style sheets can be set to
    /// be injected in the web pages loaded by a #WebKitWebView, by
    /// webkit_user_content_manager_add_style_sheet().
    ///
    /// To use a #WebKitUserContentManager, it must be created using
    /// webkit_user_content_manager_new(), and then used to construct
    /// a #WebKitWebView. User style sheets can be created with
    /// webkit_user_style_sheet_new().
    ///
    /// User style sheets can be added and removed at any time, but
    /// they will affect the web pages loaded afterwards.
    ///
    /// ## Signals
    ///
    ///
    /// #### `script-message-received`
    ///  This signal is emitted when JavaScript in a web view calls
    /// <code>window.webkit.messageHandlers.<name>.postMessage()</code>, after registering
    /// <code><name></code> using
    /// webkit_user_content_manager_register_script_message_handler()
    ///
    /// Detailed
    ///
    ///
    /// #### `script-message-with-reply-received`
    ///  This signal is emitted when JavaScript in a web view calls
    /// <code>window.webkit.messageHandlers.<name>.postMessage()</code>, after registering
    /// <code><name></code> using
    /// webkit_user_content_manager_register_script_message_handler_with_reply()
    ///
    /// The given @reply can be used to send a return value with
    /// webkit_script_message_reply_return_value() or an error message with
    /// webkit_script_message_reply_return_error_message(). If none of them are
    /// called, an automatic reply with an undefined value will be sent.
    ///
    /// It is possible to handle the reply asynchronously, by simply calling
    /// g_object_ref() on the @reply and returning [`true`].
    ///
    /// Detailed
    #[doc(alias = "WebKitUserContentManager")]
    pub struct UserContentManager(Object<ffi::WebKitUserContentManager, ffi::WebKitUserContentManagerClass>);

    match fn {
        type_ => || ffi::webkit_user_content_manager_get_type(),
    }
}

impl UserContentManager {
    /// Creates a new user content manager.
    ///
    /// # Returns
    ///
    /// A #WebKitUserContentManager
    #[doc(alias = "webkit_user_content_manager_new")]
    pub fn new() -> UserContentManager {
        assert_initialized_main_thread!();
        unsafe { from_glib_full(ffi::webkit_user_content_manager_new()) }
    }

    /// Adds a #WebKitUserContentFilter to the given #WebKitUserContentManager.
    ///
    /// The same #WebKitUserContentFilter can be reused with multiple
    /// #WebKitUserContentManager instances.
    ///
    /// Filters need to be saved and loaded from #WebKitUserContentFilterStore.
    /// ## `filter`
    /// A #WebKitUserContentFilter
    #[doc(alias = "webkit_user_content_manager_add_filter")]
    pub fn add_filter(&self, filter: &UserContentFilter) {
        unsafe {
            ffi::webkit_user_content_manager_add_filter(
                self.to_glib_none().0,
                filter.to_glib_none().0,
            );
        }
    }

    /// Adds a #WebKitUserScript to the given #WebKitUserContentManager.
    ///
    /// The same #WebKitUserScript can be reused with multiple
    /// #WebKitUserContentManager instances.
    /// ## `script`
    /// A #WebKitUserScript
    #[doc(alias = "webkit_user_content_manager_add_script")]
    pub fn add_script(&self, script: &UserScript) {
        unsafe {
            ffi::webkit_user_content_manager_add_script(
                self.to_glib_none().0,
                script.to_glib_none().0,
            );
        }
    }

    /// Adds a #WebKitUserStyleSheet to the given #WebKitUserContentManager.
    ///
    /// The same #WebKitUserStyleSheet can be reused with multiple
    /// #WebKitUserContentManager instances.
    /// ## `stylesheet`
    /// A #WebKitUserStyleSheet
    #[doc(alias = "webkit_user_content_manager_add_style_sheet")]
    pub fn add_style_sheet(&self, stylesheet: &UserStyleSheet) {
        unsafe {
            ffi::webkit_user_content_manager_add_style_sheet(
                self.to_glib_none().0,
                stylesheet.to_glib_none().0,
            );
        }
    }

    /// Registers a new user script message handler in script world.
    ///
    /// After it is registered,
    /// scripts can use `window.webkit.messageHandlers.<name>.postMessage(value)`
    /// to send messages. Those messages are received by connecting handlers
    /// to the #WebKitUserContentManager::script-message-received signal. The
    /// handler name is used as the detail of the signal. To avoid race
    /// conditions between registering the handler name, and starting to
    /// receive the signals, it is recommended to connect to the signal
    /// *before* registering the handler name:
    ///
    /// **⚠️ The following code is in c ⚠️**
    ///
    /// ```c
    /// WebKitWebView *view = webkit_web_view_new ();
    /// WebKitUserContentManager *manager = webkit_web_view_get_user_content_manager ();
    /// g_signal_connect (manager, "script-message-received::foobar",
    ///                   G_CALLBACK (handle_script_message), NULL);
    /// webkit_user_content_manager_register_script_message_handler (manager, "foobar");
    /// ```
    ///
    /// Registering a script message handler will fail if the requested
    /// name has been already registered before.
    ///
    /// If [`None`] is passed as the @world_name, the default world will be used.
    ///
    /// The registered handler can be unregistered by using
    /// webkit_user_content_manager_unregister_script_message_handler().
    /// ## `name`
    /// Name of the script message channel
    /// ## `world_name`
    /// the name of a #WebKitScriptWorld
    ///
    /// # Returns
    ///
    /// [`true`] if message handler was registered successfully, or [`false`] otherwise.
    #[doc(alias = "webkit_user_content_manager_register_script_message_handler")]
    pub fn register_script_message_handler(&self, name: &str, world_name: Option<&str>) -> bool {
        unsafe {
            from_glib(
                ffi::webkit_user_content_manager_register_script_message_handler(
                    self.to_glib_none().0,
                    name.to_glib_none().0,
                    world_name.to_glib_none().0,
                ),
            )
        }
    }

    /// Registers a new user script message handler in script world with name @world_name.
    ///
    /// Different from webkit_user_content_manager_register_script_message_handler(),
    /// when using this function to register the handler, the connected signal is
    /// script-message-with-reply-received, and a reply provided by the user is expected.
    /// Otherwise, the user will receive a default undefined value.
    ///
    /// If [`None`] is passed as the @world_name, the default world will be used.
    /// See webkit_user_content_manager_register_script_message_handler() for full description.
    ///
    /// Registering a script message handler will fail if the requested
    /// name has been already registered before.
    ///
    /// The registered handler can be unregistered by using
    /// webkit_user_content_manager_unregister_script_message_handler().
    /// ## `name`
    /// Name of the script message channel
    /// ## `world_name`
    /// the name of a #WebKitScriptWorld
    ///
    /// # Returns
    ///
    /// [`true`] if message handler was registered successfully, or [`false`] otherwise.
    #[doc(alias = "webkit_user_content_manager_register_script_message_handler_with_reply")]
    pub fn register_script_message_handler_with_reply(
        &self,
        name: &str,
        world_name: Option<&str>,
    ) -> bool {
        unsafe {
            from_glib(
                ffi::webkit_user_content_manager_register_script_message_handler_with_reply(
                    self.to_glib_none().0,
                    name.to_glib_none().0,
                    world_name.to_glib_none().0,
                ),
            )
        }
    }

    /// Removes all content filters from the given #WebKitUserContentManager.
    #[doc(alias = "webkit_user_content_manager_remove_all_filters")]
    pub fn remove_all_filters(&self) {
        unsafe {
            ffi::webkit_user_content_manager_remove_all_filters(self.to_glib_none().0);
        }
    }

    /// Removes all user scripts from the given #WebKitUserContentManager
    ///
    /// See also webkit_user_content_manager_remove_script().
    #[doc(alias = "webkit_user_content_manager_remove_all_scripts")]
    pub fn remove_all_scripts(&self) {
        unsafe {
            ffi::webkit_user_content_manager_remove_all_scripts(self.to_glib_none().0);
        }
    }

    /// Removes all user style sheets from the given #WebKitUserContentManager.
    #[doc(alias = "webkit_user_content_manager_remove_all_style_sheets")]
    pub fn remove_all_style_sheets(&self) {
        unsafe {
            ffi::webkit_user_content_manager_remove_all_style_sheets(self.to_glib_none().0);
        }
    }

    /// Removes a filter from the given #WebKitUserContentManager.
    ///
    /// Since 2.24
    /// ## `filter`
    /// A #WebKitUserContentFilter
    #[doc(alias = "webkit_user_content_manager_remove_filter")]
    pub fn remove_filter(&self, filter: &UserContentFilter) {
        unsafe {
            ffi::webkit_user_content_manager_remove_filter(
                self.to_glib_none().0,
                filter.to_glib_none().0,
            );
        }
    }

    /// Removes a filter by the given identifier.
    ///
    /// Removes a filter from the given #WebKitUserContentManager given the
    /// identifier of a #WebKitUserContentFilter as returned by
    /// webkit_user_content_filter_get_identifier().
    /// ## `filter_id`
    /// Filter identifier
    #[doc(alias = "webkit_user_content_manager_remove_filter_by_id")]
    pub fn remove_filter_by_id(&self, filter_id: &str) {
        unsafe {
            ffi::webkit_user_content_manager_remove_filter_by_id(
                self.to_glib_none().0,
                filter_id.to_glib_none().0,
            );
        }
    }

    /// Removes a #WebKitUserScript from the given #WebKitUserContentManager.
    ///
    /// See also webkit_user_content_manager_remove_all_scripts().
    /// ## `script`
    /// A #WebKitUserScript
    #[doc(alias = "webkit_user_content_manager_remove_script")]
    pub fn remove_script(&self, script: &UserScript) {
        unsafe {
            ffi::webkit_user_content_manager_remove_script(
                self.to_glib_none().0,
                script.to_glib_none().0,
            );
        }
    }

    /// Removes a #WebKitUserStyleSheet from the given #WebKitUserContentManager.
    ///
    /// See also webkit_user_content_manager_remove_all_style_sheets().
    /// ## `stylesheet`
    /// A #WebKitUserStyleSheet
    #[doc(alias = "webkit_user_content_manager_remove_style_sheet")]
    pub fn remove_style_sheet(&self, stylesheet: &UserStyleSheet) {
        unsafe {
            ffi::webkit_user_content_manager_remove_style_sheet(
                self.to_glib_none().0,
                stylesheet.to_glib_none().0,
            );
        }
    }

    /// Unregisters a previously registered message handler in script world with name @world_name.
    /// If [`None`] is passed as the @world_name, the default world will be used.
    ///
    /// Note that this does *not* disconnect handlers for the
    /// #WebKitUserContentManager::script-message-received signal;
    /// they will be kept connected, but the signal will not be emitted
    /// unless the handler name is registered again.
    ///
    /// See also webkit_user_content_manager_register_script_message_handler().
    /// ## `name`
    /// Name of the script message channel
    /// ## `world_name`
    /// the name of a #WebKitScriptWorld
    #[doc(alias = "webkit_user_content_manager_unregister_script_message_handler")]
    pub fn unregister_script_message_handler(&self, name: &str, world_name: Option<&str>) {
        unsafe {
            ffi::webkit_user_content_manager_unregister_script_message_handler(
                self.to_glib_none().0,
                name.to_glib_none().0,
                world_name.to_glib_none().0,
            );
        }
    }

    /// This signal is emitted when JavaScript in a web view calls
    /// <code>window.webkit.messageHandlers.<name>.postMessage()</code>, after registering
    /// <code><name></code> using
    /// webkit_user_content_manager_register_script_message_handler()
    /// ## `value`
    /// the value received from the JavaScript world.
    #[doc(alias = "script-message-received")]
    pub fn connect_script_message_received<F: Fn(&Self, &javascriptcore::Value) + 'static>(
        &self,
        detail: Option<&str>,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn script_message_received_trampoline<
            F: Fn(&UserContentManager, &javascriptcore::Value) + 'static,
        >(
            this: *mut ffi::WebKitUserContentManager,
            value: *mut javascriptcore::ffi::JSCValue,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this), &from_glib_borrow(value))
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            let detailed_signal_name =
                detail.map(|name| format!("script-message-received::{name}\0"));
            let signal_name: &[u8] = detailed_signal_name
                .as_ref()
                .map_or(&b"script-message-received\0"[..], |n| n.as_bytes());
            connect_raw(
                self.as_ptr() as *mut _,
                signal_name.as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    script_message_received_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    /// This signal is emitted when JavaScript in a web view calls
    /// <code>window.webkit.messageHandlers.<name>.postMessage()</code>, after registering
    /// <code><name></code> using
    /// webkit_user_content_manager_register_script_message_handler_with_reply()
    ///
    /// The given @reply can be used to send a return value with
    /// webkit_script_message_reply_return_value() or an error message with
    /// webkit_script_message_reply_return_error_message(). If none of them are
    /// called, an automatic reply with an undefined value will be sent.
    ///
    /// It is possible to handle the reply asynchronously, by simply calling
    /// g_object_ref() on the @reply and returning [`true`].
    /// ## `value`
    /// the value received from the JavaScript world.
    /// ## `reply`
    /// the #WebKitScriptMessageReply to send the reply to the script message.
    ///
    /// # Returns
    ///
    /// [`true`] to stop other handlers from being invoked for the event.
    ///    [`false`] to propagate the event further.
    #[doc(alias = "script-message-with-reply-received")]
    pub fn connect_script_message_with_reply_received<
        F: Fn(&Self, &javascriptcore::Value, &ScriptMessageReply) -> bool + 'static,
    >(
        &self,
        detail: Option<&str>,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn script_message_with_reply_received_trampoline<
            F: Fn(&UserContentManager, &javascriptcore::Value, &ScriptMessageReply) -> bool + 'static,
        >(
            this: *mut ffi::WebKitUserContentManager,
            value: *mut javascriptcore::ffi::JSCValue,
            reply: *mut ffi::WebKitScriptMessageReply,
            f: glib::ffi::gpointer,
        ) -> glib::ffi::gboolean {
            let f: &F = &*(f as *const F);
            f(
                &from_glib_borrow(this),
                &from_glib_borrow(value),
                &from_glib_borrow(reply),
            )
            .into_glib()
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            let detailed_signal_name =
                detail.map(|name| format!("script-message-with-reply-received::{name}\0"));
            let signal_name: &[u8] = detailed_signal_name
                .as_ref()
                .map_or(&b"script-message-with-reply-received\0"[..], |n| {
                    n.as_bytes()
                });
            connect_raw(
                self.as_ptr() as *mut _,
                signal_name.as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    script_message_with_reply_received_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl Default for UserContentManager {
    fn default() -> Self {
        Self::new()
    }
}