Skip to content

Commit

Permalink
Merge pull request #1 from splice/alexwood/custom-uri-origin
Browse files Browse the repository at this point in the history
feat: custom uri scheme and resource domain
  • Loading branch information
awood314 committed Mar 6, 2024
2 parents d029a83 + 1df7d89 commit 3c23805
Showing 1 changed file with 19 additions and 3 deletions.
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

0 comments on commit 3c23805

Please sign in to comment.