mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 04:48:14 +00:00
LibProtocol: Add a Download object so users don't have to manage ID's
LibProtocol::Client::start_download() now gives you a Download object with convenient hooks (on_finish & on_progress). Also, the IPC handshake is snuck into the Client constructor, so you don't need to perform it after instantiating a Client. This makes using LibProtocol much more pleasant. :^)
This commit is contained in:
parent
3dc87be891
commit
653e61d9cf
6 changed files with 112 additions and 28 deletions
|
@ -1,4 +1,5 @@
|
|||
#include <LibProtocol/Client.h>
|
||||
#include <LibProtocol/Download.h>
|
||||
#include <SharedBuffer.h>
|
||||
|
||||
namespace LibProtocol {
|
||||
|
@ -6,6 +7,7 @@ namespace LibProtocol {
|
|||
Client::Client()
|
||||
: ConnectionNG(*this, "/tmp/psportal")
|
||||
{
|
||||
handshake();
|
||||
}
|
||||
|
||||
void Client::handshake()
|
||||
|
@ -20,27 +22,36 @@ bool Client::is_supported_protocol(const String& protocol)
|
|||
return send_sync<ProtocolServer::IsSupportedProtocol>(protocol)->supported();
|
||||
}
|
||||
|
||||
i32 Client::start_download(const String& url)
|
||||
RefPtr<Download> Client::start_download(const String& url)
|
||||
{
|
||||
return send_sync<ProtocolServer::StartDownload>(url)->download_id();
|
||||
i32 download_id = send_sync<ProtocolServer::StartDownload>(url)->download_id();
|
||||
auto download = Download::create_from_id({}, *this, download_id);
|
||||
m_downloads.set(download_id, download);
|
||||
return download;
|
||||
}
|
||||
|
||||
bool Client::stop_download(i32 download_id)
|
||||
bool Client::stop_download(Badge<Download>, Download& download)
|
||||
{
|
||||
return send_sync<ProtocolServer::StopDownload>(download_id)->success();
|
||||
if (!m_downloads.contains(download.id()))
|
||||
return false;
|
||||
return send_sync<ProtocolServer::StopDownload>(download.id())->success();
|
||||
}
|
||||
|
||||
void Client::handle(const ProtocolClient::DownloadFinished& message)
|
||||
{
|
||||
if (on_download_finish)
|
||||
on_download_finish(message.download_id(), message.success(), message.total_size(), message.shared_buffer_id());
|
||||
RefPtr<Download> download;
|
||||
if ((download = m_downloads.get(message.download_id()).value_or(nullptr))) {
|
||||
download->did_finish({}, message.success(), message.total_size(), message.shared_buffer_id());
|
||||
}
|
||||
send_sync<ProtocolServer::DisownSharedBuffer>(message.shared_buffer_id());
|
||||
m_downloads.remove(message.download_id());
|
||||
}
|
||||
|
||||
void Client::handle(const ProtocolClient::DownloadProgress& message)
|
||||
{
|
||||
if (on_download_progress)
|
||||
on_download_progress(message.download_id(), message.total_size(), message.downloaded_size());
|
||||
if (auto download = m_downloads.get(message.download_id()).value_or(nullptr)) {
|
||||
download->did_progress({}, message.total_size(), message.downloaded_size());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue