| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_BINDING_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_BINDING_H_ |
| 7 |
| 8 #include "mojo/public/cpp/bindings/interface_request.h" |
| 9 #include "mojo/public/cpp/bindings/lib/interface_impl_internal.h" |
| 10 #include "mojo/public/cpp/environment/environment.h" |
| 11 #include "mojo/public/cpp/system/macros.h" |
| 12 |
| 13 namespace mojo { |
| 14 |
| 15 template <typename Interface> |
| 16 class InterfaceImpl; |
| 17 |
| 18 // InterfaceBinding is used to bind an implementation of an interface to a pipe. |
| 19 // Once bound, client() returns the client of the Interface. |
| 20 // InterfaceBinding owns the underlying pipe. Deleting InterfaceBinding |
| 21 // closes the pipe (assuming it is open). |
| 22 // InterfaceBinding can be configured to delete the supplied interface when |
| 23 // InterfaceBinding is deleted. See set_owns_interface(). |
| 24 // See also InterfaceImpl. |
| 25 template <typename Interface> |
| 26 class InterfaceBinding : public InterfaceBindingDelegate { |
| 27 public: |
| 28 typedef typename Interface::Client Client; |
| 29 |
| 30 InterfaceBinding(Interface* interface, InterfaceBindingDelegate* delegate) |
| 31 : delegate_(delegate), |
| 32 internal_state_(interface, this), |
| 33 destroyed_(nullptr), |
| 34 owns_interface_(false) {} |
| 35 |
| 36 virtual ~InterfaceBinding() { |
| 37 if (destroyed_) |
| 38 *destroyed_ = true; |
| 39 if (owns_interface_) |
| 40 delete internal_state_.instance(); |
| 41 } |
| 42 |
| 43 void BindToPipe( |
| 44 ScopedMessagePipeHandle handle, |
| 45 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 46 internal_state_.Bind(handle.Pass(), false, waiter); |
| 47 } |
| 48 |
| 49 void BindToRequest( |
| 50 InterfaceRequest<Interface>* request, |
| 51 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 52 return BindToPipe(request->PassMessagePipe(), waiter); |
| 53 } |
| 54 |
| 55 // Whether the supplied interface should be deleted when InterfaceBinding is |
| 56 // destroyed. |
| 57 void set_owns_interface(bool owns_interface) { |
| 58 owns_interface_ = owns_interface; |
| 59 } |
| 60 |
| 61 Client* client() { return internal_state_.client(); } |
| 62 |
| 63 private: |
| 64 friend class InterfaceImpl<Interface>; |
| 65 |
| 66 void set_delete_interface_on_error(bool delete_interface_on_error) { |
| 67 delete_interface_on_error_ = delete_interface_on_error; |
| 68 } |
| 69 |
| 70 // InterfaceBindingDelegate: |
| 71 void OnConnectionEstablished() override { |
| 72 delegate_->OnConnectionEstablished(); |
| 73 } |
| 74 void OnConnectionError() override { |
| 75 bool destroyed = false; |
| 76 destroyed_ = &destroyed; |
| 77 // Allow OnConnectionError() to delete 'this'. |
| 78 delegate_->OnConnectionError(); |
| 79 if (destroyed) |
| 80 return; |
| 81 destroyed_ = nullptr; |
| 82 // |delete_interface_on_error_| means we're in an InterfaceImpl and need |
| 83 // to delete the InterfaceImpl (which triggers deleting us). |
| 84 if (internal_state_.instance_bound_to_pipe()) { |
| 85 if (delete_interface_on_error_) |
| 86 delete internal_state_.instance(); |
| 87 else |
| 88 delete this; |
| 89 } |
| 90 } |
| 91 |
| 92 InterfaceBindingDelegate* delegate_; |
| 93 |
| 94 internal::InterfaceImplState<Interface> internal_state_; |
| 95 // If non-null the destructor sets the value to true. Used to detect deletion |
| 96 // while calling to delegate. |
| 97 bool* destroyed_; |
| 98 |
| 99 bool owns_interface_; |
| 100 |
| 101 // See description in OnConnectionError(). |
| 102 bool delete_interface_on_error_; |
| 103 |
| 104 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceBinding); |
| 105 }; |
| 106 |
| 107 } // namespace mojo |
| 108 |
| 109 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_BINDING_H_ |
| OLD | NEW |