/******************************************************************************
 *
 *    Name: UIDialogLibWebKit.h
 *
 *    $Id: //LinuxBasedReceivers/main/ClientEngine/unix/UIDialogLib/WebKit/UIDialogLibWebKit.h#1 $
 *
 *    Purpose: A WebKit header for the UI Dialog Library.
 *
 *    Copyright 2017 Citrix Systems, Inc. All rights reserved.
 *
 *****************************************************************************/

#include <stdlib.h>

#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <webkit/webkit.h>
#include <JavaScriptCore/JavaScript.h>

typedef struct {
    const gchar *name;
    gboolean     value;
} webkit_option;

typedef struct {
    const char     *url;
    const char     *window_title;
    const char     *post_data;
    UIDIALOG       *parent;
    pthread_cond_t *wait_condition;
} webview_data;

typedef struct {
    gulong          signal;
    pthread_cond_t *wait_condition;
} callback_data;

static void releasePointer(void **ptr)
{
    if (ptr == NULL)
    {
        return;
    }

    if (*ptr != NULL)
    {
        free(*ptr);
        *ptr = NULL;
    }
}

static void *secureZeroMemory(void *ptr, size_t cnt)
{
    if (ptr == NULL
            || cnt == 0)
    {
        return ptr;
    }

    volatile char *vptr = (volatile char *)ptr;
    while (cnt > 0)
    {
        *vptr = 0;
        vptr++;
        cnt--;
    }
    return ptr;
}

static gboolean displayWebview(webview_data *webviewData);
static gboolean closeWebview();
static void configureWebview(WebKitWebView *webview);

static gboolean onKeyPressEvent(GtkWidget   *unused_widget,
                                GdkEventKey *event);
static void onWindowObjectCleared(WebKitWebView  *unused_webview,
                                  WebKitWebFrame *unused_frame,
                                  gpointer        context,
                                  gpointer        windowObject);
static void onDestroy(GtkWidget     *unused_widget,
                      callback_data *onDestroyData);
static JSValueRef onCitrixExitWebview(JSContextRef      ctx,
                                      JSObjectRef       unused_function,
                                      JSObjectRef       unused_thisObject,
                                      size_t            argumentCount,
                                      const JSValueRef  arguments[],
                                      JSValueRef       *unused_exception);

static void loadJavascriptApi(JSContextRef jsContext,
                              JSObjectRef  parentObject);
static void getStringFromJavascriptRef(const JSContextRef   ctx,
                                       const JSValueRef     arg,
                                       char               **buf);
