2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-12-19 08:51:50 +00:00

chore: improve webview code style

This commit is contained in:
Jan Laupetin
2025-10-05 23:23:37 +01:00
parent cb2e83dfca
commit aa0134c341
7 changed files with 142 additions and 148 deletions

View File

@@ -20,43 +20,17 @@ namespace
{
std::unordered_map<std::string, UiFile> assetLookup;
const char* ContentTypeForAssetName(const std::string& assetName)
{
const char* mimeType;
if (assetName.ends_with(".html"))
{
mimeType = "text/html";
}
else if (assetName.ends_with(".js"))
{
mimeType = "text/javascript";
}
else if (assetName.ends_with(".css"))
{
mimeType = "text/css";
}
else
{
mimeType = "application/octet-stream";
}
return mimeType;
}
void ModManUriSchemeRequestCb(WebKitURISchemeRequest* request, gpointer user_data)
{
const gchar* asset = webkit_uri_scheme_request_get_path(request);
std::cout << std::format("Modman request: {}\n", asset);
const auto foundUiFile = assetLookup.find(asset);
if (foundUiFile != assetLookup.end())
{
gsize stream_length = foundUiFile->second.dataSize;
GInputStream* stream = g_memory_input_stream_new_from_data(foundUiFile->second.data, foundUiFile->second.dataSize, nullptr);
webkit_uri_scheme_request_finish(request, stream, stream_length, ContentTypeForAssetName(foundUiFile->second.filename));
webkit_uri_scheme_request_finish(request, stream, stream_length, GetMimeTypeForFileName(foundUiFile->second.filename));
g_object_unref(stream);
}
else
@@ -67,24 +41,6 @@ namespace
return;
}
}
void OnResourceLoadStarted(WebKitWebView* self, WebKitWebResource* resource, WebKitURIRequest* request, gpointer user_data)
{
std::cout << webkit_uri_request_get_http_method(request) << " " << webkit_uri_request_get_uri(request) << "\n";
std::string uri(webkit_uri_request_get_uri(request));
if (uri.starts_with("http://"))
{
uri = std::format("modman://{}", uri.substr(7));
}
else if (uri.starts_with("https://"))
{
uri = std::format("modman://{}", uri.substr(8));
}
std::cout << std::format("Setting uri: {}\n", uri);
webkit_uri_request_set_uri(request, uri.c_str());
}
} // namespace
namespace gtk
@@ -97,8 +53,6 @@ namespace gtk
assetLookup = BuildUiFileLookup();
g_signal_connect(webView, "resource-load-started", G_CALLBACK(OnResourceLoadStarted), NULL);
webkit_web_context_register_uri_scheme(context, "modman", ModManUriSchemeRequestCb, NULL, nullptr);
}
} // namespace gtk

View File

@@ -12,7 +12,9 @@
namespace gtk
{
constexpr auto URL_PREFIX = "modman://localhost/";
void InstallCustomProtocolHandler(webview::webview& wv);
}
} // namespace gtk
#endif