ab3067c57490928db4e87aa6ecfe3a9b23c308fc
[platform/framework/web/crosswalk.git] / src / mojo / system / message_pipe_dispatcher.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_DISPATCHER_H_
6 #define MOJO_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_
7
8 #include <utility>
9
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "mojo/system/dispatcher.h"
14 #include "mojo/system/system_impl_export.h"
15
16 namespace mojo {
17 namespace system {
18
19 class MessagePipe;
20 class MessagePipeDispatcherTransport;
21
22 // This is the |Dispatcher| implementation for message pipes (created by the
23 // Mojo primitive |MojoCreateMessagePipe()|). This class is thread-safe.
24 class MOJO_SYSTEM_IMPL_EXPORT MessagePipeDispatcher : public Dispatcher {
25  public:
26   MessagePipeDispatcher();
27
28   // Must be called before any other methods. (This method is not thread-safe.)
29   void Init(scoped_refptr<MessagePipe> message_pipe, unsigned port);
30
31   // |Dispatcher| public methods:
32   virtual Type GetType() const OVERRIDE;
33
34   // Creates a |MessagePipe| with a local endpoint (at port 0) and a proxy
35   // endpoint, and creates/initializes a |MessagePipeDispatcher| (attached to
36   // the message pipe, port 0).
37   static std::pair<scoped_refptr<MessagePipeDispatcher>,
38                    scoped_refptr<MessagePipe> > CreateRemoteMessagePipe();
39
40   // The "opposite" of |SerializeAndClose()|. (Typically this is called by
41   // |Dispatcher::Deserialize()|.)
42   static scoped_refptr<MessagePipeDispatcher> Deserialize(Channel* channel,
43                                                           const void* source,
44                                                           size_t size);
45
46  private:
47   friend class MessagePipeDispatcherTransport;
48
49   virtual ~MessagePipeDispatcher();
50
51   // Gets a dumb pointer to |message_pipe_|. This must be called under the
52   // |Dispatcher| lock (that it's a dumb pointer is okay since it's under lock).
53   // This is needed when sending handles across processes, where nontrivial,
54   // invasive work needs to be done.
55   MessagePipe* GetMessagePipeNoLock() const;
56   // Similarly for the port.
57   unsigned GetPortNoLock() const;
58
59   // |Dispatcher| protected methods:
60   virtual void CancelAllWaitersNoLock() OVERRIDE;
61   virtual void CloseImplNoLock() OVERRIDE;
62   virtual scoped_refptr<Dispatcher>
63       CreateEquivalentDispatcherAndCloseImplNoLock() OVERRIDE;
64   virtual MojoResult WriteMessageImplNoLock(
65       const void* bytes,
66       uint32_t num_bytes,
67       std::vector<DispatcherTransport>* transports,
68       MojoWriteMessageFlags flags) OVERRIDE;
69   virtual MojoResult ReadMessageImplNoLock(
70       void* bytes,
71       uint32_t* num_bytes,
72       std::vector<scoped_refptr<Dispatcher> >* dispatchers,
73       uint32_t* num_dispatchers,
74       MojoReadMessageFlags flags) OVERRIDE;
75   virtual MojoResult AddWaiterImplNoLock(Waiter* waiter,
76                                          MojoWaitFlags flags,
77                                          MojoResult wake_result) OVERRIDE;
78   virtual void RemoveWaiterImplNoLock(Waiter* waiter) OVERRIDE;
79   virtual size_t GetMaximumSerializedSizeImplNoLock(
80       const Channel* channel) const OVERRIDE;
81   virtual bool SerializeAndCloseImplNoLock(Channel* channel,
82                                            void* destination,
83                                            size_t* actual_size) OVERRIDE;
84
85   // Protected by |lock()|:
86   scoped_refptr<MessagePipe> message_pipe_;  // This will be null if closed.
87   unsigned port_;
88
89   DISALLOW_COPY_AND_ASSIGN(MessagePipeDispatcher);
90 };
91
92 class MessagePipeDispatcherTransport : public DispatcherTransport {
93  public:
94   explicit MessagePipeDispatcherTransport(DispatcherTransport transport);
95
96   MessagePipe* GetMessagePipe() {
97     return message_pipe_dispatcher()->GetMessagePipeNoLock();
98   }
99   unsigned GetPort() { return message_pipe_dispatcher()->GetPortNoLock(); }
100
101  private:
102   MessagePipeDispatcher* message_pipe_dispatcher() {
103     return static_cast<MessagePipeDispatcher*>(dispatcher());
104   }
105
106   // Copy and assign allowed.
107 };
108
109 }  // namespace system
110 }  // namespace mojo
111
112 #endif  // MOJO_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_