summaryrefslogtreecommitdiffstats
path: root/mojo/public/cpp/bindings/interface_request.h
blob: 17233308beed01fb5fcad6242926b490900ceabe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_REQUEST_H_
#define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_REQUEST_H_

#include "mojo/public/cpp/bindings/interface_ptr.h"

namespace mojo {

// Used in methods that return instances of remote objects.
template <typename Interface>
class InterfaceRequest {
  MOJO_MOVE_ONLY_TYPE(InterfaceRequest)
 public:
  InterfaceRequest() {}

  InterfaceRequest(InterfaceRequest&& other) { handle_ = other.handle_.Pass(); }
  InterfaceRequest& operator=(InterfaceRequest&& other) {
    handle_ = other.handle_.Pass();
    return *this;
  }

  // Returns true if the request has yet to be completed.
  bool is_pending() const { return handle_.is_valid(); }

  void Bind(ScopedMessagePipeHandle handle) { handle_ = handle.Pass(); }

  ScopedMessagePipeHandle PassMessagePipe() { return handle_.Pass(); }

 private:
  ScopedMessagePipeHandle handle_;
};

template <typename Interface>
InterfaceRequest<Interface> MakeRequest(ScopedMessagePipeHandle handle) {
  InterfaceRequest<Interface> request;
  request.Bind(handle.Pass());
  return request.Pass();
}

// Used to construct a request that synchronously binds an InterfacePtr<..>,
// making it immediately usable upon return. The resulting request object may
// then be later bound to an InterfaceImpl<..> via BindToRequest.
//
// Given the following interface:
//
//   interface Foo {
//     CreateBar(Bar& bar);
//   }
//
// The caller of CreateBar would have code similar to the following:
//
//   InterfacePtr<Foo> foo = ...;
//   InterfacePtr<Bar> bar;
//   foo->CreateBar(GetProxy(&bar));
//
// Upon return from CreateBar, |bar| is ready to have methods called on it.
//
template <typename Interface>
InterfaceRequest<Interface> GetProxy(InterfacePtr<Interface>* ptr) {
  MessagePipe pipe;
  ptr->Bind(pipe.handle0.Pass());
  return MakeRequest<Interface>(pipe.handle1.Pass());
}

}  // namespace mojo

#endif  // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_REQUEST_H_