Skip to content
Permalink
Browse files
[GTK] Add support for running JavaScript from GResources
https://bugs.webkit.org/show_bug.cgi?id=98488

Patch by Simon Pena <spena@igalia.com> on 2012-10-09
Reviewed by Carlos Garcia Campos.

GResources allow embedding certain resources, frequently used, in a "bundle"
which can be kept separated or stored in the binary. This patch adds
support for running JavaScript from GResources.

.:

* configure.ac: Add support for compiling GResources

Source/WebKit2:

For this, methods run_javascript_from_gresource and
run_javascript_from_gresource_finish are added, which load the resource
asynchronously and, when it is ready, run the JavaScript.

* UIProcess/API/gtk/WebKitWebView.cpp: Include the new methods.
(resourcesStreamReadCallback):
(webkit_web_view_run_javascript_from_gresource):
(webkit_web_view_run_javascript_from_gresource_finish):
* UIProcess/API/gtk/WebKitWebView.h:
* UIProcess/API/gtk/docs/webkit2gtk-sections.txt: Include the new
methods in the documentation.
* UIProcess/API/gtk/tests/GNUmakefile.am: Add the test resources to the
build infrastructure.
* UIProcess/API/gtk/tests/TestMain.cpp: Register the resources in the tests,
so that they can be accessed in all of them.
(register_gresource):
(main):
* UIProcess/API/gtk/tests/TestWebKitWebView.cpp: Test the
run_javascript_from_gresource method.
(testWebViewRunJavaScript):
* UIProcess/API/gtk/tests/WebViewTest.cpp: Add a method for testing the
new run_javascript_from_gresource method.
(runJavaScriptFromGResourceReadyCallback):
(WebViewTest::runJavaScriptFromGResourceAndWaitUntilFinished):
* UIProcess/API/gtk/tests/WebViewTest.h:
* UIProcess/API/gtk/tests/resources/webkit2gtk-tests.gresource.xml: Added.
Resource bundle containing the resources.
* UIProcess/API/gtk/tests/resources/webkit2gtk-tests.test.js: Added.
Example JavaScript file to test the GResources support.

Canonical link: https://commits.webkit.org/116739@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@130755 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
Simon Pena authored and webkit-commit-queue committed Oct 9, 2012
1 parent 070385d commit 8d7c39c2ce7c80036657e1d63c2bc2cbe3fc53f7
Showing 13 changed files with 305 additions and 85 deletions.
@@ -1,3 +1,16 @@
2012-10-09 Simon Pena <spena@igalia.com>

[GTK] Add support for running JavaScript from GResources
https://bugs.webkit.org/show_bug.cgi?id=98488

Reviewed by Carlos Garcia Campos.

GResources allow embedding certain resources, frequently used, in a "bundle"
which can be kept separated or stored in the binary. This patch adds
support for running JavaScript from GResources.

* configure.ac: Add support for compiling GResources

2012-10-09 Zan Dobersek <zandobersek@gmail.com>

Unreviewed GTK build fix after r130689.
@@ -1,3 +1,45 @@
2012-10-09 Simon Pena <spena@igalia.com>

[GTK] Add support for running JavaScript from GResources
https://bugs.webkit.org/show_bug.cgi?id=98488

Reviewed by Carlos Garcia Campos.

GResources allow embedding certain resources, frequently used, in a "bundle"
which can be kept separated or stored in the binary. This patch adds
support for running JavaScript from GResources.

For this, methods run_javascript_from_gresource and
run_javascript_from_gresource_finish are added, which load the resource
asynchronously and, when it is ready, run the JavaScript.

* UIProcess/API/gtk/WebKitWebView.cpp: Include the new methods.
(resourcesStreamReadCallback):
(webkit_web_view_run_javascript_from_gresource):
(webkit_web_view_run_javascript_from_gresource_finish):
* UIProcess/API/gtk/WebKitWebView.h:
* UIProcess/API/gtk/docs/webkit2gtk-sections.txt: Include the new
methods in the documentation.
* UIProcess/API/gtk/tests/GNUmakefile.am: Add the test resources to the
build infrastructure.
* UIProcess/API/gtk/tests/TestMain.cpp: Register the resources in the tests,
so that they can be accessed in all of them.
(register_gresource):
(main):
* UIProcess/API/gtk/tests/TestWebKitWebView.cpp: Test the
run_javascript_from_gresource method.
(testWebViewRunJavaScript):
* UIProcess/API/gtk/tests/WebViewTest.cpp: Add a method for testing the
new run_javascript_from_gresource method.
(runJavaScriptFromGResourceReadyCallback):
(WebViewTest::runJavaScriptFromGResourceAndWaitUntilFinished):
* UIProcess/API/gtk/tests/WebViewTest.h:
* UIProcess/API/gtk/tests/resources/webkit2gtk-tests.gresource.xml: Added.
Resource bundle containing the resources.
* UIProcess/API/gtk/tests/resources/webkit2gtk-tests.test.js: Added.
Example JavaScript file to test the GResources support.


2012-10-08 Kiran Muppala <cmuppala@apple.com>

Throttle DOM timers on hidden pages.
@@ -2429,6 +2429,94 @@ WebKitJavascriptResult* webkit_web_view_run_javascript_finish(WebKitWebView* web
return data->scriptResult ? webkit_javascript_result_ref(data->scriptResult) : 0;
}

static void resourcesStreamReadCallback(GObject* object, GAsyncResult* result, gpointer userData)
{
GOutputStream* outputStream = G_OUTPUT_STREAM(object);
GRefPtr<GSimpleAsyncResult> runJavascriptResult = adoptGRef(G_SIMPLE_ASYNC_RESULT(userData));

GError* error = 0;
g_output_stream_splice_finish(outputStream, result, &error);
if (error) {
g_simple_async_result_take_error(runJavascriptResult.get(), error);
g_simple_async_result_complete(runJavascriptResult.get());
return;
}

GRefPtr<WebKitWebView> webView = adoptGRef(WEBKIT_WEB_VIEW(g_async_result_get_source_object(G_ASYNC_RESULT(runJavascriptResult.get()))));
gpointer outputStreamData = g_memory_output_stream_get_data(G_MEMORY_OUTPUT_STREAM(outputStream));
getPage(webView.get())->runJavaScriptInMainFrame(String::fromUTF8(reinterpret_cast<const gchar*>(outputStreamData)),
ScriptValueCallback::create(runJavascriptResult.leakRef(), webkitWebViewRunJavaScriptCallback));
}

/**
* webkit_web_view_run_javascript_from_gresource:
* @web_view: a #WebKitWebView
* @resource: the location of the resource to load
* @cancellable: (allow-none): a #GCancellable or %NULL to ignore
* @callback: (scope async): a #GAsyncReadyCallback to call when the script finished
* @user_data: (closure): the data to pass to callback function
*
* Asynchronously run the script from @resource in the context of the
* current page in @web_view.
*
* When the operation is finished, @callback will be called. You can
* then call webkit_web_view_run_javascript_from_gresource_finish() to get the result
* of the operation.
*/
void webkit_web_view_run_javascript_from_gresource(WebKitWebView* webView, const gchar* resource, GCancellable* cancellable, GAsyncReadyCallback callback, gpointer userData)
{
g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
g_return_if_fail(resource);

GRefPtr<GSimpleAsyncResult> result = adoptGRef(g_simple_async_result_new(G_OBJECT(webView), callback, userData,
reinterpret_cast<gpointer>(webkit_web_view_run_javascript_from_gresource)));
RunJavaScriptAsyncData* data = createRunJavaScriptAsyncData();
data->cancellable = cancellable;
g_simple_async_result_set_op_res_gpointer(result.get(), data, reinterpret_cast<GDestroyNotify>(destroyRunJavaScriptAsyncData));

GError* error = 0;
GRefPtr<GInputStream> inputStream = adoptGRef(g_resources_open_stream(resource, G_RESOURCE_LOOKUP_FLAGS_NONE, &error));
if (error) {
g_simple_async_result_take_error(result.get(), error);
g_simple_async_result_complete_in_idle(result.get());
return;
}

GRefPtr<GOutputStream> outputStream = adoptGRef(g_memory_output_stream_new(0, 0, fastRealloc, fastFree));
g_output_stream_splice_async(outputStream.get(), inputStream.get(),
static_cast<GOutputStreamSpliceFlags>(G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET),
G_PRIORITY_DEFAULT,
cancellable, resourcesStreamReadCallback, result.leakRef());
}

/**
* webkit_web_view_run_javascript_from_gresource_finish:
* @web_view: a #WebKitWebView
* @result: a #GAsyncResult
* @error: return location for error or %NULL to ignore
*
* Finish an asynchronous operation started with webkit_web_view_run_javascript_from_gresource().
*
* Check webkit_web_view_run_javascript_finish() for a usage example.
*
* Returns: (transfer full): a #WebKitJavascriptResult with the result of the last executed statement in @script
* or %NULL in case of error
*/
WebKitJavascriptResult* webkit_web_view_run_javascript_from_gresource_finish(WebKitWebView* webView, GAsyncResult* result, GError** error)
{
g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), 0);
g_return_val_if_fail(G_IS_ASYNC_RESULT(result), 0);

GSimpleAsyncResult* simpleResult = G_SIMPLE_ASYNC_RESULT(result);
g_warn_if_fail(g_simple_async_result_get_source_tag(simpleResult) == webkit_web_view_run_javascript_from_gresource);

if (g_simple_async_result_propagate_error(simpleResult, error))
return 0;

RunJavaScriptAsyncData* data = static_cast<RunJavaScriptAsyncData*>(g_simple_async_result_get_op_res_gpointer(simpleResult));
return data->scriptResult ? webkit_javascript_result_ref(data->scriptResult) : 0;
}

/**
* webkit_web_view_get_main_resource:
* @web_view: a #WebKitWebView

0 comments on commit 8d7c39c

Please sign in to comment.