Skip to content

Commit

Permalink
fix(windows): clone HSTRING so it is not dropped eagerly (#1221)
Browse files Browse the repository at this point in the history
* fix(windows): move the HSTRING to callback

ref: https://discord.com/channels/616186924390023171/1229300159607996437/1229300159607996437

* clone `HSTRING`

Co-authored-by: Crend King <975235+CrendKing@users.noreply.github.com>

* do same for brwoser args

* Update webview-data-dir.md

* as_ref

* fmt

---------

Co-authored-by: Crend King <975235+CrendKing@users.noreply.github.com>
  • Loading branch information
amrbashir and CrendKing authored Apr 24, 2024
1 parent 07c1743 commit c7ca3db
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changes/webview-data-dir.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": "patch"
---

On Windows, fix data directory created next to the executable with a gibberish name even if it was explicitly provided in `WebConext::new`
13 changes: 7 additions & 6 deletions src/webview2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ impl InnerWebView {
.as_deref()
.and_then(|context| context.data_directory())
.and_then(|path| path.to_str())
.map(String::from);
.map(HSTRING::from);

// additional browser args
let additional_browser_args = pl_attrs.additional_browser_args.unwrap_or_else(|| {
Expand Down Expand Up @@ -280,18 +280,16 @@ impl InnerWebView {

arguments
});
let additional_browser_args = HSTRING::from(additional_browser_args);
let additional_browser_args = PCWSTR::from_raw(additional_browser_args.as_ptr());

let data_directory = data_directory.map(HSTRING::from);
let data_directory = data_directory.map(|d| PCWSTR::from_raw(d.as_ptr()));
let additional_browser_args = HSTRING::from(additional_browser_args);

let (tx, rx) = mpsc::channel();
CreateCoreWebView2EnvironmentCompletedHandler::wait_for_async_operation(
Box::new(move |environmentcreatedhandler| unsafe {
let options: ICoreWebView2EnvironmentOptions =
CoreWebView2EnvironmentOptions::default().into();

let additional_browser_args = PCWSTR::from_raw(additional_browser_args.as_ptr());
let _ = options.SetAdditionalBrowserArguments(additional_browser_args);

// Get user's system language
Expand All @@ -304,9 +302,12 @@ impl InnerWebView {
);
options.SetLanguage(PCWSTR::from_raw(lang.as_ptr()))?;

let data_directory_param = data_directory
.as_ref()
.map(|d| PCWSTR::from_raw(d.as_ptr()));
CreateCoreWebView2EnvironmentWithOptions(
PCWSTR::null(),
data_directory.unwrap_or_else(PCWSTR::null),
data_directory_param.unwrap_or_else(PCWSTR::null),
&options,
&environmentcreatedhandler,
)
Expand Down

0 comments on commit c7ca3db

Please sign in to comment.