Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <AK/Badge.h>
28#include <AK/SharedBuffer.h>
29#include <ProtocolServer/Download.h>
30#include <ProtocolServer/PSClientConnection.h>
31#include <ProtocolServer/Protocol.h>
32#include <ProtocolServer/ProtocolClientEndpoint.h>
33
34static HashMap<int, RefPtr<PSClientConnection>> s_connections;
35
36PSClientConnection::PSClientConnection(Core::LocalSocket& socket, int client_id)
37 : IPC::ClientConnection<ProtocolServerEndpoint>(*this, socket, client_id)
38{
39 s_connections.set(client_id, *this);
40}
41
42PSClientConnection::~PSClientConnection()
43{
44}
45
46void PSClientConnection::die()
47{
48 s_connections.remove(client_id());
49}
50
51OwnPtr<Messages::ProtocolServer::IsSupportedProtocolResponse> PSClientConnection::handle(const Messages::ProtocolServer::IsSupportedProtocol& message)
52{
53 bool supported = Protocol::find_by_name(message.protocol().to_lowercase());
54 return make<Messages::ProtocolServer::IsSupportedProtocolResponse>(supported);
55}
56
57OwnPtr<Messages::ProtocolServer::StartDownloadResponse> PSClientConnection::handle(const Messages::ProtocolServer::StartDownload& message)
58{
59 URL url(message.url());
60 ASSERT(url.is_valid());
61 auto* protocol = Protocol::find_by_name(url.protocol());
62 ASSERT(protocol);
63 auto download = protocol->start_download(*this, url);
64 return make<Messages::ProtocolServer::StartDownloadResponse>(download->id());
65}
66
67OwnPtr<Messages::ProtocolServer::StopDownloadResponse> PSClientConnection::handle(const Messages::ProtocolServer::StopDownload& message)
68{
69 auto* download = Download::find_by_id(message.download_id());
70 bool success = false;
71 if (download) {
72 download->stop();
73 }
74 return make<Messages::ProtocolServer::StopDownloadResponse>(success);
75}
76
77void PSClientConnection::did_finish_download(Badge<Download>, Download& download, bool success)
78{
79 RefPtr<SharedBuffer> buffer;
80 if (success && !download.payload().is_null()) {
81 buffer = SharedBuffer::create_with_size(download.payload().size());
82 memcpy(buffer->data(), download.payload().data(), download.payload().size());
83 buffer->seal();
84 buffer->share_with(client_pid());
85 m_shared_buffers.set(buffer->shared_buffer_id(), buffer);
86 }
87 post_message(Messages::ProtocolClient::DownloadFinished(download.id(), success, download.total_size(), buffer ? buffer->shared_buffer_id() : -1));
88}
89
90void PSClientConnection::did_progress_download(Badge<Download>, Download& download)
91{
92 post_message(Messages::ProtocolClient::DownloadProgress(download.id(), download.total_size(), download.downloaded_size()));
93}
94
95OwnPtr<Messages::ProtocolServer::GreetResponse> PSClientConnection::handle(const Messages::ProtocolServer::Greet&)
96{
97 return make<Messages::ProtocolServer::GreetResponse>(client_id());
98}
99
100OwnPtr<Messages::ProtocolServer::DisownSharedBufferResponse> PSClientConnection::handle(const Messages::ProtocolServer::DisownSharedBuffer& message)
101{
102 m_shared_buffers.remove(message.shared_buffer_id());
103 return make<Messages::ProtocolServer::DisownSharedBufferResponse>();
104}