Changeset 148526 in webkit


Ignore:
Timestamp:
Apr 16, 2013 11:29:43 AM (11 years ago)
Author:
Claudio Saavedra
Message:

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

Reviewed by Anders Carlsson.

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:

(didReceiveMessageToPage): Ditto.
(webkitWebExtensionCreate): Register method above.

  • WebProcess/InjectedBundle/API/gtk/WebKitWebPage.cpp:

(webkitWebPageDidReceiveMessage): Add this method. It
handles the new message "GetSnapshot".

  • WebProcess/InjectedBundle/API/gtk/WebKitWebPagePrivate.h:

Add method above.

Location:
trunk/Source/WebKit2
Files:
15 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit2/ChangeLog

    r148524 r148526  
     12013-04-16  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 Anders Carlsson.
     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        (didReceiveMessageToPage): Ditto.
     46        (webkitWebExtensionCreate): Register method above.
     47        * WebProcess/InjectedBundle/API/gtk/WebKitWebPage.cpp:
     48        (webkitWebPageDidReceiveMessage): Add this method. It
     49        handles the new message "GetSnapshot".
     50        * WebProcess/InjectedBundle/API/gtk/WebKitWebPagePrivate.h:
     51        Add method above.
     52
    1532013-04-16  Timothy Hatcher  <timothy@apple.com>
    254
  • trunk/Source/WebKit2/UIProcess/API/gtk/WebKitError.cpp

    r137469 r148526  
    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}
  • trunk/Source/WebKit2/UIProcess/API/gtk/WebKitError.h

    r111510 r148526  
    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
  • trunk/Source/WebKit2/UIProcess/API/gtk/WebKitInjectedBundleClient.cpp

    r143302 r148526  
    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();
  • trunk/Source/WebKit2/UIProcess/API/gtk/WebKitPrivate.h

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

    r148294 r148526  
    2222#include "WebKitWebView.h"
    2323
     24#include "ImageOptions.h"
    2425#include "PlatformCertificateInfo.h"
    2526#include "WebCertificateInfo.h"
     
    134135
    135136typedef HashMap<uint64_t, GRefPtr<WebKitWebResource> > LoadingResourcesMap;
     137typedef HashMap<uint64_t, GRefPtr<GSimpleAsyncResult> > SnapshotResultsMap;
    136138
    137139struct _WebKitWebViewPrivate {
     
    179181    CString faviconURI;
    180182    unsigned long faviconChangedHandlerID;
     183
     184    SnapshotResultsMap snapshotResultsMap;
    181185};
    182186
     
    29212925    return !!certificateInfo.certificate();
    29222926}
     2927
     2928struct GetSnapshotAsyncData {
     2929    GRefPtr<GCancellable> cancellable;
     2930    RefPtr<cairo_surface_t> snapshot;
     2931};
     2932WEBKIT_DEFINE_ASYNC_DATA_STRUCT(GetSnapshotAsyncData)
     2933
     2934void webKitWebViewDidReceiveSnapshot(WebKitWebView* webView, uint64_t callbackID, WebImage* webImage)
     2935{
     2936    GRefPtr<GSimpleAsyncResult> result = webView->priv->snapshotResultsMap.take(callbackID);
     2937    GetSnapshotAsyncData* data = static_cast<GetSnapshotAsyncData*>(g_simple_async_result_get_op_res_gpointer(result.get()));
     2938    GError* error = 0;
     2939    if (g_cancellable_set_error_if_cancelled(data->cancellable.get(), &error))
     2940        g_simple_async_result_take_error(result.get(), error);
     2941    else if (webImage) {
     2942        if (RefPtr<ShareableBitmap> image = webImage->bitmap())
     2943            data->snapshot = image->createCairoSurface();
     2944    } else {
     2945        g_set_error_literal(&error, WEBKIT_SNAPSHOT_ERROR, WEBKIT_SNAPSHOT_ERROR_FAILED_TO_CREATE, _("There was an error creating the snapshot"));
     2946        g_simple_async_result_take_error(result.get(), error);
     2947    }
     2948
     2949    g_simple_async_result_complete(result.get());
     2950}
     2951
     2952COMPILE_ASSERT_MATCHING_ENUM(WEBKIT_SNAPSHOT_REGION_VISIBLE, SnapshotRegionVisible);
     2953COMPILE_ASSERT_MATCHING_ENUM(WEBKIT_SNAPSHOT_REGION_FULL_DOCUMENT, SnapshotRegionFullDocument);
     2954
     2955static inline unsigned webKitSnapshotOptionsToSnapshotOptions(WebKitSnapshotOptions options)
     2956{
     2957    SnapshotOptions snapshotOptions = 0;
     2958
     2959    if (!(options & WEBKIT_SNAPSHOT_OPTIONS_INCLUDE_SELECTION_HIGHLIGHTING))
     2960        snapshotOptions |= SnapshotOptionsExcludeSelectionHighlighting;
     2961
     2962    return snapshotOptions;
     2963}
     2964
     2965static inline uint64_t generateSnapshotCallbackID()
     2966{
     2967    static uint64_t uniqueCallbackID = 1;
     2968    return uniqueCallbackID++;
     2969}
     2970
     2971/**
     2972 * webkit_web_view_get_snapshot:
     2973 * @web_view: a #WebKitWebView
     2974 * @options: #WebKitSnapshotOptions for the snapshot
     2975 * @region: the #WebKitSnapshotRegion for this snapshot
     2976 * @cancellable: (allow-none): a #GCancellable
     2977 * @callback: (scope async): a #GAsyncReadyCallback
     2978 * @user_data: (closure): user data
     2979 *
     2980 * Asynchronously retrieves a snapshot of @web_view for @region.
     2981 * @options specifies how the snapshot should be rendered.
     2982 *
     2983 * When the operation is finished, @callback will be called. You must
     2984 * call webkit_web_view_get_snapshot_finish() to get the result of the
     2985 * operation.
     2986 */
     2987void webkit_web_view_get_snapshot(WebKitWebView* webView, WebKitSnapshotRegion region, WebKitSnapshotOptions options, GCancellable* cancellable, GAsyncReadyCallback callback, gpointer userData)
     2988{
     2989    g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
     2990
     2991    GRefPtr<GSimpleAsyncResult> result = adoptGRef(g_simple_async_result_new(G_OBJECT(webView), callback, userData,
     2992        reinterpret_cast<gpointer>(webkit_web_view_get_snapshot)));
     2993    GetSnapshotAsyncData* data = createGetSnapshotAsyncData();
     2994    data->cancellable = cancellable;
     2995    g_simple_async_result_set_op_res_gpointer(result.get(), data, reinterpret_cast<GDestroyNotify>(destroyGetSnapshotAsyncData));
     2996
     2997    ImmutableDictionary::MapType message;
     2998    uint64_t callbackID = generateSnapshotCallbackID();
     2999    message.set(String::fromUTF8("SnapshotOptions"), WebUInt64::create(static_cast<uint64_t>(webKitSnapshotOptionsToSnapshotOptions(options))));
     3000    message.set(String::fromUTF8("SnapshotRegion"), WebUInt64::create(static_cast<uint64_t>(region)));
     3001    message.set(String::fromUTF8("CallbackID"), WebUInt64::create(callbackID));
     3002
     3003    webView->priv->snapshotResultsMap.set(callbackID, result.get());
     3004    getPage(webView)->postMessageToInjectedBundle(String::fromUTF8("GetSnapshot"), ImmutableDictionary::adopt(message).get());
     3005}
     3006
     3007/**
     3008 * webkit_web_view_get_snapshot_finish:
     3009 * @web_view: a #WebKitWebView
     3010 * @result: a #GAsyncResult
     3011 * @error: return location for error or %NULL to ignore
     3012 *
     3013 * Finishes an asynchronous operation started with webkit_web_view_get_snapshot().
     3014 *
     3015 * Returns: (transfer full): a #cairo_surface_t with the retrieved snapshot or %NULL in error.
     3016 */
     3017cairo_surface_t* webkit_web_view_get_snapshot_finish(WebKitWebView* webView, GAsyncResult* result, GError** error)
     3018{
     3019    g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), 0);
     3020    g_return_val_if_fail(G_IS_ASYNC_RESULT(result), 0);
     3021
     3022    GSimpleAsyncResult* simple = G_SIMPLE_ASYNC_RESULT(result);
     3023    g_warn_if_fail(g_simple_async_result_get_source_tag(simple) == webkit_web_view_get_snapshot);
     3024
     3025    if (g_simple_async_result_propagate_error(simple, error))
     3026        return 0;
     3027
     3028    return cairo_surface_reference(static_cast<GetSnapshotAsyncData*>(g_simple_async_result_get_op_res_gpointer(simple))->snapshot.get());
     3029}
  • trunk/Source/WebKit2/UIProcess/API/gtk/WebKitWebView.h

    r143341 r148526  
    159159    WEBKIT_VIEW_MODE_SOURCE
    160160} WebKitViewMode;
     161
     162/**
     163 * WebKitSnapshotOptions:
     164 * @WEBKIT_SNAPSHOT_OPTIONS_NONE: Do not include any special options.
     165 * @WEBKIT_SNAPSHOT_OPTIONS_INCLUDE_SELECTION_HIGHLIGHTING: Whether to include in the
     166 * snapshot the highlight of the selected content.
     167 *
     168 * Enum values used to specify options when taking a snapshot
     169 * from a #WebKitWebView.
     170 */
     171typedef enum {
     172  WEBKIT_SNAPSHOT_OPTIONS_NONE = 0,
     173  WEBKIT_SNAPSHOT_OPTIONS_INCLUDE_SELECTION_HIGHLIGHTING = 1 << 0,
     174} WebKitSnapshotOptions;
     175
     176/**
     177 * WebKitSnapshotRegion:
     178 * @WEBKIT_SNAPSHOT_REGION_VISIBLE: Specifies a snapshot only for the area that is
     179 * visible in the webview
     180 * @WEBKIT_SNAPSHOT_REGION_FULL_DOCUMENT: A snapshot of the entire document.
     181 *
     182 * Enum values used to specify the region from which to get a #WebKitWebView
     183 * snapshot
     184 */
     185typedef enum {
     186  WEBKIT_SNAPSHOT_REGION_VISIBLE = 0,
     187  WEBKIT_SNAPSHOT_REGION_FULL_DOCUMENT,
     188} WebKitSnapshotRegion;
    161189
    162190struct _WebKitWebView {
     
    419447                                                      GTlsCertificate          **certificate,
    420448                                                      GTlsCertificateFlags      *errors);
    421 
     449WEBKIT_API void
     450webkit_web_view_get_snapshot                         (WebKitWebView             *web_view,
     451                                                      WebKitSnapshotRegion       region,
     452                                                      WebKitSnapshotOptions      options,
     453                                                      GCancellable              *cancellable,
     454                                                      GAsyncReadyCallback        callback,
     455                                                      gpointer                   user_data);
     456
     457WEBKIT_API cairo_surface_t *
     458webkit_web_view_get_snapshot_finish                  (WebKitWebView             *web_view,
     459                                                      GAsyncResult              *result,
     460                                                      GError                   **error);
    422461G_END_DECLS
    423462
  • trunk/Source/WebKit2/UIProcess/API/gtk/WebKitWebViewPrivate.h

    r143302 r148526  
    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*);
  • trunk/Source/WebKit2/UIProcess/API/gtk/docs/webkit2gtk-sections.txt

    r143341 r148526  
    7676WebKitViewMode
    7777WebKitInsecureContentEvent
     78WebKitSnapshotOptions
     79WebKitSnapshotRegion
    7880
    7981<SUBSECTION Editing Commands>
     
    135137webkit_web_view_get_view_mode
    136138webkit_web_view_get_tls_info
     139webkit_web_view_get_snapshot
     140webkit_web_view_get_snapshot_finish
    137141
    138142<SUBSECTION WebKitJavascriptResult>
     
    608612WEBKIT_PRINT_ERROR
    609613WEBKIT_JAVASCRIPT_ERROR
     614WEBKIT_SNAPSHOT_ERROR
    610615WebKitNetworkError
    611616WebKitPluginError
     
    614619WebKitPrintError
    615620WebKitJavascriptError
     621WebKitSnapshotError
    616622webkit_network_error_quark
    617623webkit_plugin_error_quark
     
    620626webkit_print_error_quark
    621627webkit_javascript_error_quark
     628webkit_snapshot_error_quark
    622629</SECTION>
    623630
  • trunk/Source/WebKit2/UIProcess/API/gtk/tests/TestWebKitWebView.cpp

    r148088 r148526  
    11471147}
    11481148
     1149class SnapshotWebViewTest: public WebViewTest {
     1150public:
     1151    MAKE_GLIB_TEST_FIXTURE(SnapshotWebViewTest);
     1152
     1153    SnapshotWebViewTest()
     1154        : m_surface(0)
     1155    {
     1156    }
     1157
     1158    ~SnapshotWebViewTest()
     1159    {
     1160        cairo_surface_destroy(m_surface);
     1161    }
     1162
     1163    static void onSnapshotReady(WebKitWebView* web_view, GAsyncResult* res, SnapshotWebViewTest* test)
     1164    {
     1165        GOwnPtr<GError> error;
     1166        test->m_surface = webkit_web_view_get_snapshot_finish(web_view, res, &error.outPtr());
     1167        g_assert(!test->m_surface || !error.get());
     1168        if (error)
     1169            g_assert_error(error.get(), WEBKIT_SNAPSHOT_ERROR, WEBKIT_SNAPSHOT_ERROR_FAILED_TO_CREATE);
     1170        test->quitMainLoop();
     1171    }
     1172
     1173    cairo_surface_t* waitForSnapshot(WebKitSnapshotRegion region, WebKitSnapshotOptions options)
     1174    {
     1175        m_surface = 0;
     1176        webkit_web_view_get_snapshot(m_webView, region, options, 0, reinterpret_cast<GAsyncReadyCallback>(onSnapshotReady), this);
     1177        g_main_loop_run(m_mainLoop);
     1178        return cairo_surface_reference(m_surface);
     1179    }
     1180
     1181    static void onSnapshotCancelledReady(WebKitWebView* web_view, GAsyncResult* res, SnapshotWebViewTest* test)
     1182    {
     1183        GOwnPtr<GError> error;
     1184        test->m_surface = webkit_web_view_get_snapshot_finish(web_view, res, &error.outPtr());
     1185        g_assert(!test->m_surface);
     1186        g_assert_error(error.get(), G_IO_ERROR, G_IO_ERROR_CANCELLED);
     1187        test->quitMainLoop();
     1188    }
     1189
     1190    gboolean getSnapshotAndCancel()
     1191    {
     1192        m_surface = 0;
     1193        GRefPtr<GCancellable> cancellable = adoptGRef(g_cancellable_new());
     1194        webkit_web_view_get_snapshot(m_webView, WEBKIT_SNAPSHOT_REGION_VISIBLE, WEBKIT_SNAPSHOT_OPTIONS_NONE, cancellable.get(), reinterpret_cast<GAsyncReadyCallback>(onSnapshotCancelledReady), this);
     1195        g_cancellable_cancel(cancellable.get());
     1196        g_main_loop_run(m_mainLoop);
     1197
     1198        return true;
     1199    }
     1200
     1201    cairo_surface_t* m_surface;
     1202};
     1203
     1204static gboolean cairoSurfacesEqual(cairo_surface_t* s1, cairo_surface_t* s2)
     1205{
     1206    return (cairo_image_surface_get_format(s1) == cairo_image_surface_get_format(s2)
     1207        && cairo_image_surface_get_width(s1) == cairo_image_surface_get_width(s2)
     1208        && cairo_image_surface_get_height(s1) == cairo_image_surface_get_height(s2)
     1209        && cairo_image_surface_get_stride(s1) == cairo_image_surface_get_stride(s2)
     1210        && !memcmp(const_cast<const void*>(reinterpret_cast<void*>(cairo_image_surface_get_data(s1))),
     1211            const_cast<const void*>(reinterpret_cast<void*>(cairo_image_surface_get_data(s2))),
     1212            cairo_image_surface_get_height(s1)*cairo_image_surface_get_stride(s1)));
     1213}
     1214
     1215static void testWebViewSnapshot(SnapshotWebViewTest* test, gconstpointer)
     1216{
     1217    test->loadHtml("<html><body><p>Whatever</p></body></html>", 0);
     1218    test->waitUntilLoadFinished();
     1219
     1220    // WebView not visible.
     1221    cairo_surface_t* surface1 = test->waitForSnapshot(WEBKIT_SNAPSHOT_REGION_VISIBLE, WEBKIT_SNAPSHOT_OPTIONS_NONE);
     1222    g_assert(!surface1);
     1223
     1224    // Show surface, resize to 50x50, try again.
     1225    test->showInWindowAndWaitUntilMapped();
     1226    test->resizeView(50, 50);
     1227    surface1 = test->waitForSnapshot(WEBKIT_SNAPSHOT_REGION_VISIBLE, WEBKIT_SNAPSHOT_OPTIONS_NONE);
     1228    g_assert(surface1);
     1229
     1230    // obtained surface should be at the most 50x50. Store the size
     1231    // for comparison later.
     1232    int width = cairo_image_surface_get_width(surface1);
     1233    int height = cairo_image_surface_get_height(surface1);
     1234    g_assert_cmpint(width, <=, 50);
     1235    g_assert_cmpint(height, <=, 50);
     1236
     1237    cairo_surface_destroy(surface1);
     1238
     1239    // Select all text in the WebView, request a snapshot ignoring selection.
     1240    test->selectAll();
     1241    surface1 = test->waitForSnapshot(WEBKIT_SNAPSHOT_REGION_VISIBLE, WEBKIT_SNAPSHOT_OPTIONS_NONE);
     1242    g_assert(surface1);
     1243    g_assert_cmpint(cairo_image_surface_get_width(surface1), ==, width);
     1244    g_assert_cmpint(cairo_image_surface_get_height(surface1), ==, height);
     1245
     1246    // Create identical surface.
     1247    cairo_surface_t* surface2 = test->waitForSnapshot(WEBKIT_SNAPSHOT_REGION_VISIBLE, WEBKIT_SNAPSHOT_OPTIONS_NONE);
     1248    g_assert(surface2);
     1249
     1250    // Compare these two, they should be identical.
     1251    g_assert(cairoSurfacesEqual(surface1, surface2));
     1252    cairo_surface_destroy(surface2);
     1253
     1254    // Request a new snapshot, including the selection this time. The
     1255    // size should be the same but the result must be different to the
     1256    // one previously obtained.
     1257    surface2 = test->waitForSnapshot(WEBKIT_SNAPSHOT_REGION_VISIBLE, WEBKIT_SNAPSHOT_OPTIONS_INCLUDE_SELECTION_HIGHLIGHTING);
     1258    g_assert(surface2);
     1259    g_assert_cmpint(cairo_image_surface_get_width(surface2), ==, width);
     1260    g_assert_cmpint(cairo_image_surface_get_height(surface2), ==, height);
     1261    g_assert(!cairoSurfacesEqual(surface1, surface2));
     1262    cairo_surface_destroy(surface2);
     1263
     1264    // Request a snapshot of the whole document in the WebView. The
     1265    // result should be different from the size obtained previously.
     1266    surface2 = test->waitForSnapshot(WEBKIT_SNAPSHOT_REGION_FULL_DOCUMENT, WEBKIT_SNAPSHOT_OPTIONS_NONE);
     1267    g_assert(surface2);
     1268    g_assert_cmpint(cairo_image_surface_get_width(surface2),  >, width);
     1269    g_assert_cmpint(cairo_image_surface_get_height(surface2), >, height);
     1270    g_assert(!cairoSurfacesEqual(surface1, surface2));
     1271
     1272    cairo_surface_destroy(surface1);
     1273    cairo_surface_destroy(surface2);
     1274
     1275    g_assert(test->getSnapshotAndCancel());
     1276}
     1277
    11491278void beforeAll()
    11501279{
     
    11671296    SaveWebViewTest::add("WebKitWebView", "save", testWebViewSave);
    11681297    WebViewTest::add("WebKitWebView", "view-mode", testWebViewMode);
     1298    SnapshotWebViewTest::add("WebKitWebView", "snapshot", testWebViewSnapshot);
    11691299    WebViewTest::add("WebKitWebView", "page-visibility", testWebViewPageVisibility);
    11701300}
  • trunk/Source/WebKit2/UIProcess/API/gtk/tests/WebViewTest.cpp

    r148088 r148526  
    208208}
    209209
     210void WebViewTest::selectAll()
     211{
     212    webkit_web_view_execute_editing_command(m_webView, "SelectAll");
     213}
     214
    210215static void resourceGetDataCallback(GObject* object, GAsyncResult* result, gpointer userData)
    211216{
  • trunk/Source/WebKit2/UIProcess/API/gtk/tests/WebViewTest.h

    r148088 r148526  
    5050    void showInWindowAndWaitUntilMapped(GtkWindowType = GTK_WINDOW_POPUP);
    5151    void resizeView(int width, int height);
     52    void selectAll();
    5253    const char* mainResourceData(size_t& mainResourceDataSize);
    5354
  • trunk/Source/WebKit2/WebProcess/InjectedBundle/API/gtk/WebKitWebExtension.cpp

    r141102 r148526  
    106106}
    107107
     108static void didReceiveMessageToPage(WKBundleRef bundle, WKBundlePageRef page, WKStringRef name, WKTypeRef messageBody, const void* clientInfo)
     109{
     110    ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
     111    if (WebKitWebPage* webPage = WEBKIT_WEB_EXTENSION(clientInfo)->priv->pages.get(toImpl(page)).get())
     112        webkitWebPageDidReceiveMessage(webPage, toImpl(name)->string(), *toImpl(static_cast<WKDictionaryRef>(messageBody)));
     113}
     114
    108115WebKitWebExtension* webkitWebExtensionCreate(InjectedBundle* bundle)
    109116{
     
    117124        0, // didInitializePageGroup
    118125        didReceiveMessage,
    119         0 // didReceiveMessageToPage
     126        didReceiveMessageToPage
    120127    };
    121128    WKBundleSetClient(toAPI(bundle), &wkBundleClient);
  • trunk/Source/WebKit2/WebProcess/InjectedBundle/API/gtk/WebKitWebPage.cpp

    r148281 r148526  
    2121#include "WebKitWebPage.h"
    2222
     23#include "ImageOptions.h"
    2324#include "ImmutableDictionary.h"
    2425#include "InjectedBundle.h"
     
    2627#include "WKBundleFrame.h"
    2728#include "WebFrame.h"
     29#include "WebImage.h"
    2830#include "WebKitDOMDocumentPrivate.h"
    2931#include "WebKitPrivate.h"
     
    3133#include "WebProcess.h"
    3234#include <WebCore/Frame.h>
     35#include <WebCore/FrameView.h>
    3336
    3437using namespace WebKit;
     
    197200}
    198201
     202void webkitWebPageDidReceiveMessage(WebKitWebPage* page, const String& messageName, ImmutableDictionary& message)
     203{
     204    if (messageName == String("GetSnapshot")) {
     205        SnapshotOptions snapshotOptions = static_cast<SnapshotOptions>(static_cast<WebUInt64*>(message.get("SnapshotOptions"))->value());
     206        uint64_t callbackID = static_cast<WebUInt64*>(message.get("CallbackID"))->value();
     207        SnapshotRegion region = static_cast<SnapshotRegion>(static_cast<WebUInt64*>(message.get("SnapshotRegion"))->value());
     208
     209        RefPtr<WebImage> snapshotImage;
     210        WebPage* webPage = page->priv->webPage;
     211        if (WebCore::FrameView* frameView = webPage->mainFrameView()) {
     212            WebCore::IntRect snapshotRect;
     213            switch (region) {
     214            case SnapshotRegionVisible:
     215                snapshotRect = frameView->visibleContentRect(WebCore::ScrollableArea::ExcludeScrollbars);
     216                break;
     217            case SnapshotRegionFullDocument:
     218                snapshotRect = WebCore::IntRect(WebCore::IntPoint(0, 0), frameView->contentsSize());
     219                break;
     220            default:
     221                ASSERT_NOT_REACHED();
     222            }
     223            if (!snapshotRect.isEmpty())
     224                snapshotImage = webPage->scaledSnapshotWithOptions(snapshotRect, 1, snapshotOptions | SnapshotOptionsShareable);
     225        }
     226
     227        ImmutableDictionary::MapType messageReply;
     228        messageReply.set("Page", webPage);
     229        messageReply.set("CallbackID", WebUInt64::create(callbackID));
     230        messageReply.set("Snapshot", snapshotImage);
     231        WebProcess::shared().injectedBundle()->postMessage("WebPage.DidGetSnapshot", ImmutableDictionary::adopt(messageReply).get());
     232    } else
     233        ASSERT_NOT_REACHED();
     234}
     235
    199236/**
    200237 * webkit_web_page_get_dom_document:
  • trunk/Source/WebKit2/WebProcess/InjectedBundle/API/gtk/WebKitWebPagePrivate.h

    r139305 r148526  
    2121#define WebKitWebPagePrivate_h
    2222
     23#include "ImmutableDictionary.h"
    2324#include "WebKitWebPage.h"
    2425#include "WebPage.h"
    2526
    2627WebKitWebPage* webkitWebPageCreate(WebKit::WebPage*);
     28void webkitWebPageDidReceiveMessage(WebKitWebPage*, const String& messageName, WebKit::ImmutableDictionary& message);
    2729
    2830#endif // WebKitWebPagePrivate_h
Note: See TracChangeset for help on using the changeset viewer.