void NetworkDataTaskBlob::didGetSize(long long size) { ASSERT(isMainThread()); if (m_state == State::Canceling || m_state == State::Completed || (!m_client && !isDownload())) { clearStream(); return; } // If the size is -1, it means the file has been moved or changed. Fail now. if (size == -1) { didFail(Error::NotFoundError); return; } // The size passed back is the size of the whole file. If the underlying item is a sliced file, we need to use the slice length. const BlobDataItem& item = m_blobData->items().at(m_sizeItemCount); size = item.length(); // Cache the size. m_itemLengthList.append(size); // Count the size. m_totalSize += size; m_totalRemainingSize += size; m_sizeItemCount++; // Continue with the next item. getSizeForNext(); }
api_return TransferApi::handleForce(ApiRequest& aRequest) { auto item = getTransfer(aRequest); if (item->isDownload()) { ConnectionManager::getInstance()->force(item->getStringToken()); } return websocketpp::http::status_code::ok; }
bool NetworkDataTaskBlob::writeDownload(const char* data, int bytesRead) { ASSERT(isDownload()); int bytesWritten = writeToFile(m_downloadFile, data, bytesRead); if (bytesWritten == -1) { didFailDownload(cancelledError(m_firstRequest)); return false; } ASSERT(bytesWritten == bytesRead); auto* download = NetworkProcess::singleton().downloadManager().download(m_pendingDownloadID); ASSERT(download); download->didReceiveData(bytesWritten); return true; }
void NetworkDataTaskBlob::didRead(int bytesRead) { if (m_state == State::Canceling || m_state == State::Completed || (!m_client && !isDownload())) { clearStream(); return; } if (bytesRead < 0) { didFail(Error::NotReadableError); return; } Ref<NetworkDataTaskBlob> protectedThis(*this); consumeData(m_buffer.data(), bytesRead); }
void NetworkDataTaskBlob::didOpen(bool success) { if (m_state == State::Canceling || m_state == State::Completed || (!m_client && !isDownload())) { clearStream(); return; } if (!success) { didFail(Error::NotReadableError); return; } Ref<NetworkDataTaskBlob> protectedThis(*this); read(); }
void NetworkDataTaskBlob::cancel() { if (m_state == State::Canceling || m_state == State::Completed) return; m_state = State::Canceling; if (m_fileOpened) { m_fileOpened = false; m_stream->close(); } if (isDownload()) cleanDownloadFiles(); }
void NetworkDataTaskBlob::didFail(Error errorCode) { ASSERT(!m_sandboxExtension); Ref<NetworkDataTaskBlob> protectedThis(*this); if (isDownload()) { didFailDownload(ResourceError(webKitBlobResourceDomain, static_cast<int>(errorCode), m_firstRequest.url(), String())); return; } LOG(NetworkSession, "%p - NetworkDataTaskBlob::didFail", this); clearStream(); ASSERT(m_client); m_client->didCompleteWithError(ResourceError(webKitBlobResourceDomain, static_cast<int>(errorCode), m_firstRequest.url(), String())); }
void NetworkDataTaskBlob::didFinishDownload() { LOG(NetworkSession, "%p - NetworkDataTaskBlob::didFinishDownload", this); ASSERT(isDownload()); closeFile(m_downloadFile); m_downloadFile = invalidPlatformFileHandle; if (m_sandboxExtension) { m_sandboxExtension->revoke(); m_sandboxExtension = nullptr; } clearStream(); auto* download = NetworkProcess::singleton().downloadManager().download(m_pendingDownloadID); ASSERT(download); download->didFinish(); }
void NetworkDataTaskBlob::download() { ASSERT(isDownload()); ASSERT(m_pendingDownloadLocation); LOG(NetworkSession, "%p - NetworkDataTaskBlob::download to %s", this, m_pendingDownloadLocation.utf8().data()); m_downloadFile = openFile(m_pendingDownloadLocation, OpenForWrite); if (m_downloadFile == invalidPlatformFileHandle) { didFailDownload(cancelledError(m_firstRequest)); return; } auto& downloadManager = NetworkProcess::singleton().downloadManager(); auto download = std::make_unique<Download>(downloadManager, m_pendingDownloadID, *this, m_session->sessionID(), suggestedFilename()); auto* downloadPtr = download.get(); downloadManager.dataTaskBecameDownloadTask(m_pendingDownloadID, WTFMove(download)); downloadPtr->didCreateDestination(m_pendingDownloadLocation); ASSERT(!m_client); m_buffer.resize(bufferSize); read(); }