Skip to content

Commit

Permalink
Use http instead of file for windows custom protocol workaround (#173)
Browse files Browse the repository at this point in the history
* Use http instead of file for windows custom protocol workaround

Fix #170

* Add change file

* Add custom protocol example

* Replace `tauri` for `wry`
  • Loading branch information
lemarier authored Apr 20, 2021
1 parent e48a5e7 commit dd0fa46
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changes/history.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": minor
---

Fix `history.pushState` in webview2.
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ Run the `cargo run --example <file_name>` to see how each example works.
- `dragndrop`: example for file drop handler.
- `custom_titlebar`: A frameless window with custom title-bar to show `drag-region` class in action.
- `custom_user_data`: uses a custom data directory (Windows only).

- `custom_protocol`: uses a custom protocol to load files from bytes.
91 changes: 91 additions & 0 deletions examples/custom_protocol.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

fn main() -> wry::Result<()> {
use wry::{
application::{
event::{Event, StartCause, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
},
webview::WebViewBuilder,
};

let event_loop = EventLoop::new();
let window = WindowBuilder::new()
.with_title("Hello World")
.build(&event_loop)
.unwrap();

let _webview = WebViewBuilder::new(window)
.unwrap()
.with_custom_protocol("wry.dev".into(), move |requested_asset_path| {
// remove the protocol from the path for easiest match
let requested_asset_path = requested_asset_path.replace("wry.dev://", "");

// sample index.html file
// files can be bundled easilly into the binary
// with https://doc.rust-lang.org/std/macro.include_bytes.html

let index_html = r#"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<h1>Welcome to WRY!</h1>
<a href="/hello.html">Link</a>
<script type="text/javascript" src="/hello.js"></script>
</body>
</html>"#;

// sample hello.js file
let hello_js = "console.log(\"hello from javascript\");";

// sample hello.html file
let hello_html = r#"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<h1>Sample page!</h1>
<a href="/index.html">Back home</a>
<script type="text/javascript" src="/hello.js"></script>
</body>
</html>"#;

match requested_asset_path.as_str() {
// if our path match /hello.html
"/hello.html" => Ok(hello_html.as_bytes().into()),
// if our path match /hello.js
"/hello.js" => Ok(hello_js.as_bytes().into()),
// other paths should resolve index
// more logic can be applied here
_ => Ok(index_html.as_bytes().into()),
}
})
// tell the webview to load the custom protocol
.with_url("wry.dev://")?
.build()?;

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Poll;

match event {
Event::NewEvents(StartCause::Init) => println!("Wry application started!"),
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => *control_flow = ControlFlow::Exit,
_ => (),
}
});
}
6 changes: 3 additions & 3 deletions src/webview/win32/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ impl InnerWebView {
// See https://github.com/MicrosoftEdge/WebView2Feedback/issues/73
custom_protocol_names.insert(name.clone());
w.add_web_resource_requested_filter(
&format!("file://custom-protocol-{}*", name),
&format!("http://custom-protocol-{}*", name),
webview2::WebResourceContext::All,
)?;
let env_clone = env_.clone();
w.add_web_resource_requested(move |_, args| {
let uri = args.get_request()?.get_uri()?;
// Undo the protocol workaround when giving path to resolver
let path = &uri.replace(
&format!("file://custom-protocol-{}", name),
&format!("http://custom-protocol-{}", name),
&format!("{}://", name),
);

Expand Down Expand Up @@ -175,7 +175,7 @@ impl InnerWebView {
// See https://github.com/MicrosoftEdge/WebView2Feedback/issues/73
url_string = url.as_str().replace(
&format!("{}://", name),
&format!("file://custom-protocol-{}", name),
&format!("http://custom-protocol-{}", name),
)
}
w.navigate(&url_string)?;
Expand Down
6 changes: 3 additions & 3 deletions src/webview/winrt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl InnerWebView {
// See https://github.com/MicrosoftEdge/WebView2Feedback/issues/73
custom_protocol_names.insert(name.clone());
w.AddWebResourceRequestedFilter(
format!("file://custom-protocol-{}*", name).as_str(),
format!("http://custom-protocol-{}*", name).as_str(),
webview2::CoreWebView2WebResourceContext::All,
)?;
let env_ = env.clone();
Expand All @@ -154,7 +154,7 @@ impl InnerWebView {
if let Ok(uri) = String::from_utf16(args.Request()?.Uri()?.as_wide()) {
// Undo the protocol workaround when giving path to resolver
let path = uri.replace(
&format!("file://custom-protocol-{}", name),
&format!("http://custom-protocol-{}", name),
&format!("{}://", name),
);

Expand Down Expand Up @@ -208,7 +208,7 @@ impl InnerWebView {
// See https://github.com/MicrosoftEdge/WebView2Feedback/issues/73
url_string = url.as_str().replace(
&format!("{}://", name),
&format!("file://custom-protocol-{}", name),
&format!("http://custom-protocol-{}", name),
)
}
w.Navigate(url_string.as_str())?;
Expand Down

0 comments on commit dd0fa46

Please sign in to comment.