Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / mojo / public / bindings / interface.h
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_BINDINGS_INTERFACE_H_
6 #define MOJO_PUBLIC_BINDINGS_INTERFACE_H_
7
8 #include <assert.h>
9
10 #include "mojo/public/bindings/message.h"
11 #include "mojo/public/system/core_cpp.h"
12
13 namespace mojo {
14
15
16 // NoInterface is for use in cases when a non-existent or empty interface is
17 // needed (e.g., when the Mojom "Peer" attribute is not present).
18
19 class NoInterface;
20
21 class NoInterfaceStub : public MessageReceiver {
22  public:
23   NoInterfaceStub(NoInterface* unused) {}
24   virtual bool Accept(Message* message) MOJO_OVERRIDE;
25 };
26
27 class NoInterface {
28  public:
29   typedef NoInterfaceStub _Stub;
30   typedef NoInterface _Peer;
31 };
32
33
34 // AnyInterface is for use in cases where any interface would do (e.g., see the
35 // Shell::Connect method).
36
37 typedef NoInterface AnyInterface;
38
39
40 // InterfaceHandle<S>
41
42 template <typename S>
43 class InterfaceHandle : public MessagePipeHandle {
44  public:
45   InterfaceHandle() {}
46   explicit InterfaceHandle(MojoHandle value) : MessagePipeHandle(value) {}
47 };
48
49
50 // Interface<S>
51
52 template <typename S>
53 struct Interface {
54   typedef InterfaceHandle<S> Handle;
55   typedef ScopedHandleBase<InterfaceHandle<S> > ScopedHandle;
56 };
57
58 template <>
59 struct Interface<mojo::NoInterface> {
60   typedef MessagePipeHandle Handle;
61   typedef ScopedMessagePipeHandle ScopedHandle;
62 };
63
64
65 // InterfacePipe<S,P> is used to construct a MessagePipe with typed interfaces
66 // on either end.
67
68 template <typename S, typename P = typename S::_Peer>
69 class InterfacePipe {
70  public:
71   InterfacePipe() {
72     typename Interface<S>::Handle h0;
73     typename Interface<P>::Handle h1;
74     MojoResult result MOJO_ALLOW_UNUSED =
75         MojoCreateMessagePipe(h0.mutable_value(), h1.mutable_value());
76     assert(result == MOJO_RESULT_OK);
77     handle_to_self.reset(h0);
78     handle_to_peer.reset(h1);
79   }
80
81   typename Interface<S>::ScopedHandle handle_to_self;
82   typename Interface<P>::ScopedHandle handle_to_peer;
83 };
84
85 // TODO(darin): Once we have the ability to use C++11 features, consider
86 // defining a template alias for ScopedInterfaceHandle<S>.
87
88 }  // namespace mojo
89
90 #endif  // MOJO_PUBLIC_BINDINGS_SCOPED_INTERFACE_H_