Home | History | Annotate | Download | only in core
      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 #ifndef MOJO_CORE_DATA_PIPE_PRODUCER_DISPATCHER_H_
      6 #define MOJO_CORE_DATA_PIPE_PRODUCER_DISPATCHER_H_
      7 
      8 #include <stddef.h>
      9 #include <stdint.h>
     10 
     11 #include <memory>
     12 
     13 #include "base/macros.h"
     14 #include "base/memory/ref_counted.h"
     15 #include "base/memory/shared_memory_mapping.h"
     16 #include "base/memory/unsafe_shared_memory_region.h"
     17 #include "base/synchronization/lock.h"
     18 #include "mojo/core/dispatcher.h"
     19 #include "mojo/core/ports/port_ref.h"
     20 #include "mojo/core/system_impl_export.h"
     21 #include "mojo/core/watcher_set.h"
     22 
     23 namespace mojo {
     24 namespace core {
     25 
     26 class NodeController;
     27 
     28 // This is the Dispatcher implementation for the producer handle for data
     29 // pipes created by the Mojo primitive MojoCreateDataPipe(). This class is
     30 // thread-safe.
     31 class MOJO_SYSTEM_IMPL_EXPORT DataPipeProducerDispatcher final
     32     : public Dispatcher {
     33  public:
     34   static scoped_refptr<DataPipeProducerDispatcher> Create(
     35       NodeController* node_controller,
     36       const ports::PortRef& control_port,
     37       base::UnsafeSharedMemoryRegion shared_ring_buffer,
     38       const MojoCreateDataPipeOptions& options,
     39       uint64_t pipe_id);
     40 
     41   // Dispatcher:
     42   Type GetType() const override;
     43   MojoResult Close() override;
     44   MojoResult WriteData(const void* elements,
     45                        uint32_t* num_bytes,
     46                        const MojoWriteDataOptions& options) override;
     47   MojoResult BeginWriteData(void** buffer, uint32_t* buffer_num_bytes) override;
     48   MojoResult EndWriteData(uint32_t num_bytes_written) override;
     49   HandleSignalsState GetHandleSignalsState() const override;
     50   MojoResult AddWatcherRef(const scoped_refptr<WatcherDispatcher>& watcher,
     51                            uintptr_t context) override;
     52   MojoResult RemoveWatcherRef(WatcherDispatcher* watcher,
     53                               uintptr_t context) override;
     54   void StartSerialize(uint32_t* num_bytes,
     55                       uint32_t* num_ports,
     56                       uint32_t* num_handles) override;
     57   bool EndSerialize(void* destination,
     58                     ports::PortName* ports,
     59                     PlatformHandle* handles) override;
     60   bool BeginTransit() override;
     61   void CompleteTransitAndClose() override;
     62   void CancelTransit() override;
     63 
     64   static scoped_refptr<DataPipeProducerDispatcher> Deserialize(
     65       const void* data,
     66       size_t num_bytes,
     67       const ports::PortName* ports,
     68       size_t num_ports,
     69       PlatformHandle* handles,
     70       size_t num_handles);
     71 
     72  private:
     73   class PortObserverThunk;
     74   friend class PortObserverThunk;
     75 
     76   DataPipeProducerDispatcher(NodeController* node_controller,
     77                              const ports::PortRef& port,
     78                              base::UnsafeSharedMemoryRegion shared_ring_buffer,
     79                              const MojoCreateDataPipeOptions& options,
     80                              uint64_t pipe_id);
     81   ~DataPipeProducerDispatcher() override;
     82 
     83   bool InitializeNoLock();
     84   MojoResult CloseNoLock();
     85   HandleSignalsState GetHandleSignalsStateNoLock() const;
     86   void NotifyWrite(uint32_t num_bytes);
     87   void OnPortStatusChanged();
     88   void UpdateSignalsStateNoLock();
     89 
     90   const MojoCreateDataPipeOptions options_;
     91   NodeController* const node_controller_;
     92   const ports::PortRef control_port_;
     93   const uint64_t pipe_id_;
     94 
     95   // Guards access to the fields below.
     96   mutable base::Lock lock_;
     97 
     98   WatcherSet watchers_;
     99 
    100   base::UnsafeSharedMemoryRegion shared_ring_buffer_;
    101   base::WritableSharedMemoryMapping ring_buffer_mapping_;
    102 
    103   bool in_transit_ = false;
    104   bool is_closed_ = false;
    105   bool peer_closed_ = false;
    106   bool peer_remote_ = false;
    107   bool transferred_ = false;
    108   bool in_two_phase_write_ = false;
    109 
    110   uint32_t write_offset_ = 0;
    111   uint32_t available_capacity_;
    112 
    113   DISALLOW_COPY_AND_ASSIGN(DataPipeProducerDispatcher);
    114 };
    115 
    116 }  // namespace core
    117 }  // namespace mojo
    118 
    119 #endif  // MOJO_CORE_DATA_PIPE_PRODUCER_DISPATCHER_H_
    120