OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/system/message_pipe_dispatcher.h" | 5 #include "mojo/system/message_pipe_dispatcher.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "mojo/system/limits.h" | 8 #include "mojo/system/limits.h" |
9 #include "mojo/system/memory.h" | 9 #include "mojo/system/memory.h" |
10 #include "mojo/system/message_pipe.h" | 10 #include "mojo/system/message_pipe.h" |
11 | 11 |
12 namespace mojo { | 12 namespace mojo { |
13 namespace system { | 13 namespace system { |
14 | 14 |
15 MessagePipeDispatcher::MessagePipeDispatcher() { | 15 const unsigned kInvalidPort = static_cast<unsigned>(-1); |
| 16 |
| 17 MessagePipeDispatcher::MessagePipeDispatcher() |
| 18 : port_(kInvalidPort) { |
16 } | 19 } |
17 | 20 |
18 void MessagePipeDispatcher::Init(scoped_refptr<MessagePipe> message_pipe, | 21 void MessagePipeDispatcher::Init(scoped_refptr<MessagePipe> message_pipe, |
19 unsigned port) { | 22 unsigned port) { |
20 DCHECK(message_pipe.get()); | 23 DCHECK(message_pipe.get()); |
21 DCHECK(port == 0 || port == 1); | 24 DCHECK(port == 0 || port == 1); |
22 | 25 |
23 message_pipe_ = message_pipe; | 26 message_pipe_ = message_pipe; |
24 port_ = port; | 27 port_ = port; |
25 } | 28 } |
26 | 29 |
27 MessagePipeDispatcher::~MessagePipeDispatcher() { | 30 MessagePipeDispatcher::~MessagePipeDispatcher() { |
28 // |Close()|/|CloseImplNoLock()| should have taken care of the pipe. | 31 // |Close()|/|CloseImplNoLock()| should have taken care of the pipe. |
29 DCHECK(!message_pipe_.get()); | 32 DCHECK(!message_pipe_.get()); |
30 } | 33 } |
31 | 34 |
32 void MessagePipeDispatcher::CancelAllWaitersNoLock() { | 35 void MessagePipeDispatcher::CancelAllWaitersNoLock() { |
33 lock().AssertAcquired(); | 36 lock().AssertAcquired(); |
34 message_pipe_->CancelAllWaiters(port_); | 37 message_pipe_->CancelAllWaiters(port_); |
35 } | 38 } |
36 | 39 |
37 MojoResult MessagePipeDispatcher::CloseImplNoLock() { | 40 MojoResult MessagePipeDispatcher::CloseImplNoLock() { |
38 lock().AssertAcquired(); | 41 lock().AssertAcquired(); |
39 message_pipe_->Close(port_); | 42 message_pipe_->Close(port_); |
40 message_pipe_ = NULL; | 43 message_pipe_ = NULL; |
| 44 port_ = kInvalidPort; |
41 return MOJO_RESULT_OK; | 45 return MOJO_RESULT_OK; |
42 } | 46 } |
43 | 47 |
44 MojoResult MessagePipeDispatcher::WriteMessageImplNoLock( | 48 MojoResult MessagePipeDispatcher::WriteMessageImplNoLock( |
45 const void* bytes, uint32_t num_bytes, | 49 const void* bytes, uint32_t num_bytes, |
46 const std::vector<Dispatcher*>* dispatchers, | 50 const std::vector<Dispatcher*>* dispatchers, |
47 MojoWriteMessageFlags flags) { | 51 MojoWriteMessageFlags flags) { |
48 DCHECK(!dispatchers || (dispatchers->size() > 0 && | 52 DCHECK(!dispatchers || (dispatchers->size() > 0 && |
49 dispatchers->size() <= kMaxMessageNumHandles)); | 53 dispatchers->size() <= kMaxMessageNumHandles)); |
50 | 54 |
51 lock().AssertAcquired(); | 55 lock().AssertAcquired(); |
52 | 56 |
53 if (!VerifyUserPointer<void>(bytes, num_bytes)) | 57 if (!VerifyUserPointer<void>(bytes, num_bytes)) |
54 return MOJO_RESULT_INVALID_ARGUMENT; | 58 return MOJO_RESULT_INVALID_ARGUMENT; |
55 if (num_bytes > kMaxMessageNumBytes) | 59 if (num_bytes > kMaxMessageNumBytes) |
56 return MOJO_RESULT_RESOURCE_EXHAUSTED; | 60 return MOJO_RESULT_RESOURCE_EXHAUSTED; |
57 | 61 |
58 return message_pipe_->WriteMessage(port_, | 62 return message_pipe_->WriteMessage(port_, |
59 bytes, num_bytes, | 63 bytes, num_bytes, |
60 dispatchers, | 64 dispatchers, |
61 flags); | 65 flags); |
62 } | 66 } |
63 | 67 |
64 MojoResult MessagePipeDispatcher::ReadMessageImplNoLock( | 68 MojoResult MessagePipeDispatcher::ReadMessageImplNoLock( |
65 void* bytes, uint32_t* num_bytes, | 69 void* bytes, uint32_t* num_bytes, |
66 uint32_t max_num_dispatchers, | |
67 std::vector<scoped_refptr<Dispatcher> >* dispatchers, | 70 std::vector<scoped_refptr<Dispatcher> >* dispatchers, |
| 71 uint32_t* num_dispatchers, |
68 MojoReadMessageFlags flags) { | 72 MojoReadMessageFlags flags) { |
69 lock().AssertAcquired(); | 73 lock().AssertAcquired(); |
70 | 74 |
71 if (num_bytes) { | 75 if (num_bytes) { |
72 if (!VerifyUserPointer<uint32_t>(num_bytes, 1)) | 76 if (!VerifyUserPointer<uint32_t>(num_bytes, 1)) |
73 return MOJO_RESULT_INVALID_ARGUMENT; | 77 return MOJO_RESULT_INVALID_ARGUMENT; |
74 if (!VerifyUserPointer<void>(bytes, *num_bytes)) | 78 if (!VerifyUserPointer<void>(bytes, *num_bytes)) |
75 return MOJO_RESULT_INVALID_ARGUMENT; | 79 return MOJO_RESULT_INVALID_ARGUMENT; |
76 } | 80 } |
77 | 81 |
78 return message_pipe_->ReadMessage(port_, | 82 return message_pipe_->ReadMessage(port_, |
79 bytes, num_bytes, | 83 bytes, num_bytes, |
80 max_num_dispatchers, dispatchers, | 84 dispatchers, num_dispatchers, |
81 flags); | 85 flags); |
82 } | 86 } |
83 | 87 |
84 MojoResult MessagePipeDispatcher::AddWaiterImplNoLock(Waiter* waiter, | 88 MojoResult MessagePipeDispatcher::AddWaiterImplNoLock(Waiter* waiter, |
85 MojoWaitFlags flags, | 89 MojoWaitFlags flags, |
86 MojoResult wake_result) { | 90 MojoResult wake_result) { |
87 lock().AssertAcquired(); | 91 lock().AssertAcquired(); |
88 return message_pipe_->AddWaiter(port_, waiter, flags, wake_result); | 92 return message_pipe_->AddWaiter(port_, waiter, flags, wake_result); |
89 } | 93 } |
90 | 94 |
91 void MessagePipeDispatcher::RemoveWaiterImplNoLock(Waiter* waiter) { | 95 void MessagePipeDispatcher::RemoveWaiterImplNoLock(Waiter* waiter) { |
92 lock().AssertAcquired(); | 96 lock().AssertAcquired(); |
93 message_pipe_->RemoveWaiter(port_, waiter); | 97 message_pipe_->RemoveWaiter(port_, waiter); |
94 } | 98 } |
95 | 99 |
| 100 scoped_refptr<Dispatcher> |
| 101 MessagePipeDispatcher::CreateEquivalentDispatcherAndCloseImplNoLock() { |
| 102 lock().AssertAcquired(); |
| 103 |
| 104 scoped_refptr<MessagePipeDispatcher> rv = new MessagePipeDispatcher; |
| 105 rv->Init(message_pipe_, port_); |
| 106 message_pipe_ = NULL; |
| 107 port_ = kInvalidPort; |
| 108 return scoped_refptr<Dispatcher>(rv.get()); |
| 109 } |
| 110 |
96 } // namespace system | 111 } // namespace system |
97 } // namespace mojo | 112 } // namespace mojo |
OLD | NEW |