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/edk/system/data_pipe_consumer_dispatcher.h" | 5 #include "mojo/edk/system/data_pipe_consumer_dispatcher.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "mojo/edk/system/data_pipe.h" | 8 #include "mojo/edk/system/data_pipe.h" |
9 #include "mojo/edk/system/memory.h" | 9 #include "mojo/edk/system/memory.h" |
10 | 10 |
(...skipping 17 matching lines...) Expand all Loading... |
28 scoped_refptr<DataPipe> data_pipe; | 28 scoped_refptr<DataPipe> data_pipe; |
29 if (!DataPipe::ConsumerDeserialize(channel, source, size, &data_pipe)) | 29 if (!DataPipe::ConsumerDeserialize(channel, source, size, &data_pipe)) |
30 return nullptr; | 30 return nullptr; |
31 DCHECK(data_pipe); | 31 DCHECK(data_pipe); |
32 | 32 |
33 scoped_refptr<DataPipeConsumerDispatcher> dispatcher = Create(); | 33 scoped_refptr<DataPipeConsumerDispatcher> dispatcher = Create(); |
34 dispatcher->Init(data_pipe); | 34 dispatcher->Init(data_pipe); |
35 return dispatcher; | 35 return dispatcher; |
36 } | 36 } |
37 | 37 |
| 38 DataPipe* DataPipeConsumerDispatcher::GetDataPipeForTest() { |
| 39 MutexLocker locker(&mutex()); |
| 40 return data_pipe_.get(); |
| 41 } |
| 42 |
38 DataPipeConsumerDispatcher::DataPipeConsumerDispatcher() { | 43 DataPipeConsumerDispatcher::DataPipeConsumerDispatcher() { |
39 } | 44 } |
40 | 45 |
41 DataPipeConsumerDispatcher::~DataPipeConsumerDispatcher() { | 46 DataPipeConsumerDispatcher::~DataPipeConsumerDispatcher() { |
42 // |Close()|/|CloseImplNoLock()| should have taken care of the pipe. | 47 // |Close()|/|CloseImplNoLock()| should have taken care of the pipe. |
43 DCHECK(!data_pipe_); | 48 DCHECK(!data_pipe_); |
44 } | 49 } |
45 | 50 |
46 void DataPipeConsumerDispatcher::CancelAllAwakablesNoLock() { | 51 void DataPipeConsumerDispatcher::CancelAllAwakablesNoLock() { |
47 lock().AssertAcquired(); | 52 mutex().AssertHeld(); |
48 data_pipe_->ConsumerCancelAllAwakables(); | 53 data_pipe_->ConsumerCancelAllAwakables(); |
49 } | 54 } |
50 | 55 |
51 void DataPipeConsumerDispatcher::CloseImplNoLock() { | 56 void DataPipeConsumerDispatcher::CloseImplNoLock() { |
52 lock().AssertAcquired(); | 57 mutex().AssertHeld(); |
53 data_pipe_->ConsumerClose(); | 58 data_pipe_->ConsumerClose(); |
54 data_pipe_ = nullptr; | 59 data_pipe_ = nullptr; |
55 } | 60 } |
56 | 61 |
57 scoped_refptr<Dispatcher> | 62 scoped_refptr<Dispatcher> |
58 DataPipeConsumerDispatcher::CreateEquivalentDispatcherAndCloseImplNoLock() { | 63 DataPipeConsumerDispatcher::CreateEquivalentDispatcherAndCloseImplNoLock() { |
59 lock().AssertAcquired(); | 64 mutex().AssertHeld(); |
60 | 65 |
61 scoped_refptr<DataPipeConsumerDispatcher> rv = Create(); | 66 scoped_refptr<DataPipeConsumerDispatcher> rv = Create(); |
62 rv->Init(data_pipe_); | 67 rv->Init(data_pipe_); |
63 data_pipe_ = nullptr; | 68 data_pipe_ = nullptr; |
64 return scoped_refptr<Dispatcher>(rv.get()); | 69 return scoped_refptr<Dispatcher>(rv.get()); |
65 } | 70 } |
66 | 71 |
67 MojoResult DataPipeConsumerDispatcher::ReadDataImplNoLock( | 72 MojoResult DataPipeConsumerDispatcher::ReadDataImplNoLock( |
68 UserPointer<void> elements, | 73 UserPointer<void> elements, |
69 UserPointer<uint32_t> num_bytes, | 74 UserPointer<uint32_t> num_bytes, |
70 MojoReadDataFlags flags) { | 75 MojoReadDataFlags flags) { |
71 lock().AssertAcquired(); | 76 mutex().AssertHeld(); |
72 | 77 |
73 if ((flags & MOJO_READ_DATA_FLAG_DISCARD)) { | 78 if ((flags & MOJO_READ_DATA_FLAG_DISCARD)) { |
74 // These flags are mutally exclusive. | 79 // These flags are mutally exclusive. |
75 if ((flags & MOJO_READ_DATA_FLAG_QUERY) || | 80 if ((flags & MOJO_READ_DATA_FLAG_QUERY) || |
76 (flags & MOJO_READ_DATA_FLAG_PEEK)) | 81 (flags & MOJO_READ_DATA_FLAG_PEEK)) |
77 return MOJO_RESULT_INVALID_ARGUMENT; | 82 return MOJO_RESULT_INVALID_ARGUMENT; |
78 DVLOG_IF(2, !elements.IsNull()) | 83 DVLOG_IF(2, !elements.IsNull()) |
79 << "Discard mode: ignoring non-null |elements|"; | 84 << "Discard mode: ignoring non-null |elements|"; |
80 return data_pipe_->ConsumerDiscardData( | 85 return data_pipe_->ConsumerDiscardData( |
81 num_bytes, (flags & MOJO_READ_DATA_FLAG_ALL_OR_NONE)); | 86 num_bytes, (flags & MOJO_READ_DATA_FLAG_ALL_OR_NONE)); |
(...skipping 10 matching lines...) Expand all Loading... |
92 | 97 |
93 return data_pipe_->ConsumerReadData( | 98 return data_pipe_->ConsumerReadData( |
94 elements, num_bytes, !!(flags & MOJO_READ_DATA_FLAG_ALL_OR_NONE), | 99 elements, num_bytes, !!(flags & MOJO_READ_DATA_FLAG_ALL_OR_NONE), |
95 !!(flags & MOJO_READ_DATA_FLAG_PEEK)); | 100 !!(flags & MOJO_READ_DATA_FLAG_PEEK)); |
96 } | 101 } |
97 | 102 |
98 MojoResult DataPipeConsumerDispatcher::BeginReadDataImplNoLock( | 103 MojoResult DataPipeConsumerDispatcher::BeginReadDataImplNoLock( |
99 UserPointer<const void*> buffer, | 104 UserPointer<const void*> buffer, |
100 UserPointer<uint32_t> buffer_num_bytes, | 105 UserPointer<uint32_t> buffer_num_bytes, |
101 MojoReadDataFlags flags) { | 106 MojoReadDataFlags flags) { |
102 lock().AssertAcquired(); | 107 mutex().AssertHeld(); |
103 | 108 |
104 // These flags may not be used in two-phase mode. | 109 // These flags may not be used in two-phase mode. |
105 if ((flags & MOJO_READ_DATA_FLAG_DISCARD) || | 110 if ((flags & MOJO_READ_DATA_FLAG_DISCARD) || |
106 (flags & MOJO_READ_DATA_FLAG_QUERY) || (flags & MOJO_READ_DATA_FLAG_PEEK)) | 111 (flags & MOJO_READ_DATA_FLAG_QUERY) || (flags & MOJO_READ_DATA_FLAG_PEEK)) |
107 return MOJO_RESULT_INVALID_ARGUMENT; | 112 return MOJO_RESULT_INVALID_ARGUMENT; |
108 | 113 |
109 return data_pipe_->ConsumerBeginReadData( | 114 return data_pipe_->ConsumerBeginReadData( |
110 buffer, buffer_num_bytes, (flags & MOJO_READ_DATA_FLAG_ALL_OR_NONE)); | 115 buffer, buffer_num_bytes, (flags & MOJO_READ_DATA_FLAG_ALL_OR_NONE)); |
111 } | 116 } |
112 | 117 |
113 MojoResult DataPipeConsumerDispatcher::EndReadDataImplNoLock( | 118 MojoResult DataPipeConsumerDispatcher::EndReadDataImplNoLock( |
114 uint32_t num_bytes_read) { | 119 uint32_t num_bytes_read) { |
115 lock().AssertAcquired(); | 120 mutex().AssertHeld(); |
116 | 121 |
117 return data_pipe_->ConsumerEndReadData(num_bytes_read); | 122 return data_pipe_->ConsumerEndReadData(num_bytes_read); |
118 } | 123 } |
119 | 124 |
120 HandleSignalsState DataPipeConsumerDispatcher::GetHandleSignalsStateImplNoLock() | 125 HandleSignalsState DataPipeConsumerDispatcher::GetHandleSignalsStateImplNoLock() |
121 const { | 126 const { |
122 lock().AssertAcquired(); | 127 mutex().AssertHeld(); |
123 return data_pipe_->ConsumerGetHandleSignalsState(); | 128 return data_pipe_->ConsumerGetHandleSignalsState(); |
124 } | 129 } |
125 | 130 |
126 MojoResult DataPipeConsumerDispatcher::AddAwakableImplNoLock( | 131 MojoResult DataPipeConsumerDispatcher::AddAwakableImplNoLock( |
127 Awakable* awakable, | 132 Awakable* awakable, |
128 MojoHandleSignals signals, | 133 MojoHandleSignals signals, |
129 uint32_t context, | 134 uint32_t context, |
130 HandleSignalsState* signals_state) { | 135 HandleSignalsState* signals_state) { |
131 lock().AssertAcquired(); | 136 mutex().AssertHeld(); |
132 return data_pipe_->ConsumerAddAwakable(awakable, signals, context, | 137 return data_pipe_->ConsumerAddAwakable(awakable, signals, context, |
133 signals_state); | 138 signals_state); |
134 } | 139 } |
135 | 140 |
136 void DataPipeConsumerDispatcher::RemoveAwakableImplNoLock( | 141 void DataPipeConsumerDispatcher::RemoveAwakableImplNoLock( |
137 Awakable* awakable, | 142 Awakable* awakable, |
138 HandleSignalsState* signals_state) { | 143 HandleSignalsState* signals_state) { |
139 lock().AssertAcquired(); | 144 mutex().AssertHeld(); |
140 data_pipe_->ConsumerRemoveAwakable(awakable, signals_state); | 145 data_pipe_->ConsumerRemoveAwakable(awakable, signals_state); |
141 } | 146 } |
142 | 147 |
143 void DataPipeConsumerDispatcher::StartSerializeImplNoLock( | 148 void DataPipeConsumerDispatcher::StartSerializeImplNoLock( |
144 Channel* channel, | 149 Channel* channel, |
145 size_t* max_size, | 150 size_t* max_size, |
146 size_t* max_platform_handles) { | 151 size_t* max_platform_handles) { |
147 DCHECK(HasOneRef()); // Only one ref => no need to take the lock. | 152 DCHECK(HasOneRef()); // Only one ref => no need to take the lock. |
148 data_pipe_->ConsumerStartSerialize(channel, max_size, max_platform_handles); | 153 data_pipe_->ConsumerStartSerialize(channel, max_size, max_platform_handles); |
149 } | 154 } |
150 | 155 |
151 bool DataPipeConsumerDispatcher::EndSerializeAndCloseImplNoLock( | 156 bool DataPipeConsumerDispatcher::EndSerializeAndCloseImplNoLock( |
152 Channel* channel, | 157 Channel* channel, |
153 void* destination, | 158 void* destination, |
154 size_t* actual_size, | 159 size_t* actual_size, |
155 embedder::PlatformHandleVector* platform_handles) { | 160 embedder::PlatformHandleVector* platform_handles) { |
156 DCHECK(HasOneRef()); // Only one ref => no need to take the lock. | 161 DCHECK(HasOneRef()); // Only one ref => no need to take the lock. |
157 | 162 |
158 bool rv = data_pipe_->ConsumerEndSerialize(channel, destination, actual_size, | 163 bool rv = data_pipe_->ConsumerEndSerialize(channel, destination, actual_size, |
159 platform_handles); | 164 platform_handles); |
160 data_pipe_ = nullptr; | 165 data_pipe_ = nullptr; |
161 return rv; | 166 return rv; |
162 } | 167 } |
163 | 168 |
164 bool DataPipeConsumerDispatcher::IsBusyNoLock() const { | 169 bool DataPipeConsumerDispatcher::IsBusyNoLock() const { |
165 lock().AssertAcquired(); | 170 mutex().AssertHeld(); |
166 return data_pipe_->ConsumerIsBusy(); | 171 return data_pipe_->ConsumerIsBusy(); |
167 } | 172 } |
168 | 173 |
169 } // namespace system | 174 } // namespace system |
170 } // namespace mojo | 175 } // namespace mojo |
OLD | NEW |