⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 145358 in webkit


Ignore:
Timestamp:
Mar 11, 2013, 6:07:19 AM (14 years ago)
Author:
Carlos Garcia Campos
Message:

[GTK][WK2] Add API to retrieve a snapshot from a webview
​https://bugs.webkit.org/show_bug.cgi?id=98270

Patch by Claudio Saavedra <Claudio Saavedra> on 2013-03-10
Reviewed by Carlos Garcia Campos.

This adds the GTK+ API necessary to retrieve a snapshot from a
webview asynchronously. The API uses the injected bundle
internally to get the snapshot from the WebProcess.

  • UIProcess/API/gtk/WebKitError.cpp:

(webkit_snapshot_error_quark): Add snapshot API related error
quark.

  • UIProcess/API/gtk/WebKitError.h: Add snapshot error handling.
  • UIProcess/API/gtk/WebKitInjectedBundleClient.cpp:

(didReceiveWebViewMessageFromInjectedBundle): Handle the new
"DidGetSnapshot" message.

  • UIProcess/API/gtk/WebKitPrivate.h: Add SnapshotRegion enum.
  • UIProcess/API/gtk/WebKitWebView.cpp:

(_WebKitWebViewPrivate): Add a map for the snapshot results.
(GetSnapshotAsyncData):
(webKitWebViewDidReceiveSnapshot):
(webKitSnapshotRegionToSnapshotRegion): Helper method for casting
the region option enumeration.
(generateSnapshotCallbackID): Method to generate unique callback
ids.
(webkit_web_view_get_snapshot):
(webkit_web_view_get_snapshot_finish): New snapshotting API.

  • UIProcess/API/gtk/WebKitWebView.h: Ditto.
  • UIProcess/API/gtk/WebKitWebViewPrivate.h: Add the private method

to handle a received snapshot.

  • UIProcess/API/gtk/docs/webkit2gtk-sections.txt: Add the new API

bits.

  • UIProcess/API/gtk/tests/TestWebKitWebView.cpp:

(cairoSurfacesEqual): Add helper to compare cairo_surface_t
structs.
(testWebViewSnapshot): New test.
(beforeAll): Add the new test.

  • UIProcess/API/gtk/tests/WebViewTest.cpp:

(WebViewTest::selectAll): Add method to help test snapshots
including selection.

  • UIProcess/API/gtk/tests/WebViewTest.h: Ditto
  • WebProcess/InjectedBundle/API/gtk/WebKitWebExtension.cpp:

(webkitWebExtensionDidReceiveMessageToPage): Add this method. It
handles the new message "GetSnapshot".
(didReceiveMessageToPage): Ditto.
(webkitWebExtensionCreate): Register method above.

Location:
releases/WebKitGTK/webkit-2.0/Source/WebKit2
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • releases/WebKitGTK/webkit-2.0/Source/WebKit2/ChangeLog

    r145324 r145358  
     12013-03-10  Claudio Saavedra  <csaavedra@igalia.com>
     2
     3        [GTK][WK2] Add API to retrieve a snapshot from a webview
     4        https://bugs.webkit.org/show_bug.cgi?id=98270
     5
     6        Reviewed by Carlos Garcia Campos.
     7
     8        This adds the GTK+ API necessary to retrieve a snapshot from a
     9        webview asynchronously. The API uses the injected bundle
     10        internally to get the snapshot from the WebProcess.
     11
     12        * UIProcess/API/gtk/WebKitError.cpp:
     13        (webkit_snapshot_error_quark): Add snapshot API related error
     14        quark.
     15        * UIProcess/API/gtk/WebKitError.h: Add snapshot error handling.
     16        * UIProcess/API/gtk/WebKitInjectedBundleClient.cpp:
     17        (didReceiveWebViewMessageFromInjectedBundle): Handle the new
     18        "DidGetSnapshot" message.
     19        * UIProcess/API/gtk/WebKitPrivate.h: Add SnapshotRegion enum.
     20        * UIProcess/API/gtk/WebKitWebView.cpp:
     21        (_WebKitWebViewPrivate): Add a map for the snapshot results.
     22        (GetSnapshotAsyncData):
     23        (webKitWebViewDidReceiveSnapshot):
     24        (webKitSnapshotRegionToSnapshotRegion): Helper method for casting
     25        the region option enumeration.
     26        (generateSnapshotCallbackID): Method to generate unique callback
     27        ids.
     28        (webkit_web_view_get_snapshot):
     29        (webkit_web_view_get_snapshot_finish): New snapshotting API.
     30        * UIProcess/API/gtk/WebKitWebView.h: Ditto.
     31        * UIProcess/API/gtk/WebKitWebViewPrivate.h: Add the private method
     32        to handle a received snapshot.
     33        * UIProcess/API/gtk/docs/webkit2gtk-sections.txt: Add the new API
     34        bits.
     35        * UIProcess/API/gtk/tests/TestWebKitWebView.cpp:
     36        (cairoSurfacesEqual): Add helper to compare cairo_surface_t
     37        structs.
     38        (testWebViewSnapshot): New test.
     39        (beforeAll): Add the new test.
     40        * UIProcess/API/gtk/tests/WebViewTest.cpp:
     41        (WebViewTest::selectAll): Add method to help test snapshots
     42        including selection.
     43        * UIProcess/API/gtk/tests/WebViewTest.h: Ditto
     44        * WebProcess/InjectedBundle/API/gtk/WebKitWebExtension.cpp:
     45        (webkitWebExtensionDidReceiveMessageToPage): Add this method. It
     46        handles the new message "GetSnapshot".
     47        (didReceiveMessageToPage): Ditto.
     48        (webkitWebExtensionCreate): Register method above.
     49
    1502013-03-10  Xan Lopez  <xan@igalia.com>
    251
  • releases/WebKitGTK/webkit-2.0/Source/WebKit2/UIProcess/API/gtk/WebKitError.cpp

    r137469 r145358  
    9292    return g_quark_from_static_string("WebKitJavascriptError");
    9393}
     94
     95GQuark webkit_snapshot_error_quark()
     96{
     97    return g_quark_from_static_string("WebKitSnapshotError");
     98}
  • releases/WebKitGTK/webkit-2.0/Source/WebKit2/UIProcess/API/gtk/WebKitError.h

    r111510 r145358  
    3636#define WEBKIT_PRINT_ERROR      webkit_print_error_quark ()
    3737#define WEBKIT_JAVASCRIPT_ERROR webkit_print_error_quark ()
     38#define WEBKIT_SNAPSHOT_ERROR   webkit_snapshot_error_quark ()
    3839
    3940/**
    … …  
    131132} WebKitJavascriptError;
    132133
     134/**
     135 * WebKitSnapshotError:
     136 * @WEBKIT_SNAPSHOT_ERROR_FAILED_TO_CREATE: An error occurred when creating a webpage snapshot.
     137 *
     138 * Enum values used to denote errors happending when creating snapshots of #WebKitWebView
     139 */
     140typedef enum {
     141    WEBKIT_SNAPSHOT_ERROR_FAILED_TO_CREATE = 799
     142} WebKitSnapshotError;
     143
    133144WEBKIT_API GQuark
    134145webkit_network_error_quark    (void);
    … …  
    149160webkit_javascript_error_quark (void);
    150161
     162WEBKIT_API GQuark
     163webkit_snapshot_error_quark   (void);
     164
    151165G_END_DECLS
    152166
  • releases/WebKitGTK/webkit-2.0/Source/WebKit2/UIProcess/API/gtk/WebKitInjectedBundleClient.cpp

    r143302 r145358  
    2121#include "WebKitInjectedBundleClient.h"
    2222
     23#include "WebImage.h"
    2324#include "WebKitURIRequestPrivate.h"
    2425#include "WebKitURIResponsePrivate.h"
    … …  
    9192        webkitWebResourceFailed(resource.get(), resourceError.get());
    9293        webkitWebViewRemoveLoadingWebResource(webView, resourceIdentifier->value());
     94    } else if (g_str_equal(messageName, "DidGetSnapshot")) {
     95        WebUInt64* callbackID = static_cast<WebUInt64*>(message.get("CallbackID"));
     96        WebImage* image = static_cast<WebImage*>(message.get("Snapshot"));
     97        webKitWebViewDidReceiveSnapshot(webView, callbackID->value(), image);
    9398    } else
    9499        ASSERT_NOT_REACHED();
  • releases/WebKitGTK/webkit-2.0/Source/WebKit2/UIProcess/API/gtk/WebKitPrivate.h

    r135565 r145358  
    121121unsigned wkEventMouseButtonToWebKitMouseButton(WKEventMouseButton);
    122122
     123enum SnapshotRegion {
     124    SnapshotRegionVisible,
     125    SnapshotRegionFullDocument
     126};
     127
    123128#endif // WebKitPrivate_h
  • releases/WebKitGTK/webkit-2.0/Source/WebKit2/UIProcess/API/gtk/WebKitWebView.cpp

    r145324 r145358  
    2222#include "WebKitWebView.h"
    2323
     24#include "ImageOptions.h"
    2425#include "PlatformCertificateInfo.h"
    2526#include "WebCertificateInfo.h"
    … …  
    136137
    137138typedef HashMap<uint64_t, GRefPtr<WebKitWebResource> > LoadingResourcesMap;
     139typedef HashMap<uint64_t, GRefPtr<GSimpleAsyncResult> > SnapshotResultsMap;
    138140
    139141struct _WebKitWebViewPrivate {
    … …  
    183185    CString faviconURI;
    184186    unsigned long faviconChangedHandlerID;
     187
     188    SnapshotResultsMap snapshotResultsMap;
    185189};
    186190
    … …  
    30243028}
    30253029
     3030struct GetSnapshotAsyncData {
     3031    GRefPtr<GCancellable> cancellable;
     3032    RefPtr<cairo_surface_t> snapshot;
     3033};
     3034WEBKIT_DEFINE_ASYNC_DATA_STRUCT(GetSnapshotAsyncData)
     3035
     3036void webKitWebViewDidReceiveSnapshot(WebKitWebView* webView, uint64_t callbackID, WebImage* webImage)
     3037{
     3038    GRefPtr<GSimpleAsyncResult> result = webView->priv->snapshotResultsMap.take(callbackID);
     3039    GetSnapshotAsyncData* data = static_cast<GetSnapshotAsyncData*>(g_simple_async_result_get_op_res_gpointer(result.get()));
     3040    GError* error = 0;
     3041    if (g_cancellable_set_error_if_cancelled(data->cancellable.get(), &error))
     3042        g_simple_async_result_take_error(result.get(), error);
     3043    else if (webImage) {
     3044        if (RefPtr<ShareableBitmap> image = webImage->bitmap())
     3045            data->snapshot = image->createCairoSurface();
     3046    } else {
     3047        g_set_error_literal(&error, WEBKIT_SNAPSHOT_ERROR, WEBKIT_SNAPSHOT_ERROR_FAILED_TO_CREATE, _("There was an error creating the snapshot"));
     3048        g_simple_async_result_take_error(result.get(), error);
     3049    }
     3050
     3051    g_simple_async_result_complete(result.get());
     3052}
     3053
     3054COMPILE_ASSERT_MATCHING_ENUM(WEBKIT_SNAPSHOT_REGION_VISIBLE, SnapshotRegionVisible);
     3055COMPILE_ASSERT_MATCHING_ENUM(WEBKIT_SNAPSHOT_REGION_FULL_DOCUMENT, SnapshotRegionFullDocument);
     3056
     3057static inline unsigned webKitSnapshotOptionsToSnapshotOptions(WebKitSnapshotOptions options)
     3058{
     3059    SnapshotOptions snapshotOptions = 0;
     3060
     3061    if (!(options & WEBKIT_SNAPSHOT_OPTIONS_INCLUDE_SELECTION_HIGHLIGHTING))
     3062        snapshotOptions |= SnapshotOptionsExcludeSelectionHighlighting;
     3063
     3064    return snapshotOptions;
     3065}
     3066
     3067static inline uint64_t generateSnapshotCallbackID()
     3068{
     3069    static uint64_t uniqueCallbackID = 1;
     3070    return uniqueCallbackID++;
     3071}
     3072
     3073/**
     3074 * webkit_web_view_get_snapshot:
     3075 * @web_view: a #WebKitWebView
     3076 * @options: #WebKitSnapshotOptions for the snapshot
     3077 * @region: the #WebKitSnapshotRegion for this snapshot
     3078 * @cancellable: (allow-none): a #GCancellable
     3079 * @callback: (scope async): a #GAsyncReadyCallback
     3080 * @user_data: (closure): user data
     3081 *
     3082 * Asynchronously retrieves a snapshot of @web_view for @region.
     3083 * @options specifies how the snapshot should be rendered.
     3084 *
     3085 * When the operation is finished, @callback will be called. You must
     3086 * call webkit_web_view_get_snapshot_finish() to get the result of the
     3087 * operation.
     3088 **/
     3089void webkit_web_view_get_snapshot(WebKitWebView* webView, WebKitSnapshotRegion region, WebKitSnapshotOptions options, GCancellable* cancellable, GAsyncReadyCallback callback, gpointer userData)
     3090{
     3091    g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
     3092
     3093    GRefPtr<GSimpleAsyncResult> result = adoptGRef(g_simple_async_result_new(G_OBJECT(webView), callback, userData,
     3094        reinterpret_cast<gpointer>(webkit_web_view_get_snapshot)));
     3095    GetSnapshotAsyncData* data = createGetSnapshotAsyncData();
     3096    data->cancellable = cancellable;
     3097    g_simple_async_result_set_op_res_gpointer(result.get(), data, reinterpret_cast<GDestroyNotify>(destroyGetSnapshotAsyncData));
     3098
     3099    ImmutableDictionary::MapType message;
     3100    uint64_t callbackID = generateSnapshotCallbackID();
     3101    message.set(String::fromUTF8("SnapshotOptions"), WebUInt64::create(static_cast<uint64_t>(webKitSnapshotOptionsToSnapshotOptions(options))));
     3102    message.set(String::fromUTF8("SnapshotRegion"), WebUInt64::create(static_cast<uint64_t>(region)));
     3103    message.set(String::fromUTF8("CallbackID"), WebUInt64::create(callbackID));
     3104
     3105    webView->priv->snapshotResultsMap.set(callbackID, result.get());
     3106    getPage(webView)->postMessageToInjectedBundle(String::fromUTF8("GetSnapshot"), ImmutableDictionary::adopt(message).get());
     3107}
     3108
     3109/**
     3110 * webkit_web_view_get_snapshot_finish:
     3111 * @web_view: a #WebKitWebView
     3112 * @result: a #GAsyncResult
     3113 * @error: return location for error or %NULL to ignore
     3114 *
     3115 * Finishes an asynchronous operation started with webkit_web_view_get_snapshot().
     3116 *
     3117 * Returns: (transfer full): a #cairo_surface_t with the retrieved snapshot or %NULL in error.
     3118 **/
     3119cairo_surface_t* webkit_web_view_get_snapshot_finish(WebKitWebView* webView, GAsyncResult* result, GError** error)
     3120{
     3121    g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), 0);
     3122    g_return_val_if_fail(G_IS_ASYNC_RESULT(result), 0);
     3123
     3124    GSimpleAsyncResult* simple = G_SIMPLE_ASYNC_RESULT(result);
     3125    g_warn_if_fail(g_simple_async_result_get_source_tag(simple) == webkit_web_view_get_snapshot);
     3126
     3127    if (g_simple_async_result_propagate_error(simple, error))
     3128        return 0;
     3129
     3130    return cairo_surface_reference(static_cast<GetSnapshotAsyncData*>(g_simple_async_result_get_op_res_gpointer(simple))->snapshot.get());
     3131}
  • releases/WebKitGTK/webkit-2.0/Source/WebKit2/UIProcess/API/gtk/WebKitWebView.h

    r145324 r145358  
    160160    WEBKIT_VIEW_MODE_SOURCE
    161161} WebKitViewMode;
     162
     163/**
     164 * WebKitSnapshotOptions:
     165 * @WEBKIT_SNAPSHOT_OPTIONS_NONE: Do not include any special options.
     166 * @WEBKIT_SNAPSHOT_OPTIONS_INCLUDE_SELECTION_HIGHLIGHTING: Whether to include in the
     167 * snapshot the highlight of the selected content.
     168 *
     169 * Enum values used to specify options when taking a snapshot
     170 * from a #WebKitWebView.
     171 */
     172typedef enum {
     173  WEBKIT_SNAPSHOT_OPTIONS_NONE = 0,
     174  WEBKIT_SNAPSHOT_OPTIONS_INCLUDE_SELECTION_HIGHLIGHTING = 1 << 0,
     175} WebKitSnapshotOptions;
     176
     177/**
     178 * WebKitSnapshotRegion:
     179 * @WEBKIT_SNAPSHOT_REGION_VISIBLE: Specifies a snapshot only for the area that is
     180 * visible in the webview
     181 * @WEBKIT_SNAPSHOT_REGION_FULL_DOCUMENT: A snapshot of the entire document.
     182 *
     183 * Enum values used to specify the region from which to get a #WebKitWebView
     184 * snapshot
     185 */
     186typedef enum {
     187  WEBKIT_SNAPSHOT_REGION_VISIBLE = 0,
     188  WEBKIT_SNAPSHOT_REGION_FULL_DOCUMENT,
     189} WebKitSnapshotRegion;
    162190
    163191struct _WebKitWebView {
    … …  
    427455                                                      GTlsCertificate          **certificate,
    428456                                                      GTlsCertificateFlags      *errors);
    429 
     457WEBKIT_API void
     458webkit_web_view_get_snapshot                         (WebKitWebView             *web_view,
     459                                                      WebKitSnapshotRegion       region,
     460                                                      WebKitSnapshotOptions      options,
     461                                                      GCancellable              *cancellable,
     462                                                      GAsyncReadyCallback        callback,
     463                                                      gpointer                   user_data);
     464
     465WEBKIT_API cairo_surface_t *
     466webkit_web_view_get_snapshot_finish                  (WebKitWebView             *web_view,
     467                                                      GAsyncResult              *result,
     468                                                      GError                   **error);
    430469G_END_DECLS
    431470
  • releases/WebKitGTK/webkit-2.0/Source/WebKit2/UIProcess/API/gtk/WebKitWebViewPrivate.h

    r145324 r145358  
    2828#define WebKitWebViewPrivate_h
    2929
     30#include "WebImage.h"
    3031#include "WebKitWebView.h"
    3132#include <wtf/text/CString.h>
    … …  
    5152void webkitWebViewRunFileChooserRequest(WebKitWebView*, WebKitFileChooserRequest*);
    5253WebKitWebResource* webkitWebViewGetLoadingWebResource(WebKitWebView*, uint64_t resourceIdentifier);
     54void webKitWebViewDidReceiveSnapshot(WebKitWebView*, uint64_t callbackID, WebKit::WebImage*);
    5355void webkitWebViewRemoveLoadingWebResource(WebKitWebView*, uint64_t resourceIdentifier);
    5456bool webkitWebViewEnterFullScreen(WebKitWebView*);
  • releases/WebKitGTK/webkit-2.0/Source/WebKit2/UIProcess/API/gtk/docs/webkit2gtk-sections.txt

    r144900 r145358  
    7676WebKitViewMode
    7777WebKitInsecureContentEvent
     78WebKitSnapshotOptions
     79WebKitSnapshotRegion
    7880
    7981<SUBSECTION Editing Commands>
    … …  
    137139webkit_web_view_get_view_mode
    138140webkit_web_view_get_tls_info
     141webkit_web_view_get_snapshot
     142webkit_web_view_get_snapshot_finish
    139143
    140144<SUBSECTION WebKitJavascriptResult>
    … …  
    611615WEBKIT_PRINT_ERROR
    612616WEBKIT_JAVASCRIPT_ERROR
     617WEBKIT_SNAPSHOT_ERROR
    613618WebKitNetworkError
    614619WebKitPluginError
    … …  
    617622WebKitPrintError
    618623WebKitJavascriptError
     624WebKitSnapshotError
    619625webkit_network_error_quark
    620626webkit_plugin_error_quark
    … …  
    623629webkit_print_error_quark
    624630webkit_javascript_error_quark
     631webkit_snapshot_error_quark
    625632</SECTION>
    626633
  • releases/WebKitGTK/webkit-2.0/Source/WebKit2/UIProcess/API/gtk/tests/TestWebKitWebView.cpp

    r145203 r145358  
    10831083}
    10841084
     1085class SnapshotWebViewTest: public WebViewTest {
     1086public:
     1087    MAKE_GLIB_TEST_FIXTURE(SnapshotWebViewTest);
     1088
     1089    SnapshotWebViewTest()
     1090        : m_surface(0)
     1091    {
     1092    }
     1093
     1094    ~SnapshotWebViewTest()
     1095    {
     1096        cairo_surface_destroy(m_surface);
     1097    }
     1098
     1099    static void onSnapshotReady(WebKitWebView* web_view, GAsyncResult* res, SnapshotWebViewTest* test)
     1100    {
     1101        GOwnPtr<GError> error;
     1102        test->m_surface = webkit_web_view_get_snapshot_finish(web_view, res, &error.outPtr());
     1103        g_assert(!test->m_surface || !error.get());
     1104        if (error)
     1105            g_assert_error(error.get(), WEBKIT_SNAPSHOT_ERROR, WEBKIT_SNAPSHOT_ERROR_FAILED_TO_CREATE);
     1106        test->quitMainLoop();
     1107    }
     1108
     1109    cairo_surface_t* waitForSnapshot(WebKitSnapshotRegion region, WebKitSnapshotOptions options)
     1110    {
     1111        m_surface = 0;
     1112        webkit_web_view_get_snapshot(m_webView, region, options, 0, reinterpret_cast<GAsyncReadyCallback>(onSnapshotReady), this);
     1113        g_main_loop_run(m_mainLoop);
     1114        return cairo_surface_reference(m_surface);
     1115    }
     1116
     1117    static void onSnapshotCancelledReady(WebKitWebView* web_view, GAsyncResult* res, SnapshotWebViewTest* test)
     1118    {
     1119        GOwnPtr<GError> error;
     1120        test->m_surface = webkit_web_view_get_snapshot_finish(web_view, res, &error.outPtr());
     1121        g_assert(!test->m_surface);
     1122        g_assert_error(error.get(), G_IO_ERROR, G_IO_ERROR_CANCELLED);
     1123        test->quitMainLoop();
     1124    }
     1125
     1126    gboolean getSnapshotAndCancel()
     1127    {
     1128        m_surface = 0;
     1129        GRefPtr<GCancellable> cancellable = adoptGRef(g_cancellable_new());
     1130        webkit_web_view_get_snapshot(m_webView, WEBKIT_SNAPSHOT_REGION_VISIBLE, WEBKIT_SNAPSHOT_OPTIONS_NONE, cancellable.get(), reinterpret_cast<GAsyncReadyCallback>(onSnapshotCancelledReady), this);
     1131        g_cancellable_cancel(cancellable.get());
     1132        g_main_loop_run(m_mainLoop);
     1133
     1134        return true;
     1135    }
     1136
     1137    cairo_surface_t* m_surface;
     1138};
     1139
     1140static gboolean cairoSurfacesEqual(cairo_surface_t* s1, cairo_surface_t* s2)
     1141{
     1142    return (cairo_image_surface_get_format(s1) == cairo_image_surface_get_format(s2)
     1143        && cairo_image_surface_get_width(s1) == cairo_image_surface_get_width(s2)
     1144        && cairo_image_surface_get_height(s1) == cairo_image_surface_get_height(s2)
     1145        && cairo_image_surface_get_stride(s1) == cairo_image_surface_get_stride(s2)
     1146        && !memcmp(const_cast<const void*>(reinterpret_cast<void*>(cairo_image_surface_get_data(s1))),
     1147            const_cast<const void*>(reinterpret_cast<void*>(cairo_image_surface_get_data(s2))),
     1148            cairo_image_surface_get_height(s1)*cairo_image_surface_get_stride(s1)));
     1149}
     1150
     1151static void testWebViewSnapshot(SnapshotWebViewTest* test, gconstpointer)
     1152{
     1153    test->loadHtml("<html><body><p>Whatever</p></body></html>", 0);
     1154    test->waitUntilLoadFinished();
     1155
     1156    // WebView not visible.
     1157    cairo_surface_t* surface1 = test->waitForSnapshot(WEBKIT_SNAPSHOT_REGION_VISIBLE, WEBKIT_SNAPSHOT_OPTIONS_NONE);
     1158    g_assert(!surface1);
     1159
     1160    // Show surface, resize to 50x50, try again.
     1161    test->showInWindowAndWaitUntilMapped();
     1162    test->resizeView(50, 50);
     1163    surface1 = test->waitForSnapshot(WEBKIT_SNAPSHOT_REGION_VISIBLE, WEBKIT_SNAPSHOT_OPTIONS_NONE);
     1164    g_assert(surface1);
     1165
     1166    // obtained surface should be at the most 50x50. Store the size
     1167    // for comparison later.
     1168    int width = cairo_image_surface_get_width(surface1);
     1169    int height = cairo_image_surface_get_height(surface1);
     1170    g_assert_cmpint(width, <=, 50);
     1171    g_assert_cmpint(height, <=, 50);
     1172
     1173    cairo_surface_destroy(surface1);
     1174
     1175    // Select all text in the WebView, request a snapshot ignoring selection.
     1176    test->selectAll();
     1177    surface1 = test->waitForSnapshot(WEBKIT_SNAPSHOT_REGION_VISIBLE, WEBKIT_SNAPSHOT_OPTIONS_NONE);
     1178    g_assert(surface1);
     1179    g_assert_cmpint(cairo_image_surface_get_width(surface1), ==, width);
     1180    g_assert_cmpint(cairo_image_surface_get_height(surface1), ==, height);
     1181
     1182    // Create identical surface.
     1183    cairo_surface_t* surface2 = test->waitForSnapshot(WEBKIT_SNAPSHOT_REGION_VISIBLE, WEBKIT_SNAPSHOT_OPTIONS_NONE);
     1184    g_assert(surface2);
     1185
     1186    // Compare these two, they should be identical.
     1187    g_assert(cairoSurfacesEqual(surface1, surface2));
     1188    cairo_surface_destroy(surface2);
     1189
     1190    // Request a new snapshot, including the selection this time. The
     1191    // size should be the same but the result must be different to the
     1192    // one previously obtained.
     1193    surface2 = test->waitForSnapshot(WEBKIT_SNAPSHOT_REGION_VISIBLE, WEBKIT_SNAPSHOT_OPTIONS_INCLUDE_SELECTION_HIGHLIGHTING);
     1194    g_assert(surface2);
     1195    g_assert_cmpint(cairo_image_surface_get_width(surface2), ==, width);
     1196    g_assert_cmpint(cairo_image_surface_get_height(surface2), ==, height);
     1197    g_assert(!cairoSurfacesEqual(surface1, surface2));
     1198    cairo_surface_destroy(surface2);
     1199
     1200    // Request a snapshot of the whole document in the WebView. The
     1201    // result should be different from the size obtained previously.
     1202    surface2 = test->waitForSnapshot(WEBKIT_SNAPSHOT_REGION_FULL_DOCUMENT, WEBKIT_SNAPSHOT_OPTIONS_NONE);
     1203    g_assert(surface2);
     1204    g_assert_cmpint(cairo_image_surface_get_width(surface2),  >, width);
     1205    g_assert_cmpint(cairo_image_surface_get_height(surface2), >, height);
     1206    g_assert(!cairoSurfacesEqual(surface1, surface2));
     1207
     1208    cairo_surface_destroy(surface1);
     1209    cairo_surface_destroy(surface2);
     1210
     1211    g_assert(test->getSnapshotAndCancel());
     1212}
     1213
    10851214void beforeAll()
    10861215{
    … …  
    11031232    SaveWebViewTest::add("WebKitWebView", "save", testWebViewSave);
    11041233    WebViewTest::add("WebKitWebView", "view-mode", testWebViewMode);
     1234    SnapshotWebViewTest::add("WebKitWebView", "snapshot", testWebViewSnapshot);
    11051235}
    11061236
  • releases/WebKitGTK/webkit-2.0/Source/WebKit2/UIProcess/API/gtk/tests/WebViewTest.cpp

    r130755 r145358  
    199199}
    200200
     201void WebViewTest::selectAll()
     202{
     203    webkit_web_view_execute_editing_command(m_webView, "SelectAll");
     204}
     205
    201206static void resourceGetDataCallback(GObject* object, GAsyncResult* result, gpointer userData)
    202207{
  • releases/WebKitGTK/webkit-2.0/Source/WebKit2/UIProcess/API/gtk/tests/WebViewTest.h

    r130755 r145358  
    4949    void showInWindowAndWaitUntilMapped(GtkWindowType = GTK_WINDOW_POPUP);
    5050    void resizeView(int width, int height);
     51    void selectAll();
    5152    const char* mainResourceData(size_t& mainResourceDataSize);
    5253
  • releases/WebKitGTK/webkit-2.0/Source/WebKit2/WebProcess/InjectedBundle/API/gtk/WebKitWebExtension.cpp

    r141102 r145358  
    2121#include "WebKitWebExtension.h"
    2222
     23#include "FrameView.h"
     24#include "ImageOptions.h"
    2325#include "ImmutableDictionary.h"
    2426#include "WKBundleAPICast.h"
    2527#include "WKBundlePage.h"
     28#include "WebImage.h"
    2629#include "WebKitPrivate.h"
    2730#include "WebKitWebExtensionPrivate.h"
    2831#include "WebKitWebPagePrivate.h"
     32#include "WebProcess.h"
    2933#include <WebCore/DNS.h>
    3034#include <wtf/HashMap.h>
    … …  
    9094}
    9195
     96static void webkitWebExtensionDidReceiveMessageToPage(WebKitWebExtension* extension, WebPage* page, const String& messageName, ImmutableDictionary& message)
     97{
     98    if (messageName == String("GetSnapshot")) {
     99        SnapshotOptions snapshotOptions = static_cast<SnapshotOptions>(static_cast<WebUInt64*>(message.get(String::fromUTF8("SnapshotOptions")))->value());
     100        uint64_t callbackID = static_cast<WebUInt64*>(message.get("CallbackID"))->value();
     101        SnapshotRegion region = static_cast<SnapshotRegion>(static_cast<WebUInt64*>(message.get("SnapshotRegion"))->value());
     102
     103        RefPtr<WebImage> snapshotImage;
     104
     105        if (WebCore::FrameView* frameView = page->mainFrameView()) {
     106            WebCore::IntRect snapshotRect;
     107            switch (region) {
     108            case SnapshotRegionVisible:
     109                snapshotRect = frameView->visibleContentRect(WebCore::ScrollableArea::ExcludeScrollbars);
     110                break;
     111            case SnapshotRegionFullDocument:
     112                snapshotRect = WebCore::IntRect(WebCore::IntPoint(0, 0), frameView->contentsSize());
     113                break;
     114            default:
     115                ASSERT_NOT_REACHED();
     116            }
     117            if (!snapshotRect.isEmpty())
     118                snapshotImage = page->scaledSnapshotWithOptions(snapshotRect, 1, snapshotOptions | SnapshotOptionsShareable);
     119        }
     120
     121        ImmutableDictionary::MapType messageReply;
     122        messageReply.set("Page", page);
     123        messageReply.set("CallbackID", WebUInt64::create(callbackID));
     124        messageReply.set("Snapshot", snapshotImage);
     125        WebProcess::shared().injectedBundle()->postMessage("WebPage.DidGetSnapshot", ImmutableDictionary::adopt(messageReply).get());
     126    } else
     127        ASSERT_NOT_REACHED();
     128}
     129
    92130static void didCreatePage(WKBundleRef bundle, WKBundlePageRef page, const void* clientInfo)
    93131{
    … …  
    106144}
    107145
     146static void didReceiveMessageToPage(WKBundleRef bundle, WKBundlePageRef page, WKStringRef name, WKTypeRef messageBody, const void* clientInfo)
     147{
     148    ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
     149    webkitWebExtensionDidReceiveMessageToPage(WEBKIT_WEB_EXTENSION(clientInfo), toImpl(page), toImpl(name)->string(), *toImpl(static_cast<WKDictionaryRef>(messageBody)));
     150}
     151
    108152WebKitWebExtension* webkitWebExtensionCreate(InjectedBundle* bundle)
    109153{
    … …  
    117161        0, // didInitializePageGroup
    118162        didReceiveMessage,
    119         0 // didReceiveMessageToPage
     163        didReceiveMessageToPage
    120164    };
    121165    WKBundleSetClient(toAPI(bundle), &wkBundleClient);
Note: See TracChangeset for help on using the changeset viewer.