Index: mojo/public/cpp/bindings/lib/interface_impl_internal.h |
diff --git a/mojo/public/cpp/bindings/lib/interface_impl_internal.h b/mojo/public/cpp/bindings/lib/interface_impl_internal.h |
index 1c912661fd96a95d41661add0ad664f05ce063b3..193e6e4a6846b6a066acdadc9012c4175686b45f 100644 |
--- a/mojo/public/cpp/bindings/lib/interface_impl_internal.h |
+++ b/mojo/public/cpp/bindings/lib/interface_impl_internal.h |
@@ -30,12 +30,21 @@ class InterfaceImplState : public ErrorHandler { |
explicit InterfaceImplState(InterfaceImplBase<Interface>* instance) |
: router_(NULL), |
- proxy_(NULL) { |
+ proxy_(NULL), |
+ delete_on_error_(false), |
+#ifndef NDEBUG |
+ deleting_on_error_(false) |
+#endif |
+ { |
assert(instance); |
stub_.set_sink(instance); |
} |
virtual ~InterfaceImplState() { |
+#ifndef NDEBUG |
+ if (delete_on_error_) |
+ assert(deleting_on_error_); |
+#endif |
delete proxy_; |
if (router_) { |
router_->set_error_handler(NULL); |
@@ -43,16 +52,17 @@ class InterfaceImplState : public ErrorHandler { |
} |
} |
- void BindProxy( |
- InterfacePtr<Interface>* ptr, |
- const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
+ void BindProxy(InterfacePtr<Interface>* ptr, |
+ const MojoAsyncWaiter* waiter, |
+ bool delete_on_error) { |
MessagePipe pipe; |
ptr->Bind(pipe.handle0.Pass(), waiter); |
- Bind(pipe.handle1.Pass(), waiter); |
+ Bind(pipe.handle1.Pass(), waiter, delete_on_error); |
} |
void Bind(ScopedMessagePipeHandle handle, |
- const MojoAsyncWaiter* waiter) { |
+ const MojoAsyncWaiter* waiter, |
+ bool delete_on_error) { |
assert(!router_); |
FilterChain filters; |
@@ -65,6 +75,7 @@ class InterfaceImplState : public ErrorHandler { |
router_->set_error_handler(this); |
proxy_ = new typename Client::Proxy_(router_); |
+ delete_on_error_ = delete_on_error; |
instance()->OnConnectionEstablished(); |
} |
@@ -83,12 +94,27 @@ class InterfaceImplState : public ErrorHandler { |
} |
virtual void OnConnectionError() MOJO_OVERRIDE { |
+ // If the connection error behavior is not delete_on_error_ the instance |
+ // might choose to delete itself in the OnConnectionError handler, which |
+ // would in turn delete this. Save the error behavior before invoking the |
+ // error handler so we can correctly decide what to do. |
+ bool delete_on_error = delete_on_error_; |
instance()->OnConnectionError(); |
+ if (delete_on_error) { |
+#ifndef NDEBUG |
+ deleting_on_error_ = true; |
+#endif |
+ delete instance(); |
+ } |
} |
Router* router_; |
typename Client::Proxy_* proxy_; |
typename Interface::Stub_ stub_; |
+ bool delete_on_error_; |
+#ifndef NDEBUG |
+ bool deleting_on_error_; |
+#endif |
MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImplState); |
}; |