jam | e76324e | 2015-10-03 06:01:28 +0900 | [diff] [blame] | 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 | |
Avi Drissman | 20b0cb0 | 2015-12-22 03:14:57 +0900 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
rockot | c019599 | 2016-01-27 04:23:21 +0900 | [diff] [blame] | 11 | #include "base/macros.h" |
jam | e76324e | 2015-10-03 06:01:28 +0900 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
rockot | c019599 | 2016-01-27 04:23:21 +0900 | [diff] [blame] | 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" |
| 17 | #include "mojo/edk/embedder/scoped_platform_handle.h" |
jam | e76324e | 2015-10-03 06:01:28 +0900 | [diff] [blame] | 18 | #include "mojo/edk/system/awakable_list.h" |
| 19 | #include "mojo/edk/system/dispatcher.h" |
rockot | c019599 | 2016-01-27 04:23:21 +0900 | [diff] [blame] | 20 | #include "mojo/edk/system/ports/port_ref.h" |
jam | e76324e | 2015-10-03 06:01:28 +0900 | [diff] [blame] | 21 | #include "mojo/edk/system/system_impl_export.h" |
jam | e76324e | 2015-10-03 06:01:28 +0900 | [diff] [blame] | 22 | |
| 23 | namespace mojo { |
| 24 | namespace edk { |
| 25 | |
rockot | c019599 | 2016-01-27 04:23:21 +0900 | [diff] [blame] | 26 | struct DataPipeControlMessage; |
| 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 |
jam | e76324e | 2015-10-03 06:01:28 +0900 | [diff] [blame] | 31 | // thread-safe. |
| 32 | class MOJO_SYSTEM_IMPL_EXPORT DataPipeConsumerDispatcher final |
rockot | c019599 | 2016-01-27 04:23:21 +0900 | [diff] [blame] | 33 | : public Dispatcher { |
jam | e76324e | 2015-10-03 06:01:28 +0900 | [diff] [blame] | 34 | public: |
rockot | c019599 | 2016-01-27 04:23:21 +0900 | [diff] [blame] | 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); |
jam | e76324e | 2015-10-03 06:01:28 +0900 | [diff] [blame] | 42 | |
rockot | c019599 | 2016-01-27 04:23:21 +0900 | [diff] [blame] | 43 | // Dispatcher: |
jam | e76324e | 2015-10-03 06:01:28 +0900 | [diff] [blame] | 44 | Type GetType() const override; |
rockot | c019599 | 2016-01-27 04:23:21 +0900 | [diff] [blame] | 45 | MojoResult Close() override; |
rockot | 4b47271 | 2016-03-02 12:46:37 +0900 | [diff] [blame^] | 46 | MojoResult Watch(MojoHandleSignals signals, |
| 47 | const Watcher::WatchCallback& callback, |
| 48 | uintptr_t context) override; |
| 49 | MojoResult CancelWatch(uintptr_t context) override; |
rockot | c019599 | 2016-01-27 04:23:21 +0900 | [diff] [blame] | 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; |
jam | e76324e | 2015-10-03 06:01:28 +0900 | [diff] [blame] | 73 | |
jam | e76324e | 2015-10-03 06:01:28 +0900 | [diff] [blame] | 74 | static scoped_refptr<DataPipeConsumerDispatcher> |
rockot | c019599 | 2016-01-27 04:23:21 +0900 | [diff] [blame] | 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); |
jam | e76324e | 2015-10-03 06:01:28 +0900 | [diff] [blame] | 81 | |
| 82 | private: |
rockot | c019599 | 2016-01-27 04:23:21 +0900 | [diff] [blame] | 83 | class PortObserverThunk; |
| 84 | friend class PortObserverThunk; |
| 85 | |
jam | e76324e | 2015-10-03 06:01:28 +0900 | [diff] [blame] | 86 | ~DataPipeConsumerDispatcher() override; |
| 87 | |
rockot | c019599 | 2016-01-27 04:23:21 +0900 | [diff] [blame] | 88 | void InitializeNoLock(); |
| 89 | MojoResult CloseNoLock(); |
| 90 | HandleSignalsState GetHandleSignalsStateNoLock() const; |
| 91 | void NotifyRead(uint32_t num_bytes); |
| 92 | void OnPortStatusChanged(); |
| 93 | void UpdateSignalsStateNoLock(); |
jam | e76324e | 2015-10-03 06:01:28 +0900 | [diff] [blame] | 94 | |
rockot | c019599 | 2016-01-27 04:23:21 +0900 | [diff] [blame] | 95 | const MojoCreateDataPipeOptions options_; |
| 96 | NodeController* const node_controller_; |
| 97 | const ports::PortRef control_port_; |
| 98 | const uint64_t pipe_id_; |
jam | e76324e | 2015-10-03 06:01:28 +0900 | [diff] [blame] | 99 | |
rockot | c019599 | 2016-01-27 04:23:21 +0900 | [diff] [blame] | 100 | // Guards access to the fields below. |
| 101 | mutable base::Lock lock_; |
jam | e76324e | 2015-10-03 06:01:28 +0900 | [diff] [blame] | 102 | |
jam | e76324e | 2015-10-03 06:01:28 +0900 | [diff] [blame] | 103 | AwakableList awakable_list_; |
| 104 | |
rockot | c019599 | 2016-01-27 04:23:21 +0900 | [diff] [blame] | 105 | scoped_refptr<PlatformSharedBuffer> shared_ring_buffer_; |
| 106 | scoped_ptr<PlatformSharedBufferMapping> ring_buffer_mapping_; |
| 107 | ScopedPlatformHandle buffer_handle_for_transit_; |
jam | e76324e | 2015-10-03 06:01:28 +0900 | [diff] [blame] | 108 | |
rockot | c019599 | 2016-01-27 04:23:21 +0900 | [diff] [blame] | 109 | bool in_two_phase_read_ = false; |
| 110 | uint32_t two_phase_max_bytes_read_ = 0; |
jam | e76324e | 2015-10-03 06:01:28 +0900 | [diff] [blame] | 111 | |
rockot | c019599 | 2016-01-27 04:23:21 +0900 | [diff] [blame] | 112 | bool in_transit_ = false; |
| 113 | bool is_closed_ = false; |
| 114 | bool peer_closed_ = false; |
| 115 | bool transferred_ = false; |
jam | e76324e | 2015-10-03 06:01:28 +0900 | [diff] [blame] | 116 | |
rockot | c019599 | 2016-01-27 04:23:21 +0900 | [diff] [blame] | 117 | uint32_t read_offset_ = 0; |
| 118 | uint32_t bytes_available_ = 0; |
jam | e76324e | 2015-10-03 06:01:28 +0900 | [diff] [blame] | 119 | |
rockot | c019599 | 2016-01-27 04:23:21 +0900 | [diff] [blame] | 120 | DISALLOW_COPY_AND_ASSIGN(DataPipeConsumerDispatcher); |
jam | e76324e | 2015-10-03 06:01:28 +0900 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | } // namespace edk |
| 124 | } // namespace mojo |
| 125 | |
| 126 | #endif // MOJO_EDK_SYSTEM_DATA_PIPE_CONSUMER_DISPATCHER_H_ |