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
470
471
472
473
474
475
476
// 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, ConsoleMessage, ContextMenu, Frame, ScriptWorld, URIRequest, URIResponse, UserMessage,
    WebEditor, WebFormManager, WebHitTestResult,
};
use glib::{
    prelude::*,
    signal::{connect_raw, SignalHandlerId},
    translate::*,
};
use std::{boxed::Box as Box_, pin::Pin};

glib::wrapper! {
    /// A loaded web page.
    ///
    /// ## Properties
    ///
    ///
    /// #### `uri`
    ///  The current active URI of the #WebKitWebPage.
    ///
    /// Readable
    ///
    /// ## Signals
    ///
    ///
    /// #### `console-message-sent`
    ///  Emitted when a message is sent to the console. This can be a message
    /// produced by the use of JavaScript console API, a JavaScript exception,
    /// a security error or other errors, warnings, debug or log messages.
    /// The @console_message contains information of the message.
    ///
    ///
    ///
    ///
    /// #### `context-menu`
    ///  Emitted before a context menu is displayed in the UI Process to
    /// give the application a chance to customize the proposed menu,
    /// build its own context menu or pass user data to the UI Process.
    /// This signal is useful when the information available in the UI Process
    /// is not enough to build or customize the context menu, for example, to
    /// add menu entries depending on the node at the coordinates of the
    /// @hit_test_result. Otherwise, it's recommended to use #WebKitWebView::context-menu
    /// signal instead.
    ///
    ///
    ///
    ///
    /// #### `document-loaded`
    ///  This signal is emitted when the DOM document of a #WebKitWebPage has been
    /// loaded.
    ///
    /// You can wait for this signal to get the DOM document
    ///
    ///
    ///
    ///
    /// #### `send-request`
    ///  This signal is emitted when @request is about to be sent to
    /// the server. This signal can be used to modify the #WebKitURIRequest
    /// that will be sent to the server. You can also cancel the resource load
    /// operation by connecting to this signal and returning [`true`].
    ///
    /// In case of a server redirection this signal is
    /// emitted again with the @request argument containing the new
    /// request to be sent to the server due to the redirection and the
    /// @redirected_response parameter containing the response
    /// received by the server for the initial request.
    ///
    /// Modifications to the #WebKitURIRequest and its associated
    /// #SoupMessageHeaders will be taken into account when the request
    /// is sent over the network.
    ///
    ///
    ///
    ///
    /// #### `user-message-received`
    ///  This signal is emitted when a #WebKitUserMessage is received from the
    /// #WebKitWebView corresponding to @web_page. You can reply to the message
    /// using webkit_user_message_send_reply().
    ///
    /// You can handle the user message asynchronously by calling g_object_ref() on
    /// @message and returning [`true`]. If the last reference of @message is removed
    /// and the message has been replied, the operation in the #WebKitWebView will
    /// finish with error [`UserMessageError::UserMessageUnhandledMessage`][crate::UserMessageError::UserMessageUnhandledMessage].
    ///
    ///
    #[doc(alias = "WebKitWebPage")]
    pub struct WebPage(Object<ffi::WebKitWebPage, ffi::WebKitWebPageClass>);

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

impl WebPage {
    /// Gets the #WebKitWebEditor of a #WebKitWebPage.
    ///
    /// # Returns
    ///
    /// the #WebKitWebEditor
    #[doc(alias = "webkit_web_page_get_editor")]
    #[doc(alias = "get_editor")]
    pub fn editor(&self) -> Option<WebEditor> {
        unsafe { from_glib_none(ffi::webkit_web_page_get_editor(self.to_glib_none().0)) }
    }

    /// Get the #WebKitWebFormManager of @self in @world.
    /// ## `world`
    /// a #WebKitScriptWorld
    ///
    /// # Returns
    ///
    /// a #WebKitWebFormManager
    #[doc(alias = "webkit_web_page_get_form_manager")]
    #[doc(alias = "get_form_manager")]
    pub fn form_manager(&self, world: Option<&ScriptWorld>) -> Option<WebFormManager> {
        unsafe {
            from_glib_none(ffi::webkit_web_page_get_form_manager(
                self.to_glib_none().0,
                world.to_glib_none().0,
            ))
        }
    }

    /// Get the identifier of the #WebKitWebPage
    ///
    /// # Returns
    ///
    /// the identifier of @self
    #[doc(alias = "webkit_web_page_get_id")]
    #[doc(alias = "get_id")]
    pub fn id(&self) -> u64 {
        unsafe { ffi::webkit_web_page_get_id(self.to_glib_none().0) }
    }

    /// Returns the main frame of a #WebKitWebPage.
    ///
    /// # Returns
    ///
    /// the #WebKitFrame that is the main frame of @self
    #[doc(alias = "webkit_web_page_get_main_frame")]
    #[doc(alias = "get_main_frame")]
    pub fn main_frame(&self) -> Option<Frame> {
        unsafe { from_glib_none(ffi::webkit_web_page_get_main_frame(self.to_glib_none().0)) }
    }

    /// Returns the current active URI of @self.
    ///
    /// You can monitor the active URI by connecting to the notify::uri
    /// signal of @self.
    ///
    /// # Returns
    ///
    /// the current active URI of @web_view or [`None`] if nothing has been
    ///    loaded yet.
    #[doc(alias = "webkit_web_page_get_uri")]
    #[doc(alias = "get_uri")]
    pub fn uri(&self) -> Option<glib::GString> {
        unsafe { from_glib_none(ffi::webkit_web_page_get_uri(self.to_glib_none().0)) }
    }

    /// Send @message to the #WebKitWebView corresponding to @self. If @message is floating, it's consumed.
    ///
    /// If you don't expect any reply, or you simply want to ignore it, you can pass [`None`] as @callback.
    /// When the operation is finished, @callback will be called. You can then call
    /// webkit_web_page_send_message_to_view_finish() to get the message reply.
    /// ## `message`
    /// a #WebKitUserMessage
    /// ## `cancellable`
    /// a #GCancellable or [`None`] to ignore
    /// ## `callback`
    /// A #GAsyncReadyCallback to call when the request is satisfied or [`None`]
    #[doc(alias = "webkit_web_page_send_message_to_view")]
    pub fn send_message_to_view<P: FnOnce(Result<UserMessage, glib::Error>) + 'static>(
        &self,
        message: &UserMessage,
        cancellable: Option<&impl IsA<gio::Cancellable>>,
        callback: P,
    ) {
        let main_context = glib::MainContext::ref_thread_default();
        let is_main_context_owner = main_context.is_owner();
        let has_acquired_main_context = (!is_main_context_owner)
            .then(|| main_context.acquire().ok())
            .flatten();
        assert!(
            is_main_context_owner || has_acquired_main_context.is_some(),
            "Async operations only allowed if the thread is owning the MainContext"
        );

        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
        unsafe extern "C" fn send_message_to_view_trampoline<
            P: FnOnce(Result<UserMessage, glib::Error>) + 'static,
        >(
            _source_object: *mut glib::gobject_ffi::GObject,
            res: *mut gio::ffi::GAsyncResult,
            user_data: glib::ffi::gpointer,
        ) {
            let mut error = std::ptr::null_mut();
            let ret = ffi::webkit_web_page_send_message_to_view_finish(
                _source_object as *mut _,
                res,
                &mut error,
            );
            let result = if error.is_null() {
                Ok(from_glib_full(ret))
            } else {
                Err(from_glib_full(error))
            };
            let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
                Box_::from_raw(user_data as *mut _);
            let callback: P = callback.into_inner();
            callback(result);
        }
        let callback = send_message_to_view_trampoline::<P>;
        unsafe {
            ffi::webkit_web_page_send_message_to_view(
                self.to_glib_none().0,
                message.to_glib_none().0,
                cancellable.map(|p| p.as_ref()).to_glib_none().0,
                Some(callback),
                Box_::into_raw(user_data) as *mut _,
            );
        }
    }

    pub fn send_message_to_view_future(
        &self,
        message: &UserMessage,
    ) -> Pin<Box_<dyn std::future::Future<Output = Result<UserMessage, glib::Error>> + 'static>>
    {
        let message = message.clone();
        Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
            obj.send_message_to_view(&message, Some(cancellable), move |res| {
                send.resolve(res);
            });
        }))
    }

    /// Emitted when a message is sent to the console. This can be a message
    /// produced by the use of JavaScript console API, a JavaScript exception,
    /// a security error or other errors, warnings, debug or log messages.
    /// The @console_message contains information of the message.
    /// ## `console_message`
    /// the #WebKitConsoleMessage
    #[doc(alias = "console-message-sent")]
    pub fn connect_console_message_sent<F: Fn(&Self, &ConsoleMessage) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn console_message_sent_trampoline<
            F: Fn(&WebPage, &ConsoleMessage) + 'static,
        >(
            this: *mut ffi::WebKitWebPage,
            console_message: *mut ffi::WebKitConsoleMessage,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this), &from_glib_borrow(console_message))
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"console-message-sent\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    console_message_sent_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    /// Emitted before a context menu is displayed in the UI Process to
    /// give the application a chance to customize the proposed menu,
    /// build its own context menu or pass user data to the UI Process.
    /// This signal is useful when the information available in the UI Process
    /// is not enough to build or customize the context menu, for example, to
    /// add menu entries depending on the node at the coordinates of the
    /// @hit_test_result. Otherwise, it's recommended to use #WebKitWebView::context-menu
    /// signal instead.
    /// ## `context_menu`
    /// the proposed #WebKitContextMenu
    /// ## `hit_test_result`
    /// a #WebKitWebHitTestResult
    ///
    /// # Returns
    ///
    /// [`true`] if the proposed @context_menu has been modified, or [`false`] otherwise.
    #[doc(alias = "context-menu")]
    pub fn connect_context_menu<F: Fn(&Self, &ContextMenu, &WebHitTestResult) -> bool + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn context_menu_trampoline<
            F: Fn(&WebPage, &ContextMenu, &WebHitTestResult) -> bool + 'static,
        >(
            this: *mut ffi::WebKitWebPage,
            context_menu: *mut ffi::WebKitContextMenu,
            hit_test_result: *mut ffi::WebKitWebHitTestResult,
            f: glib::ffi::gpointer,
        ) -> glib::ffi::gboolean {
            let f: &F = &*(f as *const F);
            f(
                &from_glib_borrow(this),
                &from_glib_borrow(context_menu),
                &from_glib_borrow(hit_test_result),
            )
            .into_glib()
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"context-menu\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    context_menu_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    /// This signal is emitted when the DOM document of a #WebKitWebPage has been
    /// loaded.
    ///
    /// You can wait for this signal to get the DOM document
    #[doc(alias = "document-loaded")]
    pub fn connect_document_loaded<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn document_loaded_trampoline<F: Fn(&WebPage) + 'static>(
            this: *mut ffi::WebKitWebPage,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this))
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"document-loaded\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    document_loaded_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    /// This signal is emitted when @request is about to be sent to
    /// the server. This signal can be used to modify the #WebKitURIRequest
    /// that will be sent to the server. You can also cancel the resource load
    /// operation by connecting to this signal and returning [`true`].
    ///
    /// In case of a server redirection this signal is
    /// emitted again with the @request argument containing the new
    /// request to be sent to the server due to the redirection and the
    /// @redirected_response parameter containing the response
    /// received by the server for the initial request.
    ///
    /// Modifications to the #WebKitURIRequest and its associated
    /// #SoupMessageHeaders will be taken into account when the request
    /// is sent over the network.
    /// ## `request`
    /// a #WebKitURIRequest
    /// ## `redirected_response`
    /// a #WebKitURIResponse, or [`None`]
    ///
    /// # Returns
    ///
    /// [`true`] to stop other handlers from being invoked for the event.
    ///    [`false`] to continue emission of the event.
    #[doc(alias = "send-request")]
    pub fn connect_send_request<F: Fn(&Self, &URIRequest, &URIResponse) -> bool + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn send_request_trampoline<
            F: Fn(&WebPage, &URIRequest, &URIResponse) -> bool + 'static,
        >(
            this: *mut ffi::WebKitWebPage,
            request: *mut ffi::WebKitURIRequest,
            redirected_response: *mut ffi::WebKitURIResponse,
            f: glib::ffi::gpointer,
        ) -> glib::ffi::gboolean {
            let f: &F = &*(f as *const F);
            f(
                &from_glib_borrow(this),
                &from_glib_borrow(request),
                &from_glib_borrow(redirected_response),
            )
            .into_glib()
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"send-request\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    send_request_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    /// This signal is emitted when a #WebKitUserMessage is received from the
    /// #WebKitWebView corresponding to @web_page. You can reply to the message
    /// using webkit_user_message_send_reply().
    ///
    /// You can handle the user message asynchronously by calling g_object_ref() on
    /// @message and returning [`true`]. If the last reference of @message is removed
    /// and the message has been replied, the operation in the #WebKitWebView will
    /// finish with error [`UserMessageError::UserMessageUnhandledMessage`][crate::UserMessageError::UserMessageUnhandledMessage].
    /// ## `message`
    /// the #WebKitUserMessage received
    ///
    /// # Returns
    ///
    /// [`true`] if the message was handled, or [`false`] otherwise.
    #[doc(alias = "user-message-received")]
    pub fn connect_user_message_received<F: Fn(&Self, &UserMessage) -> bool + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn user_message_received_trampoline<
            F: Fn(&WebPage, &UserMessage) -> bool + 'static,
        >(
            this: *mut ffi::WebKitWebPage,
            message: *mut ffi::WebKitUserMessage,
            f: glib::ffi::gpointer,
        ) -> glib::ffi::gboolean {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this), &from_glib_borrow(message)).into_glib()
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"user-message-received\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    user_message_received_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "uri")]
    pub fn connect_uri_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_uri_trampoline<F: Fn(&WebPage) + 'static>(
            this: *mut ffi::WebKitWebPage,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this))
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::uri\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    notify_uri_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}