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 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/optional.h" | 12 #include "base/optional.h" |
13 #include "base/single_thread_task_runner.h" | 13 #include "base/sequenced_task_runner.h" |
14 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/sequenced_task_runner_handle.h" |
15 #include "mojo/public/cpp/bindings/disconnect_reason.h" | 15 #include "mojo/public/cpp/bindings/disconnect_reason.h" |
16 #include "mojo/public/cpp/bindings/interface_ptr.h" | 16 #include "mojo/public/cpp/bindings/interface_ptr.h" |
17 #include "mojo/public/cpp/bindings/pipe_control_message_proxy.h" | 17 #include "mojo/public/cpp/bindings/pipe_control_message_proxy.h" |
18 #include "mojo/public/cpp/system/message_pipe.h" | 18 #include "mojo/public/cpp/system/message_pipe.h" |
19 | 19 |
20 namespace mojo { | 20 namespace mojo { |
21 | 21 |
22 // Represents a request from a remote client for an implementation of Interface | 22 // Represents a request from a remote client for an implementation of Interface |
23 // over a specified message pipe. The implementor of the interface should | 23 // over a specified message pipe. The implementor of the interface should |
24 // remove the message pipe by calling PassMessagePipe() and bind it to the | 24 // remove the message pipe by calling PassMessagePipe() and bind it to the |
25 // implementation. If this is not done, the InterfaceRequest will automatically | 25 // implementation. If this is not done, the InterfaceRequest will automatically |
26 // close the pipe on destruction. Can also represent the absence of a request | 26 // close the pipe on destruction. Can also represent the absence of a request |
27 // if the client did not provide a message pipe. | 27 // if the client did not provide a message pipe. |
28 template <typename Interface> | 28 template <typename Interface> |
29 class InterfaceRequest { | 29 class InterfaceRequest { |
30 public: | 30 public: |
31 // Constructs an empty InterfaceRequest, representing that the client is not | 31 // Constructs an empty InterfaceRequest, representing that the client is not |
32 // requesting an implementation of Interface. | 32 // requesting an implementation of Interface. |
33 InterfaceRequest() {} | 33 InterfaceRequest() {} |
34 InterfaceRequest(decltype(nullptr)) {} | 34 InterfaceRequest(decltype(nullptr)) {} |
35 | 35 |
36 // Creates a new message pipe over which Interface is to be served, binding | 36 // Creates a new message pipe over which Interface is to be served, binding |
37 // the specified InterfacePtr to one end of the message pipe and this | 37 // the specified InterfacePtr to one end of the message pipe and this |
38 // InterfaceRequest to the other. For example usage, see comments on | 38 // InterfaceRequest to the other. For example usage, see comments on |
39 // MakeRequest(InterfacePtr*) below. | 39 // MakeRequest(InterfacePtr*) below. |
40 explicit InterfaceRequest(InterfacePtr<Interface>* ptr, | 40 explicit InterfaceRequest(InterfacePtr<Interface>* ptr, |
41 scoped_refptr<base::SingleThreadTaskRunner> runner = | 41 scoped_refptr<base::SequencedTaskRunner> runner = |
42 base::ThreadTaskRunnerHandle::Get()) { | 42 base::SequencedTaskRunnerHandle::Get()) { |
43 MessagePipe pipe; | 43 MessagePipe pipe; |
44 ptr->Bind(InterfacePtrInfo<Interface>(std::move(pipe.handle0), 0u), | 44 ptr->Bind(InterfacePtrInfo<Interface>(std::move(pipe.handle0), 0u), |
45 std::move(runner)); | 45 std::move(runner)); |
46 Bind(std::move(pipe.handle1)); | 46 Bind(std::move(pipe.handle1)); |
47 } | 47 } |
48 | 48 |
49 // Takes the message pipe from another InterfaceRequest. | 49 // Takes the message pipe from another InterfaceRequest. |
50 InterfaceRequest(InterfaceRequest&& other) { | 50 InterfaceRequest(InterfaceRequest&& other) { |
51 handle_ = std::move(other.handle_); | 51 handle_ = std::move(other.handle_); |
52 } | 52 } |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 // | 154 // |
155 // CollectorPtr collector = ...; // Connect to Collector. | 155 // CollectorPtr collector = ...; // Connect to Collector. |
156 // SourcePtr source; | 156 // SourcePtr source; |
157 // InterfaceRequest<Source> source_request(&source); | 157 // InterfaceRequest<Source> source_request(&source); |
158 // collector->RegisterSource(std::move(source)); | 158 // collector->RegisterSource(std::move(source)); |
159 // CreateSource(std::move(source_request)); // Create implementation locally. | 159 // CreateSource(std::move(source_request)); // Create implementation locally. |
160 // | 160 // |
161 template <typename Interface> | 161 template <typename Interface> |
162 InterfaceRequest<Interface> MakeRequest( | 162 InterfaceRequest<Interface> MakeRequest( |
163 InterfacePtr<Interface>* ptr, | 163 InterfacePtr<Interface>* ptr, |
164 scoped_refptr<base::SingleThreadTaskRunner> runner = | 164 scoped_refptr<base::SequencedTaskRunner> runner = |
165 base::ThreadTaskRunnerHandle::Get()) { | 165 base::SequencedTaskRunnerHandle::Get()) { |
166 return InterfaceRequest<Interface>(ptr, runner); | 166 return InterfaceRequest<Interface>(ptr, runner); |
167 } | 167 } |
168 | 168 |
169 // Fuses an InterfaceRequest<T> endpoint with an InterfacePtrInfo<T> endpoint. | 169 // Fuses an InterfaceRequest<T> endpoint with an InterfacePtrInfo<T> endpoint. |
170 // Returns |true| on success or |false| on failure. | 170 // Returns |true| on success or |false| on failure. |
171 template <typename Interface> | 171 template <typename Interface> |
172 bool FuseInterface(InterfaceRequest<Interface> request, | 172 bool FuseInterface(InterfaceRequest<Interface> request, |
173 InterfacePtrInfo<Interface> proxy_info) { | 173 InterfacePtrInfo<Interface> proxy_info) { |
174 MojoResult result = FuseMessagePipes(request.PassMessagePipe(), | 174 MojoResult result = FuseMessagePipes(request.PassMessagePipe(), |
175 proxy_info.PassHandle()); | 175 proxy_info.PassHandle()); |
176 return result == MOJO_RESULT_OK; | 176 return result == MOJO_RESULT_OK; |
177 } | 177 } |
178 | 178 |
179 } // namespace mojo | 179 } // namespace mojo |
180 | 180 |
181 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_REQUEST_H_ | 181 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_REQUEST_H_ |
OLD | NEW |