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_EDK_SYSTEM_DATA_PIPE_CONSUMER_DISPATCHER_H_ 6 #define MOJO_EDK_SYSTEM_DATA_PIPE_CONSUMER_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/synchronization/lock.h" 16 #include "mojo/edk/embedder/platform_handle_vector.h" 17 #include "mojo/edk/embedder/platform_shared_buffer.h" 18 #include "mojo/edk/embedder/scoped_platform_handle.h" 19 #include "mojo/edk/system/awakable_list.h" 20 #include "mojo/edk/system/dispatcher.h" 21 #include "mojo/edk/system/ports/port_ref.h" 22 #include "mojo/edk/system/system_impl_export.h" 23 24 namespace mojo { 25 namespace edk { 26 27 class NodeController; 28 29 // This is the Dispatcher implementation for the consumer handle for data 30 // pipes created by the Mojo primitive MojoCreateDataPipe(). This class is 31 // thread-safe. 32 class MOJO_SYSTEM_IMPL_EXPORT DataPipeConsumerDispatcher final 33 : public Dispatcher { 34 public: 35 DataPipeConsumerDispatcher( 36 NodeController* node_controller, 37 const ports::PortRef& control_port, 38 scoped_refptr<PlatformSharedBuffer> shared_ring_buffer, 39 const MojoCreateDataPipeOptions& options, 40 bool initialized, 41 uint64_t pipe_id); 42 43 // Dispatcher: 44 Type GetType() const override; 45 MojoResult Close() override; 46 MojoResult Watch(MojoHandleSignals signals, 47 const Watcher::WatchCallback& callback, 48 uintptr_t context) override; 49 MojoResult CancelWatch(uintptr_t context) override; 50 MojoResult ReadData(void* elements, 51 uint32_t* num_bytes, 52 MojoReadDataFlags flags) override; 53 MojoResult BeginReadData(const void** buffer, 54 uint32_t* buffer_num_bytes, 55 MojoReadDataFlags flags) override; 56 MojoResult EndReadData(uint32_t num_bytes_read) override; 57 HandleSignalsState GetHandleSignalsState() const override; 58 MojoResult AddAwakable(Awakable* awakable, 59 MojoHandleSignals signals, 60 uintptr_t context, 61 HandleSignalsState* signals_state) override; 62 void RemoveAwakable(Awakable* awakable, 63 HandleSignalsState* signals_state) override; 64 void StartSerialize(uint32_t* num_bytes, 65 uint32_t* num_ports, 66 uint32_t* num_handles) override; 67 bool EndSerialize(void* destination, 68 ports::PortName* ports, 69 PlatformHandle* handles) override; 70 bool BeginTransit() override; 71 void CompleteTransitAndClose() override; 72 void CancelTransit() override; 73 74 static scoped_refptr<DataPipeConsumerDispatcher> 75 Deserialize(const void* data, 76 size_t num_bytes, 77 const ports::PortName* ports, 78 size_t num_ports, 79 PlatformHandle* handles, 80 size_t num_handles); 81 82 private: 83 class PortObserverThunk; 84 friend class PortObserverThunk; 85 86 ~DataPipeConsumerDispatcher() override; 87 88 void InitializeNoLock(); 89 MojoResult CloseNoLock(); 90 HandleSignalsState GetHandleSignalsStateNoLock() const; 91 void NotifyRead(uint32_t num_bytes); 92 void OnPortStatusChanged(); 93 void UpdateSignalsStateNoLock(); 94 95 const MojoCreateDataPipeOptions options_; 96 NodeController* const node_controller_; 97 const ports::PortRef control_port_; 98 const uint64_t pipe_id_; 99 100 // Guards access to the fields below. 101 mutable base::Lock lock_; 102 103 AwakableList awakable_list_; 104 105 scoped_refptr<PlatformSharedBuffer> shared_ring_buffer_; 106 std::unique_ptr<PlatformSharedBufferMapping> ring_buffer_mapping_; 107 ScopedPlatformHandle buffer_handle_for_transit_; 108 109 bool in_two_phase_read_ = false; 110 uint32_t two_phase_max_bytes_read_ = 0; 111 112 bool in_transit_ = false; 113 bool is_closed_ = false; 114 bool peer_closed_ = false; 115 bool transferred_ = false; 116 117 uint32_t read_offset_ = 0; 118 uint32_t bytes_available_ = 0; 119 120 // Indicates whether any new data is available since the last read attempt. 121 bool new_data_available_ = false; 122 123 DISALLOW_COPY_AND_ASSIGN(DataPipeConsumerDispatcher); 124 }; 125 126 } // namespace edk 127 } // namespace mojo 128 129 #endif // MOJO_EDK_SYSTEM_DATA_PIPE_CONSUMER_DISPATCHER_H_ 130