diff options
Diffstat (limited to 'Source/WebKit2/UIProcess/API/gtk/WebKitDownload.cpp')
-rw-r--r-- | Source/WebKit2/UIProcess/API/gtk/WebKitDownload.cpp | 129 |
1 files changed, 105 insertions, 24 deletions
diff --git a/Source/WebKit2/UIProcess/API/gtk/WebKitDownload.cpp b/Source/WebKit2/UIProcess/API/gtk/WebKitDownload.cpp index e95e4c065..4fe6cb984 100644 --- a/Source/WebKit2/UIProcess/API/gtk/WebKitDownload.cpp +++ b/Source/WebKit2/UIProcess/API/gtk/WebKitDownload.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2012 Igalia S.L. + * Copyright (C) 2012, 2014 Igalia S.L. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -23,13 +23,15 @@ #include "DownloadProxy.h" #include "WebKitDownloadPrivate.h" #include "WebKitMarshal.h" +#include "WebKitPrivate.h" #include "WebKitURIRequestPrivate.h" #include "WebKitURIResponsePrivate.h" #include <WebCore/ErrorsGtk.h> #include <WebCore/ResourceResponse.h> #include <glib/gi18n-lib.h> -#include <wtf/gobject/GRefPtr.h> -#include <wtf/gobject/GUniquePtr.h> +#include <wtf/glib/GRefPtr.h> +#include <wtf/glib/GUniquePtr.h> +#include <wtf/text/CString.h> using namespace WebKit; using namespace WebCore; @@ -62,7 +64,8 @@ enum { PROP_DESTINATION, PROP_RESPONSE, - PROP_ESTIMATED_PROGRESS + PROP_ESTIMATED_PROGRESS, + PROP_ALLOW_OVERWRITE }; struct _WebKitDownloadPrivate { @@ -83,12 +86,26 @@ struct _WebKitDownloadPrivate { GUniquePtr<GTimer> timer; gdouble lastProgress; gdouble lastElapsed; + bool allowOverwrite; }; static guint signals[LAST_SIGNAL] = { 0, }; WEBKIT_DEFINE_TYPE(WebKitDownload, webkit_download, G_TYPE_OBJECT) +static void webkitDownloadSetProperty(GObject* object, guint propId, const GValue* value, GParamSpec* paramSpec) +{ + WebKitDownload* download = WEBKIT_DOWNLOAD(object); + + switch (propId) { + case PROP_ALLOW_OVERWRITE: + webkit_download_set_allow_overwrite(download, g_value_get_boolean(value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec); + } +} + static void webkitDownloadGetProperty(GObject* object, guint propId, GValue* value, GParamSpec* paramSpec) { WebKitDownload* download = WEBKIT_DOWNLOAD(object); @@ -103,6 +120,9 @@ static void webkitDownloadGetProperty(GObject* object, guint propId, GValue* val case PROP_ESTIMATED_PROGRESS: g_value_set_double(value, webkit_download_get_estimated_progress(download)); break; + case PROP_ALLOW_OVERWRITE: + g_value_set_boolean(value, webkit_download_get_allow_overwrite(download)); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec); } @@ -129,6 +149,7 @@ static gboolean webkitDownloadDecideDestination(WebKitDownload* download, const static void webkit_download_class_init(WebKitDownloadClass* downloadClass) { GObjectClass* objectClass = G_OBJECT_CLASS(downloadClass); + objectClass->set_property = webkitDownloadSetProperty; objectClass->get_property = webkitDownloadGetProperty; downloadClass->decide_destination = webkitDownloadDecideDestination; @@ -178,6 +199,25 @@ static void webkit_download_class_init(WebKitDownloadClass* downloadClass) WEBKIT_PARAM_READABLE)); /** + * WebKitDownload:allow-overwrite: + * + * Whether or not the download is allowed to overwrite an existing file on + * disk. If this property is %FALSE and the destination already exists, + * the download will fail. + * + * Since: 2.6 + */ + g_object_class_install_property( + objectClass, + PROP_ALLOW_OVERWRITE, + g_param_spec_boolean( + "allow-overwrite", + _("Allow Overwrite"), + _("Whether the destination may be overwritten"), + FALSE, + WEBKIT_PARAM_READWRITE)); + + /** * WebKitDownload::received-data: * @download: the #WebKitDownload * @data_length: the length of data received in bytes @@ -223,13 +263,14 @@ static void webkit_download_class_init(WebKitDownloadClass* downloadClass) * after an error and #WebKitDownload::finished signal is emitted after this one. */ signals[FAILED] = - g_signal_new("failed", - G_TYPE_FROM_CLASS(objectClass), - G_SIGNAL_RUN_LAST, - 0, 0, 0, - g_cclosure_marshal_VOID__POINTER, - G_TYPE_NONE, 1, - G_TYPE_POINTER); + g_signal_new( + "failed", + G_TYPE_FROM_CLASS(objectClass), + G_SIGNAL_RUN_LAST, + 0, 0, 0, + g_cclosure_marshal_VOID__BOXED, + G_TYPE_NONE, 1, + G_TYPE_ERROR | G_SIGNAL_TYPE_STATIC_SCOPE); /** * WebKitDownload::decide-destination: @@ -282,13 +323,6 @@ WebKitDownload* webkitDownloadCreate(DownloadProxy* downloadProxy) return download; } -WebKitDownload* webkitDownloadCreateForRequest(DownloadProxy* downloadProxy, const ResourceRequest& request) -{ - WebKitDownload* download = webkitDownloadCreate(downloadProxy); - download->priv->request = adoptGRef(webkitURIRequestCreateForResourceRequest(request)); - return download; -} - void webkitDownloadSetResponse(WebKitDownload* download, WebKitURIResponse* response) { download->priv->response = response; @@ -340,7 +374,7 @@ void webkitDownloadNotifyProgress(WebKitDownload* download, guint64 bytesReceive void webkitDownloadFailed(WebKitDownload* download, const ResourceError& resourceError) { GUniquePtr<GError> webError(g_error_new_literal(g_quark_from_string(resourceError.domain().utf8().data()), - resourceError.errorCode(), resourceError.localizedDescription().utf8().data())); + toWebKitError(resourceError.errorCode()), resourceError.localizedDescription().utf8().data())); if (download->priv->timer) g_timer_stop(download->priv->timer.get()); @@ -369,20 +403,26 @@ void webkitDownloadFinished(WebKitDownload* download) g_signal_emit(download, signals[FINISHED], 0, NULL); } -CString webkitDownloadDecideDestinationWithSuggestedFilename(WebKitDownload* download, const CString& suggestedFilename) +String webkitDownloadDecideDestinationWithSuggestedFilename(WebKitDownload* download, const CString& suggestedFilename, bool& allowOverwrite) { if (download->priv->isCancelled) - return ""; + return emptyString(); gboolean returnValue; g_signal_emit(download, signals[DECIDE_DESTINATION], 0, suggestedFilename.data(), &returnValue); - return download->priv->destinationURI; + allowOverwrite = download->priv->allowOverwrite; + GUniquePtr<char> destinationPath(g_filename_from_uri(download->priv->destinationURI.data(), nullptr, nullptr)); + if (!destinationPath) + return emptyString(); + return String::fromUTF8(destinationPath.get()); } -void webkitDownloadDestinationCreated(WebKitDownload* download, const CString& destinationURI) +void webkitDownloadDestinationCreated(WebKitDownload* download, const String& destinationPath) { if (download->priv->isCancelled) return; - g_signal_emit(download, signals[CREATED_DESTINATION], 0, destinationURI.data(), nullptr); + GUniquePtr<char> destinationURI(g_filename_to_uri(destinationPath.utf8().data(), nullptr, nullptr)); + ASSERT(destinationURI); + g_signal_emit(download, signals[CREATED_DESTINATION], 0, destinationURI.get()); } /** @@ -567,3 +607,44 @@ WebKitWebView* webkit_download_get_web_view(WebKitDownload* download) return download->priv->webView; } + +/** + * webkit_download_get_allow_overwrite: + * @download: a #WebKitDownload + * + * Returns the current value of the #WebKitDownload:allow-overwrite property, + * which determines whether the download will overwrite an existing file on + * disk, or if it will fail if the destination already exists. + * + * Returns: the current value of the #WebKitDownload:allow-overwrite property + * + * Since: 2.6 + */ +gboolean webkit_download_get_allow_overwrite(WebKitDownload* download) +{ + g_return_val_if_fail(WEBKIT_IS_DOWNLOAD(download), FALSE); + + return download->priv->allowOverwrite; +} + +/** + * webkit_download_set_allow_overwrite: + * @download: a #WebKitDownload + * @allowed: the new value for the #WebKitDownload:allow-overwrite property + * + * Sets the #WebKitDownload:allow-overwrite property, which determines whether + * the download may overwrite an existing file on disk, or if it will fail if + * the destination already exists. + * + * Since: 2.6 + */ +void webkit_download_set_allow_overwrite(WebKitDownload* download, gboolean allowed) +{ + g_return_if_fail(WEBKIT_IS_DOWNLOAD(download)); + + if (allowed == download->priv->allowOverwrite) + return; + + download->priv->allowOverwrite = allowed; + g_object_notify(G_OBJECT(download), "allow-overwrite"); +} |