| 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 13 matching lines...) Expand all Loading... |
| 24 virtual void OnConnectionError() = 0; | 24 virtual void OnConnectionError() = 0; |
| 25 }; | 25 }; |
| 26 | 26 |
| 27 template <typename Interface> | 27 template <typename Interface> |
| 28 class InterfaceImplState : public ErrorHandler { | 28 class InterfaceImplState : public ErrorHandler { |
| 29 public: | 29 public: |
| 30 typedef typename Interface::Client Client; | 30 typedef typename Interface::Client Client; |
| 31 | 31 |
| 32 explicit InterfaceImplState(InterfaceImplBase<Interface>* instance) | 32 explicit InterfaceImplState(InterfaceImplBase<Interface>* instance) |
| 33 : router_(NULL), | 33 : router_(NULL), |
| 34 proxy_(NULL) { | 34 proxy_(NULL), |
| 35 instance_bound_to_pipe_(false) |
| 36 #ifndef NDEBUG |
| 37 , |
| 38 deleting_instance_due_to_error_(false) |
| 39 #endif |
| 40 { |
| 35 MOJO_DCHECK(instance); | 41 MOJO_DCHECK(instance); |
| 36 stub_.set_sink(instance); | 42 stub_.set_sink(instance); |
| 37 } | 43 } |
| 38 | 44 |
| 39 virtual ~InterfaceImplState() { | 45 virtual ~InterfaceImplState() { |
| 46 #ifndef NDEBUG |
| 47 MOJO_DCHECK(!instance_bound_to_pipe_ || deleting_instance_due_to_error_); |
| 48 #endif |
| 40 delete proxy_; | 49 delete proxy_; |
| 41 if (router_) { | 50 if (router_) { |
| 42 router_->set_error_handler(NULL); | 51 router_->set_error_handler(NULL); |
| 43 delete router_; | 52 delete router_; |
| 44 } | 53 } |
| 45 } | 54 } |
| 46 | 55 |
| 47 void BindProxy( | 56 void BindProxy( |
| 48 InterfacePtr<Interface>* ptr, | 57 InterfacePtr<Interface>* ptr, |
| 58 bool instance_bound_to_pipe, |
| 49 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 59 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 50 MessagePipe pipe; | 60 MessagePipe pipe; |
| 51 ptr->Bind(pipe.handle0.Pass(), waiter); | 61 ptr->Bind(pipe.handle0.Pass(), waiter); |
| 52 Bind(pipe.handle1.Pass(), waiter); | 62 Bind(pipe.handle1.Pass(), instance_bound_to_pipe, waiter); |
| 53 } | 63 } |
| 54 | 64 |
| 55 void Bind(ScopedMessagePipeHandle handle, | 65 void Bind(ScopedMessagePipeHandle handle, |
| 66 bool instance_bound_to_pipe, |
| 56 const MojoAsyncWaiter* waiter) { | 67 const MojoAsyncWaiter* waiter) { |
| 57 MOJO_DCHECK(!router_); | 68 MOJO_DCHECK(!router_); |
| 58 | 69 |
| 59 FilterChain filters; | 70 FilterChain filters; |
| 60 filters.Append<MessageHeaderValidator>(); | 71 filters.Append<MessageHeaderValidator>(); |
| 61 filters.Append<typename Interface::RequestValidator_>(); | 72 filters.Append<typename Interface::RequestValidator_>(); |
| 62 filters.Append<typename Interface::Client::ResponseValidator_>(); | 73 filters.Append<typename Interface::Client::ResponseValidator_>(); |
| 63 | 74 |
| 64 router_ = new Router(handle.Pass(), filters.Pass(), waiter); | 75 router_ = new Router(handle.Pass(), filters.Pass(), waiter); |
| 65 router_->set_incoming_receiver(&stub_); | 76 router_->set_incoming_receiver(&stub_); |
| 66 router_->set_error_handler(this); | 77 router_->set_error_handler(this); |
| 67 | 78 |
| 68 proxy_ = new typename Client::Proxy_(router_); | 79 proxy_ = new typename Client::Proxy_(router_); |
| 69 | 80 |
| 81 instance_bound_to_pipe_ = instance_bound_to_pipe; |
| 82 |
| 70 instance()->OnConnectionEstablished(); | 83 instance()->OnConnectionEstablished(); |
| 71 } | 84 } |
| 72 | 85 |
| 73 bool WaitForIncomingMethodCall() { | 86 bool WaitForIncomingMethodCall() { |
| 74 MOJO_DCHECK(router_); | 87 MOJO_DCHECK(router_); |
| 75 return router_->WaitForIncomingMessage(); | 88 return router_->WaitForIncomingMessage(); |
| 76 } | 89 } |
| 77 | 90 |
| 78 Router* router() { return router_; } | 91 Router* router() { return router_; } |
| 79 Client* client() { return proxy_; } | 92 Client* client() { return proxy_; } |
| 80 | 93 |
| 81 private: | 94 private: |
| 82 InterfaceImplBase<Interface>* instance() { | 95 InterfaceImplBase<Interface>* instance() { |
| 83 return static_cast<InterfaceImplBase<Interface>*>(stub_.sink()); | 96 return static_cast<InterfaceImplBase<Interface>*>(stub_.sink()); |
| 84 } | 97 } |
| 85 | 98 |
| 86 virtual void OnConnectionError() MOJO_OVERRIDE { | 99 virtual void OnConnectionError() MOJO_OVERRIDE { |
| 100 // If the the instance is not bound to the pipe, the instance might choose |
| 101 // to delete itself in the OnConnectionError handler, which would in turn |
| 102 // delete this. Save the error behavior before invoking the error handler |
| 103 // so we can correctly decide what to do. |
| 104 bool bound = instance_bound_to_pipe_; |
| 87 instance()->OnConnectionError(); | 105 instance()->OnConnectionError(); |
| 106 if (!bound) |
| 107 return; |
| 108 #ifndef NDEBUG |
| 109 deleting_instance_due_to_error_ = true; |
| 110 #endif |
| 111 delete instance(); |
| 88 } | 112 } |
| 89 | 113 |
| 90 Router* router_; | 114 Router* router_; |
| 91 typename Client::Proxy_* proxy_; | 115 typename Client::Proxy_* proxy_; |
| 92 typename Interface::Stub_ stub_; | 116 typename Interface::Stub_ stub_; |
| 117 bool instance_bound_to_pipe_; |
| 118 #ifndef NDEBUG |
| 119 bool deleting_instance_due_to_error_; |
| 120 #endif |
| 93 | 121 |
| 94 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImplState); | 122 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImplState); |
| 95 }; | 123 }; |
| 96 | 124 |
| 97 } // namespace internal | 125 } // namespace internal |
| 98 } // namespace mojo | 126 } // namespace mojo |
| 99 | 127 |
| 100 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_IMPL_INTERNAL_H_ | 128 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_IMPL_INTERNAL_H_ |
| OLD | NEW |