| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_IMPL_INTERNAL_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_IMPL_INTERNAL_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_IMPL_INTERNAL_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_IMPL_INTERNAL_H_ |
| 7 | 7 |
| 8 #include "mojo/public/cpp/bindings/error_handler.h" | 8 #include "mojo/public/cpp/bindings/error_handler.h" |
| 9 #include "mojo/public/cpp/bindings/interface_ptr.h" | 9 #include "mojo/public/cpp/bindings/interface_ptr.h" |
| 10 #include "mojo/public/cpp/bindings/lib/filter_chain.h" | 10 #include "mojo/public/cpp/bindings/lib/filter_chain.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 virtual void OnConnectionError() = 0; | 23 virtual void OnConnectionError() = 0; |
| 24 }; | 24 }; |
| 25 | 25 |
| 26 template <typename Interface> | 26 template <typename Interface> |
| 27 class InterfaceImplState : public ErrorHandler { | 27 class InterfaceImplState : public ErrorHandler { |
| 28 public: | 28 public: |
| 29 typedef typename Interface::Client Client; | 29 typedef typename Interface::Client Client; |
| 30 | 30 |
| 31 explicit InterfaceImplState(InterfaceImplBase<Interface>* instance) | 31 explicit InterfaceImplState(InterfaceImplBase<Interface>* instance) |
| 32 : router_(NULL), | 32 : router_(NULL), |
| 33 proxy_(NULL) { | 33 proxy_(NULL), |
| 34 delete_on_error_(false), |
| 35 #ifndef NDEBUG |
| 36 deleting_on_error_(false) |
| 37 #endif |
| 38 { |
| 34 assert(instance); | 39 assert(instance); |
| 35 stub_.set_sink(instance); | 40 stub_.set_sink(instance); |
| 36 } | 41 } |
| 37 | 42 |
| 38 virtual ~InterfaceImplState() { | 43 virtual ~InterfaceImplState() { |
| 44 #ifndef NDEBUG |
| 45 if (delete_on_error_) |
| 46 assert(deleting_on_error_); |
| 47 #endif |
| 39 delete proxy_; | 48 delete proxy_; |
| 40 if (router_) { | 49 if (router_) { |
| 41 router_->set_error_handler(NULL); | 50 router_->set_error_handler(NULL); |
| 42 delete router_; | 51 delete router_; |
| 43 } | 52 } |
| 44 } | 53 } |
| 45 | 54 |
| 46 void BindProxy( | 55 void BindProxy(InterfacePtr<Interface>* ptr, |
| 47 InterfacePtr<Interface>* ptr, | 56 const MojoAsyncWaiter* waiter, |
| 48 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 57 bool delete_on_error) { |
| 49 MessagePipe pipe; | 58 MessagePipe pipe; |
| 50 ptr->Bind(pipe.handle0.Pass(), waiter); | 59 ptr->Bind(pipe.handle0.Pass(), waiter); |
| 51 Bind(pipe.handle1.Pass(), waiter); | 60 Bind(pipe.handle1.Pass(), waiter, delete_on_error); |
| 52 } | 61 } |
| 53 | 62 |
| 54 void Bind(ScopedMessagePipeHandle handle, | 63 void Bind(ScopedMessagePipeHandle handle, |
| 55 const MojoAsyncWaiter* waiter) { | 64 const MojoAsyncWaiter* waiter, |
| 65 bool delete_on_error) { |
| 56 assert(!router_); | 66 assert(!router_); |
| 57 | 67 |
| 58 FilterChain filters; | 68 FilterChain filters; |
| 59 filters.Append<MessageHeaderValidator>(); | 69 filters.Append<MessageHeaderValidator>(); |
| 60 filters.Append<typename Interface::RequestValidator_>(); | 70 filters.Append<typename Interface::RequestValidator_>(); |
| 61 filters.Append<typename Interface::Client::ResponseValidator_>(); | 71 filters.Append<typename Interface::Client::ResponseValidator_>(); |
| 62 | 72 |
| 63 router_ = new Router(handle.Pass(), filters.Pass(), waiter); | 73 router_ = new Router(handle.Pass(), filters.Pass(), waiter); |
| 64 router_->set_incoming_receiver(&stub_); | 74 router_->set_incoming_receiver(&stub_); |
| 65 router_->set_error_handler(this); | 75 router_->set_error_handler(this); |
| 66 | 76 |
| 67 proxy_ = new typename Client::Proxy_(router_); | 77 proxy_ = new typename Client::Proxy_(router_); |
| 78 delete_on_error_ = delete_on_error; |
| 68 | 79 |
| 69 instance()->OnConnectionEstablished(); | 80 instance()->OnConnectionEstablished(); |
| 70 } | 81 } |
| 71 | 82 |
| 72 bool WaitForIncomingMethodCall() { | 83 bool WaitForIncomingMethodCall() { |
| 73 assert(router_); | 84 assert(router_); |
| 74 return router_->WaitForIncomingMessage(); | 85 return router_->WaitForIncomingMessage(); |
| 75 } | 86 } |
| 76 | 87 |
| 77 Router* router() { return router_; } | 88 Router* router() { return router_; } |
| 78 Client* client() { return proxy_; } | 89 Client* client() { return proxy_; } |
| 79 | 90 |
| 80 private: | 91 private: |
| 81 InterfaceImplBase<Interface>* instance() { | 92 InterfaceImplBase<Interface>* instance() { |
| 82 return static_cast<InterfaceImplBase<Interface>*>(stub_.sink()); | 93 return static_cast<InterfaceImplBase<Interface>*>(stub_.sink()); |
| 83 } | 94 } |
| 84 | 95 |
| 85 virtual void OnConnectionError() MOJO_OVERRIDE { | 96 virtual void OnConnectionError() MOJO_OVERRIDE { |
| 97 // If the connection error behavior is not delete_on_error_ the instance |
| 98 // might choose to delete itself in the OnConnectionError handler, which |
| 99 // would in turn delete this. Save the error behavior before invoking the |
| 100 // error handler so we can correctly decide what to do. |
| 101 bool delete_on_error = delete_on_error_; |
| 86 instance()->OnConnectionError(); | 102 instance()->OnConnectionError(); |
| 103 if (delete_on_error) { |
| 104 #ifndef NDEBUG |
| 105 deleting_on_error_ = true; |
| 106 #endif |
| 107 delete instance(); |
| 108 } |
| 87 } | 109 } |
| 88 | 110 |
| 89 Router* router_; | 111 Router* router_; |
| 90 typename Client::Proxy_* proxy_; | 112 typename Client::Proxy_* proxy_; |
| 91 typename Interface::Stub_ stub_; | 113 typename Interface::Stub_ stub_; |
| 114 bool delete_on_error_; |
| 115 #ifndef NDEBUG |
| 116 bool deleting_on_error_; |
| 117 #endif |
| 92 | 118 |
| 93 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImplState); | 119 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImplState); |
| 94 }; | 120 }; |
| 95 | 121 |
| 96 } // namespace internal | 122 } // namespace internal |
| 97 } // namespace mojo | 123 } // namespace mojo |
| 98 | 124 |
| 99 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_IMPL_INTERNAL_H_ | 125 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_IMPL_INTERNAL_H_ |
| OLD | NEW |