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_REQUEST_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_REQUEST_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_REQUEST_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_REQUEST_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 13 matching lines...) Expand all Loading... |
24 // close the pipe on destruction. Can also represent the absence of a request | 24 // close the pipe on destruction. Can also represent the absence of a request |
25 // if the client did not provide a message pipe. | 25 // if the client did not provide a message pipe. |
26 template <typename Interface> | 26 template <typename Interface> |
27 class InterfaceRequest { | 27 class InterfaceRequest { |
28 public: | 28 public: |
29 // Constructs an empty InterfaceRequest, representing that the client is not | 29 // Constructs an empty InterfaceRequest, representing that the client is not |
30 // requesting an implementation of Interface. | 30 // requesting an implementation of Interface. |
31 InterfaceRequest() {} | 31 InterfaceRequest() {} |
32 InterfaceRequest(decltype(nullptr)) {} | 32 InterfaceRequest(decltype(nullptr)) {} |
33 | 33 |
| 34 // Creates a new message pipe over which Interface is to be served, binding |
| 35 // the specified InterfacePtr to one end of the message pipe and this |
| 36 // InterfaceRequest to the other. For example usage, see comments on |
| 37 // MakeRequest(InterfacePtr*) below. |
| 38 explicit InterfaceRequest(InterfacePtr<Interface>* ptr, |
| 39 scoped_refptr<base::SingleThreadTaskRunner> runner = |
| 40 base::ThreadTaskRunnerHandle::Get()) { |
| 41 MessagePipe pipe; |
| 42 ptr->Bind(InterfacePtrInfo<Interface>(std::move(pipe.handle0), 0u), |
| 43 std::move(runner)); |
| 44 Bind(std::move(pipe.handle1)); |
| 45 } |
| 46 |
34 // Takes the message pipe from another InterfaceRequest. | 47 // Takes the message pipe from another InterfaceRequest. |
35 InterfaceRequest(InterfaceRequest&& other) { | 48 InterfaceRequest(InterfaceRequest&& other) { |
36 handle_ = std::move(other.handle_); | 49 handle_ = std::move(other.handle_); |
37 } | 50 } |
38 InterfaceRequest& operator=(InterfaceRequest&& other) { | 51 InterfaceRequest& operator=(InterfaceRequest&& other) { |
39 handle_ = std::move(other.handle_); | 52 handle_ = std::move(other.handle_); |
40 return *this; | 53 return *this; |
41 } | 54 } |
42 | 55 |
43 // Assigning to nullptr resets the InterfaceRequest to an empty state, | 56 // Assigning to nullptr resets the InterfaceRequest to an empty state, |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 // | 142 // |
130 // Given the following interface | 143 // Given the following interface |
131 // interface Collector { | 144 // interface Collector { |
132 // RegisterSource(Source source); | 145 // RegisterSource(Source source); |
133 // } | 146 // } |
134 // | 147 // |
135 // The client would have code similar to the following: | 148 // The client would have code similar to the following: |
136 // | 149 // |
137 // CollectorPtr collector = ...; // Connect to Collector. | 150 // CollectorPtr collector = ...; // Connect to Collector. |
138 // SourcePtr source; | 151 // SourcePtr source; |
139 // InterfaceRequest<Source> source_request = MakeRequest(&source); | 152 // InterfaceRequest<Source> source_request(&source); |
140 // collector->RegisterSource(std::move(source)); | 153 // collector->RegisterSource(std::move(source)); |
141 // CreateSource(std::move(source_request)); // Create implementation locally. | 154 // CreateSource(std::move(source_request)); // Create implementation locally. |
142 // | 155 // |
143 template <typename Interface> | 156 template <typename Interface> |
144 InterfaceRequest<Interface> MakeRequest( | 157 InterfaceRequest<Interface> MakeRequest( |
145 InterfacePtr<Interface>* ptr, | 158 InterfacePtr<Interface>* ptr, |
146 scoped_refptr<base::SingleThreadTaskRunner> runner = | 159 scoped_refptr<base::SingleThreadTaskRunner> runner = |
147 base::ThreadTaskRunnerHandle::Get()) { | 160 base::ThreadTaskRunnerHandle::Get()) { |
148 MessagePipe pipe; | 161 return InterfaceRequest<Interface>(ptr, runner); |
149 ptr->Bind(InterfacePtrInfo<Interface>(std::move(pipe.handle0), 0u), | |
150 std::move(runner)); | |
151 return MakeRequest<Interface>(std::move(pipe.handle1)); | |
152 } | 162 } |
153 | 163 |
154 // Fuses an InterfaceRequest<T> endpoint with an InterfacePtrInfo<T> endpoint. | 164 // Fuses an InterfaceRequest<T> endpoint with an InterfacePtrInfo<T> endpoint. |
155 // Returns |true| on success or |false| on failure. | 165 // Returns |true| on success or |false| on failure. |
156 template <typename Interface> | 166 template <typename Interface> |
157 bool FuseInterface(InterfaceRequest<Interface> request, | 167 bool FuseInterface(InterfaceRequest<Interface> request, |
158 InterfacePtrInfo<Interface> proxy_info) { | 168 InterfacePtrInfo<Interface> proxy_info) { |
159 MojoResult result = FuseMessagePipes(request.PassMessagePipe(), | 169 MojoResult result = FuseMessagePipes(request.PassMessagePipe(), |
160 proxy_info.PassHandle()); | 170 proxy_info.PassHandle()); |
161 return result == MOJO_RESULT_OK; | 171 return result == MOJO_RESULT_OK; |
162 } | 172 } |
163 | 173 |
164 } // namespace mojo | 174 } // namespace mojo |
165 | 175 |
166 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_REQUEST_H_ | 176 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_REQUEST_H_ |
OLD | NEW |