Skip to content

Commit

Permalink
Implement reset_on_disconnect() on AssociatedReceiver
Browse files Browse the repository at this point in the history
I'm using AssociatedReceivers in the //remoting directory for our IPC
and wanted a way to reset the receiver automatically when the process
on the other side of the connection crashes. This is the same method
I recently added to AssociatedRemote however the associate remote
and receiver impls don't share a base class so I put it in the
AssociatedReceiverBase header.

Bug: NO_BUG
Change-Id: Ie40101145e5a6446f9623803fdc93e05470bd97a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3268785
Reviewed-by: Yuwei Huang <yuweih@chromium.org>
Reviewed-by: Ken Rockot <rockot@google.com>
Commit-Queue: Joe Downing <joedow@chromium.org>
Cr-Commit-Position: refs/heads/main@{#939936}
  • Loading branch information
joedow-42 authored and Chromium LUCI CQ committed Nov 9, 2021
1 parent 80b1ee8 commit cae37b5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
7 changes: 7 additions & 0 deletions mojo/public/cpp/bindings/associated_receiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class COMPONENT_EXPORT(MOJO_CPP_BINDINGS) AssociatedReceiverBase {
void set_disconnect_handler(base::OnceClosure error_handler);
void set_disconnect_with_reason_handler(
ConnectionErrorWithReasonCallback error_handler);
void reset_on_disconnect();

bool is_bound() const { return !!endpoint_client_; }
explicit operator bool() const { return !!endpoint_client_; }
Expand Down Expand Up @@ -149,6 +150,12 @@ class AssociatedReceiver : public internal::AssociatedReceiverBase {
// about why the remote endpoint was closed, if provided.
using AssociatedReceiverBase::set_disconnect_with_reason_handler;

// Resets this AssociatedReceiver on disconnect. Note that this replaces any
// previously set disconnection handler. Must be called on a bound
// AssociatedReceiver object, and only remains set as long as the
// AssociatedReceiver is both bound and connected.
using AssociatedReceiverBase::reset_on_disconnect;

// Resets this AssociatedReceiver to an unbound state. An unbound
// AssociatedReceiver will NEVER schedule method calls or disconnection
// notifications, and any pending tasks which were scheduled prior to
Expand Down
6 changes: 6 additions & 0 deletions mojo/public/cpp/bindings/lib/associated_receiver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ void AssociatedReceiverBase::set_disconnect_with_reason_handler(
std::move(error_handler));
}

void AssociatedReceiverBase::reset_on_disconnect() {
DCHECK(is_bound());
set_disconnect_handler(
base::BindOnce(&AssociatedReceiverBase::reset, base::Unretained(this)));
}

void AssociatedReceiverBase::FlushForTesting() {
endpoint_client_->FlushForTesting(); // IN-TEST
}
Expand Down
10 changes: 3 additions & 7 deletions remoting/host/desktop_session_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -747,13 +747,9 @@ void DesktopSessionWin::OnAssociatedInterfaceRequest(
pending_receiver(std::move(handle));
desktop_session_request_handler_.Bind(std::move(pending_receiver));

// Set up a disconnect handler so |desktop_session_request_handler_| can be
// re-bound if |launcher_| spawns a new desktop process.
// TODO(joedow): Add a reset_on_disconnect() method on associated receiver.
desktop_session_request_handler_.set_disconnect_handler(base::BindOnce(
[](mojo::AssociatedReceiver<mojom::DesktopSessionRequestHandler>*
receiver) { receiver->reset(); },
base::Unretained(&desktop_session_request_handler_)));
// Reset the receiver on disconnect so |desktop_session_request_handler_|
// can be re-bound if |launcher_| spawns a new desktop process.
desktop_session_request_handler_.reset_on_disconnect();
} else {
LOG(ERROR) << "Unknown associated interface requested: " << interface_name
<< ", crashing the desktop process";
Expand Down

0 comments on commit cae37b5

Please sign in to comment.