summaryrefslogtreecommitdiff
path: root/src/client/endpoint/activation/model.rs
blob: c94029e958cea29fffd9a4f04a71ac7f8c9e78d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use std::time::Instant;

use super::super::ClientEndpointId;

#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) struct EndpointLease {
    pub(super) endpoint_id: ClientEndpointId,
    pub(super) generation: u64,
    pub(super) boot_id: String,
    /// The endpoint cache may already be newer than the first activation event. Never let an
    /// activation prove coherence with a revision that the monotonic cache has discarded.
    pub(super) minimum_revision: u64,
}

#[derive(Clone, Debug, Default)]
pub(super) struct ActivationEvidence {
    pub(super) snapshot_revision: Option<u64>,
    pub(super) focused_workspace_id: Option<String>,
    pub(super) focused_tab_id: Option<String>,
    pub(super) focused_pane_id: Option<String>,
    pub(super) surface: Option<crate::protocol::PaneSurfaceFrame>,
}

impl ActivationEvidence {
    pub(super) fn record_snapshot(&mut self, snapshot: &crate::protocol::ClientShellSnapshot) {
        if self
            .snapshot_revision
            .is_none_or(|current| snapshot.revision >= current)
        {
            self.snapshot_revision = Some(snapshot.revision);
            self.focused_workspace_id = snapshot.focused_workspace_id.clone();
            self.focused_tab_id = snapshot.focused_tab_id.clone();
            self.focused_pane_id = snapshot.focused_pane_id.clone();
        }
    }

    pub(super) fn record_surface(&mut self, surface: crate::protocol::PaneSurfaceFrame) {
        let replace = self.surface.as_ref().is_none_or(|current| {
            surface.projection_revision > current.projection_revision
                || (surface.projection_revision == current.projection_revision
                    && surface.surface_revision >= current.surface_revision)
        });
        if replace {
            self.surface = Some(surface);
        }
    }

    pub(super) fn invalidate_surface(&mut self) {
        self.surface = None;
    }

    pub(super) fn coherent_surface(
        &self,
        minimum_revision: u64,
        geometry: crate::protocol::ClientSurfaceSize,
    ) -> Option<&crate::protocol::PaneSurfaceFrame> {
        self.surface.as_ref().filter(|surface| {
            self.snapshot_revision == Some(surface.projection_revision)
                && surface.projection_revision >= minimum_revision
                && super::surface_matches_geometry(surface, geometry)
        })
    }
}

#[derive(Clone, Debug)]
pub(super) enum ActivationPhase {
    ReleasingSource {
        request_id: String,
    },
    ActivatingTarget {
        request_id: String,
        acknowledged_revision: Option<u64>,
        /// At most one focus request may be in flight. Retargets only replace `focus` until
        /// this response arrives, at which point the latest target is sent.
        focus_request_id: Option<String>,
        focus_request_target: Option<crate::client::shell::ClientEndpointFocusTarget>,
        focus_acknowledged: bool,
        evidence: ActivationEvidence,
    },
    ReleasingTargetForRollback {
        request_id: String,
    },
    RestoringSource {
        request_id: String,
        acknowledged_revision: Option<u64>,
        evidence: ActivationEvidence,
    },
    SynchronizingPresentation {
        lease: EndpointLease,
        request_id: String,
        acknowledged_revision: Option<u64>,
        evidence: ActivationEvidence,
        completion: Box<ActivationCompletion>,
    },
    AwaitingPresentationEffects {
        lease: EndpointLease,
        token: String,
        ready: bool,
        completion: Box<ActivationCompletion>,
    },
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum SurfaceActivationProgress {
    Pending,
    Ready,
    Rejected {
        message: String,
        source_release_rejected: bool,
    },
    Stale,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum ActivationRollback {
    /// A correlated rollback request is in flight; retain the frozen frame.
    Pending,
    /// Neither endpoint can be made safe to present. Keep pane input frozen while presenting
    /// client chrome and this error.
    Unavailable(String),
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum ActivationCompletion {
    AwaitingPresentationSync {
        previous: ClientEndpointId,
        endpoint: ClientEndpointId,
    },
    AwaitingPresentationEffects,
    Activated,
    RestoredSource {
        error: String,
        /// A newer endpoint-qualified selection arrived while this handoff was frozen. It is
        /// started only after the original source has been coherently restored.
        successor: Option<EndpointActivationIntent>,
    },
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct EndpointActivationIntent {
    pub(crate) endpoint_id: ClientEndpointId,
    pub(crate) target: Option<crate::client::shell::ClientEndpointFocusTarget>,
}

/// Begin failures are separated by whether the transport may already have observed a lifecycle
/// write. A partial transaction must be kept and rolled back under a frozen presentation.
#[derive(Debug)]
pub(crate) enum ActivationBeginError {
    Preflight(String),
    Partial {
        activation: Box<PendingEndpointActivation>,
        error: String,
    },
}

/// The only owner of an endpoint handoff. The registry's active endpoint remains the committed
/// endpoint until the target commits, while this object owns the uncommitted lifecycle lane.
#[derive(Debug)]
pub(crate) struct PendingEndpointActivation {
    pub(super) source: EndpointLease,
    pub(super) source_available: bool,
    pub(super) target: EndpointLease,
    pub(super) focus: Option<crate::client::shell::ClientEndpointFocusTarget>,
    pub(super) host_focused: bool,
    pub(super) resize: crate::protocol::ClientMessage,
    pub(super) phase: ActivationPhase,
    pub(super) deadline: Instant,
    pub(super) epoch: u64,
    pub(super) next_focus_serial: u64,
    pub(super) rollback_error: Option<String>,
    /// A different endpoint was selected while this source-off-first transaction was in flight.
    /// Keep only the latest intent until source restoration commits.
    pub(super) successor: Option<EndpointActivationIntent>,
}