Download.cpp 943 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include <AK/SharedBuffer.h>
  2. #include <LibProtocol/Client.h>
  3. #include <LibProtocol/Download.h>
  4. namespace LibProtocol {
  5. Download::Download(Client& client, i32 download_id)
  6. : m_client(client.make_weak_ptr())
  7. , m_download_id(download_id)
  8. {
  9. }
  10. bool Download::stop()
  11. {
  12. return m_client->stop_download({}, *this);
  13. }
  14. void Download::did_finish(Badge<Client>, bool success, u32 total_size, i32 shared_buffer_id)
  15. {
  16. if (!on_finish)
  17. return;
  18. ByteBuffer payload;
  19. RefPtr<SharedBuffer> shared_buffer;
  20. if (success && shared_buffer_id != -1) {
  21. shared_buffer = SharedBuffer::create_from_shared_buffer_id(shared_buffer_id);
  22. payload = ByteBuffer::wrap(shared_buffer->data(), total_size);
  23. }
  24. on_finish(success, payload, move(shared_buffer));
  25. }
  26. void Download::did_progress(Badge<Client>, u32 total_size, u32 downloaded_size)
  27. {
  28. if (on_progress)
  29. on_progress(total_size, downloaded_size);
  30. }
  31. }