Skip to content

Commit

Permalink
refactor signature
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Dec 31, 2022
1 parent 3ee0c74 commit c9e1a61
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 56 deletions.
4 changes: 2 additions & 2 deletions .changes/load_url_with_headers.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
"wry": "patch"
"wry": "minor"
---

Add `WebViewBuilder::with_headers` and `WebView::load_url_with_headers` to navigate to urls with headers.
Add headers argument to `WebViewBuilder::with_url`, `WebViewBuilder::with_html` and `WebView::load_url`.
8 changes: 2 additions & 6 deletions src/webview/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,8 @@ impl InnerWebView {
Ok(())
}

pub fn load_url(&self, url: &str) {
MainPipe::send(WebViewMessage::LoadUrl(url.to_string(), None));
}

pub fn load_url_with_headers(&self, url: &str, headers: http::HeaderMap) {
MainPipe::send(WebViewMessage::LoadUrl(url.to_string(), Some(headers)));
pub fn load_url(&self, url: &str, headers: Option<http::HeaderMap>) {
MainPipe::send(WebViewMessage::LoadUrl(url.to_string(), headers));
}
}

Expand Down
28 changes: 12 additions & 16 deletions src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,21 +439,16 @@ impl<'a> WebViewBuilder<'a> {
self
}

/// Specify headers used when loading the requested `url`.
pub fn with_headers(mut self, headers: http::HeaderMap) -> Self {
self.webview.headers = Some(headers);
self
}

/// Load the provided URL when the builder calling [`WebViewBuilder::build`] to create the
/// [`WebView`]. The provided URL must be valid. This will be ignored if `url` or `request` is already provided.
pub fn with_url(mut self, url: &str) -> Result<Self> {
/// [`WebView`]. The provided URL must be valid.
pub fn with_url(mut self, url: &str, headers: Option<http::HeaderMap>) -> Result<Self> {
self.webview.url = Some(Url::parse(url)?);
self.webview.headers = headers;
Ok(self)
}

/// Load the provided HTML string when the builder calling [`WebViewBuilder::build`] to create the
/// [`WebView`]. This will be ignored if `url` or `request` is already provided.
/// [`WebView`]. This will be ignored if `url` is already provided.
///
/// # Warning
/// The Page loaded from html string will have different Origin on different platforms. And
Expand All @@ -470,8 +465,13 @@ impl<'a> WebViewBuilder<'a> {
/// ## PLatform-specific:
///
/// - **Windows:** the string can not be larger than 2 MB (2 * 1024 * 1024 bytes) in total size
pub fn with_html(mut self, html: impl Into<String>) -> Result<Self> {
pub fn with_html(
mut self,
html: impl Into<String>,
headers: Option<http::HeaderMap>,
) -> Result<Self> {
self.webview.html = Some(html.into());
self.webview.headers = headers;
Ok(self)
}

Expand Down Expand Up @@ -803,12 +803,8 @@ impl WebView {
self.webview.set_background_color(background_color)
}

pub fn load_url(&self, url: &str) {
self.webview.load_url(url)
}

pub fn load_url_with_headers(&self, url: &str, headers: http::HeaderMap) {
self.webview.load_url_with_headers(url, headers)
pub fn load_url(&self, url: &str, headers: Option<http::HeaderMap>) {
self.webview.load_url(url, headers)
}
}

Expand Down
30 changes: 15 additions & 15 deletions src/webview/webkitgtk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,23 +457,23 @@ impl InnerWebView {
Ok(())
}

pub fn load_url(&self, url: &str) {
self.webview.load_uri(url)
}

pub fn load_url_with_headers(&self, url: &str, headers: http::HeaderMap) {
let req = URIRequest::builder().uri(url).build();

if let Some(ref mut req_headers) = req.http_headers() {
for (header, value) in headers.iter() {
req_headers.append(
header.to_string().as_str(),
value.to_str().unwrap_or_default(),
);
pub fn load_url(&self, url: &str, headers: Option<http::HeaderMap>) {
if let Some(headers) = headers {
let req = URIRequest::builder().uri(url).build();

if let Some(ref mut req_headers) = req.http_headers() {
for (header, value) in headers.iter() {
req_headers.append(
header.to_string().as_str(),
value.to_str().unwrap_or_default(),
);
}
}
}

self.webview.load_request(&req);
self.webview.load_request(&req);
} else {
self.webview.load_uri(url)
}
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/webview/webview2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -852,13 +852,13 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_
set_background_color(&self.controller, background_color).map_err(Into::into)
}

pub fn load_url(&self, url: &str) {
let url = encode_wide(url);
let _ = unsafe { self.webview.Navigate(PCWSTR::from_raw(url.as_ptr())) };
}

pub fn load_url_with_headers(&self, url: &str, headers: http::HeaderMap) {
load_url_with_headers(&self.webview, &self.env, url, headers);
pub fn load_url(&self, url: &str, headers: Option<http::HeaderMap>) {
if let Some(headers) = headers {
load_url_with_headers(&self.webview, &self.env, url, headers);
} else {
let url = encode_wide(url);
let _ = unsafe { self.webview.Navigate(PCWSTR::from_raw(url.as_ptr())) };
}
}

pub fn set_theme(&self, theme: Theme) {
Expand Down
12 changes: 2 additions & 10 deletions src/webview/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ r#"Object.defineProperty(window, 'ipc', {
w.navigate_to_string(path);
}
} else {
w.navigate_to_url(url.as_str(), attributes.headers);
w.load_url(url.as_str(), attributes.headers);
}
} else if let Some(html) = attributes.html {
w.navigate_to_string(&html);
Expand Down Expand Up @@ -871,15 +871,7 @@ r#"Object.defineProperty(window, 'ipc', {
}
}

pub fn load_url(&self, url: &str) {
self.navigate_to_url(url, None)
}

pub fn load_url_with_headers(&self, url: &str, headers: http::HeaderMap) {
self.navigate_to_url(url, Some(headers))
}

fn navigate_to_url(&self, url: &str, headers: Option<http::HeaderMap>) {
pub fn load_url(&self, url: &str, headers: Option<http::HeaderMap>) {
// Safety: objc runtime calls are unsafe
unsafe {
let url: id = msg_send![class!(NSURL), URLWithString: NSString::new(url)];
Expand Down

0 comments on commit c9e1a61

Please sign in to comment.