• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(InterfaceRequest,RValue)15   MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(InterfaceRequest, RValue)
16  public:
17   InterfaceRequest() {}
18 
InterfaceRequest(RValue other)19   InterfaceRequest(RValue other) {
20     handle_ = other.object->handle_.Pass();
21   }
22   InterfaceRequest& operator=(RValue other) {
23     handle_ = other.object->handle_.Pass();
24     return *this;
25   }
26 
27   // Returns true if the request has yet to be completed.
is_pending()28   bool is_pending() const { return handle_.is_valid(); }
29 
Bind(ScopedMessagePipeHandle handle)30   void Bind(ScopedMessagePipeHandle handle) {
31     handle_ = handle.Pass();
32   }
33 
PassMessagePipe()34   ScopedMessagePipeHandle PassMessagePipe() {
35     return handle_.Pass();
36   }
37 
38  private:
39   ScopedMessagePipeHandle handle_;
40 };
41 
42 template <typename Interface>
MakeRequest(ScopedMessagePipeHandle handle)43 InterfaceRequest<Interface> MakeRequest(ScopedMessagePipeHandle handle) {
44   InterfaceRequest<Interface> request;
45   request.Bind(handle.Pass());
46   return request.Pass();
47 }
48 
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.
52 //
53 // Given the following interface:
54 //
55 //   interface Foo {
56 //     CreateBar(Bar& bar);
57 //   }
58 //
59 // The caller of CreateBar would have code similar to the following:
60 //
61 //   InterfacePtr<Foo> foo = ...;
62 //   InterfacePtr<Bar> bar;
63 //   foo->CreateBar(Get(&bar));
64 //
65 // Upon return from CreateBar, |bar| is ready to have methods called on it.
66 //
67 template <typename Interface>
Get(InterfacePtr<Interface> * ptr)68 InterfaceRequest<Interface> Get(InterfacePtr<Interface>* ptr) {
69   MessagePipe pipe;
70   ptr->Bind(pipe.handle0.Pass());
71   return MakeRequest<Interface>(pipe.handle1.Pass());
72 }
73 
74 }  // namespace mojo
75 
76 #endif  // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_REQUEST_H_
77