| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_ASSOCIATED_BINDING_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_BINDING_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_BINDING_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_BINDING_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/bind.h" |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 13 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 14 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
| 15 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "mojo/public/cpp/bindings/associated_group.h" | 17 #include "mojo/public/cpp/bindings/associated_group.h" |
| 17 #include "mojo/public/cpp/bindings/associated_interface_request.h" | 18 #include "mojo/public/cpp/bindings/associated_interface_request.h" |
| 18 #include "mojo/public/cpp/bindings/callback.h" | 19 #include "mojo/public/cpp/bindings/callback.h" |
| 19 #include "mojo/public/cpp/bindings/lib/interface_endpoint_client.h" | 20 #include "mojo/public/cpp/bindings/lib/interface_endpoint_client.h" |
| 20 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" | 21 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 if (!handle.is_valid() || !handle.is_local()) { | 96 if (!handle.is_valid() || !handle.is_local()) { |
| 96 endpoint_client_.reset(); | 97 endpoint_client_.reset(); |
| 97 return; | 98 return; |
| 98 } | 99 } |
| 99 | 100 |
| 100 endpoint_client_.reset(new internal::InterfaceEndpointClient( | 101 endpoint_client_.reset(new internal::InterfaceEndpointClient( |
| 101 std::move(handle), &stub_, | 102 std::move(handle), &stub_, |
| 102 base::WrapUnique(new typename Interface::RequestValidator_()), | 103 base::WrapUnique(new typename Interface::RequestValidator_()), |
| 103 Interface::HasSyncMethods_, std::move(runner))); | 104 Interface::HasSyncMethods_, std::move(runner))); |
| 104 endpoint_client_->set_connection_error_handler( | 105 endpoint_client_->set_connection_error_handler( |
| 105 [this]() { connection_error_handler_.Run(); }); | 106 base::Bind(&AssociatedBinding::RunConnectionErrorHandler, |
| 107 base::Unretained(this))); |
| 106 | 108 |
| 107 stub_.serialization_context()->router = endpoint_client_->router(); | 109 stub_.serialization_context()->router = endpoint_client_->router(); |
| 108 } | 110 } |
| 109 | 111 |
| 110 // Closes the associated interface. Puts this object into a state where it can | 112 // Closes the associated interface. Puts this object into a state where it can |
| 111 // be rebound. | 113 // be rebound. |
| 112 void Close() { | 114 void Close() { |
| 113 DCHECK(endpoint_client_); | 115 DCHECK(endpoint_client_); |
| 114 endpoint_client_.reset(); | 116 endpoint_client_.reset(); |
| 115 connection_error_handler_.reset(); | 117 connection_error_handler_.Reset(); |
| 116 } | 118 } |
| 117 | 119 |
| 118 // Unbinds and returns the associated interface request so it can be | 120 // Unbinds and returns the associated interface request so it can be |
| 119 // used in another context, such as on another thread or with a different | 121 // used in another context, such as on another thread or with a different |
| 120 // implementation. Puts this object into a state where it can be rebound. | 122 // implementation. Puts this object into a state where it can be rebound. |
| 121 AssociatedInterfaceRequest<Interface> Unbind() { | 123 AssociatedInterfaceRequest<Interface> Unbind() { |
| 122 DCHECK(endpoint_client_); | 124 DCHECK(endpoint_client_); |
| 123 | 125 |
| 124 AssociatedInterfaceRequest<Interface> request; | 126 AssociatedInterfaceRequest<Interface> request; |
| 125 request.Bind(endpoint_client_->PassHandle()); | 127 request.Bind(endpoint_client_->PassHandle()); |
| 126 | 128 |
| 127 endpoint_client_.reset(); | 129 endpoint_client_.reset(); |
| 128 connection_error_handler_.reset(); | 130 connection_error_handler_.Reset(); |
| 129 | 131 |
| 130 return request; | 132 return request; |
| 131 } | 133 } |
| 132 | 134 |
| 133 // Sets an error handler that will be called if a connection error occurs. | 135 // Sets an error handler that will be called if a connection error occurs. |
| 134 // | 136 // |
| 135 // This method may only be called after this AssociatedBinding has been bound | 137 // This method may only be called after this AssociatedBinding has been bound |
| 136 // to a message pipe. The error handler will be reset when this | 138 // to a message pipe. The error handler will be reset when this |
| 137 // AssociatedBinding is unbound or closed. | 139 // AssociatedBinding is unbound or closed. |
| 138 void set_connection_error_handler(const Closure& error_handler) { | 140 void set_connection_error_handler(const Closure& error_handler) { |
| 139 DCHECK(is_bound()); | 141 DCHECK(is_bound()); |
| 140 connection_error_handler_ = error_handler; | 142 connection_error_handler_ = error_handler; |
| 141 } | 143 } |
| 142 | 144 |
| 143 // Returns the interface implementation that was previously specified. | 145 // Returns the interface implementation that was previously specified. |
| 144 Interface* impl() { return impl_; } | 146 Interface* impl() { return impl_; } |
| 145 | 147 |
| 146 // Indicates whether the associated binding has been completed. | 148 // Indicates whether the associated binding has been completed. |
| 147 bool is_bound() const { return !!endpoint_client_; } | 149 bool is_bound() const { return !!endpoint_client_; } |
| 148 | 150 |
| 149 // Returns the associated group that this object belongs to. Returns null if | 151 // Returns the associated group that this object belongs to. Returns null if |
| 150 // the object is not bound. | 152 // the object is not bound. |
| 151 AssociatedGroup* associated_group() { | 153 AssociatedGroup* associated_group() { |
| 152 return endpoint_client_ ? endpoint_client_->associated_group() : nullptr; | 154 return endpoint_client_ ? endpoint_client_->associated_group() : nullptr; |
| 153 } | 155 } |
| 154 | 156 |
| 155 private: | 157 private: |
| 158 void RunConnectionErrorHandler() { |
| 159 if (!connection_error_handler_.is_null()) |
| 160 connection_error_handler_.Run(); |
| 161 } |
| 162 |
| 156 std::unique_ptr<internal::InterfaceEndpointClient> endpoint_client_; | 163 std::unique_ptr<internal::InterfaceEndpointClient> endpoint_client_; |
| 157 | 164 |
| 158 typename Interface::Stub_ stub_; | 165 typename Interface::Stub_ stub_; |
| 159 Interface* impl_; | 166 Interface* impl_; |
| 160 Closure connection_error_handler_; | 167 Closure connection_error_handler_; |
| 161 | 168 |
| 162 DISALLOW_COPY_AND_ASSIGN(AssociatedBinding); | 169 DISALLOW_COPY_AND_ASSIGN(AssociatedBinding); |
| 163 }; | 170 }; |
| 164 | 171 |
| 165 } // namespace mojo | 172 } // namespace mojo |
| 166 | 173 |
| 167 #endif // MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_BINDING_H_ | 174 #endif // MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_BINDING_H_ |
| OLD | NEW |