2433 * webkit_web_view_run_javascript_from_gresource:
2434 * @web_view: a #WebKitWebView
2435 * @resource: the location of the resource to load
2436 * @cancellable: (allow-none): a #GCancellable or %NULL to ignore
2437 * @callback: (scope async): a #GAsyncReadyCallback to call when the script finished
2438 * @user_data: (closure): the data to pass to callback function
2439 *
2440 * Asynchronously run the script from @resource in the context of the
2441 * current page in @web_view.
2442 *
2443 * When the operation is finished, @callback will be called. You can
2444 * then call webkit_web_view_run_javascript_finish() to get the result
2445 * of the operation.
2446 */
2447void webkit_web_view_run_javascript_from_gresource(WebKitWebView* webView, const gchar* resource, GCancellable* cancellable, GAsyncReadyCallback callback, gpointer userData)
2448{
2449 g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
2450 g_return_if_fail(resource);
2451
2452 GRefPtr<GSimpleAsyncResult> result = adoptGRef(g_simple_async_result_new(G_OBJECT(webView), callback, userData,
2453 reinterpret_cast<gpointer>(webkit_web_view_run_javascript_from_gresource)));
2454 RunJavaScriptAsyncData* data = createRunJavaScriptAsyncData();
2455 data->cancellable = cancellable;
2456 g_simple_async_result_set_op_res_gpointer(result.get(), data, reinterpret_cast<GDestroyNotify>(destroyRunJavaScriptAsyncData));
2457
2458 GError* error = 0;
2459 GRefPtr<GBytes> resourceBytes = adoptGRef(g_resources_lookup_data(resource, G_RESOURCE_LOOKUP_FLAGS_NONE, &error));
2460 if (error) {
2461 g_simple_async_result_take_error(result.get(), error);
2462 g_simple_async_result_complete_in_idle(result.get());
2463 return;
2464 }
2465 getPage(webView)->runJavaScriptInMainFrame(String::fromUTF8(reinterpret_cast<const gchar*>(g_bytes_get_data(resourceBytes.get(), 0))),
2466 ScriptValueCallback::create(result.leakRef(), webkitWebViewRunJavaScriptCallback));
2467}
2468
2469/**
2470 * webkit_web_view_run_javascript_from_gresource_finish:
2471 * @web_view: a #WebKitWebView
2472 * @result: a #GAsyncResult
2473 * @error: return location for error or %NULL to ignore
2474 *
2475 * Finish an asynchronous operation started with webkit_web_view_run_javascript_from_gresource().
2476 *
2477 * This is an example of using webkit_web_view_run_javascript_from_gresource() with a script returning
2478 * a number:
2479 *
2480 * <informalexample><programlisting>
2481 * static void
2482 * web_view_javascript_finished (GObject *object,
2483 * GAsyncResult *result,
2484 * gpointer user_data)
2485 * {
2486 * WebKitJavascriptResult *js_result;
2487 * JSValueRef value;
2488 * JSGlobalContextRef context;
2489 * GError *error = NULL;
2490 *
2491 * js_result = webkit_web_view_run_javascript_from_gresource_finish (WEBKIT_WEB_VIEW (object), result, &error);
2492 * if (!js_result) {
2493 * g_warning ("Error running javascript: %s", error->message);
2494 * g_error_free (error);
2495 * return;
2496 * }
2497 *
2498 * context = webkit_javascript_result_get_global_context (js_result);
2499 * value = webkit_javascript_result_get_value (js_result);
2500 * if (JSValueIsString (context, value)) {
2501 * JSStringRef js_str_value;
2502 * gchar *str_value;
2503 * gsize str_length;
2504 *
2505 * js_str_value = JSValueToStringCopy (context, value, NULL);
2506 * str_length = JSStringGetMaximumUTF8CStringSize (js_str_value);
2507 * str_value = (gchar *)g_malloc (str_length);
2508 * JSStringGetUTF8CString (js_str_value, str_value, str_length);
2509 * JSStringRelease (js_str_value);
2510 * g_print ("Script result: %s\n", str_value);
2511 * g_free (str_value);
2512 * } else {
2513 * g_warning ("Error running javascript: unexpected return value");
2514 * }
2515 * webkit_javascript_result_unref (js_result);
2516 * }
2517 *
2518 * static void
2519 * web_view_do_javascript_operation (WebKitWebView *web_view)
2520 * {
2521 * const gchar *resource = "/org/gnome/appname/resource.js";
2522 *
2523 * // Assuming a script like "a = 2.5;" is stored in the resource
2524 * webkit_web_view_run_javascript_from_gresource (web_view, resource, NULL, web_view_javascript_finished, NULL);
2525 * }
2526 * </programlisting></informalexample>
2527 *
2528 * Returns: (transfer full): a #WebKitJavascriptResult with the result of the last executed statement in @script
2529 * or %NULL in case of error
2530 */
2531WebKitJavascriptResult* webkit_web_view_run_javascript_from_gresource_finish(WebKitWebView* webView, GAsyncResult* result, GError** error)
2532{
2533 g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), 0);
2534 g_return_val_if_fail(G_IS_ASYNC_RESULT(result), 0);
2535
2536 GSimpleAsyncResult* simpleResult = G_SIMPLE_ASYNC_RESULT(result);
2537 g_warn_if_fail(g_simple_async_result_get_source_tag(simpleResult) == webkit_web_view_run_javascript_from_gresource);
2538
2539 if (g_simple_async_result_propagate_error(simpleResult, error))
2540 return 0;
2541
2542 RunJavaScriptAsyncData* data = static_cast<RunJavaScriptAsyncData*>(g_simple_async_result_get_op_res_gpointer(simpleResult));
2543 return data->scriptResult ? webkit_javascript_result_ref(data->scriptResult) : 0;
2544}
2545
2546/**