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 <cstddef> | 8 #include <cstddef> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "mojo/public/cpp/system/message_pipe.h" | 11 #include "mojo/public/cpp/system/message_pipe.h" |
12 | 12 |
13 namespace mojo { | 13 namespace mojo { |
14 | 14 |
15 template <typename I> | 15 template <typename I> |
16 class InterfacePtr; | 16 class InterfacePtr; |
17 | 17 |
18 template <typename I> | 18 template <typename I> |
19 class InterfaceHandle; | 19 class InterfaceHandle; |
20 | 20 |
21 // Represents a request from a remote client for an implementation of Interface | 21 // Represents a request from a remote client for an implementation of Interface |
22 // over a specified message pipe. The implementor of the interface should | 22 // over a specified message pipe. The implementor of the interface should |
23 // remove the message pipe by calling PassMessagePipe() and bind it to the | 23 // remove the message pipe by calling PassMessagePipe() and bind it to the |
24 // implementation. If this is not done, the InterfaceRequest will automatically | 24 // implementation. If this is not done, the InterfaceRequest will automatically |
25 // close the pipe on destruction. Can also represent the absence of a request | 25 // close the pipe on destruction. Can also represent the absence of a request |
26 // if the client did not provide a message pipe. | 26 // if the client did not provide a message pipe. |
27 template <typename Interface> | 27 template <typename Interface> |
28 class InterfaceRequest { | 28 class InterfaceRequest { |
29 public: | 29 public: |
30 // Constructs an empty InterfaceRequest, representing that the client is not | 30 // Constructs an "empty" InterfaceRequest, representing that the client is not |
31 // requesting an implementation of Interface. | 31 // requesting an implementation of Interface. |
32 InterfaceRequest() {} | 32 InterfaceRequest() {} |
33 InterfaceRequest(std::nullptr_t) {} | 33 InterfaceRequest(std::nullptr_t) {} |
34 | 34 |
| 35 // Constructs an InterfaceRequest from a message pipe handle (if |handle| is |
| 36 // not set, then this constructs an "empty" InterfaceRequest). |
| 37 explicit InterfaceRequest(ScopedMessagePipeHandle handle) |
| 38 : handle_(handle.Pass()) {} |
| 39 |
35 // Takes the message pipe from another InterfaceRequest. | 40 // Takes the message pipe from another InterfaceRequest. |
36 InterfaceRequest(InterfaceRequest&& other) { handle_ = other.handle_.Pass(); } | 41 InterfaceRequest(InterfaceRequest&& other) { handle_ = other.handle_.Pass(); } |
37 InterfaceRequest& operator=(InterfaceRequest&& other) { | 42 InterfaceRequest& operator=(InterfaceRequest&& other) { |
38 handle_ = other.handle_.Pass(); | 43 handle_ = other.handle_.Pass(); |
39 return *this; | 44 return *this; |
40 } | 45 } |
41 | 46 |
42 // Assigning to nullptr resets the InterfaceRequest to an empty state, | 47 // Assigning to nullptr resets the InterfaceRequest to an empty state, |
43 // closing the message pipe currently bound to it (if any). | 48 // closing the message pipe currently bound to it (if any). |
44 InterfaceRequest& operator=(std::nullptr_t) { | 49 InterfaceRequest& operator=(std::nullptr_t) { |
(...skipping 11 matching lines...) Expand all Loading... |
56 | 61 |
57 // Removes the message pipe from the request and returns it. | 62 // Removes the message pipe from the request and returns it. |
58 ScopedMessagePipeHandle PassMessagePipe() { return handle_.Pass(); } | 63 ScopedMessagePipeHandle PassMessagePipe() { return handle_.Pass(); } |
59 | 64 |
60 private: | 65 private: |
61 ScopedMessagePipeHandle handle_; | 66 ScopedMessagePipeHandle handle_; |
62 | 67 |
63 MOJO_MOVE_ONLY_TYPE(InterfaceRequest); | 68 MOJO_MOVE_ONLY_TYPE(InterfaceRequest); |
64 }; | 69 }; |
65 | 70 |
66 // Makes an InterfaceRequest bound to the specified message pipe. If |handle| | |
67 // is empty or invalid, the resulting InterfaceRequest will represent the | |
68 // absence of a request. | |
69 template <typename Interface> | |
70 InterfaceRequest<Interface> MakeRequest(ScopedMessagePipeHandle handle) { | |
71 InterfaceRequest<Interface> request; | |
72 request.Bind(handle.Pass()); | |
73 return request; | |
74 } | |
75 | |
76 // Creates a new message pipe over which Interface is to be served, and one end | 71 // Creates a new message pipe over which Interface is to be served, and one end |
77 // into the supplied |handle| and the returns the other inside an | 72 // into the supplied |handle| and the returns the other inside an |
78 // InterfaceRequest<>. The InterfaceHandle<> can be used to create a client | 73 // InterfaceRequest<>. The InterfaceHandle<> can be used to create a client |
79 // proxy (such as an InterfacePtr<> or SynchronousInterfacePtr<>). | 74 // proxy (such as an InterfacePtr<> or SynchronousInterfacePtr<>). |
80 // InterfaceRequest<> should be passed to whatever will provide the | 75 // InterfaceRequest<> should be passed to whatever will provide the |
81 // implementation. | 76 // implementation. |
82 // TODO(vardhan): Rename this function? This isn't making a "proxy" you can use, | 77 // TODO(vardhan): Rename this function? This isn't making a "proxy" you can use, |
83 // as you still need to convert it into something like InterfacePtr<>. | 78 // as you still need to convert it into something like InterfacePtr<>. |
84 template <typename Interface> | 79 template <typename Interface> |
85 InterfaceRequest<Interface> GetProxy(InterfaceHandle<Interface>* handle) { | 80 InterfaceRequest<Interface> GetProxy(InterfaceHandle<Interface>* handle) { |
86 MessagePipe pipe; | 81 MessagePipe pipe; |
87 *handle = InterfaceHandle<Interface>(pipe.handle0.Pass(), 0u); | 82 *handle = InterfaceHandle<Interface>(pipe.handle0.Pass(), 0u); |
88 return MakeRequest<Interface>(pipe.handle1.Pass()); | 83 return InterfaceRequest<Interface>(pipe.handle1.Pass()); |
89 } | 84 } |
90 | 85 |
91 // Creates a new message pipe over which Interface is to be served. Binds the | 86 // Creates a new message pipe over which Interface is to be served. Binds the |
92 // specified InterfacePtr to one end of the message pipe, and returns an | 87 // specified InterfacePtr to one end of the message pipe, and returns an |
93 // InterfaceRequest bound to the other. The InterfacePtr should be passed to | 88 // InterfaceRequest bound to the other. The InterfacePtr should be passed to |
94 // the client, and the InterfaceRequest should be passed to whatever will | 89 // the client, and the InterfaceRequest should be passed to whatever will |
95 // provide the implementation. The implementation should typically be bound to | 90 // provide the implementation. The implementation should typically be bound to |
96 // the InterfaceRequest using the Binding or StrongBinding classes. The client | 91 // the InterfaceRequest using the Binding or StrongBinding classes. The client |
97 // may begin to issue calls even before an implementation has been bound, since | 92 // may begin to issue calls even before an implementation has been bound, since |
98 // messages sent over the pipe will just queue up until they are consumed by | 93 // messages sent over the pipe will just queue up until they are consumed by |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 InterfaceRequest<Interface> GetProxy(InterfacePtr<Interface>* ptr) { | 131 InterfaceRequest<Interface> GetProxy(InterfacePtr<Interface>* ptr) { |
137 InterfaceHandle<Interface> iface_handle; | 132 InterfaceHandle<Interface> iface_handle; |
138 auto retval = GetProxy(&iface_handle); | 133 auto retval = GetProxy(&iface_handle); |
139 *ptr = InterfacePtr<Interface>::Create(std::move(iface_handle)); | 134 *ptr = InterfacePtr<Interface>::Create(std::move(iface_handle)); |
140 return retval; | 135 return retval; |
141 } | 136 } |
142 | 137 |
143 } // namespace mojo | 138 } // namespace mojo |
144 | 139 |
145 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_REQUEST_H_ | 140 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_REQUEST_H_ |
OLD | NEW |