Skip to content

Commit

Permalink
mojo: Add ReceiverSetBase::RemoveWithReason
Browse files Browse the repository at this point in the history
To support removing a receiver in ReceiverSet with reason, like
Receiver::ResetWithReason, adds ReceiverSetBase::RemoveWithReason().

BUG=chromium:1239129
TEST=builds

Change-Id: I59e2cb53bd98f92fef9f07bd95f06abf3a8e443d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3088885
Reviewed-by: Ken Rockot <rockot@google.com>
Commit-Queue: Cheng-Hao Yang <chenghaoyang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#912107}
  • Loading branch information
HarveyCHYang authored and Chromium LUCI CQ committed Aug 16, 2021
1 parent 3a40444 commit 4f71314
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
11 changes: 11 additions & 0 deletions mojo/public/cpp/bindings/receiver_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ bool ReceiverSetState::Remove(ReceiverId id) {
return true;
}

bool ReceiverSetState::RemoveWithReason(ReceiverId id,
uint32_t custom_reason_code,
const std::string& description) {
auto it = entries_.find(id);
if (it == entries_.end())
return false;
it->second->receiver().ResetWithReason(custom_reason_code, description);
entries_.erase(it);
return true;
}

void ReceiverSetState::FlushForTesting() {
// We avoid flushing while iterating over |entries_| because this set may be
// mutated during individual flush operations. Instead, snapshot the
Expand Down
24 changes: 24 additions & 0 deletions mojo/public/cpp/bindings/receiver_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class COMPONENT_EXPORT(MOJO_CPP_BINDINGS) ReceiverSetState {
std::unique_ptr<MessageFilter> filter,
RepeatingConnectionErrorWithReasonCallback disconnect_handler) = 0;
virtual void FlushForTesting() = 0;
virtual void ResetWithReason(uint32_t custom_reason_code,
const std::string& description) = 0;
};

class COMPONENT_EXPORT(MOJO_CPP_BINDINGS) Entry {
Expand Down Expand Up @@ -116,6 +118,9 @@ class COMPONENT_EXPORT(MOJO_CPP_BINDINGS) ReceiverSetState {
ReportBadMessageCallback GetBadMessageCallback();
ReceiverId Add(std::unique_ptr<ReceiverState> receiver);
bool Remove(ReceiverId id);
bool RemoveWithReason(ReceiverId id,
uint32_t custom_reason_code,
const std::string& description);
void FlushForTesting();
void SetDispatchContext(const void* context, ReceiverId receiver_id);
void OnDisconnect(ReceiverId id,
Expand Down Expand Up @@ -232,6 +237,12 @@ class ReceiverSetBase {
// disconnected. No further messages or disconnection notifications will be
// scheduled or executed for the removed receiver.
bool Remove(ReceiverId id) { return state_.Remove(id); }
// Similar to the method above, but also specifies a disconnect reason.
bool RemoveWithReason(ReceiverId id,
uint32_t custom_reason_code,
const std::string& description) {
return state_.RemoveWithReason(id, custom_reason_code, description);
}

// Unbinds and takes all receivers in this set.
std::vector<PendingType> TakeReceivers() {
Expand All @@ -250,6 +261,14 @@ class ReceiverSetBase {
// ReceiverSet will not schedule or execute any further method invocations or
// disconnection notifications until a new receiver is added to the set.
void Clear() { state_.entries().clear(); }
// Similar to the method above, but also specifies a disconnect reason.
void ClearWithReason(uint32_t custom_reason_code,
const std::string& description) {
for (auto& entry : state_.entries())
entry.second->receiver().ResetWithReason(custom_reason_code, description);

Clear();
}

// Predicate to test if a receiver exists in the set.
//
Expand Down Expand Up @@ -355,6 +374,11 @@ class ReceiverSetBase {

void FlushForTesting() override { receiver_.FlushForTesting(); }

void ResetWithReason(uint32_t custom_reason_code,
const std::string& description) override {
receiver_.ResetWithReason(custom_reason_code, description);
}

ImplPointerType SwapImplForTesting(ImplPointerType new_impl) {
return receiver_.SwapImplForTesting(std::move(new_impl));
}
Expand Down

0 comments on commit 4f71314

Please sign in to comment.