PSClientConnection.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include <ProtocolServer/Download.h>
  2. #include <ProtocolServer/PSClientConnection.h>
  3. #include <ProtocolServer/Protocol.h>
  4. #include <ProtocolServer/ProtocolClientEndpoint.h>
  5. #include <LibC/SharedBuffer.h>
  6. static HashMap<int, RefPtr<PSClientConnection>> s_connections;
  7. PSClientConnection::PSClientConnection(CLocalSocket& socket, int client_id)
  8. : IClientConnection(*this, socket, client_id)
  9. {
  10. s_connections.set(client_id, *this);
  11. }
  12. PSClientConnection::~PSClientConnection()
  13. {
  14. }
  15. void PSClientConnection::die()
  16. {
  17. s_connections.remove(client_id());
  18. }
  19. OwnPtr<ProtocolServer::IsSupportedProtocolResponse> PSClientConnection::handle(const ProtocolServer::IsSupportedProtocol& message)
  20. {
  21. bool supported = Protocol::find_by_name(message.protocol().to_lowercase());
  22. return make<ProtocolServer::IsSupportedProtocolResponse>(supported);
  23. }
  24. OwnPtr<ProtocolServer::StartDownloadResponse> PSClientConnection::handle(const ProtocolServer::StartDownload& message)
  25. {
  26. URL url(message.url());
  27. ASSERT(url.is_valid());
  28. auto* protocol = Protocol::find_by_name(url.protocol());
  29. ASSERT(protocol);
  30. auto download = protocol->start_download(*this, url);
  31. return make<ProtocolServer::StartDownloadResponse>(download->id());
  32. }
  33. OwnPtr<ProtocolServer::StopDownloadResponse> PSClientConnection::handle(const ProtocolServer::StopDownload& message)
  34. {
  35. auto* download = Download::find_by_id(message.download_id());
  36. bool success = false;
  37. if (download) {
  38. download->stop();
  39. }
  40. return make<ProtocolServer::StopDownloadResponse>(success);
  41. }
  42. void PSClientConnection::did_finish_download(Badge<Download>, Download& download, bool success)
  43. {
  44. RefPtr<SharedBuffer> buffer;
  45. if (success && !download.payload().is_null()) {
  46. buffer = SharedBuffer::create_with_size(download.payload().size());
  47. memcpy(buffer->data(), download.payload().data(), download.payload().size());
  48. buffer->seal();
  49. buffer->share_with(client_pid());
  50. m_shared_buffers.set(buffer->shared_buffer_id(), buffer);
  51. }
  52. post_message(ProtocolClient::DownloadFinished(download.id(), success, download.total_size(), buffer ? buffer->shared_buffer_id() : -1));
  53. }
  54. void PSClientConnection::did_progress_download(Badge<Download>, Download& download)
  55. {
  56. post_message(ProtocolClient::DownloadProgress(download.id(), download.total_size(), download.downloaded_size()));
  57. }
  58. OwnPtr<ProtocolServer::GreetResponse> PSClientConnection::handle(const ProtocolServer::Greet&)
  59. {
  60. return make<ProtocolServer::GreetResponse>(client_id());
  61. }
  62. OwnPtr<ProtocolServer::DisownSharedBufferResponse> PSClientConnection::handle(const ProtocolServer::DisownSharedBuffer& message)
  63. {
  64. m_shared_buffers.remove(message.shared_buffer_id());
  65. return make<ProtocolServer::DisownSharedBufferResponse>();
  66. }