Skip to content

Commit

Permalink
desktop: Close dialogs when the player is destroyed (#18238)
Browse files Browse the repository at this point in the history
* frontend_utils: Handle failures when sending SocketAction

Instead of panicking, we now handle the failures by closing the connection.
The failures are always logged as warnings.

* desktop: Close dialogs when the player is destroyed

This patch closes all dialogs upon destroying a player
(e.g. by closing the file or opening a new one).
Only dialogs that have a notifier are closed.
  • Loading branch information
kjarosh authored Oct 17, 2024
1 parent 7ed23b0 commit 5faa313
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 27 deletions.
2 changes: 1 addition & 1 deletion desktop/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ impl ApplicationHandler<RuffleEvent> for App {

(Some(main_window), RuffleEvent::CloseFile) => {
main_window.gui.window().set_title("Ruffle"); // Reset title since file has been closed.
main_window.player.destroy();
main_window.gui.close_movie(&mut main_window.player);
}

(Some(main_window), RuffleEvent::EnterFullScreen) => {
Expand Down
5 changes: 5 additions & 0 deletions desktop/src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ impl RuffleGui {
self.context_menu.is_some()
}

/// Notifies the GUI that the player has been destroyed.
fn on_player_destroyed(&mut self) {
self.dialogs.close_dialogs_with_notifiers();
}

/// Notifies the GUI that a new player was created.
fn on_player_created(
&mut self,
Expand Down
6 changes: 6 additions & 0 deletions desktop/src/gui/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,18 @@ impl GuiController {
response.consumed
}

pub fn close_movie(&mut self, player: &mut PlayerController) {
player.destroy();
self.gui.on_player_destroyed();
}

pub fn create_movie(
&mut self,
player: &mut PlayerController,
opt: LaunchOptions,
movie_url: Url,
) {
self.close_movie(player);
let movie_view = MovieView::new(
self.movie_view_renderer.clone(),
&self.descriptors.device,
Expand Down
10 changes: 10 additions & 0 deletions desktop/src/gui/dialogs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ impl Dialogs {
self.picker.clone()
}

/// Close all dialogs that have someone waiting for an answer.
///
/// This method may be used when the original receiver is closed,
/// e.g. by loading a new movie or destroying the existing one.
pub fn close_dialogs_with_notifiers(&mut self) {
self.network_access_dialog_queue.clear();
self.filesystem_access_dialog = None;
self.filesystem_access_dialog_queue.clear();
}

pub fn recreate_open_dialog(
&mut self,
opt: LaunchOptions,
Expand Down
56 changes: 30 additions & 26 deletions frontend-utils/src/backends/navigator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,18 @@ impl<F: FutureSpawner + 'static, I: NavigatorInterface> NavigatorBackend
receiver: Receiver<Vec<u8>>,
sender: Sender<SocketAction>,
) {
/// Tries to send the given action properly handling failures.
///
/// Returns `true` when the action has been sent properly,
/// `false` when the channel is closed.
async fn send_action(sender: &Sender<SocketAction>, action: SocketAction) -> bool {
sender
.send(action)
.await
.inspect_err(|err| tracing::warn!("Failed to send SocketAction: {}", err))
.is_ok()
}

let addr = format!("{}:{}", host, port);
let is_allowed = self.socket_allowed.contains(&addr);
let socket_mode = self.socket_mode;
Expand All @@ -325,9 +337,8 @@ impl<F: FutureSpawner + 'static, I: NavigatorInterface> NavigatorBackend
(false, SocketMode::Allow) | (true, _) => {} // the process is allowed to continue. just dont do anything.
(false, SocketMode::Deny) => {
// Just fail the connection.
sender
.try_send(SocketAction::Connect(handle, ConnectionState::Failed))
.expect("working channel send");
let action = SocketAction::Connect(handle, ConnectionState::Failed);
let _ = send_action(&sender, action).await;

tracing::warn!(
"SWF tried to open a socket, but opening a socket is not allowed"
Expand All @@ -340,10 +351,8 @@ impl<F: FutureSpawner + 'static, I: NavigatorInterface> NavigatorBackend

if !attempt_sandbox_connect {
// fail the connection.
sender
.try_send(SocketAction::Connect(handle, ConnectionState::Failed))
.expect("working channel send");

let action = SocketAction::Connect(handle, ConnectionState::Failed);
let _ = send_action(&sender, action).await;
return;
}
}
Expand All @@ -359,23 +368,21 @@ impl<F: FutureSpawner + 'static, I: NavigatorInterface> NavigatorBackend
let mut stream = match TcpStream::connect((host, port)).or(timeout).await {
Err(e) if e.kind() == ErrorKind::TimedOut => {
warn!("Connection to {}:{} timed out", host2, port);
sender
.try_send(SocketAction::Connect(handle, ConnectionState::TimedOut))
.expect("working channel send");
let action = SocketAction::Connect(handle, ConnectionState::TimedOut);
let _ = send_action(&sender, action).await;
return;
}
Ok(stream) => {
sender
.try_send(SocketAction::Connect(handle, ConnectionState::Connected))
.expect("working channel send");

let action = SocketAction::Connect(handle, ConnectionState::Connected);
if !send_action(&sender, action).await {
return;
}
stream
}
Err(err) => {
warn!("Failed to connect to {}:{}, error: {}", host2, port, err);
sender
.try_send(SocketAction::Connect(handle, ConnectionState::Failed))
.expect("working channel send");
let action = SocketAction::Connect(handle, ConnectionState::Failed);
let _ = send_action(&sender, action).await;
return;
}
};
Expand All @@ -392,17 +399,16 @@ impl<F: FutureSpawner + 'static, I: NavigatorInterface> NavigatorBackend
match read.read(&mut buffer).await {
Err(e) if e.kind() == ErrorKind::TimedOut => {} // try again later.
Err(_) | Ok(0) => {
sender
.try_send(SocketAction::Close(handle))
.expect("working channel send");
let _ = send_action(&sender, SocketAction::Close(handle)).await;
break;
}
Ok(read) => {
let buffer = buffer.into_iter().take(read).collect::<Vec<_>>();

sender
.try_send(SocketAction::Data(handle, buffer))
.expect("working channel send");
let action = SocketAction::Data(handle, buffer);
if !send_action(&sender, action).await {
return;
}
}
};
}
Expand Down Expand Up @@ -431,9 +437,7 @@ impl<F: FutureSpawner + 'static, I: NavigatorInterface> NavigatorBackend
match write.write(&pending_write).await {
Err(e) if e.kind() == ErrorKind::TimedOut => {} // try again later.
Err(_) => {
sender2
.try_send(SocketAction::Close(handle))
.expect("working channel send");
let _ = send_action(&sender2, SocketAction::Close(handle)).await;
return;
}
Ok(written) => {
Expand Down

0 comments on commit 5faa313

Please sign in to comment.