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/limits.h"
9 #include "mojo/system/memory.h"
10 #include "mojo/system/message_pipe.h"
15 MessagePipeDispatcher::MessagePipeDispatcher() {
18 void MessagePipeDispatcher::Init(scoped_refptr<MessagePipe> message_pipe,
20 DCHECK(message_pipe.get());
21 DCHECK(port == 0 || port == 1);
23 message_pipe_ = message_pipe;
27 MessagePipeDispatcher::~MessagePipeDispatcher() {
28 // |Close()|/|CloseImplNoLock()| should have taken care of the pipe.
29 DCHECK(!message_pipe_.get());
32 void MessagePipeDispatcher::CancelAllWaitersNoLock() {
33 lock().AssertAcquired();
34 message_pipe_->CancelAllWaiters(port_);
37 MojoResult MessagePipeDispatcher::CloseImplNoLock() {
38 lock().AssertAcquired();
39 message_pipe_->Close(port_);
41 return MOJO_RESULT_OK;
44 MojoResult MessagePipeDispatcher::WriteMessageImplNoLock(
45 const void* bytes, uint32_t num_bytes,
46 const MojoHandle* handles, uint32_t num_handles,
47 MojoWriteMessageFlags flags) {
48 lock().AssertAcquired();
50 if (!VerifyUserPointer<void>(bytes, num_bytes))
51 return MOJO_RESULT_INVALID_ARGUMENT;
52 if (num_bytes > kMaxMessageNumBytes)
53 return MOJO_RESULT_RESOURCE_EXHAUSTED;
55 if (!VerifyUserPointer<MojoHandle>(handles, num_handles))
56 return MOJO_RESULT_INVALID_ARGUMENT;
57 if (num_handles > kMaxMessageNumHandles)
58 return MOJO_RESULT_RESOURCE_EXHAUSTED;
59 if (num_handles > 0) {
60 // TODO(vtl): Verify each handle.
62 return MOJO_RESULT_UNIMPLEMENTED;
65 return message_pipe_->WriteMessage(port_,
71 MojoResult MessagePipeDispatcher::ReadMessageImplNoLock(
72 void* bytes, uint32_t* num_bytes,
73 MojoHandle* handles, uint32_t* num_handles,
74 MojoReadMessageFlags flags) {
75 lock().AssertAcquired();
77 // TODO(vtl): I suppose we should verify |num_bytes| and |num_handles| (i.e.,
78 // those pointers themselves). Hmmm.
80 if (num_bytes && !VerifyUserPointer<void>(bytes, *num_bytes))
81 return MOJO_RESULT_INVALID_ARGUMENT;
83 if (num_handles && !VerifyUserPointer<MojoHandle>(handles, *num_handles))
84 return MOJO_RESULT_INVALID_ARGUMENT;
86 return message_pipe_->ReadMessage(port_,
92 MojoResult MessagePipeDispatcher::AddWaiterImplNoLock(Waiter* waiter,
94 MojoResult wake_result) {
95 lock().AssertAcquired();
96 return message_pipe_->AddWaiter(port_, waiter, flags, wake_result);
99 void MessagePipeDispatcher::RemoveWaiterImplNoLock(Waiter* waiter) {
100 lock().AssertAcquired();
101 message_pipe_->RemoveWaiter(port_, waiter);
104 } // namespace system