Upstream version 7.36.149.0
[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(void* bytes,
70                                            uint32_t* num_bytes,
71                                            DispatcherVector* dispatchers,
72                                            uint32_t* num_dispatchers,
73                                            MojoReadMessageFlags flags) OVERRIDE;
74   virtual MojoResult AddWaiterImplNoLock(Waiter* waiter,
75                                          MojoWaitFlags flags,
76                                          MojoResult wake_result) OVERRIDE;
77   virtual void RemoveWaiterImplNoLock(Waiter* waiter) OVERRIDE;
78   virtual void StartSerializeImplNoLock(Channel* channel,
79                                         size_t* max_size,
80                                         size_t* max_platform_handles) OVERRIDE;
81   virtual bool EndSerializeAndCloseImplNoLock(
82       Channel* channel,
83       void* destination,
84       size_t* actual_size,
85       std::vector<embedder::PlatformHandle>* platform_handles) OVERRIDE;
86
87   // Protected by |lock()|:
88   scoped_refptr<MessagePipe> message_pipe_;  // This will be null if closed.
89   unsigned port_;
90
91   DISALLOW_COPY_AND_ASSIGN(MessagePipeDispatcher);
92 };
93
94 class MessagePipeDispatcherTransport : public DispatcherTransport {
95  public:
96   explicit MessagePipeDispatcherTransport(DispatcherTransport transport);
97
98   MessagePipe* GetMessagePipe() {
99     return message_pipe_dispatcher()->GetMessagePipeNoLock();
100   }
101   unsigned GetPort() { return message_pipe_dispatcher()->GetPortNoLock(); }
102
103  private:
104   MessagePipeDispatcher* message_pipe_dispatcher() {
105     return static_cast<MessagePipeDispatcher*>(dispatcher());
106   }
107
108   // Copy and assign allowed.
109 };
110
111 }  // namespace system
112 }  // namespace mojo
113
114 #endif  // MOJO_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_