| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MOJO_EDK_SYSTEM_DATA_PIPE_PRODUCER_DISPATCHER_H_ | 5 #ifndef MOJO_EDK_SYSTEM_DATA_PIPE_PRODUCER_DISPATCHER_H_ |
| 6 #define MOJO_EDK_SYSTEM_DATA_PIPE_PRODUCER_DISPATCHER_H_ | 6 #define MOJO_EDK_SYSTEM_DATA_PIPE_PRODUCER_DISPATCHER_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/synchronization/lock.h" |
| 15 #include "mojo/edk/embedder/platform_handle_vector.h" |
| 16 #include "mojo/edk/embedder/platform_shared_buffer.h" |
| 12 #include "mojo/edk/system/awakable_list.h" | 17 #include "mojo/edk/system/awakable_list.h" |
| 13 #include "mojo/edk/system/dispatcher.h" | 18 #include "mojo/edk/system/dispatcher.h" |
| 14 #include "mojo/edk/system/raw_channel.h" | 19 #include "mojo/edk/system/ports/port_ref.h" |
| 15 #include "mojo/edk/system/system_impl_export.h" | 20 #include "mojo/edk/system/system_impl_export.h" |
| 16 #include "mojo/public/cpp/system/macros.h" | |
| 17 | 21 |
| 18 namespace mojo { | 22 namespace mojo { |
| 19 namespace edk { | 23 namespace edk { |
| 20 | 24 |
| 21 // This is the |Dispatcher| implementation for the producer handle for data | 25 struct DataPipeControlMessage; |
| 22 // pipes (created by the Mojo primitive |MojoCreateDataPipe()|). This class is | 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 |
| 23 // thread-safe. | 30 // thread-safe. |
| 24 class MOJO_SYSTEM_IMPL_EXPORT DataPipeProducerDispatcher final | 31 class MOJO_SYSTEM_IMPL_EXPORT DataPipeProducerDispatcher final |
| 25 : public Dispatcher, public RawChannel::Delegate { | 32 : public Dispatcher { |
| 26 public: | 33 public: |
| 27 static scoped_refptr<DataPipeProducerDispatcher> Create( | 34 DataPipeProducerDispatcher( |
| 28 const MojoCreateDataPipeOptions& options) { | 35 NodeController* node_controller, |
| 29 return make_scoped_refptr(new DataPipeProducerDispatcher(options)); | 36 const ports::PortRef& port, |
| 30 } | 37 scoped_refptr<PlatformSharedBuffer> shared_ring_buffer, |
| 38 const MojoCreateDataPipeOptions& options, |
| 39 bool initialized, |
| 40 uint64_t pipe_id); |
| 31 | 41 |
| 32 // Must be called before any other methods. | 42 // Dispatcher: |
| 33 void Init(ScopedPlatformHandle message_pipe, | 43 Type GetType() const override; |
| 34 char* serialized_write_buffer, size_t serialized_write_buffer_size); | 44 MojoResult Close() override; |
| 45 MojoResult WriteData(const void* elements, |
| 46 uint32_t* num_bytes, |
| 47 MojoReadDataFlags flags) override; |
| 48 MojoResult BeginWriteData(void** buffer, |
| 49 uint32_t* buffer_num_bytes, |
| 50 MojoWriteDataFlags flags) override; |
| 51 MojoResult EndWriteData(uint32_t num_bytes_written) override; |
| 52 HandleSignalsState GetHandleSignalsState() const override; |
| 53 MojoResult AddAwakable(Awakable* awakable, |
| 54 MojoHandleSignals signals, |
| 55 uintptr_t context, |
| 56 HandleSignalsState* signals_state) override; |
| 57 void RemoveAwakable(Awakable* awakable, |
| 58 HandleSignalsState* signals_state) override; |
| 59 void StartSerialize(uint32_t* num_bytes, |
| 60 uint32_t* num_ports, |
| 61 uint32_t* num_handles) override; |
| 62 bool EndSerialize(void* destination, |
| 63 ports::PortName* ports, |
| 64 PlatformHandle* handles) override; |
| 65 bool BeginTransit() override; |
| 66 void CompleteTransitAndClose() override; |
| 67 void CancelTransit() override; |
| 35 | 68 |
| 36 // |Dispatcher| public methods: | |
| 37 Type GetType() const override; | |
| 38 | |
| 39 // The "opposite" of |SerializeAndClose()|. (Typically this is called by | |
| 40 // |Dispatcher::Deserialize()|.) | |
| 41 static scoped_refptr<DataPipeProducerDispatcher> | 69 static scoped_refptr<DataPipeProducerDispatcher> |
| 42 Deserialize(const void* source, | 70 Deserialize(const void* data, |
| 43 size_t size, | 71 size_t num_bytes, |
| 44 PlatformHandleVector* platform_handles); | 72 const ports::PortName* ports, |
| 73 size_t num_ports, |
| 74 PlatformHandle* handles, |
| 75 size_t num_handles); |
| 45 | 76 |
| 46 private: | 77 private: |
| 47 DataPipeProducerDispatcher(const MojoCreateDataPipeOptions& options); | 78 class PortObserverThunk; |
| 79 friend class PortObserverThunk; |
| 80 |
| 48 ~DataPipeProducerDispatcher() override; | 81 ~DataPipeProducerDispatcher() override; |
| 49 | 82 |
| 50 void InitOnIO(); | 83 void OnSharedBufferCreated(const scoped_refptr<PlatformSharedBuffer>& buffer); |
| 51 void CloseOnIO(); | 84 void InitializeNoLock(); |
| 85 MojoResult CloseNoLock(); |
| 86 HandleSignalsState GetHandleSignalsStateNoLock() const; |
| 87 void NotifyWrite(uint32_t num_bytes); |
| 88 void OnPortStatusChanged(); |
| 89 void UpdateSignalsStateNoLock(); |
| 90 bool ProcessMessageNoLock(const DataPipeControlMessage& message, |
| 91 ScopedPlatformHandleVectorPtr handles); |
| 52 | 92 |
| 53 // |Dispatcher| protected methods: | 93 const MojoCreateDataPipeOptions options_; |
| 54 void CancelAllAwakablesNoLock() override; | 94 NodeController* const node_controller_; |
| 55 void CloseImplNoLock() override; | 95 const ports::PortRef control_port_; |
| 56 scoped_refptr<Dispatcher> CreateEquivalentDispatcherAndCloseImplNoLock() | 96 const uint64_t pipe_id_; |
| 57 override; | |
| 58 MojoResult WriteDataImplNoLock(const void* elements, | |
| 59 uint32_t* num_bytes, | |
| 60 MojoWriteDataFlags flags) override; | |
| 61 MojoResult BeginWriteDataImplNoLock(void** buffer, | |
| 62 uint32_t* buffer_num_bytes, | |
| 63 MojoWriteDataFlags flags) override; | |
| 64 MojoResult EndWriteDataImplNoLock(uint32_t num_bytes_written) override; | |
| 65 HandleSignalsState GetHandleSignalsStateImplNoLock() const override; | |
| 66 MojoResult AddAwakableImplNoLock(Awakable* awakable, | |
| 67 MojoHandleSignals signals, | |
| 68 uintptr_t context, | |
| 69 HandleSignalsState* signals_state) override; | |
| 70 void RemoveAwakableImplNoLock(Awakable* awakable, | |
| 71 HandleSignalsState* signals_state) override; | |
| 72 void StartSerializeImplNoLock(size_t* max_size, | |
| 73 size_t* max_platform_handles) override; | |
| 74 bool EndSerializeAndCloseImplNoLock( | |
| 75 void* destination, | |
| 76 size_t* actual_size, | |
| 77 PlatformHandleVector* platform_handles) override; | |
| 78 void TransportStarted() override; | |
| 79 void TransportEnded() override; | |
| 80 bool IsBusyNoLock() const override; | |
| 81 | 97 |
| 82 // |RawChannel::Delegate methods: | 98 // Guards access to the fields below. |
| 83 void OnReadMessage( | 99 mutable base::Lock lock_; |
| 84 const MessageInTransit::View& message_view, | |
| 85 ScopedPlatformHandleVectorPtr platform_handles) override; | |
| 86 void OnError(Error error) override; | |
| 87 | |
| 88 bool InTwoPhaseWrite() const; | |
| 89 bool WriteDataIntoMessages(const void* elements, uint32_t num_bytes); | |
| 90 | |
| 91 // See comment in MessagePipeDispatcher for this method. | |
| 92 void SerializeInternal(); | |
| 93 | |
| 94 MojoCreateDataPipeOptions options_; | |
| 95 | |
| 96 // Protected by |lock()|: | |
| 97 RawChannel* channel_; // This will be null if closed. | |
| 98 | 100 |
| 99 AwakableList awakable_list_; | 101 AwakableList awakable_list_; |
| 100 | 102 |
| 101 // If DispatcherTransport is created. Must be set before lock() is called to | 103 bool buffer_requested_ = false; |
| 102 // avoid deadlocks with RawChannel calling us. | |
| 103 base::Lock started_transport_; | |
| 104 | 104 |
| 105 bool error_; | 105 scoped_refptr<PlatformSharedBuffer> shared_ring_buffer_; |
| 106 scoped_ptr<PlatformSharedBufferMapping> ring_buffer_mapping_; |
| 107 ScopedPlatformHandle buffer_handle_for_transit_; |
| 106 | 108 |
| 107 bool serialized_; | 109 bool in_transit_ = false; |
| 108 ScopedPlatformHandle serialized_platform_handle_; | 110 bool is_closed_ = false; |
| 109 std::vector<char> serialized_write_buffer_; | 111 bool peer_closed_ = false; |
| 110 std::vector<char> two_phase_data_; | 112 bool transferred_ = false; |
| 113 bool in_two_phase_write_ = false; |
| 111 | 114 |
| 112 MOJO_DISALLOW_COPY_AND_ASSIGN(DataPipeProducerDispatcher); | 115 uint32_t write_offset_ = 0; |
| 116 uint32_t available_capacity_; |
| 117 |
| 118 DISALLOW_COPY_AND_ASSIGN(DataPipeProducerDispatcher); |
| 113 }; | 119 }; |
| 114 | 120 |
| 115 } // namespace edk | 121 } // namespace edk |
| 116 } // namespace mojo | 122 } // namespace mojo |
| 117 | 123 |
| 118 #endif // MOJO_EDK_SYSTEM_DATA_PIPE_PRODUCER_DISPATCHER_H_ | 124 #endif // MOJO_EDK_SYSTEM_DATA_PIPE_PRODUCER_DISPATCHER_H_ |
| OLD | NEW |