blob: f4b1359eae6c63e308ad9879752e9cae345bd14b [file] [log] [blame]
Avi Drissmand387f0922022-09-14 20:51:311// Copyright 2015 The Chromium Authors
jam76bcf0c2015-10-02 21:01:282// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ken Rockotdba46db2018-07-04 18:41:045#ifndef MOJO_CORE_MESSAGE_PIPE_DISPATCHER_H_
6#define MOJO_CORE_MESSAGE_PIPE_DISPATCHER_H_
jam76bcf0c2015-10-02 21:01:287
Avi Drissman2e88ac372015-12-21 18:14:578#include <stdint.h>
9
dcheng40c732a82016-04-22 00:57:3810#include <memory>
Ken Rockot602825e2024-01-24 19:35:4311#include <optional>
rockotce69a042016-01-26 19:23:2112
Keishi Hattori488b7602022-05-02 13:09:3113#include "base/memory/raw_ptr_exclusion.h"
Ken Rockotdba46db2018-07-04 18:41:0414#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"
jamb6e0981a2015-12-09 01:54:5918
jam76bcf0c2015-10-02 21:01:2819namespace mojo {
Ken Rockotdba46db2018-07-04 18:41:0420namespace core {
jam76bcf0c2015-10-02 21:01:2821
rockotce69a042016-01-26 19:23:2122class NodeController;
rockotce69a042016-01-26 19:23:2123
24class MessagePipeDispatcher : public Dispatcher {
jam76bcf0c2015-10-02 21:01:2825 public:
rockotce69a042016-01-26 19:23:2126 // 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);
jam76bcf0c2015-10-02 21:01:2841
Peter Boströmfeef05a2021-10-05 21:35:0842 MessagePipeDispatcher(const MessagePipeDispatcher&) = delete;
43 MessagePipeDispatcher& operator=(const MessagePipeDispatcher&) = delete;
44
rockotb1e74df2016-03-14 13:18:3045 // 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
rockotce69a042016-01-26 19:23:2150 // Dispatcher:
jam76bcf0c2015-10-02 21:01:2851 Type GetType() const override;
rockotce69a042016-01-26 19:23:2152 MojoResult Close() override;
Ken Rockotc12080c2018-05-03 18:10:2153 MojoResult WriteMessage(
54 std::unique_ptr<ports::UserMessageEvent> message) override;
Ken Rockot349df722017-06-16 05:48:4955 MojoResult ReadMessage(
56 std::unique_ptr<ports::UserMessageEvent>* message) override;
Ken Rockot0db0bcf2018-07-12 22:01:1257 MojoResult SetQuota(MojoQuotaType type, uint64_t limit) override;
58 MojoResult QueryQuota(MojoQuotaType type,
59 uint64_t* limit,
60 uint64_t* usage) override;
rockotce69a042016-01-26 19:23:2161 HandleSignalsState GetHandleSignalsState() const override;
rockot9eadaba2017-03-15 23:57:4762 MojoResult AddWatcherRef(const scoped_refptr<WatcherDispatcher>& watcher,
63 uintptr_t context) override;
64 MojoResult RemoveWatcherRef(WatcherDispatcher* watcher,
65 uintptr_t context) override;
rockotce69a042016-01-26 19:23:2166 void StartSerialize(uint32_t* num_bytes,
67 uint32_t* num_ports,
68 uint32_t* num_handles) override;
69 bool EndSerialize(void* destination,
Ken Rockot863f3482019-04-17 17:47:5070 ports::PortName* ports,
Ken Rockot29b0a322018-06-29 17:38:0771 PlatformHandle* handles) override;
rockotce69a042016-01-26 19:23:2172 bool BeginTransit() override;
73 void CompleteTransitAndClose() override;
74 void CancelTransit() override;
jam76bcf0c2015-10-02 21:01:2875
Ken Rockot863f3482019-04-17 17:47:5076 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);
jam76bcf0c2015-10-02 21:01:2882
83 private:
Ken Rockot7d291fcc2019-04-17 16:25:0284 class PortObserverThunk;
85 friend class PortObserverThunk;
rockotce69a042016-01-26 19:23:2186
jam76bcf0c2015-10-02 21:01:2887 ~MessagePipeDispatcher() override;
88
rockotce69a042016-01-26 19:23:2189 MojoResult CloseNoLock();
90 HandleSignalsState GetHandleSignalsStateNoLock() const;
Ken Rockot7d291fcc2019-04-17 16:25:0291 void OnPortStatusChanged();
jam76bcf0c2015-10-02 21:01:2892
rockotce69a042016-01-26 19:23:2193 // These are safe to access from any thread without locking.
Keishi Hattori7ccb88c2022-01-18 16:48:4594 // `node_controller_` is not a raw_ptr<...> for performance reasons (based on
95 // analysis of sampling profiler data).
Keishi Hattori488b7602022-05-02 13:09:3196 RAW_PTR_EXCLUSION NodeController* const node_controller_;
Ken Rockot7d291fcc2019-04-17 16:25:0297 const ports::PortRef port_;
rockotce69a042016-01-26 19:23:2198 const uint64_t pipe_id_;
99 const int endpoint_;
jam76bcf0c2015-10-02 21:01:28100
rockotce69a042016-01-26 19:23:21101 // Guards access to all the fields below.
102 mutable base::Lock signal_lock_;
jam76bcf0c2015-10-02 21:01:28103
rockotce69a042016-01-26 19:23:21104 // This is not the same is |port_transferred_|. It's only held true between
105 // BeginTransit() and Complete/CancelTransit().
amistry1352e1272016-04-19 01:40:41106 AtomicFlag in_transit_;
jam76bcf0c2015-10-02 21:01:28107
Ken Rockot8fad6a912019-08-02 20:20:12108 mutable MojoHandleSignals last_known_satisfied_signals_ = 0;
rockotce69a042016-01-26 19:23:21109 bool port_transferred_ = false;
amistry1352e1272016-04-19 01:40:41110 AtomicFlag port_closed_;
rockot9eadaba2017-03-15 23:57:47111 WatcherSet watchers_;
Arthur Sonzogni59ac8222023-11-10 09:46:54112 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_;
jam76bcf0c2015-10-02 21:01:28115};
116
Ken Rockotdba46db2018-07-04 18:41:04117} // namespace core
jam76bcf0c2015-10-02 21:01:28118} // namespace mojo
119
Ken Rockotdba46db2018-07-04 18:41:04120#endif // MOJO_CORE_MESSAGE_PIPE_DISPATCHER_H_