mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:57:35 +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
37
Libraries/LibProtocol/Download.h
Normal file
37
Libraries/LibProtocol/Download.h
Normal file
|
@ -0,0 +1,37 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/Badge.h>
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <AK/Function.h>
|
||||
#include <AK/RefCounted.h>
|
||||
#include <AK/WeakPtr.h>
|
||||
|
||||
class SharedBuffer;
|
||||
|
||||
namespace LibProtocol {
|
||||
|
||||
class Client;
|
||||
|
||||
class Download : public RefCounted<Download> {
|
||||
public:
|
||||
static NonnullRefPtr<Download> create_from_id(Badge<Client>, Client& client, i32 download_id)
|
||||
{
|
||||
return adopt(*new Download(client, download_id));
|
||||
}
|
||||
|
||||
int id() const { return m_download_id; }
|
||||
bool stop();
|
||||
|
||||
Function<void(bool success, const ByteBuffer& payload, RefPtr<SharedBuffer> payload_storage)> on_finish;
|
||||
Function<void(u32 total_size, u32 downloaded_size)> on_progress;
|
||||
|
||||
void did_finish(Badge<Client>, bool success, u32 total_size, i32 shared_buffer_id);
|
||||
void did_progress(Badge<Client>, u32 total_size, u32 downloaded_size);
|
||||
|
||||
private:
|
||||
explicit Download(Client&, i32 download_id);
|
||||
WeakPtr<Client> m_client;
|
||||
int m_download_id { -1 };
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue