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_PTR_H_ | |
6 #define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ | |
7 | |
8 #include <algorithm> | |
9 | |
10 #include "mojo/public/cpp/bindings/error_handler.h" | |
11 #include "mojo/public/cpp/bindings/lib/interface_ptr_internal.h" | |
12 #include "mojo/public/cpp/environment/environment.h" | |
13 #include "mojo/public/cpp/system/macros.h" | |
14 | |
15 namespace mojo { | |
16 class ErrorHandler; | |
17 | |
18 // InterfacePtr represents a proxy to a remote instance of an interface. | |
19 template <typename Interface> | |
20 class InterfacePtr { | |
21 MOJO_MOVE_ONLY_TYPE(InterfacePtr) | |
22 public: | |
23 InterfacePtr() {} | |
24 InterfacePtr(decltype(nullptr)) {} | |
25 | |
26 InterfacePtr(InterfacePtr&& other) { | |
27 internal_state_.Swap(&other.internal_state_); | |
28 } | |
29 InterfacePtr& operator=(InterfacePtr&& other) { | |
30 reset(); | |
31 internal_state_.Swap(&other.internal_state_); | |
32 return *this; | |
33 } | |
34 | |
35 InterfacePtr& operator=(decltype(nullptr)) { | |
36 reset(); | |
37 return *this; | |
38 } | |
39 | |
40 ~InterfacePtr() {} | |
41 | |
42 Interface* get() const { return internal_state_.instance(); } | |
43 Interface* operator->() const { return get(); } | |
44 Interface& operator*() const { return *get(); } | |
45 | |
46 void reset() { | |
47 State doomed; | |
48 internal_state_.Swap(&doomed); | |
49 } | |
50 | |
51 // Blocks the current thread for the first incoming method call, i.e., either | |
52 // a call to a client method or a callback method. Returns |true| if a method | |
53 // has been called, |false| in case of error. It must only be called on a | |
54 // bound object. | |
55 bool WaitForIncomingMethodCall() { | |
56 return internal_state_.WaitForIncomingMethodCall(); | |
57 } | |
58 | |
59 // This method configures the InterfacePtr<..> to be a proxy to a remote | |
60 // object on the other end of the given pipe. | |
61 // | |
62 // The proxy is bound to the current thread, which means its methods may | |
63 // only be called on the current thread. | |
64 // | |
65 // To move a bound InterfacePtr<..> to another thread, call PassMessagePipe(). | |
66 // Then create a new InterfacePtr<..> on another thread, and bind the new | |
67 // InterfacePtr<..> to the message pipe on that thread. | |
68 void Bind( | |
69 ScopedMessagePipeHandle handle, | |
70 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | |
71 reset(); | |
72 internal_state_.Bind(handle.Pass(), waiter); | |
73 } | |
74 | |
75 // The client interface may only be set after this InterfacePtr<..> is bound. | |
76 void set_client(typename Interface::Client* client) { | |
77 internal_state_.set_client(client); | |
78 } | |
79 | |
80 // This method may be called to query if the underlying pipe has encountered | |
81 // an error. If true, this means method calls made on this interface will be | |
82 // dropped (and may have already been dropped) on the floor. | |
83 bool encountered_error() const { return internal_state_.encountered_error(); } | |
84 | |
85 // This method may be called to register an ErrorHandler to observe a | |
86 // connection error on the underlying pipe. It must only be called on a bound | |
87 // object. | |
88 // The callback runs asynchronously from the current message loop. | |
89 void set_error_handler(ErrorHandler* error_handler) { | |
90 internal_state_.set_error_handler(error_handler); | |
91 } | |
92 | |
93 // Returns the underlying message pipe handle (if any) and resets the | |
94 // InterfacePtr<..> to its uninitialized state. This method is helpful if you | |
95 // need to move a proxy to another thread. See related notes for Bind. | |
96 ScopedMessagePipeHandle PassMessagePipe() { | |
97 State state; | |
98 internal_state_.Swap(&state); | |
99 return state.PassMessagePipe(); | |
100 } | |
101 | |
102 // DO NOT USE. Exposed only for internal use and for testing. | |
103 internal::InterfacePtrState<Interface>* internal_state() { | |
104 return &internal_state_; | |
105 } | |
106 | |
107 // Allow InterfacePtr<> to be used in boolean expressions, but not | |
108 // implicitly convertible to a real bool (which is dangerous). | |
109 private: | |
110 typedef internal::InterfacePtrState<Interface> InterfacePtr::*Testable; | |
111 | |
112 public: | |
113 operator Testable() const { | |
114 return internal_state_.is_bound() ? &InterfacePtr::internal_state_ | |
115 : nullptr; | |
116 } | |
117 | |
118 private: | |
119 typedef internal::InterfacePtrState<Interface> State; | |
120 mutable State internal_state_; | |
121 }; | |
122 | |
123 // Takes a handle to the proxy end-point of a pipe. On the other end is | |
124 // presumed to be an interface implementation of type |Interface|. Returns a | |
125 // generated proxy to that interface, which may be used on the current thread. | |
126 // It is valid to call set_client on the returned InterfacePtr<..> to set an | |
127 // instance of Interface::Client. | |
128 template <typename Interface> | |
129 InterfacePtr<Interface> MakeProxy( | |
130 ScopedMessagePipeHandle handle, | |
131 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | |
132 InterfacePtr<Interface> ptr; | |
133 if (handle.is_valid()) | |
134 ptr.Bind(handle.Pass(), waiter); | |
135 return ptr.Pass(); | |
136 } | |
137 | |
138 } // namespace mojo | |
139 | |
140 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ | |
OLD | NEW |