/* * Copyright (c) 2018-2020, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include namespace Core { class NetworkJob : public Object { C_OBJECT_ABSTRACT(NetworkJob) public: enum class Error { None, ConnectionFailed, TransmissionFailed, ProtocolFailed, Cancelled, }; virtual ~NetworkJob() override; // Could fire twice, after Headers and after Trailers! Function& response_headers, Optional response_code)> on_headers_received; Function on_finish; Function, u32)> on_progress; bool is_cancelled() const { return m_error == Error::Cancelled; } bool has_error() const { return m_error != Error::None; } Error error() const { return m_error; } NetworkResponse* response() { return m_response.ptr(); } const NetworkResponse* response() const { return m_response.ptr(); } enum class ShutdownMode { DetachFromSocket, CloseSocket, }; virtual void start(NonnullRefPtr) = 0; virtual void shutdown(ShutdownMode) = 0; void cancel() { shutdown(ShutdownMode::DetachFromSocket); m_error = Error::Cancelled; } protected: NetworkJob(OutputStream&); void did_finish(NonnullRefPtr&&); void did_fail(Error); void did_progress(Optional total_size, u32 downloaded); size_t do_write(ReadonlyBytes bytes) { return m_output_stream.write(bytes); } private: RefPtr m_response; OutputStream& m_output_stream; Error m_error { Error::None }; }; const char* to_string(NetworkJob::Error); } template<> struct AK::Formatter : Formatter { };