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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files.git)
// from webkit2gtk-gir-files
// DO NOT EDIT

#[cfg(any(feature = "v2_8", feature = "dox"))]
#[cfg_attr(feature = "dox", doc(cfg(feature = "v2_8")))]
use crate::JavascriptResult;
#[cfg(any(feature = "v2_24", feature = "dox"))]
#[cfg_attr(feature = "dox", doc(cfg(feature = "v2_24")))]
use crate::UserContentFilter;
use crate::UserScript;
use crate::UserStyleSheet;
#[cfg(any(feature = "v2_8", feature = "dox"))]
#[cfg_attr(feature = "dox", doc(cfg(feature = "v2_8")))]
use glib::object::Cast;
use glib::object::IsA;
#[cfg(any(feature = "v2_8", feature = "dox"))]
#[cfg_attr(feature = "dox", doc(cfg(feature = "v2_8")))]
use glib::signal::connect_raw;
#[cfg(any(feature = "v2_8", feature = "dox"))]
#[cfg_attr(feature = "dox", doc(cfg(feature = "v2_8")))]
use glib::signal::SignalHandlerId;
use glib::translate::*;
#[cfg(any(feature = "v2_8", feature = "dox"))]
#[cfg_attr(feature = "dox", doc(cfg(feature = "v2_8")))]
use std::boxed::Box as Box_;
use std::fmt;
#[cfg(any(feature = "v2_8", feature = "dox"))]
#[cfg_attr(feature = "dox", doc(cfg(feature = "v2_8")))]
use std::mem::transmute;

glib::wrapper! {
    /// Manages user-defined content which affects web pages.
    ///
    /// Using a [`UserContentManager`][crate::UserContentManager] user CSS style sheets can be set to
    /// be injected in the web pages loaded by a [`WebView`][crate::WebView], by
    /// [`UserContentManagerExt::add_style_sheet()`][crate::prelude::UserContentManagerExt::add_style_sheet()].
    ///
    /// To use a [`UserContentManager`][crate::UserContentManager], it must be created using
    /// [`new()`][Self::new()], and then passed to
    /// [`WebView::with_user_content_manager()`][crate::WebView::with_user_content_manager()]. User style
    /// sheets can be created with [`UserStyleSheet::new()`][crate::UserStyleSheet::new()].
    ///
    /// User style sheets can be added and removed at any time, but
    /// they will affect the web pages loaded afterwards.
    ///
    /// # Implements
    ///
    /// [`UserContentManagerExt`][trait@crate::prelude::UserContentManagerExt], [`trait@glib::ObjectExt`]
    #[doc(alias = "WebKitUserContentManager")]
    pub struct UserContentManager(Object<ffi::WebKitUserContentManager, ffi::WebKitUserContentManagerClass>);

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

impl UserContentManager {
    pub const NONE: Option<&'static UserContentManager> = None;

    /// Creates a new user content manager.
    ///
    /// # Returns
    ///
    /// A [`UserContentManager`][crate::UserContentManager]
    #[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()) }
    }
}

#[cfg(any(feature = "v2_6", feature = "dox"))]
#[cfg_attr(feature = "dox", doc(cfg(feature = "v2_6")))]
impl Default for UserContentManager {
    fn default() -> Self {
        Self::new()
    }
}

/// Trait containing all [`struct@UserContentManager`] methods.
///
/// # Implementors
///
/// [`UserContentManager`][struct@crate::UserContentManager]
pub trait UserContentManagerExt: 'static {
    /// Adds a [`UserContentFilter`][crate::UserContentFilter] to the given [`UserContentManager`][crate::UserContentManager].
    ///
    /// The same [`UserContentFilter`][crate::UserContentFilter] can be reused with multiple
    /// [`UserContentManager`][crate::UserContentManager] instances.
    ///
    /// Filters need to be saved and loaded from [`UserContentFilterStore`][crate::UserContentFilterStore].
    /// ## `filter`
    /// A [`UserContentFilter`][crate::UserContentFilter]
    #[cfg(any(feature = "v2_24", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_24")))]
    #[doc(alias = "webkit_user_content_manager_add_filter")]
    fn add_filter(&self, filter: &UserContentFilter);

    /// Adds a [`UserScript`][crate::UserScript] to the given [`UserContentManager`][crate::UserContentManager].
    ///
    /// The same [`UserScript`][crate::UserScript] can be reused with multiple
    /// [`UserContentManager`][crate::UserContentManager] instances.
    /// ## `script`
    /// A [`UserScript`][crate::UserScript]
    #[doc(alias = "webkit_user_content_manager_add_script")]
    fn add_script(&self, script: &UserScript);

    /// Adds a [`UserStyleSheet`][crate::UserStyleSheet] to the given [`UserContentManager`][crate::UserContentManager].
    ///
    /// The same [`UserStyleSheet`][crate::UserStyleSheet] can be reused with multiple
    /// [`UserContentManager`][crate::UserContentManager] instances.
    /// ## `stylesheet`
    /// A [`UserStyleSheet`][crate::UserStyleSheet]
    #[doc(alias = "webkit_user_content_manager_add_style_sheet")]
    fn add_style_sheet(&self, stylesheet: &UserStyleSheet);

    /// Registers a new user script message handler.
    ///
    /// 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 `signal::UserContentManager::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.
    /// ## `name`
    /// Name of the script message channel
    ///
    /// # Returns
    ///
    /// [`true`] if message handler was registered successfully, or [`false`] otherwise.
    #[cfg(any(feature = "v2_8", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_8")))]
    #[doc(alias = "webkit_user_content_manager_register_script_message_handler")]
    fn register_script_message_handler(&self, name: &str) -> bool;

    /// Registers a new user script message handler in script world.
    ///
    /// Registers a new user script message handler in script world with name `world_name`.
    /// See [`register_script_message_handler()`][Self::register_script_message_handler()] for full description.
    ///
    /// Registering a script message handler will fail if the requested
    /// name has been already registered before.
    /// ## `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.
    #[cfg(any(feature = "v2_22", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_22")))]
    #[doc(alias = "webkit_user_content_manager_register_script_message_handler_in_world")]
    fn register_script_message_handler_in_world(&self, name: &str, world_name: &str) -> bool;

    /// Removes all content filters from the given [`UserContentManager`][crate::UserContentManager].
    #[cfg(any(feature = "v2_24", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_24")))]
    #[doc(alias = "webkit_user_content_manager_remove_all_filters")]
    fn remove_all_filters(&self);

    /// Removes all user scripts from the given [`UserContentManager`][crate::UserContentManager]
    ///
    /// See also [`remove_script()`][Self::remove_script()].
    #[doc(alias = "webkit_user_content_manager_remove_all_scripts")]
    fn remove_all_scripts(&self);

    /// Removes all user style sheets from the given [`UserContentManager`][crate::UserContentManager].
    #[doc(alias = "webkit_user_content_manager_remove_all_style_sheets")]
    fn remove_all_style_sheets(&self);

    /// Removes a filter from the given [`UserContentManager`][crate::UserContentManager].
    ///
    /// Since 2.24
    /// ## `filter`
    /// A [`UserContentFilter`][crate::UserContentFilter]
    #[cfg(any(feature = "v2_24", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_24")))]
    #[doc(alias = "webkit_user_content_manager_remove_filter")]
    fn remove_filter(&self, filter: &UserContentFilter);

    /// Removes a filter by the given identifier.
    ///
    /// Removes a filter from the given [`UserContentManager`][crate::UserContentManager] given the
    /// identifier of a [`UserContentFilter`][crate::UserContentFilter] as returned by
    /// [`UserContentFilter::identifier()`][crate::UserContentFilter::identifier()].
    /// ## `filter_id`
    /// Filter identifier
    #[cfg(any(feature = "v2_26", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_26")))]
    #[doc(alias = "webkit_user_content_manager_remove_filter_by_id")]
    fn remove_filter_by_id(&self, filter_id: &str);

    /// Removes a [`UserScript`][crate::UserScript] from the given [`UserContentManager`][crate::UserContentManager].
    ///
    /// See also [`remove_all_scripts()`][Self::remove_all_scripts()].
    /// ## `script`
    /// A [`UserScript`][crate::UserScript]
    #[cfg(any(feature = "v2_32", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_32")))]
    #[doc(alias = "webkit_user_content_manager_remove_script")]
    fn remove_script(&self, script: &UserScript);

    /// Removes a [`UserStyleSheet`][crate::UserStyleSheet] from the given [`UserContentManager`][crate::UserContentManager].
    ///
    /// See also [`remove_all_style_sheets()`][Self::remove_all_style_sheets()].
    /// ## `stylesheet`
    /// A [`UserStyleSheet`][crate::UserStyleSheet]
    #[cfg(any(feature = "v2_32", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_32")))]
    #[doc(alias = "webkit_user_content_manager_remove_style_sheet")]
    fn remove_style_sheet(&self, stylesheet: &UserStyleSheet);

    /// Unregisters a previously registered message handler.
    ///
    /// Note that this does *not* disconnect handlers for the
    /// `signal::UserContentManager::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 [`register_script_message_handler()`][Self::register_script_message_handler()].
    /// ## `name`
    /// Name of the script message channel
    #[cfg(any(feature = "v2_8", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_8")))]
    #[doc(alias = "webkit_user_content_manager_unregister_script_message_handler")]
    fn unregister_script_message_handler(&self, name: &str);

    /// Unregisters a previously registered message handler in script world with name `world_name`.
    ///
    /// Note that this does *not* disconnect handlers for the
    /// `signal::UserContentManager::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 [`register_script_message_handler_in_world()`][Self::register_script_message_handler_in_world()].
    /// ## `name`
    /// Name of the script message channel
    /// ## `world_name`
    /// the name of a `WebKitScriptWorld`
    #[cfg(any(feature = "v2_22", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_22")))]
    #[doc(alias = "webkit_user_content_manager_unregister_script_message_handler_in_world")]
    fn unregister_script_message_handler_in_world(&self, name: &str, world_name: &str);

    /// This signal is emitted when JavaScript in a web view calls
    /// `<code>`window.webkit.messageHandlers.`<name>`.postMessage()`</code>`, after registering
    /// `<code>``<name>``</code>` using
    /// [`register_script_message_handler()`][Self::register_script_message_handler()]
    /// ## `js_result`
    /// the [`JavascriptResult`][crate::JavascriptResult] holding the value received from the JavaScript world.
    #[cfg(any(feature = "v2_8", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_8")))]
    #[doc(alias = "script-message-received")]
    fn connect_script_message_received<F: Fn(&Self, &JavascriptResult) + 'static>(
        &self,
        detail: Option<&str>,
        f: F,
    ) -> SignalHandlerId;
}

impl<O: IsA<UserContentManager>> UserContentManagerExt for O {
    #[cfg(any(feature = "v2_24", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_24")))]
    fn add_filter(&self, filter: &UserContentFilter) {
        unsafe {
            ffi::webkit_user_content_manager_add_filter(
                self.as_ref().to_glib_none().0,
                filter.to_glib_none().0,
            );
        }
    }

    fn add_script(&self, script: &UserScript) {
        unsafe {
            ffi::webkit_user_content_manager_add_script(
                self.as_ref().to_glib_none().0,
                script.to_glib_none().0,
            );
        }
    }

    fn add_style_sheet(&self, stylesheet: &UserStyleSheet) {
        unsafe {
            ffi::webkit_user_content_manager_add_style_sheet(
                self.as_ref().to_glib_none().0,
                stylesheet.to_glib_none().0,
            );
        }
    }

    #[cfg(any(feature = "v2_8", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_8")))]
    fn register_script_message_handler(&self, name: &str) -> bool {
        unsafe {
            from_glib(
                ffi::webkit_user_content_manager_register_script_message_handler(
                    self.as_ref().to_glib_none().0,
                    name.to_glib_none().0,
                ),
            )
        }
    }

    #[cfg(any(feature = "v2_22", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_22")))]
    fn register_script_message_handler_in_world(&self, name: &str, world_name: &str) -> bool {
        unsafe {
            from_glib(
                ffi::webkit_user_content_manager_register_script_message_handler_in_world(
                    self.as_ref().to_glib_none().0,
                    name.to_glib_none().0,
                    world_name.to_glib_none().0,
                ),
            )
        }
    }

    #[cfg(any(feature = "v2_24", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_24")))]
    fn remove_all_filters(&self) {
        unsafe {
            ffi::webkit_user_content_manager_remove_all_filters(self.as_ref().to_glib_none().0);
        }
    }

    fn remove_all_scripts(&self) {
        unsafe {
            ffi::webkit_user_content_manager_remove_all_scripts(self.as_ref().to_glib_none().0);
        }
    }

    fn remove_all_style_sheets(&self) {
        unsafe {
            ffi::webkit_user_content_manager_remove_all_style_sheets(
                self.as_ref().to_glib_none().0,
            );
        }
    }

    #[cfg(any(feature = "v2_24", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_24")))]
    fn remove_filter(&self, filter: &UserContentFilter) {
        unsafe {
            ffi::webkit_user_content_manager_remove_filter(
                self.as_ref().to_glib_none().0,
                filter.to_glib_none().0,
            );
        }
    }

    #[cfg(any(feature = "v2_26", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_26")))]
    fn remove_filter_by_id(&self, filter_id: &str) {
        unsafe {
            ffi::webkit_user_content_manager_remove_filter_by_id(
                self.as_ref().to_glib_none().0,
                filter_id.to_glib_none().0,
            );
        }
    }

    #[cfg(any(feature = "v2_32", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_32")))]
    fn remove_script(&self, script: &UserScript) {
        unsafe {
            ffi::webkit_user_content_manager_remove_script(
                self.as_ref().to_glib_none().0,
                script.to_glib_none().0,
            );
        }
    }

    #[cfg(any(feature = "v2_32", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_32")))]
    fn remove_style_sheet(&self, stylesheet: &UserStyleSheet) {
        unsafe {
            ffi::webkit_user_content_manager_remove_style_sheet(
                self.as_ref().to_glib_none().0,
                stylesheet.to_glib_none().0,
            );
        }
    }

    #[cfg(any(feature = "v2_8", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_8")))]
    fn unregister_script_message_handler(&self, name: &str) {
        unsafe {
            ffi::webkit_user_content_manager_unregister_script_message_handler(
                self.as_ref().to_glib_none().0,
                name.to_glib_none().0,
            );
        }
    }

    #[cfg(any(feature = "v2_22", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_22")))]
    fn unregister_script_message_handler_in_world(&self, name: &str, world_name: &str) {
        unsafe {
            ffi::webkit_user_content_manager_unregister_script_message_handler_in_world(
                self.as_ref().to_glib_none().0,
                name.to_glib_none().0,
                world_name.to_glib_none().0,
            );
        }
    }

    #[cfg(any(feature = "v2_8", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_8")))]
    fn connect_script_message_received<F: Fn(&Self, &JavascriptResult) + 'static>(
        &self,
        detail: Option<&str>,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn script_message_received_trampoline<
            P: IsA<UserContentManager>,
            F: Fn(&P, &JavascriptResult) + 'static,
        >(
            this: *mut ffi::WebKitUserContentManager,
            js_result: *mut ffi::WebKitJavascriptResult,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(
                UserContentManager::from_glib_borrow(this).unsafe_cast_ref(),
                &from_glib_borrow(js_result),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            let detailed_signal_name =
                detail.map(|name| format!("script-message-received::{}\0", name));
            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(transmute::<_, unsafe extern "C" fn()>(
                    script_message_received_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl fmt::Display for UserContentManager {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("UserContentManager")
    }
}