- add sources.
[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/limits.h"
9 #include "mojo/system/memory.h"
10 #include "mojo/system/message_pipe.h"
11
12 namespace mojo {
13 namespace system {
14
15 MessagePipeDispatcher::MessagePipeDispatcher() {
16 }
17
18 void MessagePipeDispatcher::Init(scoped_refptr<MessagePipe> message_pipe,
19                                  unsigned port) {
20   DCHECK(message_pipe.get());
21   DCHECK(port == 0 || port == 1);
22
23   message_pipe_ = message_pipe;
24   port_ = port;
25 }
26
27 MessagePipeDispatcher::~MessagePipeDispatcher() {
28   // |Close()|/|CloseImplNoLock()| should have taken care of the pipe.
29   DCHECK(!message_pipe_.get());
30 }
31
32 void MessagePipeDispatcher::CancelAllWaitersNoLock() {
33   lock().AssertAcquired();
34   message_pipe_->CancelAllWaiters(port_);
35 }
36
37 MojoResult MessagePipeDispatcher::CloseImplNoLock() {
38   lock().AssertAcquired();
39   message_pipe_->Close(port_);
40   message_pipe_ = NULL;
41   return MOJO_RESULT_OK;
42 }
43
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();
49
50   if (!VerifyUserPointer<void>(bytes, num_bytes))
51     return MOJO_RESULT_INVALID_ARGUMENT;
52   if (num_bytes > kMaxMessageNumBytes)
53     return MOJO_RESULT_RESOURCE_EXHAUSTED;
54
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.
61     NOTIMPLEMENTED();
62     return MOJO_RESULT_UNIMPLEMENTED;
63   }
64
65   return message_pipe_->WriteMessage(port_,
66                                      bytes, num_bytes,
67                                      handles, num_handles,
68                                      flags);
69 }
70
71 MojoResult MessagePipeDispatcher::ReadMessageImplNoLock(
72     void* bytes, uint32_t* num_bytes,
73     MojoHandle* handles, uint32_t* num_handles,
74     MojoReadMessageFlags flags) {
75   lock().AssertAcquired();
76
77   // TODO(vtl): I suppose we should verify |num_bytes| and |num_handles| (i.e.,
78   // those pointers themselves). Hmmm.
79
80   if (num_bytes && !VerifyUserPointer<void>(bytes, *num_bytes))
81     return MOJO_RESULT_INVALID_ARGUMENT;
82
83   if (num_handles && !VerifyUserPointer<MojoHandle>(handles, *num_handles))
84     return MOJO_RESULT_INVALID_ARGUMENT;
85
86   return message_pipe_->ReadMessage(port_,
87                                     bytes, num_bytes,
88                                     handles, num_handles,
89                                     flags);
90 }
91
92 MojoResult MessagePipeDispatcher::AddWaiterImplNoLock(Waiter* waiter,
93                                                       MojoWaitFlags flags,
94                                                       MojoResult wake_result) {
95   lock().AssertAcquired();
96   return message_pipe_->AddWaiter(port_, waiter, flags, wake_result);
97 }
98
99 void MessagePipeDispatcher::RemoveWaiterImplNoLock(Waiter* waiter) {
100   lock().AssertAcquired();
101   message_pipe_->RemoveWaiter(port_, waiter);
102 }
103
104 }  // namespace system
105 }  // namespace mojo