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/dispatcher.h" | 5 #include "mojo/system/dispatcher.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "mojo/system/limits.h" |
8 | 9 |
9 namespace mojo { | 10 namespace mojo { |
10 namespace system { | 11 namespace system { |
11 | 12 |
12 MojoResult Dispatcher::Close() { | 13 MojoResult Dispatcher::Close() { |
13 base::AutoLock locker(lock_); | 14 base::AutoLock locker(lock_); |
14 if (is_closed_) | 15 if (is_closed_) |
15 return MOJO_RESULT_INVALID_ARGUMENT; | 16 return MOJO_RESULT_INVALID_ARGUMENT; |
16 | 17 |
17 is_closed_ = true; | 18 is_closed_ = true; |
18 CancelAllWaitersNoLock(); | 19 CancelAllWaitersNoLock(); |
19 return CloseImplNoLock(); | 20 return CloseImplNoLock(); |
20 } | 21 } |
21 | 22 |
22 MojoResult Dispatcher::WriteMessage(const void* bytes, | 23 MojoResult Dispatcher::WriteMessage(const void* bytes, uint32_t num_bytes, |
23 uint32_t num_bytes, | 24 const std::vector<Dispatcher*>* dispatchers, |
24 const MojoHandle* handles, | |
25 uint32_t num_handles, | |
26 MojoWriteMessageFlags flags) { | 25 MojoWriteMessageFlags flags) { |
| 26 DCHECK(!dispatchers || (dispatchers->size() > 0 && |
| 27 dispatchers->size() < kMaxMessageNumHandles)); |
| 28 |
27 base::AutoLock locker(lock_); | 29 base::AutoLock locker(lock_); |
28 if (is_closed_) | 30 if (is_closed_) |
29 return MOJO_RESULT_INVALID_ARGUMENT; | 31 return MOJO_RESULT_INVALID_ARGUMENT; |
30 | 32 |
31 return WriteMessageImplNoLock(bytes, num_bytes, handles, num_handles, flags); | 33 return WriteMessageImplNoLock(bytes, num_bytes, dispatchers, flags); |
32 } | 34 } |
33 | 35 |
34 MojoResult Dispatcher::ReadMessage(void* bytes, | 36 MojoResult Dispatcher::ReadMessage( |
35 uint32_t* num_bytes, | 37 void* bytes, uint32_t* num_bytes, |
36 MojoHandle* handles, | 38 uint32_t max_num_dispatchers, |
37 uint32_t* num_handles, | 39 std::vector<scoped_refptr<Dispatcher> >* dispatchers, |
38 MojoReadMessageFlags flags) { | 40 MojoReadMessageFlags flags) { |
| 41 DCHECK(max_num_dispatchers == 0 || !!dispatchers); |
| 42 |
39 base::AutoLock locker(lock_); | 43 base::AutoLock locker(lock_); |
40 if (is_closed_) | 44 if (is_closed_) |
41 return MOJO_RESULT_INVALID_ARGUMENT; | 45 return MOJO_RESULT_INVALID_ARGUMENT; |
42 | 46 |
43 return ReadMessageImplNoLock(bytes, num_bytes, handles, num_handles, flags); | 47 return ReadMessageImplNoLock(bytes, num_bytes, |
| 48 max_num_dispatchers, dispatchers, |
| 49 flags); |
44 } | 50 } |
45 | 51 |
46 MojoResult Dispatcher::AddWaiter(Waiter* waiter, | 52 MojoResult Dispatcher::AddWaiter(Waiter* waiter, |
47 MojoWaitFlags flags, | 53 MojoWaitFlags flags, |
48 MojoResult wake_result) { | 54 MojoResult wake_result) { |
49 DCHECK_GE(wake_result, 0); | 55 DCHECK_GE(wake_result, 0); |
50 | 56 |
51 base::AutoLock locker(lock_); | 57 base::AutoLock locker(lock_); |
52 if (is_closed_) | 58 if (is_closed_) |
53 return MOJO_RESULT_INVALID_ARGUMENT; | 59 return MOJO_RESULT_INVALID_ARGUMENT; |
(...skipping 25 matching lines...) Expand all Loading... |
79 } | 85 } |
80 | 86 |
81 MojoResult Dispatcher::CloseImplNoLock() { | 87 MojoResult Dispatcher::CloseImplNoLock() { |
82 lock_.AssertAcquired(); | 88 lock_.AssertAcquired(); |
83 DCHECK(is_closed_); | 89 DCHECK(is_closed_); |
84 // This may not need to do anything. Dispatchers should override this to do | 90 // This may not need to do anything. Dispatchers should override this to do |
85 // any actual close-time cleanup necessary. | 91 // any actual close-time cleanup necessary. |
86 return MOJO_RESULT_OK; | 92 return MOJO_RESULT_OK; |
87 } | 93 } |
88 | 94 |
89 MojoResult Dispatcher::WriteMessageImplNoLock(const void* bytes, | 95 MojoResult Dispatcher::WriteMessageImplNoLock( |
90 uint32_t num_bytes, | 96 const void* bytes, uint32_t num_bytes, |
91 const MojoHandle* handles, | 97 const std::vector<Dispatcher*>* dispatchers, |
92 uint32_t num_handles, | 98 MojoWriteMessageFlags flags) { |
93 MojoWriteMessageFlags flags) { | |
94 lock_.AssertAcquired(); | 99 lock_.AssertAcquired(); |
95 DCHECK(!is_closed_); | 100 DCHECK(!is_closed_); |
96 // By default, this isn't supported. Only dispatchers for message pipes (with | 101 // By default, this isn't supported. Only dispatchers for message pipes (with |
97 // whatever implementation, possibly a proxy) will do something nontrivial. | 102 // whatever implementation, possibly a proxy) will do something nontrivial. |
98 return MOJO_RESULT_INVALID_ARGUMENT; | 103 return MOJO_RESULT_INVALID_ARGUMENT; |
99 } | 104 } |
100 | 105 |
101 MojoResult Dispatcher::ReadMessageImplNoLock(void* bytes, | 106 MojoResult Dispatcher::ReadMessageImplNoLock( |
102 uint32_t* num_bytes, | 107 void* bytes, uint32_t* num_bytes, |
103 MojoHandle* handles, | 108 uint32_t max_num_dispatchers, |
104 uint32_t* num_handles, | 109 std::vector<scoped_refptr<Dispatcher> >* dispatchers, |
105 MojoReadMessageFlags flags) { | 110 MojoReadMessageFlags flags) { |
106 lock_.AssertAcquired(); | 111 lock_.AssertAcquired(); |
107 DCHECK(!is_closed_); | 112 DCHECK(!is_closed_); |
108 // By default, this isn't supported. Only dispatchers for message pipes (with | 113 // By default, this isn't supported. Only dispatchers for message pipes (with |
109 // whatever implementation, possibly a proxy) will do something nontrivial. | 114 // whatever implementation, possibly a proxy) will do something nontrivial. |
110 return MOJO_RESULT_INVALID_ARGUMENT; | 115 return MOJO_RESULT_INVALID_ARGUMENT; |
111 } | 116 } |
112 | 117 |
113 MojoResult Dispatcher::AddWaiterImplNoLock(Waiter* waiter, | 118 MojoResult Dispatcher::AddWaiterImplNoLock(Waiter* waiter, |
114 MojoWaitFlags flags, | 119 MojoWaitFlags flags, |
115 MojoResult wake_result) { | 120 MojoResult wake_result) { |
116 lock_.AssertAcquired(); | 121 lock_.AssertAcquired(); |
117 DCHECK(!is_closed_); | 122 DCHECK(!is_closed_); |
118 // By default, waiting isn't supported. Only dispatchers that can be waited on | 123 // By default, waiting isn't supported. Only dispatchers that can be waited on |
119 // will do something nontrivial. | 124 // will do something nontrivial. |
120 return MOJO_RESULT_FAILED_PRECONDITION; | 125 return MOJO_RESULT_FAILED_PRECONDITION; |
121 } | 126 } |
122 | 127 |
123 void Dispatcher::RemoveWaiterImplNoLock(Waiter* waiter) { | 128 void Dispatcher::RemoveWaiterImplNoLock(Waiter* waiter) { |
124 lock_.AssertAcquired(); | 129 lock_.AssertAcquired(); |
125 DCHECK(!is_closed_); | 130 DCHECK(!is_closed_); |
126 // By default, waiting isn't supported. Only dispatchers that can be waited on | 131 // By default, waiting isn't supported. Only dispatchers that can be waited on |
127 // will do something nontrivial. | 132 // will do something nontrivial. |
128 } | 133 } |
129 | 134 |
130 } // namespace system | 135 } // namespace system |
131 } // namespace mojo | 136 } // namespace mojo |
OLD | NEW |