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.
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_REQUEST_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_REQUEST_H_
8 #include "mojo/public/cpp/bindings/interface_ptr.h"
12 // Used in methods that return instances of remote objects.
13 template <typename Interface
>
14 class InterfaceRequest
{
15 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(InterfaceRequest
, RValue
)
19 InterfaceRequest(RValue other
) { handle_
= other
.object
->handle_
.Pass(); }
20 InterfaceRequest
& operator=(RValue other
) {
21 handle_
= other
.object
->handle_
.Pass();
25 // Returns true if the request has yet to be completed.
26 bool is_pending() const { return handle_
.is_valid(); }
28 void Bind(ScopedMessagePipeHandle handle
) { handle_
= handle
.Pass(); }
30 ScopedMessagePipeHandle
PassMessagePipe() { return handle_
.Pass(); }
33 ScopedMessagePipeHandle handle_
;
36 template <typename Interface
>
37 InterfaceRequest
<Interface
> MakeRequest(ScopedMessagePipeHandle handle
) {
38 InterfaceRequest
<Interface
> request
;
39 request
.Bind(handle
.Pass());
40 return request
.Pass();
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.
47 // Given the following interface:
50 // CreateBar(Bar& bar);
53 // The caller of CreateBar would have code similar to the following:
55 // InterfacePtr<Foo> foo = ...;
56 // InterfacePtr<Bar> bar;
57 // foo->CreateBar(GetProxy(&bar));
59 // Upon return from CreateBar, |bar| is ready to have methods called on it.
61 template <typename Interface
>
62 InterfaceRequest
<Interface
> GetProxy(InterfacePtr
<Interface
>* ptr
) {
64 ptr
->Bind(pipe
.handle0
.Pass());
65 return MakeRequest
<Interface
>(pipe
.handle1
.Pass());
70 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_REQUEST_H_