Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / mojo / system / message_pipe.h
1 // Copyright 2013 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_SYSTEM_MESSAGE_PIPE_H_
6 #define MOJO_SYSTEM_MESSAGE_PIPE_H_
7
8 #include <vector>
9
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/synchronization/lock.h"
14 #include "mojo/public/c/system/core.h"
15 #include "mojo/system/dispatcher.h"
16 #include "mojo/system/message_in_transit.h"
17 #include "mojo/system/message_pipe_endpoint.h"
18 #include "mojo/system/system_impl_export.h"
19
20 namespace mojo {
21 namespace system {
22
23 class Channel;
24 class Waiter;
25
26 // |MessagePipe| is the secondary object implementing a message pipe (see the
27 // explanatory comment in core.cc). It is typically owned by the dispatcher(s)
28 // corresponding to the local endpoints. This class is thread-safe.
29 class MOJO_SYSTEM_IMPL_EXPORT MessagePipe :
30     public base::RefCountedThreadSafe<MessagePipe> {
31  public:
32   MessagePipe(scoped_ptr<MessagePipeEndpoint> endpoint0,
33               scoped_ptr<MessagePipeEndpoint> endpoint1);
34
35   // Convenience constructor that constructs a |MessagePipe| with two new
36   // |LocalMessagePipeEndpoint|s.
37   MessagePipe();
38
39   // Gets the other port number (i.e., 0 -> 1, 1 -> 0).
40   static unsigned GetPeerPort(unsigned port);
41
42   // Gets the type of the endpoint (used for assertions, etc.).
43   MessagePipeEndpoint::Type GetType(unsigned port);
44
45   // These are called by the dispatcher to implement its methods of
46   // corresponding names. In all cases, the port |port| must be open.
47   void CancelAllWaiters(unsigned port);
48   void Close(unsigned port);
49   // Unlike |MessagePipeDispatcher::WriteMessage()|, this does not validate its
50   // arguments.
51   MojoResult WriteMessage(unsigned port,
52                           const void* bytes,
53                           uint32_t num_bytes,
54                           std::vector<DispatcherTransport>* transports,
55                           MojoWriteMessageFlags flags);
56   // Unlike |MessagePipeDispatcher::ReadMessage()|, this does not validate its
57   // arguments.
58   MojoResult ReadMessage(unsigned port,
59                          void* bytes,
60                          uint32_t* num_bytes,
61                          DispatcherVector* dispatchers,
62                          uint32_t* num_dispatchers,
63                          MojoReadMessageFlags flags);
64   MojoResult AddWaiter(unsigned port,
65                        Waiter* waiter,
66                        MojoWaitFlags flags,
67                        MojoResult wake_result);
68   void RemoveWaiter(unsigned port, Waiter* waiter);
69
70   // This is called by the dispatcher to convert a local endpoint to a proxy
71   // endpoint.
72   void ConvertLocalToProxy(unsigned port);
73
74   // This is used by |Channel| to enqueue messages (typically to a
75   // |LocalMessagePipeEndpoint|). Unlike |WriteMessage()|, |port| is the
76   // *destination* port.
77   MojoResult EnqueueMessage(unsigned port,
78                             scoped_ptr<MessageInTransit> message);
79
80   // These are used by |Channel|.
81   bool Attach(unsigned port,
82               scoped_refptr<Channel> channel,
83               MessageInTransit::EndpointId local_id);
84   void Run(unsigned port, MessageInTransit::EndpointId remote_id);
85   void OnRemove(unsigned port);
86
87  private:
88   friend class base::RefCountedThreadSafe<MessagePipe>;
89   virtual ~MessagePipe();
90
91   // This is used internally by |WriteMessage()| and by |EnqueueMessage()|.
92   // |transports| may be non-null only if it's nonempty and |message| has no
93   // dispatchers attached.
94   MojoResult EnqueueMessageInternal(
95       unsigned port,
96       scoped_ptr<MessageInTransit> message,
97       std::vector<DispatcherTransport>* transports);
98
99   // Helper for |EnqueueMessageInternal()|. Must be called with |lock_| held.
100   MojoResult AttachTransportsNoLock(
101       unsigned port,
102       MessageInTransit* message,
103       std::vector<DispatcherTransport>* transports);
104
105   // Used by |EnqueueMessageInternal()| to handle control messages that are
106   // actually meant for us.
107   MojoResult HandleControlMessage(unsigned port,
108                                   scoped_ptr<MessageInTransit> message);
109
110   base::Lock lock_;  // Protects the following members.
111   scoped_ptr<MessagePipeEndpoint> endpoints_[2];
112
113   DISALLOW_COPY_AND_ASSIGN(MessagePipe);
114 };
115
116 }  // namespace system
117 }  // namespace mojo
118
119 #endif  // MOJO_SYSTEM_MESSAGE_PIPE_H_