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 | |
| 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) { |
| 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, 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 OnConnectionErrorBehavior behavior) { | |
| 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 behavior_ = behavior; | |
| 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 DO_NOTHING the instance might delete | |
| 89 // itself in the OnConnectionError handler, which would in turn delete this. | |
| 90 // Save the error behavior before invoking the error handler so we can | |
| 91 // correctly decide what to do. | |
| 92 OnConnectionErrorBehavior behavior = behavior_; | |
| 86 instance()->OnConnectionError(); | 93 instance()->OnConnectionError(); |
| 94 if (behavior == DELETE_ON_ERROR) { | |
|
DaveMoore
2014/07/15 00:08:29
Nit: no braces
| |
| 95 delete instance(); | |
| 96 } | |
| 87 } | 97 } |
| 88 | 98 |
| 89 Router* router_; | 99 Router* router_; |
| 90 typename Client::Proxy_* proxy_; | 100 typename Client::Proxy_* proxy_; |
| 91 typename Interface::Stub_ stub_; | 101 typename Interface::Stub_ stub_; |
| 102 OnConnectionErrorBehavior behavior_; | |
| 92 | 103 |
| 93 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImplState); | 104 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImplState); |
| 94 }; | 105 }; |
| 95 | 106 |
| 96 } // namespace internal | 107 } // namespace internal |
| 97 } // namespace mojo | 108 } // namespace mojo |
| 98 | 109 |
| 99 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_IMPL_INTERNAL_H_ | 110 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_IMPL_INTERNAL_H_ |
| OLD | NEW |