📄 webkitdownload.cpp
字号:
// FIXME: Use the actual request object when WebKitNetworkRequest is finished. ResourceRequest request(webkit_network_request_get_uri(priv->networkRequest)); priv->resourceHandle = ResourceHandle::create(request, priv->downloadClient, 0, false, false, false); } priv->timer = g_timer_new(); webkit_download_open_stream_for_uri(download, priv->destinationURI);}/** * webkit_download_cancel: * @download: the #WebKitDownload * * Cancels the download. Calling this will not free the * #WebKitDownload object, so you still need to call * g_object_unref() on it, if you are the owner of a reference. Notice * that cancelling the download provokes the emission of the * WebKitDownload::error signal, reporting that the download was * cancelled. * * Since: 1.1.2 */void webkit_download_cancel(WebKitDownload* download){ g_return_if_fail(WEBKIT_IS_DOWNLOAD(download)); WebKitDownloadPrivate* priv = download->priv; // Cancel may be called even if start was not called, so we need // to make sure timer is non-NULL. if (priv->timer) g_timer_stop(priv->timer); if (priv->resourceHandle) priv->resourceHandle->cancel(); priv->state = WEBKIT_DOWNLOAD_STATE_CANCELLED; gboolean handled; g_signal_emit_by_name(download, "error", 0, WEBKIT_DOWNLOAD_ERROR_CANCELLED_BY_USER, "User cancelled the download", &handled);}/** * webkit_download_get_uri: * @download: the #WebKitDownload * * Convenience method to retrieve the URI from the * #WebKitNetworkRequest which is being downloaded. * * Returns: the uri * * Since: 1.1.2 */const gchar* webkit_download_get_uri(WebKitDownload* download){ g_return_val_if_fail(WEBKIT_IS_DOWNLOAD(download), NULL); WebKitDownloadPrivate* priv = download->priv; return webkit_network_request_get_uri(priv->networkRequest);}/** * webkit_download_get_network_request: * @download: the #WebKitDownload * * Retrieves the #WebKitNetworkRequest object that backs the download * process. * * Returns: the #WebKitNetworkRequest instance * * Since: 1.1.2 */WebKitNetworkRequest* webkit_download_get_network_request(WebKitDownload* download){ g_return_val_if_fail(WEBKIT_IS_DOWNLOAD(download), NULL); WebKitDownloadPrivate* priv = download->priv; return priv->networkRequest;}static void webkit_download_set_response(WebKitDownload* download, const ResourceResponse& response){ // FIXME Use WebKitNetworkResponse when it's merged. WebKitDownloadPrivate* priv = download->priv; priv->networkResponse = new ResourceResponse(response);}/** * webkit_download_get_suggested_filename: * @download: the #WebKitDownload * * Retrieves the filename that was suggested by the server, or the one * derived by WebKit from the URI. * * Returns: the suggested filename * * Since: 1.1.2 */const gchar* webkit_download_get_suggested_filename(WebKitDownload* download){ g_return_val_if_fail(WEBKIT_IS_DOWNLOAD(download), NULL); WebKitDownloadPrivate* priv = download->priv; return priv->suggestedFilename;}/** * webkit_download_get_destination_uri: * @download: the #WebKitDownload * * Obtains the URI to which the downloaded file will be written. This * must have been set by the application before calling * webkit_download_start(), and may be %NULL. * * Returns: the destination URI or %NULL * * Since: 1.1.2 */const gchar* webkit_download_get_destination_uri(WebKitDownload* download){ g_return_val_if_fail(WEBKIT_IS_DOWNLOAD(download), NULL); WebKitDownloadPrivate* priv = download->priv; return priv->destinationURI;}/** * webkit_download_set_destination_uri: * @download: the #WebKitDownload * @destination_uri: the destination URI * * Defines the URI that should be used to save the downloaded file to. * * Since: 1.1.2 */void webkit_download_set_destination_uri(WebKitDownload* download, const gchar* destination_uri){ g_return_if_fail(WEBKIT_IS_DOWNLOAD(download)); g_return_if_fail(destination_uri); WebKitDownloadPrivate* priv = download->priv; if (priv->destinationURI && !strcmp(priv->destinationURI, destination_uri)) return; if (priv->state != WEBKIT_DOWNLOAD_STATE_CREATED && priv->state != WEBKIT_DOWNLOAD_STATE_CANCELLED) { ASSERT(priv->destinationURI); gboolean downloading = priv->outputStream != NULL; if (downloading) webkit_download_close_stream(download); GFile* src = g_file_new_for_uri(priv->destinationURI); GFile* dest = g_file_new_for_uri(destination_uri); GError* error = NULL; g_file_move(src, dest, G_FILE_COPY_BACKUP, NULL, NULL, NULL, &error); g_object_unref(src); g_object_unref(dest); g_free(priv->destinationURI); priv->destinationURI = g_strdup(destination_uri); if (error) { gboolean handled; g_signal_emit_by_name(download, "error", 0, WEBKIT_DOWNLOAD_ERROR_DESTINATION, error->message, &handled); g_error_free(error); return; } if (downloading) { if (!webkit_download_open_stream_for_uri(download, destination_uri, TRUE)) { webkit_download_cancel(download); return; } } } else { g_free(priv->destinationURI); priv->destinationURI = g_strdup(destination_uri); } // Only notify change if everything went fine. g_object_notify(G_OBJECT(download), "destination-uri");}/** * webkit_download_get_state: * @download: the #WebKitDownload * * Obtains the current state of the download, as a * #WebKitDownloadState. * * Returns: the current #WebKitDownloadState * * Since: 1.1.2 */WebKitDownloadState webkit_download_get_state(WebKitDownload* download){ g_return_val_if_fail(WEBKIT_IS_DOWNLOAD(download), WEBKIT_DOWNLOAD_STATE_ERROR); WebKitDownloadPrivate* priv = download->priv; return priv->state;}/** * webkit_download_get_total_size: * @download: the #WebKitDownload * * Returns the expected total size of the download. This is expected * because the server may provide incorrect or missing * Content-Length. Notice that this may grow over time, as it will be * always the same as current_size in the cases where current size * surpasses it. * * Returns: the expected total size of the downloaded file * * Since: 1.1.2 */guint64 webkit_download_get_total_size(WebKitDownload* download){ g_return_val_if_fail(WEBKIT_IS_DOWNLOAD(download), 0); WebKitDownloadPrivate* priv = download->priv; if (!priv->networkResponse) return 0; return MAX(priv->currentSize, priv->networkResponse->expectedContentLength());}/** * webkit_download_get_current_size: * @download: the #WebKitDownload * * Current already downloaded size. * * Returns: the already downloaded size * * Since: 1.1.2 */guint64 webkit_download_get_current_size(WebKitDownload* download){ g_return_val_if_fail(WEBKIT_IS_DOWNLOAD(download), 0); WebKitDownloadPrivate* priv = download->priv; return priv->currentSize;}/** * webkit_download_get_progress: * @download: a #WebKitDownload * * Determines the current progress of the download. * * Returns: a #gdouble ranging from 0.0 to 1.0. * * Since: 1.1.2 */gdouble webkit_download_get_progress(WebKitDownload* download){ g_return_val_if_fail(WEBKIT_IS_DOWNLOAD(download), 1.0); WebKitDownloadPrivate* priv = download->priv; gdouble total_size = (gdouble)priv->networkResponse->expectedContentLength(); if (total_size == 0) return 1.0; return ((gdouble)priv->currentSize) / total_size;}/** * webkit_download_get_elapsed_time: * @download: a #WebKitDownload * * Elapsed time for the download in seconds, including any fractional * part. If the download is finished, had an error or was cancelled * this is the time between its start and the event. * * Returns: seconds since the download was started, as a #gdouble * * Since: 1.1.2 */gdouble webkit_download_get_elapsed_time(WebKitDownload* download){ g_return_val_if_fail(WEBKIT_IS_DOWNLOAD(download), 0.0); WebKitDownloadPrivate* priv = download->priv; return g_timer_elapsed(priv->timer, NULL);}static void webkit_download_received_data(WebKitDownload* download, const gchar* data, int length){ WebKitDownloadPrivate* priv = download->priv; if (priv->currentSize == 0) priv->state = WEBKIT_DOWNLOAD_STATE_STARTED; ASSERT(priv->outputStream); gsize bytes_written; GError* error = NULL; g_output_stream_write_all(G_OUTPUT_STREAM(priv->outputStream), data, length, &bytes_written, NULL, &error); if (error) { gboolean handled; g_signal_emit_by_name(download, "error", 0, WEBKIT_DOWNLOAD_ERROR_DESTINATION, error->message, &handled); g_error_free(error); return; } priv->currentSize += length; g_object_notify(G_OBJECT(download), "current-size"); ASSERT(priv->networkResponse); if (priv->currentSize > priv->networkResponse->expectedContentLength()) g_object_notify(G_OBJECT(download), "total-size"); // FIXME: Throttle the number of updates? Should we remove the // previous g_object_notify()s if we are going to throttle the // progress updates? g_object_notify(G_OBJECT(download), "progress");}static void webkit_download_finished_loading(WebKitDownload* download){ webkit_download_close_stream(download); WebKitDownloadPrivate* priv = download->priv; g_timer_stop(priv->timer); priv->state = WEBKIT_DOWNLOAD_STATE_FINISHED; g_object_notify(G_OBJECT(download), "progress");}static void webkit_download_error(WebKitDownload* download, const ResourceError& error){ webkit_download_close_stream(download); WebKitDownloadPrivate* priv = download->priv; g_timer_stop(priv->timer); priv->state = WEBKIT_DOWNLOAD_STATE_ERROR; gboolean handled; g_signal_emit_by_name(download, "error", 0, WEBKIT_DOWNLOAD_ERROR_NETWORK, error.localizedDescription().utf8().data(), &handled);}DownloadClient::DownloadClient(WebKitDownload* download) : m_download(download){}void DownloadClient::didReceiveResponse(ResourceHandle*, const ResourceResponse& response){ webkit_download_set_response(m_download, response);}void DownloadClient::didReceiveData(ResourceHandle*, const char* data, int length, int lengthReceived){ webkit_download_received_data(m_download, data, length);}void DownloadClient::didFinishLoading(ResourceHandle*){ webkit_download_finished_loading(m_download);}void DownloadClient::didFail(ResourceHandle*, const ResourceError& error){ webkit_download_error(m_download, error);}void DownloadClient::wasBlocked(ResourceHandle*){ // FIXME: Implement this when we have the new frame loader signals // and error handling. notImplemented();}void DownloadClient::cannotShowURL(ResourceHandle*){ // FIXME: Implement this when we have the new frame loader signals // and error handling. notImplemented();}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -