Changeset 173456 in webkit


Ignore:
Timestamp:
Sep 10, 2014 1:37:17 AM (10 years ago)
Author:
commit-queue@webkit.org
Message:

[GTK] allow overwriting destination of download
https://bugs.webkit.org/show_bug.cgi?id=136372

Patch by Michael Catanzaro <Michael Catanzaro> on 2014-09-10
Reviewed by Carlos Garcia Campos.

Source/WebKit2:

  • UIProcess/API/gtk/WebKitDownload.cpp:

(webkitDownloadGetProperty): Added
(webkit_download_class_init): Install webkitDownloadGetProperty
(webkitDownloadDecideDestinationWithSuggestedFilename): Add
allowOverwrite parameter and set it appropriately
(webkit_download_get_allow_overwrite): Added
(webkit_download_set_allow_overwrite): Added

  • UIProcess/API/gtk/WebKitDownload.h: New API
  • UIProcess/API/gtk/WebKitDownloadClient.cpp:

(decideDestinationWithSuggestedFilename): Pass allowOverwrite to
webkitDownloadDecideDestinationWithSuggestedFilename

  • UIProcess/API/gtk/WebKitDownloadPrivate.h: Add allowOverwrite

parameter to webkitDownloadDecideDestinationWithSuggestedFilename

  • UIProcess/API/gtk/docs/webkit2gtk-sections.txt: New API

Tools:

  • TestWebKitAPI/Tests/WebKit2Gtk/TestDownloads.cpp:

(downloadLocalFileSuccessfully): Split from testDownloadLocalFile
(testDownloadLocalFile): Split off downloadLocalFileSuccessfully
(createFileAtDestination): Added
(testDownloadOverwriteDestinationAllowed): Added
(testDownloadOverwriteDestinationDisallowed): Added
(testDownloadLocalFileError): Use new DownloadErrorTest::ExpectedError
(testDownloadRemoteFileError): Use new DownloadErrorTest::ExpectedError
(beforeAll): New tests

Location:
trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit2/ChangeLog

    r173455 r173456  
     12014-09-10  Michael Catanzaro  <mcatanzaro@igalia.com>
     2
     3        [GTK] allow overwriting destination of download
     4        https://bugs.webkit.org/show_bug.cgi?id=136372
     5
     6        Reviewed by Carlos Garcia Campos.
     7
     8        * UIProcess/API/gtk/WebKitDownload.cpp:
     9        (webkitDownloadGetProperty): Added
     10        (webkit_download_class_init): Install webkitDownloadGetProperty
     11        (webkitDownloadDecideDestinationWithSuggestedFilename): Add
     12        allowOverwrite parameter and set it appropriately
     13        (webkit_download_get_allow_overwrite): Added
     14        (webkit_download_set_allow_overwrite): Added
     15        * UIProcess/API/gtk/WebKitDownload.h: New API
     16        * UIProcess/API/gtk/WebKitDownloadClient.cpp:
     17        (decideDestinationWithSuggestedFilename): Pass allowOverwrite to
     18        webkitDownloadDecideDestinationWithSuggestedFilename
     19        * UIProcess/API/gtk/WebKitDownloadPrivate.h: Add allowOverwrite
     20        parameter to webkitDownloadDecideDestinationWithSuggestedFilename
     21        * UIProcess/API/gtk/docs/webkit2gtk-sections.txt: New API
     22
    1232014-09-10  Rohit Kumar  <kumar.rohit@samsung.com>
    224
  • trunk/Source/WebKit2/UIProcess/API/gtk/WebKitDownload.cpp

    r164438 r173456  
    11/*
    2  * Copyright (C) 2012 Igalia S.L.
     2 * Copyright (C) 2012, 2014 Igalia S.L.
    33 *
    44 * This library is free software; you can redistribute it and/or
     
    6464    PROP_DESTINATION,
    6565    PROP_RESPONSE,
    66     PROP_ESTIMATED_PROGRESS
     66    PROP_ESTIMATED_PROGRESS,
     67    PROP_ALLOW_OVERWRITE
    6768};
    6869
     
    8586    gdouble lastProgress;
    8687    gdouble lastElapsed;
     88    bool allowOverwrite;
    8789};
    8890
     
    9092
    9193WEBKIT_DEFINE_TYPE(WebKitDownload, webkit_download, G_TYPE_OBJECT)
     94
     95static void webkitDownloadSetProperty(GObject* object, guint propId, const GValue* value, GParamSpec* paramSpec)
     96{
     97    WebKitDownload* download = WEBKIT_DOWNLOAD(object);
     98
     99    switch (propId) {
     100    case PROP_ALLOW_OVERWRITE:
     101        webkit_download_set_allow_overwrite(download, g_value_get_boolean(value));
     102        break;
     103    default:
     104        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
     105    }
     106}
    92107
    93108static void webkitDownloadGetProperty(GObject* object, guint propId, GValue* value, GParamSpec* paramSpec)
     
    104119    case PROP_ESTIMATED_PROGRESS:
    105120        g_value_set_double(value, webkit_download_get_estimated_progress(download));
     121        break;
     122    case PROP_ALLOW_OVERWRITE:
     123        g_value_set_boolean(value, webkit_download_get_allow_overwrite(download));
    106124        break;
    107125    default:
     
    131149{
    132150    GObjectClass* objectClass = G_OBJECT_CLASS(downloadClass);
     151    objectClass->set_property = webkitDownloadSetProperty;
    133152    objectClass->get_property = webkitDownloadGetProperty;
    134153
     
    178197                                                        0.0, 1.0, 1.0,
    179198                                                        WEBKIT_PARAM_READABLE));
     199
     200    /**
     201     * WebKitDownload:allow-overwrite:
     202     *
     203     * Whether or not the download is allowed to overwrite an existing file on
     204     * disk. If this property is %FALSE and the destination already exists,
     205     * the download will fail.
     206     *
     207     * Since: 2.6
     208     */
     209    g_object_class_install_property(
     210        objectClass,
     211        PROP_ALLOW_OVERWRITE,
     212        g_param_spec_boolean(
     213            "allow-overwrite",
     214            _("Allow Overwrite"),
     215            _("Whether the destination may be overwritten"),
     216            FALSE,
     217            WEBKIT_PARAM_READWRITE));
    180218
    181219    /**
     
    371409}
    372410
    373 CString webkitDownloadDecideDestinationWithSuggestedFilename(WebKitDownload* download, const CString& suggestedFilename)
     411CString webkitDownloadDecideDestinationWithSuggestedFilename(WebKitDownload* download, const CString& suggestedFilename, bool& allowOverwrite)
    374412{
    375413    if (download->priv->isCancelled)
     
    377415    gboolean returnValue;
    378416    g_signal_emit(download, signals[DECIDE_DESTINATION], 0, suggestedFilename.data(), &returnValue);
     417    allowOverwrite = download->priv->allowOverwrite;
    379418    return download->priv->destinationURI;
    380419}
     
    569608    return download->priv->webView;
    570609}
     610
     611/**
     612 * webkit_download_get_allow_overwrite:
     613 * @download: a #WebKitDownload
     614 *
     615 * Returns the current value of the #WebKitDownload:allow-overwrite property,
     616 * which determines whether the download will overwrite an existing file on
     617 * disk, or if it will fail if the destination already exists.
     618 *
     619 * Returns: the current value of the #WebKitDownload:allow-overwrite property
     620 *
     621 * Since: 2.6
     622 */
     623gboolean webkit_download_get_allow_overwrite(WebKitDownload* download)
     624{
     625    g_return_val_if_fail(WEBKIT_IS_DOWNLOAD(download), FALSE);
     626
     627    return download->priv->allowOverwrite;
     628}
     629
     630/**
     631 * webkit_download_set_allow_overwrite:
     632 * @download: a #WebKitDownload
     633 * @allowed: the new value for the #WebKitDownload:allow-overwrite property
     634 *
     635 * Sets the #WebKitDownload:allow-overwrite property, which determines whether
     636 * the download may overwrite an existing file on disk, or if it will fail if
     637 * the destination already exists.
     638 *
     639 * Since: 2.6
     640 */
     641void webkit_download_set_allow_overwrite(WebKitDownload* download, gboolean allowed)
     642{
     643    g_return_if_fail(WEBKIT_IS_DOWNLOAD(download));
     644
     645    if (allowed == download->priv->allowOverwrite)
     646        return;
     647
     648    download->priv->allowOverwrite = allowed;
     649    g_object_notify(G_OBJECT(download), "allow-overwrite");
     650}
  • trunk/Source/WebKit2/UIProcess/API/gtk/WebKitDownload.h

    r150130 r173456  
    9393webkit_download_get_web_view             (WebKitDownload *download);
    9494
     95WEBKIT_API gboolean
     96webkit_download_get_allow_overwrite      (WebKitDownload *download);
     97
     98WEBKIT_API void
     99webkit_download_set_allow_overwrite      (WebKitDownload *download,
     100                                          gboolean        allowed);
     101
    95102G_END_DECLS
    96103
  • trunk/Source/WebKit2/UIProcess/API/gtk/WebKitDownloadClient.cpp

    r168961 r173456  
    5555}
    5656
    57 static WKStringRef decideDestinationWithSuggestedFilename(WKContextRef, WKDownloadRef wkDownload, WKStringRef filename, bool* /* allowOverwrite */, const void* /* clientInfo */)
     57static WKStringRef decideDestinationWithSuggestedFilename(WKContextRef, WKDownloadRef wkDownload, WKStringRef filename, bool* allowOverwrite, const void* /* clientInfo */)
    5858{
    5959    GRefPtr<WebKitDownload> download = webkitWebContextGetOrCreateDownload(toImpl(wkDownload));
    60     CString destinationURI = webkitDownloadDecideDestinationWithSuggestedFilename(download.get(),
    61                                                                                   toImpl(filename)->string().utf8());
     60    CString destinationURI = webkitDownloadDecideDestinationWithSuggestedFilename(download.get(), toImpl(filename)->string().utf8(), *allowOverwrite);
    6261    return WKStringCreateWithUTF8CString(destinationURI.data());
    6362}
  • trunk/Source/WebKit2/UIProcess/API/gtk/WebKitDownloadPrivate.h

    r145244 r173456  
    3636void webkitDownloadCancelled(WebKitDownload*);
    3737void webkitDownloadFinished(WebKitDownload*);
    38 CString webkitDownloadDecideDestinationWithSuggestedFilename(WebKitDownload*, const CString& suggestedFilename);
     38CString webkitDownloadDecideDestinationWithSuggestedFilename(WebKitDownload*, const CString& suggestedFilename, bool& allowOverwrite);
    3939void webkitDownloadDestinationCreated(WebKitDownload*, const CString& destinationURI);
    4040
  • trunk/Source/WebKit2/UIProcess/API/gtk/docs/webkit2gtk-sections.txt

    r173060 r173456  
    523523webkit_download_get_received_data_length
    524524webkit_download_get_web_view
     525webkit_download_get_allow_overwrite
     526webkit_download_set_allow_overwrite
    525527
    526528<SUBSECTION Standard>
  • trunk/Tools/ChangeLog

    r173452 r173456  
     12014-09-10  Michael Catanzaro  <mcatanzaro@igalia.com>
     2
     3        [GTK] allow overwriting destination of download
     4        https://bugs.webkit.org/show_bug.cgi?id=136372
     5
     6        Reviewed by Carlos Garcia Campos.
     7
     8        * TestWebKitAPI/Tests/WebKit2Gtk/TestDownloads.cpp:
     9        (downloadLocalFileSuccessfully): Split from testDownloadLocalFile
     10        (testDownloadLocalFile): Split off downloadLocalFileSuccessfully
     11        (createFileAtDestination): Added
     12        (testDownloadOverwriteDestinationAllowed): Added
     13        (testDownloadOverwriteDestinationDisallowed): Added
     14        (testDownloadLocalFileError): Use new DownloadErrorTest::ExpectedError
     15        (testDownloadRemoteFileError): Use new DownloadErrorTest::ExpectedError
     16        (beforeAll): New tests
     17
    1182014-08-05  David Farler  <dfarler@apple.com>
    219
  • trunk/Tools/TestWebKitAPI/Tests/WebKit2Gtk/TestDownloads.cpp

    r167008 r173456  
    11/*
    2  * Copyright (C) 2012 Igalia S.L.
     2 * Copyright (C) 2012, 2014 Igalia S.L.
    33 *
    44 * This library is free software; you can redistribute it and/or
     
    5757        g_assert(webkit_download_get_destination(download));
    5858        g_assert_cmpstr(webkit_download_get_destination(download), ==, destination);
     59        GRefPtr<GFile> file = adoptGRef(g_file_new_for_uri(destination));
     60        g_assert(g_file_query_exists(file.get(), nullptr));
    5961        test->createdDestination(download, destination);
    6062    }
     
    7375    {
    7476        g_assert(error);
     77
     78        const char* destinationURI = webkit_download_get_destination(download);
     79        if (destinationURI) {
     80            GUniquePtr<char> tempFileURI(g_strconcat(destinationURI, ".wkdownload", nullptr));
     81            GRefPtr<GFile> tempFile = adoptGRef(g_file_new_for_uri(tempFileURI.get()));
     82            g_assert(!g_file_query_exists(tempFile.get(), nullptr));
     83        }
     84
    7585        test->failed(download, error);
    7686    }
     
    99109        , m_mainLoop(g_main_loop_new(0, TRUE))
    100110        , m_downloadSize(0)
     111        , m_allowOverwrite(false)
    101112    {
    102113        g_signal_connect(m_webContext, "download-started", G_CALLBACK(downloadStartedCallback), this);
     
    111122    virtual void started(WebKitDownload* download)
    112123    {
     124        m_downloadSize = 0;
     125        m_downloadEvents.clear();
    113126        m_downloadEvents.append(Started);
    114127    }
     
    154167        WebKitDownload* download = webkit_web_context_download_uri(m_webContext, requestURI.data());
    155168        assertObjectIsDeletedWhenTestFinishes(G_OBJECT(download));
     169
     170        g_assert(!webkit_download_get_allow_overwrite(download));
     171        webkit_download_set_allow_overwrite(download, m_allowOverwrite);
     172        g_assert(webkit_download_get_allow_overwrite(download) == m_allowOverwrite);
    156173
    157174        WebKitURIRequest* request = webkit_download_get_request(download);
     
    179196    Vector<DownloadEvent> m_downloadEvents;
    180197    guint64 m_downloadSize;
     198    bool m_allowOverwrite;
    181199};
    182200
    183 static void testDownloadLocalFile(DownloadTest* test, gconstpointer)
    184 {
    185     GUniquePtr<char> sourcePath(g_build_filename(Test::getResourcesDir().data(), "test.pdf", nullptr));
     201static GRefPtr<WebKitDownload> downloadLocalFileSuccessfully(DownloadTest* test, const char* filename)
     202{
     203    GUniquePtr<char> sourcePath(g_build_filename(Test::getResourcesDir().data(), filename, nullptr));
    186204    GRefPtr<GFile> source = adoptGRef(g_file_new_for_path(sourcePath.get()));
    187205    GRefPtr<GFileInfo> sourceInfo = adoptGRef(g_file_query_info(source.get(), G_FILE_ATTRIBUTE_STANDARD_SIZE, static_cast<GFileQueryInfoFlags>(0), 0, 0));
     
    205223    g_assert(webkit_download_get_destination(download.get()));
    206224    g_assert_cmpfloat(webkit_download_get_estimated_progress(download.get()), ==, 1);
    207     test->checkDestinationAndDeleteFile(download.get(), "test.pdf");
     225
     226    return download;
     227}
     228
     229static void testDownloadLocalFile(DownloadTest* test, gconstpointer)
     230{
     231    static const char* filename = "test.pdf";
     232    GRefPtr<WebKitDownload> download = downloadLocalFileSuccessfully(test, filename);
     233    test->checkDestinationAndDeleteFile(download.get(), filename);
     234}
     235
     236static void createFileAtDestination(const char* filename)
     237{
     238    GUniquePtr<char> path(g_build_filename(kTempDirectory, filename, nullptr));
     239    GRefPtr<GFile> file = adoptGRef(g_file_new_for_path(path.get()));
     240    GUniqueOutPtr<GError> error;
     241    g_file_create(file.get(), G_FILE_CREATE_NONE, nullptr, &error.outPtr());
     242    g_assert(!error);
     243    g_assert(g_file_query_exists(file.get(), nullptr));
     244}
     245
     246static void testDownloadOverwriteDestinationAllowed(DownloadTest* test, gconstpointer)
     247{
     248    static const char* filename = "test.pdf";
     249    createFileAtDestination(filename);
     250
     251    test->m_allowOverwrite = true;
     252    GRefPtr<WebKitDownload> download = downloadLocalFileSuccessfully(test, filename);
     253    test->checkDestinationAndDeleteFile(download.get(), filename);
    208254}
    209255
     
    212258    MAKE_GLIB_TEST_FIXTURE(DownloadErrorTest);
    213259
     260    enum ExpectedError {
     261        NetworkError,
     262        DownloadCancelled,
     263        InvalidDestination,
     264        DestinationExists
     265    };
     266
    214267    DownloadErrorTest()
    215         : m_expectedError(WEBKIT_DOWNLOAD_ERROR_NETWORK)
     268        : m_expectedError(NetworkError)
    216269    {
    217270    }
     
    224277    void createdDestination(WebKitDownload* download, const char* destination)
    225278    {
    226         if (m_expectedError == WEBKIT_DOWNLOAD_ERROR_CANCELLED_BY_USER)
     279        if (m_expectedError == DownloadCancelled)
    227280            webkit_download_cancel(download);
    228281        else
     
    232285    void failed(WebKitDownload* download, GError* error)
    233286    {
    234         g_assert(g_error_matches(error, WEBKIT_DOWNLOAD_ERROR, m_expectedError));
     287        g_assert(g_error_matches(error, WEBKIT_DOWNLOAD_ERROR, expectedErrorToWebKitDownloadError(m_expectedError)));
    235288        DownloadTest::failed(download, error);
    236289    }
     
    238291    void decideDestination(WebKitDownload* download, const gchar* suggestedFilename)
    239292    {
    240         if (m_expectedError != WEBKIT_DOWNLOAD_ERROR_DESTINATION) {
     293        if (m_expectedError != InvalidDestination) {
    241294            DownloadTest::decideDestination(download, suggestedFilename);
    242295            return;
     
    245298    }
    246299
    247     WebKitDownloadError m_expectedError;
     300    static WebKitDownloadError expectedErrorToWebKitDownloadError(ExpectedError expected)
     301    {
     302        switch (expected) {
     303        case NetworkError:
     304            return WEBKIT_DOWNLOAD_ERROR_NETWORK;
     305        case DownloadCancelled:
     306            return WEBKIT_DOWNLOAD_ERROR_CANCELLED_BY_USER;
     307        case InvalidDestination:
     308            return WEBKIT_DOWNLOAD_ERROR_DESTINATION;
     309        case DestinationExists:
     310            return WEBKIT_DOWNLOAD_ERROR_DESTINATION;
     311        default:
     312            g_assert_not_reached();
     313        }
     314    }
     315
     316    ExpectedError m_expectedError;
    248317};
    249318
     319static void testDownloadOverwriteDestinationDisallowed(DownloadErrorTest* test, gconstpointer)
     320{
     321    static const char* filename = "test.pdf";
     322    createFileAtDestination(filename);
     323
     324    test->m_expectedError = DownloadErrorTest::DestinationExists;
     325    GUniquePtr<char> sourcePath(g_build_filename(Test::getResourcesDir().data(), filename, nullptr));
     326    GRefPtr<GFile> source = adoptGRef(g_file_new_for_path(sourcePath.get()));
     327    GUniquePtr<char> sourceURI(g_file_get_uri(source.get()));
     328    GRefPtr<WebKitDownload> download = adoptGRef(test->downloadURIAndWaitUntilFinishes(sourceURI.get()));
     329    g_assert(!webkit_download_get_web_view(download.get()));
     330
     331    Vector<DownloadTest::DownloadEvent>& events = test->m_downloadEvents;
     332    g_assert_cmpint(events.size(), ==, 4);
     333    g_assert_cmpint(events[0], ==, DownloadTest::Started);
     334    g_assert_cmpint(events[1], ==, DownloadTest::ReceivedResponse);
     335    g_assert_cmpint(events[2], ==, DownloadTest::Failed);
     336    g_assert_cmpint(events[3], ==, DownloadTest::Finished);
     337    g_assert_cmpfloat(webkit_download_get_estimated_progress(download.get()), ==, 0);
     338
     339    test->checkDestinationAndDeleteFile(download.get(), filename);
     340}
     341
    250342static void testDownloadLocalFileError(DownloadErrorTest* test, gconstpointer)
    251343{
    252     test->m_expectedError = WEBKIT_DOWNLOAD_ERROR_NETWORK;
     344    test->m_expectedError = DownloadErrorTest::NetworkError;
    253345    GRefPtr<WebKitDownload> download = adoptGRef(test->downloadURIAndWaitUntilFinishes("file:///foo/bar"));
    254346    g_assert(!webkit_download_get_web_view(download.get()));
     
    262354    g_assert_cmpfloat(webkit_download_get_estimated_progress(download.get()), <, 1);
    263355
    264     test->m_expectedError = WEBKIT_DOWNLOAD_ERROR_DESTINATION;
     356    test->m_expectedError = DownloadErrorTest::InvalidDestination;
    265357    GUniquePtr<char> path(g_build_filename(Test::getResourcesDir().data(), "test.pdf", nullptr));
    266358    GRefPtr<GFile> file = adoptGRef(g_file_new_for_path(path.get()));
     
    278370    test->checkDestinationAndDeleteFile(download.get(), "bar");
    279371
    280     test->m_expectedError = WEBKIT_DOWNLOAD_ERROR_CANCELLED_BY_USER;
     372    test->m_expectedError = DownloadErrorTest::DownloadCancelled;
    281373    download = adoptGRef(test->downloadURIAndWaitUntilFinishes(uri.get()));
    282374    g_assert(!webkit_download_get_web_view(download.get()));
     
    370462static void testDownloadRemoteFileError(DownloadErrorTest* test, gconstpointer)
    371463{
    372     test->m_expectedError = WEBKIT_DOWNLOAD_ERROR_NETWORK;
     464    test->m_expectedError = DownloadErrorTest::NetworkError;
    373465    GRefPtr<WebKitDownload> download = adoptGRef(test->downloadURIAndWaitUntilFinishes(kServer->getURIForPath("/foo")));
    374466    g_assert(!webkit_download_get_web_view(download.get()));
     
    385477    g_assert_cmpfloat(webkit_download_get_estimated_progress(download.get()), <, 1);
    386478
    387     test->m_expectedError = WEBKIT_DOWNLOAD_ERROR_DESTINATION;
     479    test->m_expectedError = DownloadErrorTest::InvalidDestination;
    388480    download = adoptGRef(test->downloadURIAndWaitUntilFinishes(kServer->getURIForPath("/test.pdf")));
    389481    g_assert(!webkit_download_get_web_view(download.get()));
     
    398490    test->checkDestinationAndDeleteFile(download.get(), "bar");
    399491
    400     test->m_expectedError = WEBKIT_DOWNLOAD_ERROR_CANCELLED_BY_USER;
     492    test->m_expectedError = DownloadErrorTest::DownloadCancelled;
    401493    download = adoptGRef(test->downloadURIAndWaitUntilFinishes(kServer->getURIForPath("/cancel-after-destination")));
    402494    g_assert(!webkit_download_get_web_view(download.get()));
     
    534626
    535627    DownloadTest::add("Downloads", "local-file", testDownloadLocalFile);
     628    DownloadTest::add("Downloads", "overwrite-destination-allowed", testDownloadOverwriteDestinationAllowed);
     629    DownloadErrorTest::add("Downloads", "overwrite-destination-disallowed", testDownloadOverwriteDestinationDisallowed);
    536630    DownloadErrorTest::add("Downloads", "local-file-error", testDownloadLocalFileError);
    537631    DownloadTest::add("Downloads", "remote-file", testDownloadRemoteFile);
Note: See TracChangeset for help on using the changeset viewer.