| 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" |
| 11 #include "mojo/public/cpp/bindings/binding.h" | 11 #include "mojo/public/cpp/bindings/binding.h" |
| 12 #include "mojo/public/cpp/bindings/callback.h" |
| 12 #include "mojo/public/cpp/bindings/error_handler.h" | 13 #include "mojo/public/cpp/bindings/error_handler.h" |
| 13 #include "mojo/public/cpp/bindings/interface_ptr.h" | 14 #include "mojo/public/cpp/bindings/interface_ptr.h" |
| 14 #include "mojo/public/cpp/bindings/interface_request.h" | 15 #include "mojo/public/cpp/bindings/interface_request.h" |
| 15 #include "mojo/public/cpp/bindings/lib/filter_chain.h" | 16 #include "mojo/public/cpp/bindings/lib/filter_chain.h" |
| 16 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" | 17 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" |
| 17 #include "mojo/public/cpp/bindings/lib/router.h" | 18 #include "mojo/public/cpp/bindings/lib/router.h" |
| 18 #include "mojo/public/cpp/system/core.h" | 19 #include "mojo/public/cpp/system/core.h" |
| 19 | 20 |
| 20 namespace mojo { | 21 namespace mojo { |
| 21 | 22 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 37 // }; | 38 // }; |
| 38 // | 39 // |
| 39 // class MyFooFactory : public InterfaceFactory<Foo> { | 40 // class MyFooFactory : public InterfaceFactory<Foo> { |
| 40 // public: | 41 // public: |
| 41 // void Create(..., InterfaceRequest<Foo> request) override { | 42 // void Create(..., InterfaceRequest<Foo> request) override { |
| 42 // new StronglyBound(request.Pass()); // The binding now owns the | 43 // new StronglyBound(request.Pass()); // The binding now owns the |
| 43 // // instance of StronglyBound. | 44 // // instance of StronglyBound. |
| 44 // } | 45 // } |
| 45 // }; | 46 // }; |
| 46 template <typename Interface> | 47 template <typename Interface> |
| 47 class StrongBinding : public ErrorHandler { | 48 class StrongBinding { |
| 48 MOJO_MOVE_ONLY_TYPE(StrongBinding) | 49 MOJO_MOVE_ONLY_TYPE(StrongBinding) |
| 49 | 50 |
| 50 public: | 51 public: |
| 51 explicit StrongBinding(Interface* impl) : binding_(impl) { | 52 explicit StrongBinding(Interface* impl) : binding_(impl) { |
| 52 binding_.set_error_handler(this); | 53 binding_.set_connection_error_handler([this]() { OnConnectionError(); }); |
| 53 } | 54 } |
| 54 | 55 |
| 55 StrongBinding( | 56 StrongBinding( |
| 56 Interface* impl, | 57 Interface* impl, |
| 57 ScopedMessagePipeHandle handle, | 58 ScopedMessagePipeHandle handle, |
| 58 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) | 59 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) |
| 59 : StrongBinding(impl) { | 60 : StrongBinding(impl) { |
| 60 binding_.Bind(handle.Pass(), waiter); | 61 binding_.Bind(handle.Pass(), waiter); |
| 61 } | 62 } |
| 62 | 63 |
| 63 StrongBinding( | 64 StrongBinding( |
| 64 Interface* impl, | 65 Interface* impl, |
| 65 InterfacePtr<Interface>* ptr, | 66 InterfacePtr<Interface>* ptr, |
| 66 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) | 67 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) |
| 67 : StrongBinding(impl) { | 68 : StrongBinding(impl) { |
| 68 binding_.Bind(ptr, waiter); | 69 binding_.Bind(ptr, waiter); |
| 69 } | 70 } |
| 70 | 71 |
| 71 StrongBinding( | 72 StrongBinding( |
| 72 Interface* impl, | 73 Interface* impl, |
| 73 InterfaceRequest<Interface> request, | 74 InterfaceRequest<Interface> request, |
| 74 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) | 75 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) |
| 75 : StrongBinding(impl) { | 76 : StrongBinding(impl) { |
| 76 binding_.Bind(request.Pass(), waiter); | 77 binding_.Bind(request.Pass(), waiter); |
| 77 } | 78 } |
| 78 | 79 |
| 79 ~StrongBinding() override {} | 80 ~StrongBinding() {} |
| 80 | 81 |
| 81 void Bind( | 82 void Bind( |
| 82 ScopedMessagePipeHandle handle, | 83 ScopedMessagePipeHandle handle, |
| 83 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 84 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 84 assert(!binding_.is_bound()); | 85 assert(!binding_.is_bound()); |
| 85 binding_.Bind(handle.Pass(), waiter); | 86 binding_.Bind(handle.Pass(), waiter); |
| 86 } | 87 } |
| 87 | 88 |
| 88 void Bind( | 89 void Bind( |
| 89 InterfacePtr<Interface>* ptr, | 90 InterfacePtr<Interface>* ptr, |
| 90 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 91 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 91 assert(!binding_.is_bound()); | 92 assert(!binding_.is_bound()); |
| 92 binding_.Bind(ptr, waiter); | 93 binding_.Bind(ptr, waiter); |
| 93 } | 94 } |
| 94 | 95 |
| 95 void Bind( | 96 void Bind( |
| 96 InterfaceRequest<Interface> request, | 97 InterfaceRequest<Interface> request, |
| 97 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 98 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 98 assert(!binding_.is_bound()); | 99 assert(!binding_.is_bound()); |
| 99 binding_.Bind(request.Pass(), waiter); | 100 binding_.Bind(request.Pass(), waiter); |
| 100 } | 101 } |
| 101 | 102 |
| 102 bool WaitForIncomingMethodCall() { | 103 bool WaitForIncomingMethodCall() { |
| 103 return binding_.WaitForIncomingMethodCall(); | 104 return binding_.WaitForIncomingMethodCall(); |
| 104 } | 105 } |
| 105 | 106 |
| 107 void set_connection_error_handler(const Closure& error_handler) { |
| 108 connection_error_handler_ = error_handler; |
| 109 } |
| 110 |
| 111 // NOTE: Deprecated. Please use the method above. |
| 112 // TODO(yzshen): Remove this method once all callsites are converted. |
| 106 void set_error_handler(ErrorHandler* error_handler) { | 113 void set_error_handler(ErrorHandler* error_handler) { |
| 107 error_handler_ = error_handler; | 114 set_connection_error_handler( |
| 115 [error_handler]() { error_handler->OnConnectionError(); }); |
| 108 } | 116 } |
| 109 | 117 |
| 110 Interface* impl() { return binding_.impl(); } | 118 Interface* impl() { return binding_.impl(); } |
| 111 // Exposed for testing, should not generally be used. | 119 // Exposed for testing, should not generally be used. |
| 112 internal::Router* internal_router() { return binding_.internal_router(); } | 120 internal::Router* internal_router() { return binding_.internal_router(); } |
| 113 | 121 |
| 114 // ErrorHandler implementation | 122 void OnConnectionError() { |
| 115 void OnConnectionError() override { | 123 connection_error_handler_.Run(); |
| 116 if (error_handler_) | |
| 117 error_handler_->OnConnectionError(); | |
| 118 delete binding_.impl(); | 124 delete binding_.impl(); |
| 119 } | 125 } |
| 120 | 126 |
| 121 private: | 127 private: |
| 122 ErrorHandler* error_handler_ = nullptr; | 128 Closure connection_error_handler_; |
| 123 Binding<Interface> binding_; | 129 Binding<Interface> binding_; |
| 124 }; | 130 }; |
| 125 | 131 |
| 126 } // namespace mojo | 132 } // namespace mojo |
| 127 | 133 |
| 128 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ | 134 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ |
| OLD | NEW |