Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: mojo/public/cpp/bindings/binding.h

Issue 1518293002: [mojo] Add Mojo bindings support for IPC::ParamTraits (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@pickle4
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/public/cpp/bindings/BUILD.gn ('k') | mojo/public/cpp/bindings/interface_request.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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_BINDING_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_
7 7
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "mojo/public/c/environment/async_waiter.h" 9 #include "mojo/public/c/environment/async_waiter.h"
10 #include "mojo/public/cpp/bindings/callback.h" 10 #include "mojo/public/cpp/bindings/callback.h"
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 // However, the caller may provide their own implementation if needed. The 52 // However, the caller may provide their own implementation if needed. The
53 // |Binding| will not take ownership of the waiter, and the waiter must outlive 53 // |Binding| will not take ownership of the waiter, and the waiter must outlive
54 // the |Binding|. The provided waiter must be able to signal the implementation 54 // the |Binding|. The provided waiter must be able to signal the implementation
55 // which generally means it needs to be able to schedule work on the thread the 55 // which generally means it needs to be able to schedule work on the thread the
56 // implementation runs on. If writing library code that has to work on different 56 // implementation runs on. If writing library code that has to work on different
57 // types of threads callers may need to provide different waiter 57 // types of threads callers may need to provide different waiter
58 // implementations. 58 // implementations.
59 template <typename Interface> 59 template <typename Interface>
60 class Binding { 60 class Binding {
61 public: 61 public:
62 using GenericInterface_ = typename Interface::GenericInterface_;
63
62 // Constructs an incomplete binding that will use the implementation |impl|. 64 // Constructs an incomplete binding that will use the implementation |impl|.
63 // The binding may be completed with a subsequent call to the |Bind| method. 65 // The binding may be completed with a subsequent call to the |Bind| method.
64 // Does not take ownership of |impl|, which must outlive the binding. 66 // Does not take ownership of |impl|, which must outlive the binding.
65 explicit Binding(Interface* impl) : internal_state_(impl) {} 67 explicit Binding(Interface* impl) : internal_state_(impl) {}
66 68
67 // Constructs a completed binding of message pipe |handle| to implementation 69 // Constructs a completed binding of message pipe |handle| to implementation
68 // |impl|. Does not take ownership of |impl|, which must outlive the binding. 70 // |impl|. Does not take ownership of |impl|, which must outlive the binding.
69 // See class comment for definition of |waiter|. 71 // See class comment for definition of |waiter|.
70 Binding(Interface* impl, 72 Binding(Interface* impl,
71 ScopedMessagePipeHandle handle, 73 ScopedMessagePipeHandle handle,
72 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) 74 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter())
73 : Binding(impl) { 75 : Binding(impl) {
74 Bind(handle.Pass(), waiter); 76 Bind(handle.Pass(), waiter);
75 } 77 }
76 78
77 // Constructs a completed binding of |impl| to a new message pipe, passing the 79 // Constructs a completed binding of |impl| to a new message pipe, passing the
78 // client end to |ptr|, which takes ownership of it. The caller is expected to 80 // client end to |ptr|, which takes ownership of it. The caller is expected to
79 // pass |ptr| on to the client of the service. Does not take ownership of any 81 // pass |ptr| on to the client of the service. Does not take ownership of any
80 // of the parameters. |impl| must outlive the binding. |ptr| only needs to 82 // of the parameters. |impl| must outlive the binding. |ptr| only needs to
81 // last until the constructor returns. See class comment for definition of 83 // last until the constructor returns. See class comment for definition of
82 // |waiter|. 84 // |waiter|.
83 Binding(Interface* impl, 85 Binding(Interface* impl,
84 InterfacePtr<Interface>* ptr, 86 InterfacePtr<GenericInterface_>* ptr,
85 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) 87 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter())
86 : Binding(impl) { 88 : Binding(impl) {
87 Bind(ptr, waiter); 89 Bind(ptr, waiter);
88 } 90 }
89 91
90 // Constructs a completed binding of |impl| to the message pipe endpoint in 92 // Constructs a completed binding of |impl| to the message pipe endpoint in
91 // |request|, taking ownership of the endpoint. Does not take ownership of 93 // |request|, taking ownership of the endpoint. Does not take ownership of
92 // |impl|, which must outlive the binding. See class comment for definition of 94 // |impl|, which must outlive the binding. See class comment for definition of
93 // |waiter|. 95 // |waiter|.
94 Binding(Interface* impl, 96 Binding(Interface* impl,
95 InterfaceRequest<Interface> request, 97 InterfaceRequest<GenericInterface_> request,
96 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) 98 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter())
97 : Binding(impl) { 99 : Binding(impl) {
98 Bind(request.PassMessagePipe(), waiter); 100 Bind(request.PassMessagePipe(), waiter);
99 } 101 }
100 102
101 // Tears down the binding, closing the message pipe and leaving the interface 103 // Tears down the binding, closing the message pipe and leaving the interface
102 // implementation unbound. 104 // implementation unbound.
103 ~Binding() {} 105 ~Binding() {}
104 106
105 // Completes a binding that was constructed with only an interface 107 // Completes a binding that was constructed with only an interface
106 // implementation. Takes ownership of |handle| and binds it to the previously 108 // implementation. Takes ownership of |handle| and binds it to the previously
107 // specified implementation. See class comment for definition of |waiter|. 109 // specified implementation. See class comment for definition of |waiter|.
108 void Bind( 110 void Bind(
109 ScopedMessagePipeHandle handle, 111 ScopedMessagePipeHandle handle,
110 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { 112 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) {
111 internal_state_.Bind(handle.Pass(), waiter); 113 internal_state_.Bind(handle.Pass(), waiter);
112 } 114 }
113 115
114 // Completes a binding that was constructed with only an interface 116 // Completes a binding that was constructed with only an interface
115 // implementation by creating a new message pipe, binding one end of it to the 117 // implementation by creating a new message pipe, binding one end of it to the
116 // previously specified implementation, and passing the other to |ptr|, which 118 // previously specified implementation, and passing the other to |ptr|, which
117 // takes ownership of it. The caller is expected to pass |ptr| on to the 119 // takes ownership of it. The caller is expected to pass |ptr| on to the
118 // eventual client of the service. Does not take ownership of |ptr|. See 120 // eventual client of the service. Does not take ownership of |ptr|. See
119 // class comment for definition of |waiter|. 121 // class comment for definition of |waiter|.
120 void Bind( 122 void Bind(
121 InterfacePtr<Interface>* ptr, 123 InterfacePtr<GenericInterface_>* ptr,
122 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { 124 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) {
123 MessagePipe pipe; 125 MessagePipe pipe;
124 ptr->Bind( 126 ptr->Bind(
125 InterfacePtrInfo<Interface>(pipe.handle0.Pass(), Interface::Version_), 127 InterfacePtrInfo<Interface>(pipe.handle0.Pass(), Interface::Version_),
126 waiter); 128 waiter);
127 Bind(pipe.handle1.Pass(), waiter); 129 Bind(pipe.handle1.Pass(), waiter);
128 } 130 }
129 131
130 // Completes a binding that was constructed with only an interface 132 // Completes a binding that was constructed with only an interface
131 // implementation by removing the message pipe endpoint from |request| and 133 // implementation by removing the message pipe endpoint from |request| and
132 // binding it to the previously specified implementation. See class comment 134 // binding it to the previously specified implementation. See class comment
133 // for definition of |waiter|. 135 // for definition of |waiter|.
134 void Bind( 136 void Bind(
135 InterfaceRequest<Interface> request, 137 InterfaceRequest<GenericInterface_> request,
136 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { 138 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) {
137 Bind(request.PassMessagePipe(), waiter); 139 Bind(request.PassMessagePipe(), waiter);
138 } 140 }
139 141
140 // Whether there are any associated interfaces running on the pipe currently. 142 // Whether there are any associated interfaces running on the pipe currently.
141 bool HasAssociatedInterfaces() const { 143 bool HasAssociatedInterfaces() const {
142 return internal_state_.HasAssociatedInterfaces(); 144 return internal_state_.HasAssociatedInterfaces();
143 } 145 }
144 146
145 // Stops processing incoming messages until 147 // Stops processing incoming messages until
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 // implementation. Put this object into a state where it can be rebound to a 181 // implementation. Put this object into a state where it can be rebound to a
180 // new pipe. 182 // new pipe.
181 // 183 //
182 // This method may only be called if the object has been bound to a message 184 // This method may only be called if the object has been bound to a message
183 // pipe and there are no associated interfaces running. 185 // pipe and there are no associated interfaces running.
184 // 186 //
185 // TODO(yzshen): For now, users need to make sure there is no one holding 187 // TODO(yzshen): For now, users need to make sure there is no one holding
186 // on to associated interface endpoint handles at both sides of the 188 // on to associated interface endpoint handles at both sides of the
187 // message pipe in order to call this method. We need a way to forcefully 189 // message pipe in order to call this method. We need a way to forcefully
188 // invalidate associated interface endpoint handles. 190 // invalidate associated interface endpoint handles.
189 InterfaceRequest<Interface> Unbind() { 191 InterfaceRequest<GenericInterface_> Unbind() {
190 CHECK(!HasAssociatedInterfaces()); 192 CHECK(!HasAssociatedInterfaces());
191 return internal_state_.Unbind(); 193 return internal_state_.Unbind();
192 } 194 }
193 195
194 // Sets an error handler that will be called if a connection error occurs on 196 // Sets an error handler that will be called if a connection error occurs on
195 // the bound message pipe. 197 // the bound message pipe.
196 void set_connection_error_handler(const Closure& error_handler) { 198 void set_connection_error_handler(const Closure& error_handler) {
197 internal_state_.set_connection_error_handler(error_handler); 199 internal_state_.set_connection_error_handler(error_handler);
198 } 200 }
199 201
(...skipping 25 matching lines...) Expand all
225 private: 227 private:
226 internal::BindingState<Interface, Interface::PassesAssociatedKinds_> 228 internal::BindingState<Interface, Interface::PassesAssociatedKinds_>
227 internal_state_; 229 internal_state_;
228 230
229 DISALLOW_COPY_AND_ASSIGN(Binding); 231 DISALLOW_COPY_AND_ASSIGN(Binding);
230 }; 232 };
231 233
232 } // namespace mojo 234 } // namespace mojo
233 235
234 #endif // MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_ 236 #endif // MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/BUILD.gn ('k') | mojo/public/cpp/bindings/interface_request.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698