|
5 | 5 | //! Unix platform extensions for [`WebContext`](super::WebContext).
|
6 | 6 |
|
7 | 7 | use crate::{Error, RequestAsyncResponder};
|
8 |
| -use gtk::glib::{self, MainContext, ObjectExt}; |
| 8 | +use gtk::{ |
| 9 | + glib::{self, MainContext, ObjectExt}, |
| 10 | + traits::WidgetExt, |
| 11 | +}; |
9 | 12 | use http::{header::CONTENT_TYPE, HeaderName, HeaderValue, Request, Response as HttpResponse};
|
10 | 13 | use soup::{MessageHeaders, MessageHeadersType};
|
11 | 14 | use std::{
|
@@ -474,13 +477,36 @@ impl WebViewUriLoader {
|
474 | 477 | headers,
|
475 | 478 | }) = self.pop()
|
476 | 479 | {
|
477 |
| - // we do not need to listen to failed events because those will finish the change event anyways |
478 |
| - webview.connect_load_changed(move |_, event| { |
| 480 | + // ensure that the lock is released when the webview is destroyed before LoadEvent::Finished is handled |
| 481 | + let self_ = self.clone(); |
| 482 | + let destroy_id = webview.connect_destroy(move |_| { |
| 483 | + self_.unlock(); |
| 484 | + self_.clone().flush(); |
| 485 | + }); |
| 486 | + let destroy_id_guard = Mutex::new(Some(destroy_id)); |
| 487 | + |
| 488 | + let load_changed_id_guard = Rc::new(Mutex::new(None)); |
| 489 | + let load_changed_id_guard_ = load_changed_id_guard.clone(); |
| 490 | + let self_ = self.clone(); |
| 491 | + // noet: we do not need to listen to failed events because those will finish the change event anyways |
| 492 | + let load_changed_id = webview.connect_load_changed(move |w, event| { |
479 | 493 | if let LoadEvent::Finished = event {
|
480 |
| - self.unlock(); |
481 |
| - self.clone().flush(); |
| 494 | + self_.unlock(); |
| 495 | + self_.clone().flush(); |
| 496 | + |
| 497 | + // unregister listeners |
| 498 | + if let Some(id) = destroy_id_guard.lock().unwrap().take() { |
| 499 | + w.disconnect(id); |
| 500 | + } |
| 501 | + if let Some(id) = load_changed_id_guard_.lock().unwrap().take() { |
| 502 | + w.disconnect(id); |
| 503 | + } |
482 | 504 | };
|
483 | 505 | });
|
| 506 | + load_changed_id_guard |
| 507 | + .lock() |
| 508 | + .unwrap() |
| 509 | + .replace(load_changed_id); |
484 | 510 |
|
485 | 511 | if let Some(headers) = headers {
|
486 | 512 | let req = URIRequest::builder().uri(&uri).build();
|
|
0 commit comments