microsoft-webui-desktop 3.0.3

Configurable native desktop SDK for WebUI applications
Documentation
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

//! Host-only WebView2 capture. The WebView2 interface is touched only by its
//! owning window STA; a bounded COM stream is shared with its completion.

use std::ffi::c_void;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, Weak};

use webview2_com::CapturePreviewCompletedHandler;
use webview2_com::Microsoft::Web::WebView2::Win32::{
    ICoreWebView2, ICoreWebView2Controller, ICoreWebView2Controller3,
    COREWEBVIEW2_BOUNDS_MODE_USE_RAW_PIXELS, COREWEBVIEW2_CAPTURE_PREVIEW_IMAGE_FORMAT_PNG,
};
use windows::core::{implement, Error, Interface, Ref, Result, HRESULT};
use windows::Win32::Foundation::{
    E_INVALIDARG, E_NOTIMPL, HWND, STG_E_INVALIDFUNCTION, STG_E_MEDIUMFULL,
};
use windows::Win32::System::Com::{
    ISequentialStream_Impl, IStream, IStream_Impl, LOCKTYPE, STATFLAG, STATSTG, STGC, STGM_WRITE,
    STGTY_STREAM, STREAM_SEEK, STREAM_SEEK_CUR, STREAM_SEEK_END, STREAM_SEEK_SET,
};
use windows::Win32::UI::WindowsAndMessaging::PostMessageW;

use crate::capture::{
    windows_png, CaptureError, CaptureOptions, CaptureState, MAX_WEB_CAPTURE_PNG_BYTES,
};

static NEXT_DISPATCH: AtomicUsize = AtomicUsize::new(1);
pub(super) const CAPTURE_WAKE_MESSAGE: u32 = windows::Win32::UI::WindowsAndMessaging::WM_APP + 5;

pub(crate) struct Dispatch {
    hwnd: usize,
    id: usize,
    closed: AtomicBool,
    pending: Mutex<Option<(u64, u64, CaptureOptions)>>,
    stream: Mutex<Option<(u64, Weak<Mutex<Bytes>>)>>,
}

pub(super) struct Registration {
    dispatch: Arc<Dispatch>,
    owner: Arc<CaptureState>,
}

impl Registration {
    pub(super) fn owner_viewport_changed(&self) {
        self.owner.viewport_changed();
    }
    pub(super) fn close(&self) {
        self.dispatch.closed.store(true, Ordering::Release);
        self.dispatch.discard_stream(None);
        if let Ok(mut pending) = self.dispatch.pending.lock() {
            pending.take();
        }
        self.owner.notify_closed();
    }

    pub(super) fn drain(
        &self,
        cookie: usize,
        webview: &ICoreWebView2,
        controller: &ICoreWebView2Controller,
        (hwnd, content): (HWND, HWND),
    ) {
        if cookie != self.dispatch.id {
            return;
        }
        let pending = self
            .dispatch
            .pending
            .lock()
            .ok()
            .and_then(|mut slot| slot.take());
        if let Some((id, epoch, options)) = pending {
            if self.dispatch.closed.load(Ordering::Acquire) || !self.owner.current(id, epoch) {
                self.owner.complete(id, epoch, Err(CaptureError::Cancelled));
            } else {
                start_capture(
                    &self.owner,
                    &self.dispatch,
                    (webview, controller),
                    (hwnd, content),
                    (id, epoch, options),
                );
            }
        }
    }
}

impl Drop for Registration {
    fn drop(&mut self) {
        self.close();
    }
}

pub(super) fn install(
    owner: &Arc<CaptureState>,
    hwnd: HWND,
) -> std::result::Result<Registration, CaptureError> {
    let id = NEXT_DISPATCH
        .fetch_update(Ordering::Relaxed, Ordering::Relaxed, |id| id.checked_add(1))
        .map_err(|_| CaptureError::Scheduler)?;
    let dispatch = Arc::new(Dispatch {
        hwnd: hwnd.0 as usize,
        id,
        closed: AtomicBool::new(false),
        pending: Mutex::new(None),
        stream: Mutex::new(None),
    });
    owner.attach(Arc::clone(&dispatch));
    Ok(Registration {
        dispatch,
        owner: Arc::clone(owner),
    })
}

impl Dispatch {
    pub(crate) fn discard_stream(&self, expected_id: Option<u64>) {
        let stream = self.stream.lock().ok().and_then(|slot| {
            slot.as_ref().and_then(|(id, weak)| {
                (expected_id.is_none() || expected_id == Some(*id))
                    .then(|| weak.upgrade())
                    .flatten()
            })
        });
        if let Some(stream) = stream {
            if let Ok(mut bytes) = stream.lock() {
                bytes.aborted = true;
                // Dropping the old allocation releases source bytes promptly
                // even when WebView2 never calls back.
                bytes.data = Vec::new();
            }
        }
    }

    fn track_stream(
        &self,
        id: u64,
        stream: &Arc<Mutex<Bytes>>,
    ) -> std::result::Result<(), CaptureError> {
        let mut slot = self.stream.lock().map_err(|_| CaptureError::Scheduler)?;
        if self.closed.load(Ordering::Acquire) {
            return Err(CaptureError::Closed);
        }
        *slot = Some((id, Arc::downgrade(stream)));
        Ok(())
    }

    pub(crate) fn submit(
        &self,
        id: u64,
        epoch: u64,
        options: CaptureOptions,
    ) -> std::result::Result<(), CaptureError> {
        let mut pending = self.pending.lock().map_err(|_| CaptureError::Scheduler)?;
        if self.closed.load(Ordering::Acquire) {
            return Err(CaptureError::Closed);
        }
        if pending.is_some() {
            return Err(CaptureError::Busy);
        }
        *pending = Some((id, epoch, options));
        // Keep the target lock until PostMessageW has accepted this wake. The
        // window generation is checked again before touching its COM object.
        let hwnd = HWND(self.hwnd as *mut c_void);
        // SAFETY: PostMessageW never waits for the UI thread; window teardown
        // closes this dispatch and an HWND reuse cannot match its cookie.
        if unsafe {
            PostMessageW(
                Some(hwnd),
                CAPTURE_WAKE_MESSAGE,
                windows::Win32::Foundation::WPARAM(self.id),
                windows::Win32::Foundation::LPARAM(0),
            )
        }
        .is_err()
        {
            pending.take();
            return Err(CaptureError::Scheduler);
        }
        Ok(())
    }
}

// A single bounded allocation for the native encoded output. All writes
// (including overwrites after Seek) consume the source-byte budget.
struct Bytes {
    data: Vec<u8>,
    position: usize,
    written: usize,
    limit: usize,
    overflow: bool,
    aborted: bool,
}

impl Bytes {
    fn new(limit: usize) -> Self {
        Self {
            data: Vec::new(),
            position: 0,
            written: 0,
            limit: limit.min(MAX_WEB_CAPTURE_PNG_BYTES),
            overflow: false,
            aborted: false,
        }
    }

    fn write(&mut self, bytes: &[u8]) -> HRESULT {
        if self.aborted {
            return STG_E_INVALIDFUNCTION;
        }
        let Some(end) = self.position.checked_add(bytes.len()) else {
            self.overflow = true;
            return STG_E_MEDIUMFULL;
        };
        if end > self.limit
            || self
                .written
                .checked_add(bytes.len())
                .is_none_or(|n| n > self.limit)
        {
            self.overflow = true;
            return STG_E_MEDIUMFULL;
        }
        if end > self.data.len() {
            if end > self.data.capacity() {
                // Grow only when existing capacity is exhausted, and never
                // request more than the selected source/output byte limit.
                let target = end
                    .max(self.data.capacity().saturating_mul(2))
                    .max(4096)
                    .min(self.limit);
                if self
                    .data
                    .try_reserve_exact(target - self.data.len())
                    .is_err()
                {
                    self.overflow = true;
                    return STG_E_MEDIUMFULL;
                }
            }
            self.data.resize(end, 0);
        }
        self.data[self.position..end].copy_from_slice(bytes);
        self.position = end;
        self.written += bytes.len();
        HRESULT(0)
    }

    fn seek(&mut self, offset: i64, origin: STREAM_SEEK) -> Result<u64> {
        if self.aborted {
            return Err(Error::from(STG_E_INVALIDFUNCTION));
        }
        let base = if origin == STREAM_SEEK_SET {
            0_i64
        } else if origin == STREAM_SEEK_CUR {
            i64::try_from(self.position).map_err(|_| Error::from(E_INVALIDARG))?
        } else if origin == STREAM_SEEK_END {
            i64::try_from(self.data.len()).map_err(|_| Error::from(E_INVALIDARG))?
        } else {
            return Err(Error::from(STG_E_INVALIDFUNCTION));
        };
        let next = base
            .checked_add(offset)
            .and_then(|n| usize::try_from(n).ok())
            .filter(|&n| n <= self.limit)
            .ok_or_else(|| Error::from(STG_E_INVALIDFUNCTION))?;
        self.position = next;
        u64::try_from(next).map_err(|_| Error::from(E_INVALIDARG))
    }
}

#[implement(IStream)]
struct BoundedStream {
    bytes: Arc<Mutex<Bytes>>,
    owner: Weak<CaptureState>,
    id: u64,
    epoch: u64,
}

impl ISequentialStream_Impl for BoundedStream_Impl {
    fn Read(&self, pv: *mut c_void, cb: u32, pcbread: *mut u32) -> HRESULT {
        if !pcbread.is_null() {
            // SAFETY: COM caller supplies the output pointer for this call.
            unsafe { *pcbread = 0 };
        }
        if cb > 0 && pv.is_null() {
            return E_INVALIDARG;
        }
        let Ok(mut bytes) = self.bytes.lock() else {
            return STG_E_INVALIDFUNCTION;
        };
        if bytes.aborted {
            return STG_E_INVALIDFUNCTION;
        }
        let available = bytes.data.len().saturating_sub(bytes.position);
        let count = available.min(cb as usize);
        if count > 0 {
            // SAFETY: pv points to at least cb writable bytes by IStream's
            // contract; count is <= cb and data bounds were checked above.
            unsafe {
                std::ptr::copy_nonoverlapping(
                    bytes.data.as_ptr().add(bytes.position),
                    pv.cast::<u8>(),
                    count,
                );
            }
            bytes.position += count;
        }
        if !pcbread.is_null() {
            // SAFETY: count <= cb <= u32::MAX.
            unsafe { *pcbread = u32::try_from(count).unwrap_or(cb) };
        }
        HRESULT(0)
    }

    fn Write(&self, pv: *const c_void, cb: u32, pcbwritten: *mut u32) -> HRESULT {
        if !pcbwritten.is_null() {
            // SAFETY: COM caller supplies the output pointer for this call.
            unsafe { *pcbwritten = 0 };
        }
        if cb > 0 && pv.is_null() {
            return E_INVALIDARG;
        }
        let Ok(mut bytes) = self.bytes.lock() else {
            return STG_E_INVALIDFUNCTION;
        };
        if self
            .owner
            .upgrade()
            .is_none_or(|owner| !owner.current(self.id, self.epoch))
        {
            bytes.aborted = true;
            bytes.data = Vec::new();
            return STG_E_INVALIDFUNCTION;
        }
        // SAFETY: COM's ISequentialStream contract requires cb readable bytes
        // at pv. Zero-length writes use an empty slice without dereferencing pv.
        let input = if cb == 0 {
            &[]
        } else {
            unsafe { std::slice::from_raw_parts(pv.cast::<u8>(), cb as usize) }
        };
        let result = bytes.write(input);
        if result.is_ok() && !pcbwritten.is_null() {
            // SAFETY: Valid writable out-pointer supplied by COM.
            unsafe { *pcbwritten = cb };
        }
        result
    }
}

#[allow(clippy::unnecessary_wraps)]
impl IStream_Impl for BoundedStream_Impl {
    fn Seek(&self, offset: i64, origin: STREAM_SEEK, out: *mut u64) -> Result<()> {
        let position = self
            .bytes
            .lock()
            .map_err(|_| Error::from(STG_E_INVALIDFUNCTION))?
            .seek(offset, origin)?;
        if !out.is_null() {
            // SAFETY: COM supplied writable optional position storage.
            unsafe { *out = position };
        }
        Ok(())
    }
    fn SetSize(&self, size: u64) -> Result<()> {
        let mut bytes = self
            .bytes
            .lock()
            .map_err(|_| Error::from(STG_E_INVALIDFUNCTION))?;
        if self
            .owner
            .upgrade()
            .is_none_or(|owner| !owner.current(self.id, self.epoch))
        {
            bytes.aborted = true;
            bytes.data = Vec::new();
            return Err(Error::from(STG_E_INVALIDFUNCTION));
        }
        if bytes.aborted {
            return Err(Error::from(STG_E_INVALIDFUNCTION));
        }
        let size = usize::try_from(size).map_err(|_| Error::from(STG_E_MEDIUMFULL))?;
        if size > bytes.limit {
            bytes.overflow = true;
            return Err(Error::from(STG_E_MEDIUMFULL));
        }
        if size > bytes.data.len() {
            let additional = size - bytes.data.len();
            bytes
                .data
                .try_reserve_exact(additional)
                .map_err(|_| Error::from(STG_E_MEDIUMFULL))?;
        }
        bytes.data.resize(size, 0);
        bytes.position = bytes.position.min(size);
        Ok(())
    }
    fn CopyTo(&self, _: Ref<'_, IStream>, _: u64, _: *mut u64, _: *mut u64) -> Result<()> {
        Err(Error::from(E_NOTIMPL))
    }
    fn Commit(&self, _: &STGC) -> Result<()> {
        Ok(())
    }
    fn Revert(&self) -> Result<()> {
        Err(Error::from(E_NOTIMPL))
    }
    fn LockRegion(&self, _: u64, _: u64, _: &LOCKTYPE) -> Result<()> {
        Err(Error::from(E_NOTIMPL))
    }
    fn UnlockRegion(&self, _: u64, _: u64, _: u32) -> Result<()> {
        Err(Error::from(E_NOTIMPL))
    }
    fn Stat(&self, out: *mut STATSTG, _: &STATFLAG) -> Result<()> {
        if out.is_null() {
            return Err(Error::from(E_INVALIDARG));
        }
        let bytes = self
            .bytes
            .lock()
            .map_err(|_| Error::from(STG_E_INVALIDFUNCTION))?;
        let stat = STATSTG {
            r#type: STGTY_STREAM.0 as u32,
            cbSize: u64::try_from(bytes.data.len()).map_err(|_| Error::from(E_INVALIDARG))?,
            grfMode: STGM_WRITE,
            ..Default::default()
        };
        // SAFETY: COM supplies writable STATSTG; the default name is null so
        // there is no ownership transfer or external allocation.
        unsafe { *out = stat };
        Ok(())
    }
    fn Clone(&self) -> Result<IStream> {
        Err(Error::from(E_NOTIMPL))
    }
}

fn start_capture(
    owner: &Arc<CaptureState>,
    dispatch: &Dispatch,
    (webview, controller): (&ICoreWebView2, &ICoreWebView2Controller),
    (hwnd, content): (HWND, HWND),
    (id, epoch, options): (u64, u64, CaptureOptions),
) {
    let mut rect = windows::Win32::Foundation::RECT::default();
    // SAFETY: Called only on the live window's owning STA with its HWND.
    if unsafe { windows::Win32::UI::WindowsAndMessaging::IsIconic(hwnd) }.as_bool()
        || !unsafe { windows::Win32::UI::WindowsAndMessaging::IsWindowVisible(hwnd) }.as_bool()
    {
        owner.complete(id, epoch, Err(CaptureError::Unavailable));
        return;
    }
    // SAFETY: content is the live controller parent, distinct from the frame
    // HWND used to route the wake and inspect window visibility.
    if unsafe { windows::Win32::UI::WindowsAndMessaging::GetClientRect(content, &mut rect) }
        .is_err()
    {
        owner.complete(id, epoch, Err(CaptureError::Unavailable));
        return;
    }
    let (Ok(width), Ok(height)) = (
        u32::try_from(rect.right.saturating_sub(rect.left)),
        u32::try_from(rect.bottom.saturating_sub(rect.top)),
    ) else {
        owner.complete(id, epoch, Err(CaptureError::Incomplete));
        return;
    };
    // The host sets controller.Bounds from this same content HWND. In
    // RAW_PIXELS mode Bounds is the physical extent even at high DPI; changing
    // RasterizationScale does not change it. Do not guess for logical Bounds,
    // an older controller, or a stale resize.
    let viewport = (|| {
        let controller3 = controller
            .cast::<ICoreWebView2Controller3>()
            .map_err(|_| CaptureError::Unavailable)?;
        let mut mode = Default::default();
        let mut bounds = windows::Win32::Foundation::RECT::default();
        // SAFETY: The controller, view and HWND belong to this live STA.
        unsafe {
            controller3
                .BoundsMode(&mut mode)
                .map_err(|_| CaptureError::Unavailable)?;
            controller
                .Bounds(&mut bounds)
                .map_err(|_| CaptureError::Unavailable)?;
        }
        if mode != COREWEBVIEW2_BOUNDS_MODE_USE_RAW_PIXELS
            || bounds.left != 0
            || bounds.top != 0
            || bounds.right != rect.right
            || bounds.bottom != rect.bottom
        {
            return Err(CaptureError::Unavailable);
        }
        windows_png::preflight(width, height, options)
    })();
    let viewport = match viewport {
        Ok(viewport) => viewport,
        Err(error) => {
            owner.complete(id, epoch, Err(error));
            return;
        }
    };
    let bytes = Arc::new(Mutex::new(Bytes::new(options.max_png_bytes)));
    if let Err(error) = dispatch.track_stream(id, &bytes) {
        owner.complete(id, epoch, Err(error));
        return;
    }
    if !owner.current(id, epoch) {
        dispatch.discard_stream(Some(id));
        owner.complete(id, epoch, Err(CaptureError::Cancelled));
        return;
    }
    let stream: IStream = BoundedStream {
        bytes: Arc::clone(&bytes),
        owner: Arc::downgrade(owner),
        id,
        epoch,
    }
    .into();
    let completion_owner = Arc::clone(owner);
    let handler = CapturePreviewCompletedHandler::create(Box::new(move |result| {
        completion_owner.complete_if_current(id, epoch, || {
            let bytes = bytes.lock().map_err(|_| CaptureError::Scheduler)?;
            if bytes.overflow {
                return Err(CaptureError::TooLarge);
            }
            result.map_err(|error| CaptureError::NativeCode(error.code().0 as isize))?;
            let mut bytes = bytes;
            let png = std::mem::take(&mut bytes.data);
            let (width, height) = windows_png::validate_png(&png, viewport, options)?;
            Ok((width, height, png))
        });
        Ok(())
    }));
    // SAFETY: This runs on the exact controller's owning STA. WebView2 holds
    // its own COM references to stream and handler through completion; our
    // callback retains the buffer and capture reservation until invoked.
    if let Err(error) = unsafe {
        webview.CapturePreview(
            COREWEBVIEW2_CAPTURE_PREVIEW_IMAGE_FORMAT_PNG,
            &stream,
            &handler,
        )
    } {
        owner.complete(
            id,
            epoch,
            Err(CaptureError::NativeCode(error.code().0 as isize)),
        );
    }
}

#[cfg(test)]
#[allow(clippy::disallowed_methods)]
mod tests {
    use super::*;

    #[test]
    fn small_png_writes_reuse_capacity_through_retention() {
        // Valid 1x1 RGBA PNG, also used by the private macOS pasteboard test.
        let png = [
            137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 1, 0, 0, 0, 1,
            8, 6, 0, 0, 0, 31, 21, 196, 137, 0, 0, 0, 13, 73, 68, 65, 84, 120, 1, 99, 248, 63, 139,
            225, 63, 0, 6, 206, 2, 153, 89, 149, 178, 136, 0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96,
            130,
        ];
        assert_eq!(png.len(), 70);
        let mut bytes = Bytes::new(MAX_WEB_CAPTURE_PNG_BYTES);
        assert!(bytes.write(&png[..5]).is_ok());
        let capacity = bytes.data.capacity();
        assert!((4096..=8192).contains(&capacity));
        for chunk in png[5..].chunks(5) {
            assert!(bytes.write(chunk).is_ok());
            assert_eq!(bytes.data.capacity(), capacity);
        }
        assert_eq!(bytes.written, png.len());
        let retained = Arc::new(bytes.data);
        assert_eq!(retained.as_slice(), png);
        assert_eq!(retained.capacity(), capacity);
    }

    #[test]
    fn stream_grows_for_real_capacity_exhaustion_not_rewrites() {
        let limit = 12 * 1024;
        let mut bytes = Bytes::new(limit);
        assert!(bytes.write(&[7; 4096]).is_ok());
        let initial = bytes.data.capacity();
        assert!(bytes
            .seek(i64::try_from(initial).unwrap(), STREAM_SEEK_SET)
            .is_ok());
        assert!(bytes.write(&[8]).is_ok());
        let grown = bytes.data.capacity();
        assert!(grown > initial && grown <= limit);
        assert_eq!(bytes.data[initial], 8);

        assert_eq!(bytes.seek(0, STREAM_SEEK_SET).unwrap(), 0);
        assert!(bytes.write(&[9; 16]).is_ok());
        assert_eq!(bytes.data.capacity(), grown);
        assert_eq!(&bytes.data[..16], &[9; 16]);

        assert!(bytes
            .seek(i64::try_from(grown).unwrap(), STREAM_SEEK_SET)
            .is_ok());
        assert!(bytes.write(&[10]).is_ok());
        assert!(bytes.data.capacity() <= limit);
        assert!(bytes.data[initial + 1..grown].iter().all(|byte| *byte == 0));
        assert_eq!(bytes.data[grown], 10);
        assert!(bytes
            .seek(i64::try_from(limit).unwrap(), STREAM_SEEK_SET)
            .is_ok());
        assert_eq!(bytes.write(&[11]).0, STG_E_MEDIUMFULL.0);
    }

    #[test]
    fn stream_caps_growth_rewrites_and_seek_without_allocating_past_limit() {
        let mut bytes = Bytes::new(32);
        assert!(bytes.write(&[7; 16]).is_ok());
        assert_eq!(bytes.seek(0, STREAM_SEEK_SET).unwrap(), 0);
        assert!(bytes.write(&[8; 16]).is_ok());
        assert_eq!(bytes.data.len(), 16);
        assert_eq!(bytes.write(&[9]).0, STG_E_MEDIUMFULL.0);
        assert!(bytes.overflow);
        assert_eq!(bytes.data.len(), 16);
        assert!(bytes.seek(33, STREAM_SEEK_SET).is_err());
        assert!(bytes.seek(-1, STREAM_SEEK_SET).is_err());
        bytes.aborted = true;
        bytes.data = Vec::new();
        assert_eq!(bytes.write(&[1]).0, STG_E_INVALIDFUNCTION.0);
        assert!(bytes.seek(0, STREAM_SEEK_SET).is_err());
        let mut bytes = Bytes::new(32);
        assert!(bytes.write(&[0; 32]).is_ok());
        assert_eq!(bytes.write(&[0]).0, STG_E_MEDIUMFULL.0);
    }

    #[test]
    fn stale_discard_cannot_cancel_a_newer_stream() {
        let dispatch = Dispatch {
            hwnd: 0,
            id: 1,
            closed: AtomicBool::new(false),
            pending: Mutex::new(None),
            stream: Mutex::new(None),
        };
        let bytes = Arc::new(Mutex::new(Bytes::new(64)));
        dispatch.track_stream(2, &bytes).unwrap();
        assert!(bytes.lock().unwrap().write(&[1; 32]).is_ok());
        dispatch.discard_stream(Some(1));
        assert_eq!(bytes.lock().unwrap().data.len(), 32);
        dispatch.discard_stream(Some(2));
        let mut state = bytes.lock().unwrap();
        assert!(state.aborted);
        assert!(state.data.is_empty());
        assert_eq!(state.write(&[1]).0, STG_E_INVALIDFUNCTION.0);
    }
}