Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / mojo / public / cpp / bindings / interface_request.h
blob6b7d3033c9d36d206b67d3f77a2e07dce0ee6892
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"
10 namespace mojo {
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)
16 public:
17 InterfaceRequest() {}
19 InterfaceRequest(RValue other) {
20 handle_ = other.object->handle_.Pass();
22 InterfaceRequest& operator=(RValue other) {
23 handle_ = other.object->handle_.Pass();
24 return *this;
27 // Returns true if the request has yet to be completed.
28 bool is_pending() const { return handle_.is_valid(); }
30 void Bind(ScopedMessagePipeHandle handle) {
31 handle_ = handle.Pass();
34 ScopedMessagePipeHandle PassMessagePipe() {
35 return handle_.Pass();
38 private:
39 ScopedMessagePipeHandle handle_;
42 template <typename Interface>
43 InterfaceRequest<Interface> MakeRequest(ScopedMessagePipeHandle handle) {
44 InterfaceRequest<Interface> request;
45 request.Bind(handle.Pass());
46 return request.Pass();
49 // Used to construct a request that synchronously binds an InterfacePtr<..>,
50 // making it immediately usable upon return. The resulting request object may
51 // then be later bound to an InterfaceImpl<..> via BindToRequest.
53 // Given the following interface:
55 // interface Foo {
56 // CreateBar(Bar& bar);
57 // }
59 // The caller of CreateBar would have code similar to the following:
61 // InterfacePtr<Foo> foo = ...;
62 // InterfacePtr<Bar> bar;
63 // foo->CreateBar(Get(&bar));
65 // Upon return from CreateBar, |bar| is ready to have methods called on it.
67 template <typename Interface>
68 InterfaceRequest<Interface> Get(InterfacePtr<Interface>* ptr) {
69 MessagePipe pipe;
70 ptr->Bind(pipe.handle0.Pass());
71 return MakeRequest<Interface>(pipe.handle1.Pass());
74 } // namespace mojo
76 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_REQUEST_H_