Home | History | Annotate | Download | only in system
      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.
      4 
      5 #include "mojo/system/data_pipe_producer_dispatcher.h"
      6 
      7 #include "base/logging.h"
      8 #include "mojo/system/data_pipe.h"
      9 #include "mojo/system/memory.h"
     10 
     11 namespace mojo {
     12 namespace system {
     13 
     14 DataPipeProducerDispatcher::DataPipeProducerDispatcher() {
     15 }
     16 
     17 void DataPipeProducerDispatcher::Init(scoped_refptr<DataPipe> data_pipe) {
     18   DCHECK(data_pipe.get());
     19   data_pipe_ = data_pipe;
     20 }
     21 
     22 DataPipeProducerDispatcher::~DataPipeProducerDispatcher() {
     23   // |Close()|/|CloseImplNoLock()| should have taken care of the pipe.
     24   DCHECK(!data_pipe_.get());
     25 }
     26 
     27 void DataPipeProducerDispatcher::CancelAllWaitersNoLock() {
     28   lock().AssertAcquired();
     29   data_pipe_->ProducerCancelAllWaiters();
     30 }
     31 
     32 MojoResult DataPipeProducerDispatcher::CloseImplNoLock() {
     33   lock().AssertAcquired();
     34   data_pipe_->ProducerClose();
     35   data_pipe_ = NULL;
     36   return MOJO_RESULT_OK;
     37 }
     38 
     39 MojoResult DataPipeProducerDispatcher::WriteDataImplNoLock(
     40     const void* elements,
     41     uint32_t* num_elements,
     42     MojoWriteDataFlags flags) {
     43   lock().AssertAcquired();
     44 
     45   if (!VerifyUserPointer<uint32_t>(num_elements, 1))
     46     return MOJO_RESULT_INVALID_ARGUMENT;
     47   if (!VerifyUserPointerForSize(elements, data_pipe_->element_size(),
     48                                 *num_elements))
     49     return MOJO_RESULT_INVALID_ARGUMENT;
     50 
     51   return data_pipe_->ProducerWriteData(elements, num_elements, flags);
     52 }
     53 
     54 MojoResult DataPipeProducerDispatcher::BeginWriteDataImplNoLock(
     55     void** buffer,
     56     uint32_t* buffer_num_elements,
     57     MojoWriteDataFlags flags) {
     58   lock().AssertAcquired();
     59 
     60   if (!VerifyUserPointer<void*>(buffer, 1))
     61     return MOJO_RESULT_INVALID_ARGUMENT;
     62   if (!VerifyUserPointer<uint32_t>(buffer_num_elements, 1))
     63     return MOJO_RESULT_INVALID_ARGUMENT;
     64 
     65   return data_pipe_->ProducerBeginWriteData(buffer, buffer_num_elements, flags);
     66 }
     67 
     68 MojoResult DataPipeProducerDispatcher::EndWriteDataImplNoLock(
     69     uint32_t num_elements_written) {
     70   lock().AssertAcquired();
     71 
     72   return data_pipe_->ProducerEndWriteData(num_elements_written);
     73 }
     74 
     75 MojoResult DataPipeProducerDispatcher::AddWaiterImplNoLock(
     76     Waiter* waiter,
     77     MojoWaitFlags flags,
     78     MojoResult wake_result) {
     79   lock().AssertAcquired();
     80   return data_pipe_->ProducerAddWaiter(waiter, flags, wake_result);
     81 }
     82 
     83 void DataPipeProducerDispatcher::RemoveWaiterImplNoLock(Waiter* waiter) {
     84   lock().AssertAcquired();
     85   data_pipe_->ProducerRemoveWaiter(waiter);
     86 }
     87 
     88 scoped_refptr<Dispatcher>
     89 DataPipeProducerDispatcher::CreateEquivalentDispatcherAndCloseImplNoLock() {
     90   lock().AssertAcquired();
     91 
     92   scoped_refptr<DataPipeProducerDispatcher> rv =
     93       new DataPipeProducerDispatcher();
     94   rv->Init(data_pipe_);
     95   data_pipe_ = NULL;
     96   return scoped_refptr<Dispatcher>(rv.get());
     97 }
     98 
     99 }  // namespace system
    100 }  // namespace mojo
    101