| 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_STRONG_BINDING_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ |
| 7 | 7 |
| 8 #include <assert.h> | 8 #include <assert.h> |
| 9 | 9 |
| 10 #include "mojo/public/c/environment/async_waiter.h" | 10 #include "mojo/public/c/environment/async_waiter.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 // void Create(..., InterfaceRequest<Foo> request) override { | 41 // void Create(..., InterfaceRequest<Foo> request) override { |
| 42 // new StronglyBound(request.Pass()); // The binding now owns the | 42 // new StronglyBound(request.Pass()); // The binding now owns the |
| 43 // // instance of StronglyBound. | 43 // // instance of StronglyBound. |
| 44 // } | 44 // } |
| 45 // }; | 45 // }; |
| 46 template <typename Interface> | 46 template <typename Interface> |
| 47 class StrongBinding { | 47 class StrongBinding { |
| 48 MOJO_MOVE_ONLY_TYPE(StrongBinding) | 48 MOJO_MOVE_ONLY_TYPE(StrongBinding) |
| 49 | 49 |
| 50 public: | 50 public: |
| 51 explicit StrongBinding(Interface* impl) : binding_(impl) { | 51 explicit StrongBinding(Interface* impl) : binding_(impl) {} |
| 52 binding_.set_connection_error_handler([this]() { OnConnectionError(); }); | |
| 53 } | |
| 54 | 52 |
| 55 StrongBinding( | 53 StrongBinding( |
| 56 Interface* impl, | 54 Interface* impl, |
| 57 ScopedMessagePipeHandle handle, | 55 ScopedMessagePipeHandle handle, |
| 58 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) | 56 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) |
| 59 : StrongBinding(impl) { | 57 : StrongBinding(impl) { |
| 60 binding_.Bind(handle.Pass(), waiter); | 58 Bind(handle.Pass(), waiter); |
| 61 } | 59 } |
| 62 | 60 |
| 63 StrongBinding( | 61 StrongBinding( |
| 64 Interface* impl, | 62 Interface* impl, |
| 65 InterfacePtr<Interface>* ptr, | 63 InterfacePtr<Interface>* ptr, |
| 66 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) | 64 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) |
| 67 : StrongBinding(impl) { | 65 : StrongBinding(impl) { |
| 68 binding_.Bind(ptr, waiter); | 66 Bind(ptr, waiter); |
| 69 } | 67 } |
| 70 | 68 |
| 71 StrongBinding( | 69 StrongBinding( |
| 72 Interface* impl, | 70 Interface* impl, |
| 73 InterfaceRequest<Interface> request, | 71 InterfaceRequest<Interface> request, |
| 74 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) | 72 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) |
| 75 : StrongBinding(impl) { | 73 : StrongBinding(impl) { |
| 76 binding_.Bind(request.Pass(), waiter); | 74 Bind(request.Pass(), waiter); |
| 77 } | 75 } |
| 78 | 76 |
| 79 ~StrongBinding() {} | 77 ~StrongBinding() {} |
| 80 | 78 |
| 81 void Bind( | 79 void Bind( |
| 82 ScopedMessagePipeHandle handle, | 80 ScopedMessagePipeHandle handle, |
| 83 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 81 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 84 assert(!binding_.is_bound()); | 82 assert(!binding_.is_bound()); |
| 85 binding_.Bind(handle.Pass(), waiter); | 83 binding_.Bind(handle.Pass(), waiter); |
| 84 binding_.set_connection_error_handler([this]() { OnConnectionError(); }); |
| 86 } | 85 } |
| 87 | 86 |
| 88 void Bind( | 87 void Bind( |
| 89 InterfacePtr<Interface>* ptr, | 88 InterfacePtr<Interface>* ptr, |
| 90 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 89 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 91 assert(!binding_.is_bound()); | 90 assert(!binding_.is_bound()); |
| 92 binding_.Bind(ptr, waiter); | 91 binding_.Bind(ptr, waiter); |
| 92 binding_.set_connection_error_handler([this]() { OnConnectionError(); }); |
| 93 } | 93 } |
| 94 | 94 |
| 95 void Bind( | 95 void Bind( |
| 96 InterfaceRequest<Interface> request, | 96 InterfaceRequest<Interface> request, |
| 97 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 97 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 98 assert(!binding_.is_bound()); | 98 assert(!binding_.is_bound()); |
| 99 binding_.Bind(request.Pass(), waiter); | 99 binding_.Bind(request.Pass(), waiter); |
| 100 binding_.set_connection_error_handler([this]() { OnConnectionError(); }); |
| 100 } | 101 } |
| 101 | 102 |
| 102 bool WaitForIncomingMethodCall() { | 103 bool WaitForIncomingMethodCall() { |
| 103 return binding_.WaitForIncomingMethodCall(); | 104 return binding_.WaitForIncomingMethodCall(); |
| 104 } | 105 } |
| 105 | 106 |
| 106 // Note: The error handler must not delete the interface implementation. | 107 // Note: The error handler must not delete the interface implementation. |
| 107 void set_connection_error_handler(const Closure& error_handler) { | 108 void set_connection_error_handler(const Closure& error_handler) { |
| 109 assert(binding_.is_bound()); |
| 108 connection_error_handler_ = error_handler; | 110 connection_error_handler_ = error_handler; |
| 109 } | 111 } |
| 110 | 112 |
| 111 Interface* impl() { return binding_.impl(); } | 113 Interface* impl() { return binding_.impl(); } |
| 112 // Exposed for testing, should not generally be used. | 114 // Exposed for testing, should not generally be used. |
| 113 internal::Router* internal_router() { return binding_.internal_router(); } | 115 internal::Router* internal_router() { return binding_.internal_router(); } |
| 114 | 116 |
| 115 void OnConnectionError() { | 117 void OnConnectionError() { |
| 116 connection_error_handler_.Run(); | 118 connection_error_handler_.Run(); |
| 117 delete binding_.impl(); | 119 delete binding_.impl(); |
| 118 } | 120 } |
| 119 | 121 |
| 120 private: | 122 private: |
| 121 Closure connection_error_handler_; | 123 Closure connection_error_handler_; |
| 122 Binding<Interface> binding_; | 124 Binding<Interface> binding_; |
| 123 }; | 125 }; |
| 124 | 126 |
| 125 } // namespace mojo | 127 } // namespace mojo |
| 126 | 128 |
| 127 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ | 129 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ |
| OLD | NEW |