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/edk/system/data_pipe_consumer_dispatcher.h"
7 #include "base/logging.h"
8 #include "mojo/edk/system/data_pipe.h"
9 #include "mojo/edk/system/memory.h"
14 DataPipeConsumerDispatcher::DataPipeConsumerDispatcher() {
17 void DataPipeConsumerDispatcher::Init(scoped_refptr
<DataPipe
> data_pipe
) {
19 data_pipe_
= data_pipe
;
22 Dispatcher::Type
DataPipeConsumerDispatcher::GetType() const {
23 return kTypeDataPipeConsumer
;
26 DataPipeConsumerDispatcher::~DataPipeConsumerDispatcher() {
27 // |Close()|/|CloseImplNoLock()| should have taken care of the pipe.
31 void DataPipeConsumerDispatcher::CancelAllAwakablesNoLock() {
32 lock().AssertAcquired();
33 data_pipe_
->ConsumerCancelAllAwakables();
36 void DataPipeConsumerDispatcher::CloseImplNoLock() {
37 lock().AssertAcquired();
38 data_pipe_
->ConsumerClose();
42 scoped_refptr
<Dispatcher
>
43 DataPipeConsumerDispatcher::CreateEquivalentDispatcherAndCloseImplNoLock() {
44 lock().AssertAcquired();
46 scoped_refptr
<DataPipeConsumerDispatcher
> rv
=
47 new DataPipeConsumerDispatcher();
50 return scoped_refptr
<Dispatcher
>(rv
.get());
53 MojoResult
DataPipeConsumerDispatcher::ReadDataImplNoLock(
54 UserPointer
<void> elements
,
55 UserPointer
<uint32_t> num_bytes
,
56 MojoReadDataFlags flags
) {
57 lock().AssertAcquired();
59 if ((flags
& MOJO_READ_DATA_FLAG_DISCARD
)) {
60 // These flags are mutally exclusive.
61 if ((flags
& MOJO_READ_DATA_FLAG_QUERY
) ||
62 (flags
& MOJO_READ_DATA_FLAG_PEEK
))
63 return MOJO_RESULT_INVALID_ARGUMENT
;
64 DVLOG_IF(2, !elements
.IsNull())
65 << "Discard mode: ignoring non-null |elements|";
66 return data_pipe_
->ConsumerDiscardData(
67 num_bytes
, (flags
& MOJO_READ_DATA_FLAG_ALL_OR_NONE
));
70 if ((flags
& MOJO_READ_DATA_FLAG_QUERY
)) {
71 if ((flags
& MOJO_READ_DATA_FLAG_PEEK
))
72 return MOJO_RESULT_INVALID_ARGUMENT
;
73 DCHECK(!(flags
& MOJO_READ_DATA_FLAG_DISCARD
)); // Handled above.
74 DVLOG_IF(2, !elements
.IsNull())
75 << "Query mode: ignoring non-null |elements|";
76 return data_pipe_
->ConsumerQueryData(num_bytes
);
79 return data_pipe_
->ConsumerReadData(
80 elements
, num_bytes
, !!(flags
& MOJO_READ_DATA_FLAG_ALL_OR_NONE
),
81 !!(flags
& MOJO_READ_DATA_FLAG_PEEK
));
84 MojoResult
DataPipeConsumerDispatcher::BeginReadDataImplNoLock(
85 UserPointer
<const void*> buffer
,
86 UserPointer
<uint32_t> buffer_num_bytes
,
87 MojoReadDataFlags flags
) {
88 lock().AssertAcquired();
90 // These flags may not be used in two-phase mode.
91 if ((flags
& MOJO_READ_DATA_FLAG_DISCARD
) ||
92 (flags
& MOJO_READ_DATA_FLAG_QUERY
) || (flags
& MOJO_READ_DATA_FLAG_PEEK
))
93 return MOJO_RESULT_INVALID_ARGUMENT
;
95 return data_pipe_
->ConsumerBeginReadData(
96 buffer
, buffer_num_bytes
, (flags
& MOJO_READ_DATA_FLAG_ALL_OR_NONE
));
99 MojoResult
DataPipeConsumerDispatcher::EndReadDataImplNoLock(
100 uint32_t num_bytes_read
) {
101 lock().AssertAcquired();
103 return data_pipe_
->ConsumerEndReadData(num_bytes_read
);
106 HandleSignalsState
DataPipeConsumerDispatcher::GetHandleSignalsStateImplNoLock()
108 lock().AssertAcquired();
109 return data_pipe_
->ConsumerGetHandleSignalsState();
112 MojoResult
DataPipeConsumerDispatcher::AddAwakableImplNoLock(
114 MojoHandleSignals signals
,
116 HandleSignalsState
* signals_state
) {
117 lock().AssertAcquired();
118 return data_pipe_
->ConsumerAddAwakable(awakable
, signals
, context
,
122 void DataPipeConsumerDispatcher::RemoveAwakableImplNoLock(
124 HandleSignalsState
* signals_state
) {
125 lock().AssertAcquired();
126 data_pipe_
->ConsumerRemoveAwakable(awakable
, signals_state
);
129 bool DataPipeConsumerDispatcher::IsBusyNoLock() const {
130 lock().AssertAcquired();
131 return data_pipe_
->ConsumerIsBusy();
134 } // namespace system