Skip to content

Commit

Permalink
webview: Added callback for when a javascript error is encountered
Browse files Browse the repository at this point in the history
[RDMLI-2895]
CR: DRinguet
  • Loading branch information
nparr-devo authored and awakecoding committed Aug 13, 2024
1 parent fa06788 commit d440c9c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
13 changes: 13 additions & 0 deletions recipes/webview/src/WebView.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ typedef struct web_view{
callback_decide_policy_evnt_fn decide_policy_handler;
callback_decide_new_window_policy_evnt_fn decide_new_window_policy_handler;
callback_js_ready_evnt_fn js_ready_handler;
callback_js_error_evnt_fn js_error_handler;
callback_context_menu_evnt_fn context_menu_handler;
callback_script_message_received_evnt_fn script_message_received_handler;
callback_get_cookies_evnt_fn get_cookie_handler;
Expand Down Expand Up @@ -87,6 +88,7 @@ LAUNCHER_EXPORT void* webview_new()
wv->load_failed_handler = 0;
wv->decide_policy_handler = 0;
wv->js_ready_handler = 0;
wv->js_error_handler = 0;
wv->clear_data_manager_finish_handler = 0;
WebKitWebContext* context = webkit_web_context_new ();
wv->view = WEBKIT_WEB_VIEW(webkit_web_view_new_with_context (context));
Expand All @@ -105,6 +107,7 @@ LAUNCHER_EXPORT void* webview_new_ephemeral()
wv->load_failed_handler = 0;
wv->decide_policy_handler = 0;
wv->js_ready_handler = 0;
wv->js_error_handler = 0;
wv->clear_data_manager_finish_handler = 0;
WebKitWebContext* context = webkit_web_context_new_ephemeral ();
wv->view = WEBKIT_WEB_VIEW(webkit_web_view_new_with_context (context));
Expand Down Expand Up @@ -210,6 +213,9 @@ static void evaluate_javascript_cb(GObject *obj, GAsyncResult *result, gpointer
g_warning ("Error running javascript: %s", error->message);
}

if(wv->js_error_handler)
wv->js_error_handler(error->message);

g_error_free (error);
return;
}
Expand Down Expand Up @@ -397,6 +403,13 @@ LAUNCHER_EXPORT bool set_callback_js_ready(void* view, callback_js_ready_evnt_fn
return 1;
}

LAUNCHER_EXPORT bool set_callback_js_error(void* view, callback_js_error_evnt_fn handler)
{
webView* wv = (webView*)view;
wv->js_error_handler = handler;
return 1;
}

LAUNCHER_EXPORT bool set_callback_clear_data_manager_finish(void* view, callback_clear_data_manager_finish_evnt_fn handler)
{
webView* wv = (webView*)view;
Expand Down
2 changes: 2 additions & 0 deletions recipes/webview/src/WebView.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#endif

typedef void (*callback_js_ready_evnt_fn)();
typedef void (*callback_js_error_evnt_fn)(char* error_message);
typedef bool (*callback_load_failed_evnt_fn)(char* failed_uri, char* message);
typedef bool (*callback_load_changed_evnt_fn)(WebKitWebView* view, WebKitLoadEvent load_event, gpointer user_data);
typedef bool (*callback_decide_policy_evnt_fn)(WebKitWebView* view, gpointer decision, WebKitPolicyDecisionType type);
Expand All @@ -30,6 +31,7 @@ LAUNCHER_EXPORT char* get_evaluate_javascript_string(void* view);
LAUNCHER_EXPORT bool set_callback_decide_policy(void* view, callback_decide_policy_evnt_fn handler);
LAUNCHER_EXPORT bool set_callback_decide_new_window_policy(void* view, callback_decide_new_window_policy_evnt_fn handler);
LAUNCHER_EXPORT bool set_callback_js_ready(void* view, callback_js_ready_evnt_fn handler);
LAUNCHER_EXPORT bool set_callback_js_error(void* view, callback_js_error_evnt_fn handler);
LAUNCHER_EXPORT bool set_callback_clear_data_manager_finish(void* view, callback_clear_data_manager_finish_evnt_fn handler);
LAUNCHER_EXPORT bool set_callback_load_changed(void* view, callback_load_changed_evnt_fn handler);
LAUNCHER_EXPORT bool set_callback_menu(void* view, callback_context_menu_evnt_fn handler);
Expand Down

0 comments on commit d440c9c

Please sign in to comment.