Avi Drissman | d387f092 | 2022-09-14 20:51:31 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Ken Rockot | dba46db | 2018-07-04 18:41:04 | [diff] [blame] | 5 | #ifndef MOJO_CORE_MESSAGE_PIPE_DISPATCHER_H_ |
| 6 | #define MOJO_CORE_MESSAGE_PIPE_DISPATCHER_H_ |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 7 | |
Avi Drissman | 2e88ac37 | 2015-12-21 18:14:57 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
dcheng | 40c732a8 | 2016-04-22 00:57:38 | [diff] [blame] | 10 | #include <memory> |
Ken Rockot | 602825e | 2024-01-24 19:35:43 | [diff] [blame] | 11 | #include <optional> |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 12 | |
Keishi Hattori | 488b760 | 2022-05-02 13:09:31 | [diff] [blame] | 13 | #include "base/memory/raw_ptr_exclusion.h" |
Ken Rockot | dba46db | 2018-07-04 18:41:04 | [diff] [blame] | 14 | #include "mojo/core/atomic_flag.h" |
| 15 | #include "mojo/core/dispatcher.h" |
| 16 | #include "mojo/core/ports/port_ref.h" |
| 17 | #include "mojo/core/watcher_set.h" |
jam | b6e0981a | 2015-12-09 01:54:59 | [diff] [blame] | 18 | |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 19 | namespace mojo { |
Ken Rockot | dba46db | 2018-07-04 18:41:04 | [diff] [blame] | 20 | namespace core { |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 21 | |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 22 | class NodeController; |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 23 | |
| 24 | class MessagePipeDispatcher : public Dispatcher { |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 25 | public: |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 26 | // Constructs a MessagePipeDispatcher permanently tied to a specific port. |
| 27 | // |connected| must indicate the state of the port at construction time; if |
| 28 | // the port is initialized with a peer, |connected| must be true. Otherwise it |
| 29 | // must be false. |
| 30 | // |
| 31 | // A MessagePipeDispatcher may not be transferred while in a disconnected |
| 32 | // state, and one can never return to a disconnected once connected. |
| 33 | // |
| 34 | // |pipe_id| is a unique identifier which can be used to track pipe endpoints |
| 35 | // as they're passed around. |endpoint| is either 0 or 1 and again is only |
| 36 | // used for tracking pipes (one side is always 0, the other is always 1.) |
| 37 | MessagePipeDispatcher(NodeController* node_controller, |
| 38 | const ports::PortRef& port, |
| 39 | uint64_t pipe_id, |
| 40 | int endpoint); |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 41 | |
Peter Boström | feef05a | 2021-10-05 21:35:08 | [diff] [blame] | 42 | MessagePipeDispatcher(const MessagePipeDispatcher&) = delete; |
| 43 | MessagePipeDispatcher& operator=(const MessagePipeDispatcher&) = delete; |
| 44 | |
rockot | b1e74df | 2016-03-14 13:18:30 | [diff] [blame] | 45 | // Fuses this pipe with |other|. Returns |true| on success or |false| on |
| 46 | // failure. Regardless of the return value, both dispatchers are closed by |
| 47 | // this call. |
| 48 | bool Fuse(MessagePipeDispatcher* other); |
| 49 | |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 50 | // Dispatcher: |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 51 | Type GetType() const override; |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 52 | MojoResult Close() override; |
Ken Rockot | c12080c | 2018-05-03 18:10:21 | [diff] [blame] | 53 | MojoResult WriteMessage( |
| 54 | std::unique_ptr<ports::UserMessageEvent> message) override; |
Ken Rockot | 349df72 | 2017-06-16 05:48:49 | [diff] [blame] | 55 | MojoResult ReadMessage( |
| 56 | std::unique_ptr<ports::UserMessageEvent>* message) override; |
Ken Rockot | 0db0bcf | 2018-07-12 22:01:12 | [diff] [blame] | 57 | MojoResult SetQuota(MojoQuotaType type, uint64_t limit) override; |
| 58 | MojoResult QueryQuota(MojoQuotaType type, |
| 59 | uint64_t* limit, |
| 60 | uint64_t* usage) override; |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 61 | HandleSignalsState GetHandleSignalsState() const override; |
rockot | 9eadaba | 2017-03-15 23:57:47 | [diff] [blame] | 62 | MojoResult AddWatcherRef(const scoped_refptr<WatcherDispatcher>& watcher, |
| 63 | uintptr_t context) override; |
| 64 | MojoResult RemoveWatcherRef(WatcherDispatcher* watcher, |
| 65 | uintptr_t context) override; |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 66 | void StartSerialize(uint32_t* num_bytes, |
| 67 | uint32_t* num_ports, |
| 68 | uint32_t* num_handles) override; |
| 69 | bool EndSerialize(void* destination, |
Ken Rockot | 863f348 | 2019-04-17 17:47:50 | [diff] [blame] | 70 | ports::PortName* ports, |
Ken Rockot | 29b0a32 | 2018-06-29 17:38:07 | [diff] [blame] | 71 | PlatformHandle* handles) override; |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 72 | bool BeginTransit() override; |
| 73 | void CompleteTransitAndClose() override; |
| 74 | void CancelTransit() override; |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 75 | |
Ken Rockot | 863f348 | 2019-04-17 17:47:50 | [diff] [blame] | 76 | static scoped_refptr<Dispatcher> Deserialize(const void* data, |
| 77 | size_t num_bytes, |
| 78 | const ports::PortName* ports, |
| 79 | size_t num_ports, |
| 80 | PlatformHandle* handles, |
| 81 | size_t num_handles); |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 82 | |
| 83 | private: |
Ken Rockot | 7d291fcc | 2019-04-17 16:25:02 | [diff] [blame] | 84 | class PortObserverThunk; |
| 85 | friend class PortObserverThunk; |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 86 | |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 87 | ~MessagePipeDispatcher() override; |
| 88 | |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 89 | MojoResult CloseNoLock(); |
| 90 | HandleSignalsState GetHandleSignalsStateNoLock() const; |
Ken Rockot | 7d291fcc | 2019-04-17 16:25:02 | [diff] [blame] | 91 | void OnPortStatusChanged(); |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 92 | |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 93 | // These are safe to access from any thread without locking. |
Keishi Hattori | 7ccb88c | 2022-01-18 16:48:45 | [diff] [blame] | 94 | // `node_controller_` is not a raw_ptr<...> for performance reasons (based on |
| 95 | // analysis of sampling profiler data). |
Keishi Hattori | 488b760 | 2022-05-02 13:09:31 | [diff] [blame] | 96 | RAW_PTR_EXCLUSION NodeController* const node_controller_; |
Ken Rockot | 7d291fcc | 2019-04-17 16:25:02 | [diff] [blame] | 97 | const ports::PortRef port_; |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 98 | const uint64_t pipe_id_; |
| 99 | const int endpoint_; |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 100 | |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 101 | // Guards access to all the fields below. |
| 102 | mutable base::Lock signal_lock_; |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 103 | |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 104 | // This is not the same is |port_transferred_|. It's only held true between |
| 105 | // BeginTransit() and Complete/CancelTransit(). |
amistry | 1352e127 | 2016-04-19 01:40:41 | [diff] [blame] | 106 | AtomicFlag in_transit_; |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 107 | |
Ken Rockot | 8fad6a91 | 2019-08-02 20:20:12 | [diff] [blame] | 108 | mutable MojoHandleSignals last_known_satisfied_signals_ = 0; |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 109 | bool port_transferred_ = false; |
amistry | 1352e127 | 2016-04-19 01:40:41 | [diff] [blame] | 110 | AtomicFlag port_closed_; |
rockot | 9eadaba | 2017-03-15 23:57:47 | [diff] [blame] | 111 | WatcherSet watchers_; |
Arthur Sonzogni | 59ac822 | 2023-11-10 09:46:54 | [diff] [blame] | 112 | std::optional<uint64_t> receive_queue_length_limit_; |
| 113 | std::optional<uint64_t> receive_queue_memory_size_limit_; |
| 114 | std::optional<uint64_t> unread_message_count_limit_; |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 115 | }; |
| 116 | |
Ken Rockot | dba46db | 2018-07-04 18:41:04 | [diff] [blame] | 117 | } // namespace core |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 118 | } // namespace mojo |
| 119 | |
Ken Rockot | dba46db | 2018-07-04 18:41:04 | [diff] [blame] | 120 | #endif // MOJO_CORE_MESSAGE_PIPE_DISPATCHER_H_ |