| 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> | |
| 9 #include <utility> | 8 #include <utility> |
| 10 | 9 |
| 10 #include "base/logging.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/callback.h" |
| 13 #include "mojo/public/cpp/bindings/interface_ptr.h" | 13 #include "mojo/public/cpp/bindings/interface_ptr.h" |
| 14 #include "mojo/public/cpp/bindings/interface_request.h" | 14 #include "mojo/public/cpp/bindings/interface_request.h" |
| 15 #include "mojo/public/cpp/bindings/lib/filter_chain.h" | 15 #include "mojo/public/cpp/bindings/lib/filter_chain.h" |
| 16 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" | 16 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" |
| 17 #include "mojo/public/cpp/bindings/lib/router.h" | 17 #include "mojo/public/cpp/bindings/lib/router.h" |
| 18 #include "mojo/public/cpp/system/core.h" | 18 #include "mojo/public/cpp/system/core.h" |
| 19 | 19 |
| 20 namespace mojo { | 20 namespace mojo { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 } | 64 } |
| 65 | 65 |
| 66 StrongBinding(Interface* impl, InterfaceRequest<Interface> request) | 66 StrongBinding(Interface* impl, InterfaceRequest<Interface> request) |
| 67 : StrongBinding(impl) { | 67 : StrongBinding(impl) { |
| 68 Bind(std::move(request)); | 68 Bind(std::move(request)); |
| 69 } | 69 } |
| 70 | 70 |
| 71 ~StrongBinding() {} | 71 ~StrongBinding() {} |
| 72 | 72 |
| 73 void Bind(ScopedMessagePipeHandle handle) { | 73 void Bind(ScopedMessagePipeHandle handle) { |
| 74 assert(!binding_.is_bound()); | 74 DCHECK(!binding_.is_bound()); |
| 75 binding_.Bind(std::move(handle)); | 75 binding_.Bind(std::move(handle)); |
| 76 binding_.set_connection_error_handler([this]() { OnConnectionError(); }); | 76 binding_.set_connection_error_handler([this]() { OnConnectionError(); }); |
| 77 } | 77 } |
| 78 | 78 |
| 79 void Bind(InterfacePtr<Interface>* ptr) { | 79 void Bind(InterfacePtr<Interface>* ptr) { |
| 80 assert(!binding_.is_bound()); | 80 DCHECK(!binding_.is_bound()); |
| 81 binding_.Bind(ptr); | 81 binding_.Bind(ptr); |
| 82 binding_.set_connection_error_handler([this]() { OnConnectionError(); }); | 82 binding_.set_connection_error_handler([this]() { OnConnectionError(); }); |
| 83 } | 83 } |
| 84 | 84 |
| 85 void Bind(InterfaceRequest<Interface> request) { | 85 void Bind(InterfaceRequest<Interface> request) { |
| 86 assert(!binding_.is_bound()); | 86 DCHECK(!binding_.is_bound()); |
| 87 binding_.Bind(std::move(request)); | 87 binding_.Bind(std::move(request)); |
| 88 binding_.set_connection_error_handler([this]() { OnConnectionError(); }); | 88 binding_.set_connection_error_handler([this]() { OnConnectionError(); }); |
| 89 } | 89 } |
| 90 | 90 |
| 91 bool WaitForIncomingMethodCall() { | 91 bool WaitForIncomingMethodCall() { |
| 92 return binding_.WaitForIncomingMethodCall(); | 92 return binding_.WaitForIncomingMethodCall(); |
| 93 } | 93 } |
| 94 | 94 |
| 95 // Note: The error handler must not delete the interface implementation. | 95 // Note: The error handler must not delete the interface implementation. |
| 96 // | 96 // |
| 97 // This method may only be called after this StrongBinding has been bound to a | 97 // This method may only be called after this StrongBinding has been bound to a |
| 98 // message pipe. | 98 // message pipe. |
| 99 void set_connection_error_handler(const Closure& error_handler) { | 99 void set_connection_error_handler(const Closure& error_handler) { |
| 100 assert(binding_.is_bound()); | 100 DCHECK(binding_.is_bound()); |
| 101 connection_error_handler_ = error_handler; | 101 connection_error_handler_ = error_handler; |
| 102 } | 102 } |
| 103 | 103 |
| 104 Interface* impl() { return binding_.impl(); } | 104 Interface* impl() { return binding_.impl(); } |
| 105 // Exposed for testing, should not generally be used. | 105 // Exposed for testing, should not generally be used. |
| 106 internal::Router* internal_router() { return binding_.internal_router(); } | 106 internal::Router* internal_router() { return binding_.internal_router(); } |
| 107 | 107 |
| 108 void OnConnectionError() { | 108 void OnConnectionError() { |
| 109 connection_error_handler_.Run(); | 109 connection_error_handler_.Run(); |
| 110 delete binding_.impl(); | 110 delete binding_.impl(); |
| 111 } | 111 } |
| 112 | 112 |
| 113 private: | 113 private: |
| 114 Closure connection_error_handler_; | 114 Closure connection_error_handler_; |
| 115 Binding<Interface> binding_; | 115 Binding<Interface> binding_; |
| 116 }; | 116 }; |
| 117 | 117 |
| 118 } // namespace mojo | 118 } // namespace mojo |
| 119 | 119 |
| 120 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ | 120 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ |
| OLD | NEW |