| 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_INTERFACE_PTR_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ |
| 7 | 7 |
| 8 #include <assert.h> | 8 #include <assert.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 bool WaitForIncomingMethodCall() { | 53 bool WaitForIncomingMethodCall() { |
| 54 return internal_state_.WaitForIncomingMethodCall(); | 54 return internal_state_.WaitForIncomingMethodCall(); |
| 55 } | 55 } |
| 56 | 56 |
| 57 // This method configures the InterfacePtr<..> to be a proxy to a remote | 57 // This method configures the InterfacePtr<..> to be a proxy to a remote |
| 58 // object on the other end of the given pipe. | 58 // object on the other end of the given pipe. |
| 59 // | 59 // |
| 60 // The proxy is bound to the current thread, which means its methods may | 60 // The proxy is bound to the current thread, which means its methods may |
| 61 // only be called on the current thread. | 61 // only be called on the current thread. |
| 62 // | 62 // |
| 63 // To move a bound InterfacePtr<..> to another thread, call | 63 // To move a bound InterfacePtr<..> to another thread, call PassMessagePipe(). |
| 64 // ResetAndReturnMessagePipe. Then create a new InterfacePtr<..> on another | 64 // Then create a new InterfacePtr<..> on another thread, and bind the new |
| 65 // thread, and bind the new InterfacePtr<..> to the message pipe on that | 65 // InterfacePtr<..> to the message pipe on that thread. |
| 66 // thread. | |
| 67 void Bind( | 66 void Bind( |
| 68 ScopedMessagePipeHandle handle, | 67 ScopedMessagePipeHandle handle, |
| 69 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 68 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 70 reset(); | 69 reset(); |
| 71 internal_state_.ConfigureProxy(handle.Pass(), waiter); | 70 internal_state_.Bind(handle.Pass(), waiter); |
| 72 } | 71 } |
| 73 | 72 |
| 74 // The client interface may only be set after this InterfacePtr<..> is bound. | 73 // The client interface may only be set after this InterfacePtr<..> is bound. |
| 75 void set_client(typename Interface::Client* client) { | 74 void set_client(typename Interface::Client* client) { |
| 76 internal_state_.set_client(client); | 75 internal_state_.set_client(client); |
| 77 } | 76 } |
| 78 | 77 |
| 79 // This method may be called to query if the underlying pipe has encountered | 78 // This method may be called to query if the underlying pipe has encountered |
| 80 // an error. If true, this means method calls made on this interface will be | 79 // an error. If true, this means method calls made on this interface will be |
| 81 // dropped (and may have already been dropped) on the floor. | 80 // dropped (and may have already been dropped) on the floor. |
| 82 bool encountered_error() const { | 81 bool encountered_error() const { |
| 83 assert(internal_state_.router()); | 82 return internal_state_.encountered_error(); |
| 84 return internal_state_.router()->encountered_error(); | |
| 85 } | 83 } |
| 86 | 84 |
| 87 // This method may be called to register an ErrorHandler to observe a | 85 // This method may be called to register an ErrorHandler to observe a |
| 88 // connection error on the underlying pipe. The callback runs asynchronously | 86 // connection error on the underlying pipe. It must only be called on a bound |
| 89 // from the current message loop. | 87 // object. |
| 88 // The callback runs asynchronously from the current message loop. |
| 90 void set_error_handler(ErrorHandler* error_handler) { | 89 void set_error_handler(ErrorHandler* error_handler) { |
| 91 assert(internal_state_.router()); | 90 internal_state_.set_error_handler(error_handler); |
| 92 internal_state_.router()->set_error_handler(error_handler); | |
| 93 } | 91 } |
| 94 | 92 |
| 95 // Returns the underlying message pipe handle (if any) and resets the | 93 // Returns the underlying message pipe handle (if any) and resets the |
| 96 // InterfacePtr<..> to its uninitialized state. This method is helpful if you | 94 // InterfacePtr<..> to its uninitialized state. This method is helpful if you |
| 97 // need to move a proxy to another thread. See related notes for Bind. | 95 // need to move a proxy to another thread. See related notes for Bind. |
| 98 ScopedMessagePipeHandle PassMessagePipe() { | 96 ScopedMessagePipeHandle PassMessagePipe() { |
| 99 State state; | 97 State state; |
| 100 internal_state_.Swap(&state); | 98 internal_state_.Swap(&state); |
| 101 return state.router() ? | 99 return state.PassMessagePipe(); |
| 102 state.router()->PassMessagePipe() : ScopedMessagePipeHandle(); | |
| 103 } | 100 } |
| 104 | 101 |
| 105 // DO NOT USE. Exposed only for internal use and for testing. | 102 // DO NOT USE. Exposed only for internal use and for testing. |
| 106 internal::InterfacePtrState<Interface>* internal_state() { | 103 internal::InterfacePtrState<Interface>* internal_state() { |
| 107 return &internal_state_; | 104 return &internal_state_; |
| 108 } | 105 } |
| 109 | 106 |
| 110 private: | 107 private: |
| 111 typedef internal::InterfacePtrState<Interface> State; | 108 typedef internal::InterfacePtrState<Interface> State; |
| 112 State internal_state_; | 109 mutable State internal_state_; |
| 113 }; | 110 }; |
| 114 | 111 |
| 115 // Takes a handle to the proxy end-point of a pipe. On the other end is | 112 // Takes a handle to the proxy end-point of a pipe. On the other end is |
| 116 // presumed to be an interface implementation of type |Interface|. Returns a | 113 // presumed to be an interface implementation of type |Interface|. Returns a |
| 117 // generated proxy to that interface, which may be used on the current thread. | 114 // generated proxy to that interface, which may be used on the current thread. |
| 118 // It is valid to call set_client on the returned InterfacePtr<..> to set an | 115 // It is valid to call set_client on the returned InterfacePtr<..> to set an |
| 119 // instance of Interface::Client. | 116 // instance of Interface::Client. |
| 120 template <typename Interface> | 117 template <typename Interface> |
| 121 InterfacePtr<Interface> MakeProxy( | 118 InterfacePtr<Interface> MakeProxy( |
| 122 ScopedMessagePipeHandle handle, | 119 ScopedMessagePipeHandle handle, |
| 123 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 120 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 124 InterfacePtr<Interface> ptr; | 121 InterfacePtr<Interface> ptr; |
| 125 if (handle.is_valid()) | 122 if (handle.is_valid()) |
| 126 ptr.Bind(handle.Pass(), waiter); | 123 ptr.Bind(handle.Pass(), waiter); |
| 127 return ptr.Pass(); | 124 return ptr.Pass(); |
| 128 } | 125 } |
| 129 | 126 |
| 130 } // namespace mojo | 127 } // namespace mojo |
| 131 | 128 |
| 132 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ | 129 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ |
| OLD | NEW |