data_pipe_producer_dispatcher.h 4.0 KB

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