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_REQUEST_H_ | |
6 #define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_REQUEST_H_ | |
7 | |
8 #include "mojo/public/cpp/bindings/interface_ptr.h" | |
9 | |
10 namespace mojo { | |
11 | |
12 // Used in methods that return instances of remote objects. | |
13 template <typename Interface> | |
14 class InterfaceRequest { | |
15 MOJO_MOVE_ONLY_TYPE(InterfaceRequest) | |
16 public: | |
17 InterfaceRequest() {} | |
18 | |
19 InterfaceRequest(InterfaceRequest&& other) { handle_ = other.handle_.Pass(); } | |
20 InterfaceRequest& operator=(InterfaceRequest&& other) { | |
21 handle_ = other.handle_.Pass(); | |
22 return *this; | |
23 } | |
24 | |
25 // Returns true if the request has yet to be completed. | |
26 bool is_pending() const { return handle_.is_valid(); } | |
27 | |
28 void Bind(ScopedMessagePipeHandle handle) { handle_ = handle.Pass(); } | |
29 | |
30 ScopedMessagePipeHandle PassMessagePipe() { return handle_.Pass(); } | |
31 | |
32 private: | |
33 ScopedMessagePipeHandle handle_; | |
34 }; | |
35 | |
36 template <typename Interface> | |
37 InterfaceRequest<Interface> MakeRequest(ScopedMessagePipeHandle handle) { | |
38 InterfaceRequest<Interface> request; | |
39 request.Bind(handle.Pass()); | |
40 return request.Pass(); | |
41 } | |
42 | |
43 // Used to construct a request that synchronously binds an InterfacePtr<..>, | |
44 // making it immediately usable upon return. The resulting request object may | |
45 // then be later bound to an InterfaceImpl<..> via BindToRequest. | |
46 // | |
47 // Given the following interface: | |
48 // | |
49 // interface Foo { | |
50 // CreateBar(Bar& bar); | |
51 // } | |
52 // | |
53 // The caller of CreateBar would have code similar to the following: | |
54 // | |
55 // InterfacePtr<Foo> foo = ...; | |
56 // InterfacePtr<Bar> bar; | |
57 // foo->CreateBar(GetProxy(&bar)); | |
58 // | |
59 // Upon return from CreateBar, |bar| is ready to have methods called on it. | |
60 // | |
61 template <typename Interface> | |
62 InterfaceRequest<Interface> GetProxy(InterfacePtr<Interface>* ptr) { | |
63 MessagePipe pipe; | |
64 ptr->Bind(pipe.handle0.Pass()); | |
65 return MakeRequest<Interface>(pipe.handle1.Pass()); | |
66 } | |
67 | |
68 } // namespace mojo | |
69 | |
70 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_REQUEST_H_ | |
OLD | NEW |