MCPcopy Create free account
hub / github.com/axmolengine/axmol / createWebView

Method createWebView

core/ui/UIWebView/UIWebViewImpl-linux.cpp:383–467  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

381 }
382
383 bool createWebView(const ContinueUrlCallback_t& shouldStartLoading,
384 const GeneralUrlCallback_t& didFinishLoading,
385 const GeneralUrlCallback_t didFailLoading,
386 const GeneralUrlCallback_t& onJsCallback)
387 {
388 if (m_Window == nullptr)
389 return false;
390
391 m_WebView = webkit_web_view_new();
392 if (m_WebView == nullptr)
393 return false;
394
395 WebKitUserContentManager* manager = m_UserContentManager =
396 webkit_web_view_get_user_content_manager(WEBKIT_WEB_VIEW(m_WebView));
397 if (manager == nullptr)
398 return false;
399
400 g_signal_connect(manager, "script-message-received::__webview__",
401 G_CALLBACK(+[](WebKitUserContentManager*, WebKitJavascriptResult* r, gpointer arg) {
402 auto* w = static_cast<GTKWebKit*>(arg);
403 // webkit2 2.22+
404 JSCValue* value = webkit_javascript_result_get_js_value(r);
405 char* s = jsc_value_to_string(value);
406 w->onMessage(s);
407 g_free(s);
408 }),
409 this);
410 webkit_user_content_manager_register_script_message_handler(manager, "__webview__");
411 initScript("function(message){return window.webkit.messageHandlers.__webview__.postMessage(message);}");
412
413 m_ShouldStartLoading = shouldStartLoading;
414 m_DidFinishLoading = didFinishLoading;
415 m_DidFailLoading = didFailLoading;
416 m_OnJsCallback = onJsCallback;
417 g_signal_connect(m_WebView, "load-changed",
418 G_CALLBACK(+[](WebKitWebView* webView, WebKitLoadEvent loadEvent, gpointer userData) {
419 auto thiz = static_cast<GTKWebKit*>(userData);
420 switch (loadEvent)
421 {
422 case WEBKIT_LOAD_COMMITTED:
423 {
424 std::string_view uri = webkit_web_view_get_uri(webView);
425 const auto scheme = uri.substr(0, uri.find_first_of(':'));
426
427 if (scheme == thiz->m_JsScheme)
428 {
429 thiz->m_OnJsCallback(uri);
430 return;
431 }
432
433 if (!thiz->m_ShouldStartLoading(uri))
434 webkit_web_view_stop_loading(webView);
435 break;
436 }
437 case WEBKIT_LOAD_FINISHED:
438 {
439 std::string_view uri = webkit_web_view_get_uri(webView);
440 thiz->m_DidFinishLoading(uri);

Callers 1

WebViewImplMethod · 0.45

Calls 3

onMessageMethod · 0.45
substrMethod · 0.45
find_first_ofMethod · 0.45

Tested by

no test coverage detected