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.
5 #include "mojo/system/message_pipe_dispatcher.h"
7 #include "base/logging.h"
8 #include "mojo/system/constants.h"
9 #include "mojo/system/memory.h"
10 #include "mojo/system/message_pipe.h"
15 const unsigned kInvalidPort = static_cast<unsigned>(-1);
17 // MessagePipeDispatcher -------------------------------------------------------
19 MessagePipeDispatcher::MessagePipeDispatcher()
20 : port_(kInvalidPort) {
23 void MessagePipeDispatcher::Init(scoped_refptr<MessagePipe> message_pipe,
25 DCHECK(message_pipe.get());
26 DCHECK(port == 0 || port == 1);
28 message_pipe_ = message_pipe;
32 Dispatcher::Type MessagePipeDispatcher::GetType() const {
33 return kTypeMessagePipe;
36 MessagePipeDispatcher::~MessagePipeDispatcher() {
37 // |Close()|/|CloseImplNoLock()| should have taken care of the pipe.
38 DCHECK(!message_pipe_.get());
41 MessagePipe* MessagePipeDispatcher::GetMessagePipeNoLock() const {
42 lock().AssertAcquired();
43 return message_pipe_.get();
46 unsigned MessagePipeDispatcher::GetPortNoLock() const {
47 lock().AssertAcquired();
51 void MessagePipeDispatcher::CancelAllWaitersNoLock() {
52 lock().AssertAcquired();
53 message_pipe_->CancelAllWaiters(port_);
56 void MessagePipeDispatcher::CloseImplNoLock() {
57 lock().AssertAcquired();
58 message_pipe_->Close(port_);
63 scoped_refptr<Dispatcher>
64 MessagePipeDispatcher::CreateEquivalentDispatcherAndCloseImplNoLock() {
65 lock().AssertAcquired();
67 scoped_refptr<MessagePipeDispatcher> rv = new MessagePipeDispatcher();
68 rv->Init(message_pipe_, port_);
71 return scoped_refptr<Dispatcher>(rv.get());
74 MojoResult MessagePipeDispatcher::WriteMessageImplNoLock(
77 std::vector<DispatcherTransport>* transports,
78 MojoWriteMessageFlags flags) {
79 DCHECK(!transports || (transports->size() > 0 &&
80 transports->size() <= kMaxMessageNumHandles));
82 lock().AssertAcquired();
84 if (!VerifyUserPointer<void>(bytes, num_bytes))
85 return MOJO_RESULT_INVALID_ARGUMENT;
86 if (num_bytes > kMaxMessageNumBytes)
87 return MOJO_RESULT_RESOURCE_EXHAUSTED;
89 return message_pipe_->WriteMessage(port_, bytes, num_bytes, transports,
93 MojoResult MessagePipeDispatcher::ReadMessageImplNoLock(
96 std::vector<scoped_refptr<Dispatcher> >* dispatchers,
97 uint32_t* num_dispatchers,
98 MojoReadMessageFlags flags) {
99 lock().AssertAcquired();
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;
108 return message_pipe_->ReadMessage(port_, bytes, num_bytes, dispatchers,
109 num_dispatchers, flags);
112 MojoResult MessagePipeDispatcher::AddWaiterImplNoLock(Waiter* waiter,
114 MojoResult wake_result) {
115 lock().AssertAcquired();
116 return message_pipe_->AddWaiter(port_, waiter, flags, wake_result);
119 void MessagePipeDispatcher::RemoveWaiterImplNoLock(Waiter* waiter) {
120 lock().AssertAcquired();
121 message_pipe_->RemoveWaiter(port_, waiter);
124 // MessagePipeDispatcherTransport ----------------------------------------------
126 MessagePipeDispatcherTransport::MessagePipeDispatcherTransport(
127 DispatcherTransport transport) : DispatcherTransport(transport) {
128 DCHECK_EQ(message_pipe_dispatcher()->GetType(), Dispatcher::kTypeMessagePipe);
131 } // namespace system