Commit 6d6dde7e authored by Tal Pressman's avatar Tal Pressman Committed by Commit Bot

Add ReportBadMessage capabilities to AssociatedReceiver.

This mirrors the same APIs on Receiver, that should also exist for
associated interfaces.

Change-Id: Ie2feef11f86b2597a8b431b2bbbbb8110b54b0f0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2461981
Commit-Queue: Ken Rockot <rockot@google.com>
Reviewed-by: default avatarKen Rockot <rockot@google.com>
Cr-Commit-Position: refs/heads/master@{#816266}
parent e53cf7a6
......@@ -149,9 +149,29 @@ class AssociatedBinding : public AssociatedBindingBase {
return old_impl;
}
// Acquires a callback which may be run to report the currently dispatching
// message as bad and close this binding. Note that this is only legal to
// call from directly within stack frame of a message dispatch, but the
// returned callback may be called exactly once any time thereafter to report
// the message as bad. |GetBadMessageCallback()| may only be called once per
// message, and the returned callback must be run on the same sequence to
// which this binding is bound.
ReportBadMessageCallback GetBadMessageCallback() {
return base::BindOnce(
[](ReportBadMessageCallback inner_callback,
base::WeakPtr<AssociatedBinding> binding, const std::string& error) {
std::move(inner_callback).Run(error);
if (binding)
binding->Close();
},
mojo::GetBadMessageCallback(), weak_ptr_factory_.GetWeakPtr());
}
private:
typename Interface::template Stub_<ImplRefTraits> stub_;
base::WeakPtrFactory<AssociatedBinding> weak_ptr_factory_{this};
DISALLOW_COPY_AND_ASSIGN(AssociatedBinding);
};
......
......@@ -241,6 +241,26 @@ class AssociatedReceiver {
return binding_.SwapImplForTesting(new_impl);
}
// Reports the currently dispatching message as bad and resets this receiver.
// Note that this is only legal to call from within the stack frame of a
// message dispatch. If you need to do asynchronous work before determining
// the legitimacy of a message, use GetBadMessageCallback() and retain its
// result until ready to invoke or discard it.
void ReportBadMessage(const std::string& error) {
GetBadMessageCallback().Run(error);
}
// Acquires a callback which may be run to report the currently dispatching
// message as bad and reset this receiver. Note that this is only legal to
// call from directly within stack frame of a message dispatch, but the
// returned callback may be called exactly once any time thereafter to report
// the message as bad. |GetBadMessageCallback()| may only be called once per
// message, and the returned callback must be run on the same sequence to
// which this Receiver is bound.
ReportBadMessageCallback GetBadMessageCallback() {
return binding_.GetBadMessageCallback();
}
private:
// TODO(https://crbug.com/875030): Move AssociatedBinding details into this
// class.
......
......@@ -53,6 +53,10 @@ class IntegerSenderImpl : public IntegerSender {
}
void Echo(int32_t value, EchoCallback callback) override {
if (value == -1) {
receiver_.ReportBadMessage("Reporting bad message for value == -1");
return;
}
std::move(callback).Run(value);
}
void Send(int32_t value) override { notify_send_method_called_.Run(value); }
......@@ -1076,6 +1080,34 @@ TEST_F(AssociatedInterfaceTest, AsyncErrorHandlersWhenClosingPrimaryInterface) {
EXPECT_TRUE(error_handler_invoked);
}
TEST_F(AssociatedInterfaceTest, AssociatedReceiverReportBadMessage) {
PendingAssociatedReceiver<IntegerSender> pending_receiver;
PendingAssociatedRemote<IntegerSender> pending_remote;
CreateIntegerSender(&pending_remote, &pending_receiver);
IntegerSenderImpl impl(std::move(pending_receiver));
AssociatedRemote<IntegerSender> remote(std::move(pending_remote));
bool called = false;
base::RunLoop run_loop;
remote.set_disconnect_handler(base::BindLambdaForTesting([&] {
called = true;
run_loop.Quit();
}));
std::string received_error;
SetDefaultProcessErrorHandler(base::BindLambdaForTesting(
[&](const std::string& error) { received_error = error; }));
remote->Echo(-1, IntegerSenderImpl::EchoCallback());
EXPECT_FALSE(called);
run_loop.Run();
EXPECT_TRUE(called);
EXPECT_EQ("Reporting bad message for value == -1", received_error);
SetDefaultProcessErrorHandler(base::NullCallback());
}
} // namespace
} // namespace test
} // namespace mojo
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment