Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / mojo / system / message_pipe_dispatcher.cc
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 #include "mojo/system/message_pipe_dispatcher.h"
6
7 #include "base/logging.h"
8 #include "mojo/system/constants.h"
9 #include "mojo/system/memory.h"
10 #include "mojo/system/message_pipe.h"
11
12 namespace mojo {
13 namespace system {
14
15 const unsigned kInvalidPort = static_cast<unsigned>(-1);
16
17 // MessagePipeDispatcher -------------------------------------------------------
18
19 MessagePipeDispatcher::MessagePipeDispatcher()
20     : port_(kInvalidPort) {
21 }
22
23 void MessagePipeDispatcher::Init(scoped_refptr<MessagePipe> message_pipe,
24                                  unsigned port) {
25   DCHECK(message_pipe.get());
26   DCHECK(port == 0 || port == 1);
27
28   message_pipe_ = message_pipe;
29   port_ = port;
30 }
31
32 Dispatcher::Type MessagePipeDispatcher::GetType() const {
33   return kTypeMessagePipe;
34 }
35
36 MessagePipeDispatcher::~MessagePipeDispatcher() {
37   // |Close()|/|CloseImplNoLock()| should have taken care of the pipe.
38   DCHECK(!message_pipe_.get());
39 }
40
41 MessagePipe* MessagePipeDispatcher::GetMessagePipeNoLock() const {
42   lock().AssertAcquired();
43   return message_pipe_.get();
44 }
45
46 unsigned MessagePipeDispatcher::GetPortNoLock() const {
47   lock().AssertAcquired();
48   return port_;
49 }
50
51 void MessagePipeDispatcher::CancelAllWaitersNoLock() {
52   lock().AssertAcquired();
53   message_pipe_->CancelAllWaiters(port_);
54 }
55
56 void MessagePipeDispatcher::CloseImplNoLock() {
57   lock().AssertAcquired();
58   message_pipe_->Close(port_);
59   message_pipe_ = NULL;
60   port_ = kInvalidPort;
61 }
62
63 scoped_refptr<Dispatcher>
64 MessagePipeDispatcher::CreateEquivalentDispatcherAndCloseImplNoLock() {
65   lock().AssertAcquired();
66
67   scoped_refptr<MessagePipeDispatcher> rv = new MessagePipeDispatcher();
68   rv->Init(message_pipe_, port_);
69   message_pipe_ = NULL;
70   port_ = kInvalidPort;
71   return scoped_refptr<Dispatcher>(rv.get());
72 }
73
74 MojoResult MessagePipeDispatcher::WriteMessageImplNoLock(
75     const void* bytes,
76     uint32_t num_bytes,
77     std::vector<DispatcherTransport>* transports,
78     MojoWriteMessageFlags flags) {
79   DCHECK(!transports || (transports->size() > 0 &&
80                          transports->size() <= kMaxMessageNumHandles));
81
82   lock().AssertAcquired();
83
84   if (!VerifyUserPointer<void>(bytes, num_bytes))
85     return MOJO_RESULT_INVALID_ARGUMENT;
86   if (num_bytes > kMaxMessageNumBytes)
87     return MOJO_RESULT_RESOURCE_EXHAUSTED;
88
89   return message_pipe_->WriteMessage(port_, bytes, num_bytes, transports,
90                                      flags);
91 }
92
93 MojoResult MessagePipeDispatcher::ReadMessageImplNoLock(
94     void* bytes,
95     uint32_t* num_bytes,
96     std::vector<scoped_refptr<Dispatcher> >* dispatchers,
97     uint32_t* num_dispatchers,
98     MojoReadMessageFlags flags) {
99   lock().AssertAcquired();
100
101   if (num_bytes) {
102     if (!VerifyUserPointer<uint32_t>(num_bytes, 1))
103       return MOJO_RESULT_INVALID_ARGUMENT;
104     if (!VerifyUserPointer<void>(bytes, *num_bytes))
105       return MOJO_RESULT_INVALID_ARGUMENT;
106   }
107
108   return message_pipe_->ReadMessage(port_, bytes, num_bytes, dispatchers,
109                                     num_dispatchers, flags);
110 }
111
112 MojoResult MessagePipeDispatcher::AddWaiterImplNoLock(Waiter* waiter,
113                                                       MojoWaitFlags flags,
114                                                       MojoResult wake_result) {
115   lock().AssertAcquired();
116   return message_pipe_->AddWaiter(port_, waiter, flags, wake_result);
117 }
118
119 void MessagePipeDispatcher::RemoveWaiterImplNoLock(Waiter* waiter) {
120   lock().AssertAcquired();
121   message_pipe_->RemoveWaiter(port_, waiter);
122 }
123
124 // MessagePipeDispatcherTransport ----------------------------------------------
125
126 MessagePipeDispatcherTransport::MessagePipeDispatcherTransport(
127     DispatcherTransport transport) : DispatcherTransport(transport) {
128   DCHECK_EQ(message_pipe_dispatcher()->GetType(), Dispatcher::kTypeMessagePipe);
129 }
130
131 }  // namespace system
132 }  // namespace mojo