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/system/data_pipe_consumer_dispatcher.h"
7 #include "base/logging.h"
8 #include "mojo/system/data_pipe.h"
9 #include "mojo/system/memory.h"
14 DataPipeConsumerDispatcher::DataPipeConsumerDispatcher() {
17 void DataPipeConsumerDispatcher::Init(scoped_refptr
<DataPipe
> data_pipe
) {
18 DCHECK(data_pipe
.get());
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.
28 DCHECK(!data_pipe_
.get());
31 void DataPipeConsumerDispatcher::CancelAllWaitersNoLock() {
32 lock().AssertAcquired();
33 data_pipe_
->ConsumerCancelAllWaiters();
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 return MOJO_RESULT_INVALID_ARGUMENT
;
63 DVLOG_IF(2, !elements
.IsNull())
64 << "Discard mode: ignoring non-null |elements|";
65 return data_pipe_
->ConsumerDiscardData(
66 num_bytes
, (flags
& MOJO_READ_DATA_FLAG_ALL_OR_NONE
));
69 if ((flags
& MOJO_READ_DATA_FLAG_QUERY
)) {
70 DCHECK(!(flags
& MOJO_READ_DATA_FLAG_DISCARD
)); // Handled above.
71 DVLOG_IF(2, !elements
.IsNull())
72 << "Query mode: ignoring non-null |elements|";
73 return data_pipe_
->ConsumerQueryData(num_bytes
);
76 return data_pipe_
->ConsumerReadData(
77 elements
, num_bytes
, (flags
& MOJO_READ_DATA_FLAG_ALL_OR_NONE
));
80 MojoResult
DataPipeConsumerDispatcher::BeginReadDataImplNoLock(
81 UserPointer
<const void*> buffer
,
82 UserPointer
<uint32_t> buffer_num_bytes
,
83 MojoReadDataFlags flags
) {
84 lock().AssertAcquired();
86 // These flags may not be used in two-phase mode.
87 if ((flags
& MOJO_READ_DATA_FLAG_DISCARD
) ||
88 (flags
& MOJO_READ_DATA_FLAG_QUERY
))
89 return MOJO_RESULT_INVALID_ARGUMENT
;
91 return data_pipe_
->ConsumerBeginReadData(
92 buffer
, buffer_num_bytes
, (flags
& MOJO_READ_DATA_FLAG_ALL_OR_NONE
));
95 MojoResult
DataPipeConsumerDispatcher::EndReadDataImplNoLock(
96 uint32_t num_bytes_read
) {
97 lock().AssertAcquired();
99 return data_pipe_
->ConsumerEndReadData(num_bytes_read
);
102 HandleSignalsState
DataPipeConsumerDispatcher::GetHandleSignalsStateImplNoLock()
104 lock().AssertAcquired();
105 return data_pipe_
->ConsumerGetHandleSignalsState();
108 MojoResult
DataPipeConsumerDispatcher::AddWaiterImplNoLock(
110 MojoHandleSignals signals
,
112 HandleSignalsState
* signals_state
) {
113 lock().AssertAcquired();
114 return data_pipe_
->ConsumerAddWaiter(waiter
, signals
, context
, signals_state
);
117 void DataPipeConsumerDispatcher::RemoveWaiterImplNoLock(
119 HandleSignalsState
* signals_state
) {
120 lock().AssertAcquired();
121 data_pipe_
->ConsumerRemoveWaiter(waiter
, signals_state
);
124 bool DataPipeConsumerDispatcher::IsBusyNoLock() const {
125 lock().AssertAcquired();
126 return data_pipe_
->ConsumerIsBusy();
129 } // namespace system