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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
use core::ffi::c_void;
use std::sync::{
    atomic::{self, AtomicU64},
    Arc, Weak,
};

use ffi_sdk::BoxedAttachmentHandle;
use_prelude!();

use crate::{
    ditto::{DittoHandleWrapper, WeakDittoHandleWrapper},
    error::DittoError,
    store::{
        ditto_attachment::DittoAttachment, ditto_attachment_fetch_event::DittoAttachmentFetchEvent,
        ditto_attachment_token::DittoAttachmentToken, DittoFields,
    },
};

/// Type-level `enum` to distinguish between a [`DittoAttachmentFetcher`] that
/// can and must be cancelled, and the legacy behavior where it was auto-cancelled on drop.
///
/// ```rust ,ignore
/// // It represents the following, but at the type-level ("const generic" sort to speak).
/// enum FetcherVersion /* : Sealed */ {
///     /// The deprecated `.collection.fetch_attachment(...)` API.
///     V1,
///
///     /// The proper `.store.fetch_attachment(...)` API.
///     V2,
/// }
/// ```
#[allow(nonstandard_style)]
pub mod FetcherVersion {
    /// The deprecated
    /// [`collection.fetch_attachment(...)`][crate::store::Collection::fetch_attachment()] API.
    pub enum V1 {}
    /// The proper [`store.fetch_attachment(...)`][crate::store::Store::fetch_attachment()] API.
    pub enum V2 {}

    mod seal {
        pub trait Sealed: 'static {}
    }
    pub(crate) use seal::Sealed;

    impl Sealed for V1 {}
    impl Sealed for V2 {}
}

/// The output of [`store.fetch_attachment()`](crate::store::Store::fetch_attachment).
///
///   - In the deprecated [`FetcherVersion::V1`] case, they must be kept alive for the fetching of
///     the attachment to proceed and for you to be notified once the status of the fetch request
///     has changed.
///
///   - In the proper [`FetcherVersion::V2`] case, they are only
///     [`.cancel()`][DittoAttachmentFetcher::cancel]-ed *explicitly*, that is, they are safe to
///     discard / not to safe-keep.
pub struct DittoAttachmentFetcher<'a, Version: FetcherVersion::Sealed = FetcherVersion::V1> {
    pub(crate) context: Arc<DittoAttachmentFetcherCtx<'a>>,
    _phantom: ::core::marker::PhantomData<fn() -> Version>,
}

pub(crate) type DittoAttachmentFetcherV2 = DittoAttachmentFetcher<'static, FetcherVersion::V2>;

impl Clone for DittoAttachmentFetcherV2 {
    fn clone(&self) -> Self {
        Self {
            context: self.context.retain(),
            ..*self
        }
    }
}

impl<'a, Version: FetcherVersion::Sealed> DittoAttachmentFetcher<'a, Version> {
    pub(crate) fn new(
        token: DittoAttachmentToken,
        ditto: Option<&Arc<DittoFields>>,
        raw_ditto: &DittoHandleWrapper,
        on_fetch_event: impl 'a + Send + Sync + Fn(DittoAttachmentFetchEvent, &AtomicU64),
    ) -> Result<Self, DittoError> {
        let context = Arc::new(DittoAttachmentFetcherCtx {
            token: token.clone(),
            ditto: ditto.map_or_else(::std::sync::Weak::new, Arc::downgrade),
            raw_ditto: Arc::downgrade(raw_ditto),
            on_fetch_event: Box::new(on_fetch_event),
            cancel_token: 0.into(),
        });
        let raw_context = Arc::as_ptr(&context) as *mut c_void;

        let cancel_token = unsafe {
            ffi_sdk::ditto_resolve_attachment(
                raw_ditto,
                token.id.as_ref().into(),
                raw_context,
                Some(DittoAttachmentFetcherCtx::retain),
                Some(DittoAttachmentFetcherCtx::release),
                DittoAttachmentFetcherCtx::on_complete_cb,
                DittoAttachmentFetcherCtx::on_progress_cb,
                DittoAttachmentFetcherCtx::on_deleted_cb,
            )
            .ok()?
        };
        // HACK: until `dittoffi` exposes the `cancel_token` / `resolve_id`
        // to the callback itself, we manually back-smuggle it using the shared
        // context.
        //
        // This value is later accessed in the callback registered by
        // `Store::fetch_attachment()`, in the `on_complete` case, after having
        // acquired a lock which happens to be held during this whole `new()` call,
        // hence why `Relaxed` suffices.
        context
            .cancel_token
            .store(cancel_token as _, atomic::Ordering::Relaxed);

        Ok(Self {
            context,
            _phantom: <_>::default(),
        })
    }
}

impl<Version: FetcherVersion::Sealed> DittoAttachmentFetcher<'_, Version> {
    pub(crate) fn cancel_token(&self) -> u64 {
        self.context.cancel_token.load(atomic::Ordering::SeqCst)
    }
}

impl DittoAttachmentFetcherV2 {
    /// Yields `true` if the original `cancel_token` was `0`.
    pub(crate) fn cancel_token_ensure_unique(&self) -> (u64, bool) {
        let mut cancel_token = self.cancel_token();
        let was_zero = cancel_token == 0;
        if was_zero {
            // Currently, FFI `.resolve_attachment()` will yield `0` for fetchers
            // it believes to have invoked synchronously (fast path); even
            // though our callback is —outside of Wasm— dispatching these
            // invocations onto the `attachments_signal_sender` queue.
            //
            // So, to keep the property of unicity of these tokens across
            // the lifetime of a ditto instance (TODO: process-wide?), we
            // currently hack a fallback unicity mechanism. The `core`
            // auto-increments off `1..`, so let us do `(..-2).rev()`.
            static FALLBACK_UNIQUE_CANCEL_TOKEN: AtomicU64 = AtomicU64::new(u64::MAX - 1);
            cancel_token =
                FALLBACK_UNIQUE_CANCEL_TOKEN.fetch_sub(1, ::std::sync::atomic::Ordering::Relaxed);
            self.context
                .cancel_token
                .store(cancel_token, atomic::Ordering::SeqCst);
        };
        (cancel_token, was_zero)
    }
}

impl DittoAttachmentFetcherV2 {
    /// Stops fetching the fetcher's associated attachment and cleans up any
    /// associated resources.
    ///
    /// Note that you are not required to call it once your attachment
    /// fetch operation has finished. The method primarily exists to allow you
    /// to cancel an attachment fetch request while it is ongoing if you no
    /// longer wish for the attachment to be made available locally to the
    /// device nor for its evolution to be observed.
    pub fn cancel(&self) {
        if let Some(ditto) = self.context.ditto.upgrade() {
            ditto.store.unregister_fetcher(self.cancel_token(), None);
        }
    }
}

impl<'a> crate::observer::Observer for DittoAttachmentFetcher<'a, FetcherVersion::V1> {}

// Does nothing for V2.
impl<'a, V1: FetcherVersion::Sealed> Drop for DittoAttachmentFetcher<'a, V1> {
    fn drop(&mut self) {
        /// Poorman's specialization.
        use ::core::any::TypeId;

        if TypeId::of::<V1>() == TypeId::of::<FetcherVersion::V1>() {
            if let Some(ditto) = self.context.raw_ditto.upgrade() {
                let status = ffi_sdk::ditto_cancel_resolve_attachment(
                    &ditto,
                    self.context.token.id.as_ref().into(),
                    self.cancel_token(),
                );
                if status != 0 {
                    log::error!("Failed to clean up attachment fetcher");
                }
            }
        }
    }
}

pub(crate) struct DittoAttachmentFetcherCtx<'a> {
    pub(crate) token: DittoAttachmentToken,
    ditto: Weak<DittoFields>,
    raw_ditto: WeakDittoHandleWrapper,
    on_fetch_event: Box<dyn Fn(DittoAttachmentFetchEvent, &AtomicU64) + Send + Sync + 'a>,
    cancel_token: atomic::AtomicU64,
}

impl<'a> DittoAttachmentFetcherCtx<'a> {
    pub(crate) unsafe extern "C" fn retain(ctx: *mut c_void) {
        let ptr = ctx.cast::<DittoAttachmentFetcherCtx<'_>>();
        Arc::increment_strong_count(ptr);
    }

    pub(crate) unsafe extern "C" fn release(ctx: *mut c_void) {
        let ptr = ctx.cast::<DittoAttachmentFetcherCtx<'_>>();
        Arc::decrement_strong_count(ptr);
    }

    pub(crate) unsafe extern "C" fn on_complete_cb(
        ctx: *mut c_void,
        attachment_handle: BoxedAttachmentHandle,
    ) {
        let ctx_ref = ctx
            .cast::<DittoAttachmentFetcherCtx<'_>>()
            .as_ref()
            .expect("got null");

        let ditto_attachment = DittoAttachment::new_with_token(
            ctx_ref.token.clone(),
            ctx_ref.raw_ditto.clone(),
            attachment_handle,
        );
        let event = DittoAttachmentFetchEvent::Completed {
            attachment: ditto_attachment,
        };
        (ctx_ref.on_fetch_event)(event, &ctx_ref.cancel_token);
    }

    pub(crate) unsafe extern "C" fn on_progress_cb(
        ctx: *mut c_void,
        downloaded_bytes: u64,
        total_bytes: u64,
    ) {
        let ctx_ref = ctx
            .cast::<DittoAttachmentFetcherCtx<'_>>()
            .as_ref()
            .expect("got null");

        let event = DittoAttachmentFetchEvent::Progress {
            downloaded_bytes,
            total_bytes,
        };
        (ctx_ref.on_fetch_event)(event, &ctx_ref.cancel_token);
    }

    pub(crate) unsafe extern "C" fn on_deleted_cb(ctx: *mut c_void) {
        let ctx_ref = ctx
            .cast::<DittoAttachmentFetcherCtx<'_>>()
            .as_ref()
            .expect("got null");

        let event = DittoAttachmentFetchEvent::Deleted;
        (ctx_ref.on_fetch_event)(event, &ctx_ref.cancel_token);
    }
}