/* * Copyright (c) 2018-2020, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include namespace Core { NetworkJob::NetworkJob(OutputStream& output_stream) : m_output_stream(output_stream) { } NetworkJob::~NetworkJob() { } void NetworkJob::start(NonnullRefPtr) { } void NetworkJob::shutdown() { } void NetworkJob::did_finish(NonnullRefPtr&& response) { if (is_cancelled()) return; // NOTE: We protect ourselves here, since the on_finish callback may otherwise // trigger destruction of this job somehow. NonnullRefPtr protector(*this); m_response = move(response); dbgln_if(CNETWORKJOB_DEBUG, "{} job did_finish", *this); VERIFY(on_finish); on_finish(true); shutdown(); } void NetworkJob::did_fail(Error error) { if (is_cancelled()) return; // NOTE: We protect ourselves here, since the on_finish callback may otherwise // trigger destruction of this job somehow. NonnullRefPtr protector(*this); m_error = error; dbgln_if(CNETWORKJOB_DEBUG, "{}{{{:p}}} job did_fail! error: {} ({})", class_name(), this, (unsigned)error, to_string(error)); VERIFY(on_finish); on_finish(false); shutdown(); } void NetworkJob::did_progress(Optional total_size, u32 downloaded) { if (is_cancelled()) return; // NOTE: We protect ourselves here, since the callback may otherwise // trigger destruction of this job somehow. NonnullRefPtr protector(*this); if (on_progress) on_progress(total_size, downloaded); } const char* to_string(NetworkJob::Error error) { switch (error) { case NetworkJob::Error::ProtocolFailed: return "ProtocolFailed"; case NetworkJob::Error::ConnectionFailed: return "ConnectionFailed"; case NetworkJob::Error::TransmissionFailed: return "TransmissionFailed"; case NetworkJob::Error::Cancelled: return "Cancelled"; default: return "(Unknown error)"; } } }