Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: custom uri scheme and resource domain #1

Merged
merged 2 commits into from Mar 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 19 additions & 3 deletions gui/choc_WebView.h
Expand Up @@ -82,6 +82,14 @@ class WebView
// this empty for default behaviour.
std::string customUserAgent;

/// [Mac only] Optional application URI scheme which can be used to override
// the default. Leave this empty for default behaviour.
std::string customUriScheme;

/// Optional domain to use for fetching local resources which can be used to
// override the default. Leave this empty for default behaviour.
std::string customResourceDomain;

/// Serve resources to the browser from a C++ callback function.
/// This can effectively be used to implement a basic web server,
/// serving resources to the browser in any way the client code chooses.
Expand Down Expand Up @@ -351,8 +359,9 @@ struct choc::ui::WebView::Pimpl
call<void> (manager, "retain");
call<void> (manager, "addScriptMessageHandler:name:", delegate, getNSString ("external"));

const auto uriScheme = options.customUriScheme.empty() ? "choc" : options.customUriScheme;
if (options.fetchResource)
call<void> (config, "setURLSchemeHandler:forURLScheme:", delegate, getNSString ("choc"));
call<void> (config, "setURLSchemeHandler:forURLScheme:", delegate, getNSString (uriScheme));

webview = call<id> (allocateWebview(), "initWithFrame:configuration:", CGRect(), config);
objc_setAssociatedObject (webview, "choc_webview", (id) this, OBJC_ASSOCIATION_ASSIGN);
Expand All @@ -366,7 +375,11 @@ struct choc::ui::WebView::Pimpl
call<void> (config, "release");

if (options.fetchResource)
navigate ("choc://choc.choc/");
{
const auto resourceDomain = options.customResourceDomain.empty() ? "choc.choc" : options.customResourceDomain;
const auto navigationUrl = uriScheme + "://" + resourceDomain + '/';
navigate (navigationUrl);
}
}

~Pimpl()
Expand Down Expand Up @@ -1019,6 +1032,9 @@ struct WebView::Pimpl
Pimpl (WebView& v, const Options& options)
: owner (v), fetchResource (options.fetchResource), customUserAgent (options.customUserAgent)
{
if (!options.customResourceDomain.empty())
resourceRequestFilterUriPrefix = "https://" + options.customResourceDomain + "/";

// You cam define this macro to provide a custom way of getting a
// choc::file::DynamicLibrary that contains the redistributable
// Microsoft WebView2Loader.dll
Expand Down Expand Up @@ -1093,7 +1109,7 @@ struct WebView::Pimpl
private:
WindowClass windowClass { L"CHOCWebView", (WNDPROC) wndProc };
HWNDHolder hwnd;
const std::string resourceRequestFilterUriPrefix = "https://choc.localhost/";
std::string resourceRequestFilterUriPrefix = "https://choc.localhost/";

static Pimpl* getPimpl (HWND h) { return (Pimpl*) GetWindowLongPtr (h, GWLP_USERDATA); }

Expand Down