Currently we use ErrorHandler to report connection error for InterfacePtr<> and Binding<>. For example,
class Foo : ErrorHandler {
void SomeMethod() {
...
bar_binding_.set_error_handler(this);
...
}
void OnConnectionError() override { ... }
Binding<Bar> bar_binding_;
QuxPtr qux_ptr_;
};
I've encountered a few problems with this design:
(1) When I want to handle errors differently for multiple interface pointers or bindings, I have to define separate ErrorHandler subclasses. For example,there is no way for Foo to provide different error handlers for |bar_binding_| and |qux_ptr_|.
(2) The name ErrorHandler and OnConnectionError seem a little vague. Without looking at the method body, I usually cannot tell which pointer/binding the handler is used for.
I feel that it is more flexible if we replace the ErrorHandler interface with a mojo::Callback.
That way, Foo can provide error handling for both |bar_binding_| and |qux_ptr_| directly.
We can use lambda and base::Bind (in chromium):
qux_ptr_.set_connection_error_handler(
[this]() { qux_ptr_ = nullptr; });
We can choose more meaningful names as well.
bar_binding_.set_connection_error_handler(
[this] () { OnBarBindingConnectionError(); });
What do you think? Thanks!