Chromium Code Reviews| 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" |
| 11 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" | 11 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" |
| 12 #include "mojo/public/cpp/environment/environment.h" | 12 #include "mojo/public/cpp/environment/environment.h" |
| 13 #include "mojo/public/cpp/system/macros.h" | 13 #include "mojo/public/cpp/system/macros.h" |
| 14 | 14 |
| 15 namespace mojo { | 15 namespace mojo { |
| 16 | |
|
darin (slow to review)
2014/07/15 06:10:38
nit: no new line here is OK, right?
| |
| 16 namespace internal { | 17 namespace internal { |
| 17 | 18 |
| 18 template <typename Interface> | 19 template <typename Interface> |
| 19 class InterfaceImplBase : public Interface { | 20 class InterfaceImplBase : public Interface { |
| 20 public: | 21 public: |
| 21 virtual ~InterfaceImplBase() {} | 22 virtual ~InterfaceImplBase() {} |
| 22 virtual void OnConnectionEstablished() = 0; | 23 virtual void OnConnectionEstablished() = 0; |
| 23 virtual void OnConnectionError() = 0; | 24 virtual void OnConnectionError() = 0; |
| 24 }; | 25 }; |
| 25 | 26 |
| 26 template <typename Interface> | 27 template <typename Interface> |
| 27 class InterfaceImplState : public ErrorHandler { | 28 class InterfaceImplState : public ErrorHandler { |
| 28 public: | 29 public: |
| 29 typedef typename Interface::Client Client; | 30 typedef typename Interface::Client Client; |
| 30 | 31 |
| 31 explicit InterfaceImplState(InterfaceImplBase<Interface>* instance) | 32 explicit InterfaceImplState(InterfaceImplBase<Interface>* instance) |
| 32 : router_(NULL), | 33 : router_(NULL), proxy_(NULL), delete_on_error_(false) { |
| 33 proxy_(NULL) { | |
| 34 assert(instance); | 34 assert(instance); |
| 35 stub_.set_sink(instance); | 35 stub_.set_sink(instance); |
| 36 } | 36 } |
| 37 | 37 |
| 38 virtual ~InterfaceImplState() { | 38 virtual ~InterfaceImplState() { |
| 39 delete proxy_; | 39 delete proxy_; |
| 40 if (router_) { | 40 if (router_) { |
| 41 router_->set_error_handler(NULL); | 41 router_->set_error_handler(NULL); |
| 42 delete router_; | 42 delete router_; |
| 43 } | 43 } |
| 44 } | 44 } |
| 45 | 45 |
| 46 void BindProxy( | 46 void BindProxy( |
| 47 InterfacePtr<Interface>* ptr, | 47 InterfacePtr<Interface>* ptr, |
| 48 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 48 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 49 MessagePipe pipe; | 49 MessagePipe pipe; |
| 50 ptr->Bind(pipe.handle0.Pass(), waiter); | 50 ptr->Bind(pipe.handle0.Pass(), waiter); |
| 51 Bind(pipe.handle1.Pass(), waiter); | 51 Bind(pipe.handle1.Pass(), waiter, true /* delete_on_error */); |
| 52 } | 52 } |
| 53 | 53 |
| 54 void Bind(ScopedMessagePipeHandle handle, | 54 void Bind(ScopedMessagePipeHandle handle, |
| 55 const MojoAsyncWaiter* waiter) { | 55 const MojoAsyncWaiter* waiter, |
| 56 bool delete_on_error) { | |
| 56 assert(!router_); | 57 assert(!router_); |
| 57 | 58 |
| 58 FilterChain filters; | 59 FilterChain filters; |
| 59 filters.Append<MessageHeaderValidator>(); | 60 filters.Append<MessageHeaderValidator>(); |
| 60 filters.Append<typename Interface::RequestValidator_>(); | 61 filters.Append<typename Interface::RequestValidator_>(); |
| 61 filters.Append<typename Interface::Client::ResponseValidator_>(); | 62 filters.Append<typename Interface::Client::ResponseValidator_>(); |
| 62 | 63 |
| 63 router_ = new Router(handle.Pass(), filters.Pass(), waiter); | 64 router_ = new Router(handle.Pass(), filters.Pass(), waiter); |
| 64 router_->set_incoming_receiver(&stub_); | 65 router_->set_incoming_receiver(&stub_); |
| 65 router_->set_error_handler(this); | 66 router_->set_error_handler(this); |
| 66 | 67 |
| 67 proxy_ = new typename Client::Proxy_(router_); | 68 proxy_ = new typename Client::Proxy_(router_); |
| 69 delete_on_error_ = delete_on_error; | |
| 68 | 70 |
| 69 instance()->OnConnectionEstablished(); | 71 instance()->OnConnectionEstablished(); |
| 70 } | 72 } |
| 71 | 73 |
| 72 bool WaitForIncomingMethodCall() { | 74 bool WaitForIncomingMethodCall() { |
| 73 assert(router_); | 75 assert(router_); |
| 74 return router_->WaitForIncomingMessage(); | 76 return router_->WaitForIncomingMessage(); |
| 75 } | 77 } |
| 76 | 78 |
| 77 Router* router() { return router_; } | 79 Router* router() { return router_; } |
| 78 Client* client() { return proxy_; } | 80 Client* client() { return proxy_; } |
| 79 | 81 |
| 80 private: | 82 private: |
| 81 InterfaceImplBase<Interface>* instance() { | 83 InterfaceImplBase<Interface>* instance() { |
| 82 return static_cast<InterfaceImplBase<Interface>*>(stub_.sink()); | 84 return static_cast<InterfaceImplBase<Interface>*>(stub_.sink()); |
| 83 } | 85 } |
| 84 | 86 |
| 85 virtual void OnConnectionError() MOJO_OVERRIDE { | 87 virtual void OnConnectionError() MOJO_OVERRIDE { |
| 88 // If the connection error behavior is not delete_on_error_ the instance | |
| 89 // might choose to delete itself in the OnConnectionError handler, which | |
| 90 // would in turn delete this. Save the error behavior before invoking the | |
| 91 // error handler so we can correctly decide what to do. | |
| 92 bool delete_on_error = delete_on_error_; | |
| 86 instance()->OnConnectionError(); | 93 instance()->OnConnectionError(); |
| 94 if (delete_on_error) | |
|
darin (slow to review)
2014/07/15 06:10:38
It might be useful to add some debug assertions th
jamesr
2014/07/15 18:07:22
I added a !NDEBUG-only boolean member that I set b
| |
| 95 delete instance(); | |
| 87 } | 96 } |
| 88 | 97 |
| 89 Router* router_; | 98 Router* router_; |
| 90 typename Client::Proxy_* proxy_; | 99 typename Client::Proxy_* proxy_; |
| 91 typename Interface::Stub_ stub_; | 100 typename Interface::Stub_ stub_; |
| 101 bool delete_on_error_; | |
| 92 | 102 |
| 93 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImplState); | 103 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImplState); |
| 94 }; | 104 }; |
| 95 | 105 |
| 96 } // namespace internal | 106 } // namespace internal |
| 97 } // namespace mojo | 107 } // namespace mojo |
| 98 | 108 |
| 99 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_IMPL_INTERNAL_H_ | 109 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_IMPL_INTERNAL_H_ |
| OLD | NEW |