Skip to content

Commit

Permalink
refactor(webview): use the more efficent SHCreateMemStream (#1031)
Browse files Browse the repository at this point in the history
* refactor(webview): use the more efficent `SHCreateMemStream`

ref: https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-createstreamonhglobal?redirectedfrom=MSDN

* remove unused
  • Loading branch information
amrbashir authored Oct 4, 2023
1 parent 2d62f36 commit cf34c2b
Showing 1 changed file with 5 additions and 19 deletions.
24 changes: 5 additions & 19 deletions src/webview/webview2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use std::{
collections::HashSet,
fmt::Write,
iter::once,
mem::MaybeUninit,
os::windows::prelude::OsStrExt,
path::PathBuf,
rc::Rc,
Expand All @@ -37,13 +36,13 @@ use windows::{
Globalization::{self, MAX_LOCALE_NAME},
Graphics::Gdi::{RedrawWindow, HRGN, RDW_INTERNALPAINT},
System::{
Com::{IStream, StructuredStorage::CreateStreamOnHGlobal},
Com::IStream,
LibraryLoader::{GetProcAddress, LoadLibraryW},
SystemInformation::OSVERSIONINFOW,
WinRT::EventRegistrationToken,
},
UI::{
Shell::{DefSubclassProc, SetWindowSubclass},
Shell::{DefSubclassProc, SHCreateMemStream, SetWindowSubclass},
WindowsAndMessaging::{self as win32wm, PostMessageW, RegisterWindowMessageA},
},
},
Expand Down Expand Up @@ -992,28 +991,15 @@ unsafe fn prepare_web_request_response(
}
}

let mut body_sent = None;
let mut stream = None;
if !content.is_empty() {
let stream = CreateStreamOnHGlobal(HGLOBAL(0), true)?;
stream.SetSize(content.len() as u64)?;
let mut cb_write = MaybeUninit::uninit();
if stream
.Write(
content.as_ptr() as *const _,
content.len() as u32,
Some(cb_write.as_mut_ptr()),
)
.is_ok()
&& cb_write.assume_init() as usize == content.len()
{
body_sent = Some(stream);
}
stream = SHCreateMemStream(Some(content));
}

// FIXME: Set http response version

env.CreateWebResourceResponse(
body_sent.as_ref(),
stream.as_ref(),
status_code.as_u16() as i32,
PCWSTR::from_raw(encode_wide(status_code.canonical_reason().unwrap_or("OK")).as_ptr()),
PCWSTR::from_raw(encode_wide(headers_map).as_ptr()),
Expand Down

0 comments on commit cf34c2b

Please sign in to comment.