Example #1
0
static void web_view_javascript_finished(GObject      *object,
                              GAsyncResult *result,
                              gpointer      user_data)
{
    WebKitJavascriptResult *js_result;
    JSValueRef              value;
    JSGlobalContextRef      context;
    GError                 *error = NULL;

    js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object), result, &error);
    if (!js_result) {
        g_warning ("Error running javascript: %s", error->message);
        g_error_free (error);
        return;
    }

    context = webkit_javascript_result_get_global_context(js_result);
    value = webkit_javascript_result_get_value(js_result);
    if (JSValueIsString(context, value)) {
        JSStringRef js_str_value;
        gchar      *str_value;
        gsize       str_length;

        js_str_value = JSValueToStringCopy(context, value, NULL);
        str_length = JSStringGetMaximumUTF8CStringSize(js_str_value);
        str_value = (gchar *)g_malloc(str_length);
        JSStringGetUTF8CString(js_str_value, str_value, str_length);
        JSStringRelease(js_str_value);
        g_free (str_value);
    } else {
        g_warning("Error running javascript: unexpected return value");
    }
    webkit_javascript_result_unref(js_result);
}
Example #2
0
WebViewTest::~WebViewTest()
{
    if (m_parentWindow)
        gtk_widget_destroy(m_parentWindow);
    if (m_javascriptResult)
        webkit_javascript_result_unref(m_javascriptResult);
    g_object_unref(m_webView);
    g_main_loop_unref(m_mainLoop);
}
Example #3
0
WebKitJavascriptResult* WebViewTest::runJavaScriptFromGResourceAndWaitUntilFinished(const char* resource, GError** error)
{
    if (m_javascriptResult)
        webkit_javascript_result_unref(m_javascriptResult);
    m_javascriptResult = 0;
    m_javascriptError = error;
    webkit_web_view_run_javascript_from_gresource(m_webView, resource, 0, reinterpret_cast<GAsyncReadyCallback>(runJavaScriptFromGResourceReadyCallback), this);
    g_main_loop_run(m_mainLoop);

    return m_javascriptResult;
}
Example #4
0
static void
js_cb (GObject *src, GAsyncResult *res, gpointer data)
{
	WebKitJavascriptResult *jsres;
	GError *err = NULL;

	jsres = webkit_web_view_run_javascript_finish (WEBKIT_WEB_VIEW (src), res, &err);
	if (!jsres) {
		g_printerr ("Error running javascript: %s\n", err->message);
		g_error_free (err);
		return;
	}
	webkit_javascript_result_unref (jsres);
}
static void get_javascript_result(
    GObject *object,
    GAsyncResult *result,
    gpointer user_data)
{
  g_printf("Here? Surely?\n");
  WebKitJavascriptResult *js_result;
  JSValueRef              value;
  JSGlobalContextRef      context;
  GError                 *error = NULL;

  js_result = webkit_web_view_run_javascript_finish (WEBKIT_WEB_VIEW (object), result, &error);
  if (!js_result) {
    g_warning ("Error running javascript: %s", error->message);
    g_error_free (error);
    return;
  }

  g_printf("I'm here innit 1\n");
  context = webkit_javascript_result_get_global_context (js_result);
  g_printf("I'm here innit 2\n");
  value = webkit_javascript_result_get_value (js_result);
  int value_type = JSValueGetType(context, value);
  switch (value_type) {
    case kJSTypeUndefined:
      g_printf("Value undefined!\n");
      break;
    case kJSTypeNull:
      g_printf("Value null!\n");
      break;
    case kJSTypeBoolean:
      g_printf("Value boolean!\n");
      break;
    case kJSTypeNumber:
      g_printf("Value number!\n");
      break;
    case kJSTypeString:
      g_printf("Value string!\n");
      break;
    case kJSTypeObject:
      g_printf("Value object!\n");
      break;
  }

  if (JSValueIsString (context, value)) {
    g_printf("I'm here innit 3\n");
    JSStringRef js_str_value = JSValueToStringCopy(context, value, NULL);
    gsize       str_length = JSStringGetMaximumUTF8CStringSize(js_str_value);
    gchar      *str_value;

    g_printf("String length: %d\n", str_length);
    str_value = (gchar *)g_malloc (str_length);
    JSStringGetUTF8CString (js_str_value, str_value, str_length);
    JSStringRelease (js_str_value);

    g_print ("Script result: %s\n", str_value);
    g_free (str_value);
  } else {
    g_warning ("Error running javascript: unexpected return value");
  }
  webkit_javascript_result_unref (js_result);
}
Example #6
0
static void testWebContextSecurityFileXHR(WebViewTest* test, gconstpointer)
{
    GUniquePtr<char> fileURL(g_strdup_printf("file://%s/simple.html", Test::getResourcesDir(Test::WebKit2Resources).data()));
    test->loadURI(fileURL.get());
    test->waitUntilLoadFinished();

    GUniquePtr<char> jsonURL(g_strdup_printf("file://%s/simple.json", Test::getResourcesDir().data()));
    GUniquePtr<char> xhr(g_strdup_printf("var xhr = new XMLHttpRequest; xhr.open(\"GET\", \"%s\"); xhr.send();", jsonURL.get()));

    WebKitJavascriptResult* consoleMessage = nullptr;
    webkit_user_content_manager_register_script_message_handler(test->m_userContentManager.get(), "console");
    g_signal_connect(test->m_userContentManager.get(), "script-message-received::console", G_CALLBACK(consoleMessageReceivedCallback), &consoleMessage);

    // By default file access is not allowed, this will show a console message with a cross-origin error.
    GUniqueOutPtr<GError> error;
    WebKitJavascriptResult* javascriptResult = test->runJavaScriptAndWaitUntilFinished(xhr.get(), &error.outPtr());
    g_assert(javascriptResult);
    g_assert(!error);
    g_assert(consoleMessage);
    GUniquePtr<char> messageString(WebViewTest::javascriptResultToCString(consoleMessage));
    GRefPtr<GVariant> variant = g_variant_parse(G_VARIANT_TYPE("(uusus)"), messageString.get(), nullptr, nullptr, nullptr);
    g_assert(variant.get());
    unsigned level;
    const char* messageText;
    g_variant_get(variant.get(), "(uu&su&s)", nullptr, &level, &messageText, nullptr, nullptr);
    g_assert_cmpuint(level, ==, 3); // Console error message.
    GUniquePtr<char> expectedErrorMessage(g_strdup_printf("XMLHttpRequest cannot load %s. Cross origin requests are only supported for HTTP.", jsonURL.get()));
    g_assert_cmpstr(messageText, ==, expectedErrorMessage.get());
    webkit_javascript_result_unref(consoleMessage);
    consoleMessage = nullptr;
    level = 0;
    messageText = nullptr;
    variant = nullptr;

    // Allow file access from file URLs.
    webkit_settings_set_allow_file_access_from_file_urls(webkit_web_view_get_settings(test->m_webView), TRUE);
    test->loadURI(fileURL.get());
    test->waitUntilLoadFinished();
    javascriptResult = test->runJavaScriptAndWaitUntilFinished(xhr.get(), &error.outPtr());
    g_assert(javascriptResult);
    g_assert(!error);

    // It isn't still possible to load file from an HTTP URL.
    test->loadURI(kServer->getURIForPath("/").data());
    test->waitUntilLoadFinished();
    javascriptResult = test->runJavaScriptAndWaitUntilFinished(xhr.get(), &error.outPtr());
    g_assert(javascriptResult);
    g_assert(!error);
    g_assert(consoleMessage);
    variant = g_variant_parse(G_VARIANT_TYPE("(uusus)"), messageString.get(), nullptr, nullptr, nullptr);
    g_assert(variant.get());
    g_variant_get(variant.get(), "(uu&su&s)", nullptr, &level, &messageText, nullptr, nullptr);
    g_assert_cmpuint(level, ==, 3); // Console error message.
    g_assert_cmpstr(messageText, ==, expectedErrorMessage.get());
    webkit_javascript_result_unref(consoleMessage);

    g_signal_handlers_disconnect_matched(test->m_userContentManager.get(), G_SIGNAL_MATCH_DATA, 0, 0, nullptr, nullptr, &consoleMessage);
    webkit_user_content_manager_unregister_script_message_handler(test->m_userContentManager.get(), "console");

    webkit_settings_set_allow_file_access_from_file_urls(webkit_web_view_get_settings(test->m_webView), FALSE);
}