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.
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_
12 #include "mojo/public/cpp/bindings/error_handler.h"
13 #include "mojo/public/cpp/bindings/lib/interface_ptr_internal.h"
14 #include "mojo/public/cpp/system/macros.h"
19 // InterfacePtr represents a proxy to a remote instance of an interface.
20 template <typename Interface>
22 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(InterfacePtr, RValue)
26 InterfacePtr(RValue other) {
27 other.object->internal_state_.Swap(&internal_state_);
29 InterfacePtr& operator=(RValue other) {
30 other.object->internal_state_.Swap(&internal_state_);
36 Interface* get() const {
37 return internal_state_.instance();
39 Interface* operator->() const { return get(); }
40 Interface* operator*() const { return get(); }
44 internal_state_.Swap(&doomed);
47 // This method configures the InterfacePtr<..> to be a proxy to a remote
48 // object on the other end of the given pipe.
50 // The proxy is bound to the current thread, which means its methods may
51 // only be called on the current thread.
53 // To move a bound InterfacePtr<..> to another thread, call
54 // ResetAndReturnMessagePipe. Then create a new InterfacePtr<..> on another
55 // thread, and bind the new InterfacePtr<..> to the message pipe on that
57 void Bind(ScopedMessagePipeHandle handle,
58 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) {
60 internal_state_.ConfigureProxy(handle.Pass(), waiter);
63 // This method may be called to query if the underlying pipe has encountered
64 // an error. If true, this means method calls made on this interface will be
65 // dropped (and may have already been dropped) on the floor.
66 bool encountered_error() const {
67 assert(internal_state_.router());
68 return internal_state_.router()->encountered_error();
71 // This method may be called to register an ErrorHandler to observe a
72 // connection error on the underlying pipe. The callback runs asynchronously
73 // from the current message loop.
74 void set_error_handler(ErrorHandler* error_handler) {
75 assert(internal_state_.router());
76 internal_state_.router()->set_error_handler(error_handler);
79 // Returns the underlying message pipe handle (if any) and resets the
80 // InterfacePtr<..> to its uninitialized state. This method is helpful if you
81 // need to move a proxy to another thread. See related notes for Bind.
82 ScopedMessagePipeHandle ResetAndReturnMessagePipe() {
84 internal_state_.Swap(&state);
85 return state.router() ?
86 state.router()->ReleaseMessagePipe() : ScopedMessagePipeHandle();
89 // DO NOT USE. Exposed only for internal use and for testing.
90 internal::InterfacePtrState<Interface>* internal_state() {
91 return &internal_state_;
95 typedef internal::InterfacePtrState<Interface> State;
96 State internal_state_;
99 // Takes a handle to the proxy end-point of a pipe. On the other end is
100 // presumed to be an interface implementation of type |Interface|. Returns a
101 // generated proxy to that interface, which may be used on the current thread.
102 // It is valid to call SetClient on the returned Interface to set an instance
103 // of Interface::Client.
104 template <typename Interface>
105 InterfacePtr<Interface> MakeProxy(
106 ScopedMessagePipeHandle handle,
107 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) {
108 InterfacePtr<Interface> ptr;
109 if (handle.is_valid())
110 ptr.Bind(handle.Pass(), waiter);
116 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_