ladybird/Userland/Libraries/LibCore/NetworkJob.h
Ali Mohammad Pur 5e1499d104 Everywhere: Rename {Deprecated => Byte}String
This commit un-deprecates DeprecatedString, and repurposes it as a byte
string.
As the null state has already been removed, there are no other
particularly hairy blockers in repurposing this type as a byte string
(what it _really_ is).

This commit is auto-generated:
  $ xs=$(ack -l \bDeprecatedString\b\|deprecated_string AK Userland \
    Meta Ports Ladybird Tests Kernel)
  $ perl -pie 's/\bDeprecatedString\b/ByteString/g;
    s/deprecated_string/byte_string/g' $xs
  $ clang-format --style=file -i \
    $(git diff --name-only | grep \.cpp\|\.h)
  $ gn format $(git ls-files '*.gn' '*.gni')
2023-12-17 18:25:10 +03:30

75 lines
2.0 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Function.h>
#include <AK/Stream.h>
#include <LibCore/EventReceiver.h>
#include <LibCore/Forward.h>
namespace Core {
class NetworkJob : public EventReceiver {
C_OBJECT_ABSTRACT(NetworkJob)
public:
enum class Error {
None,
ConnectionFailed,
TransmissionFailed,
ProtocolFailed,
Cancelled,
};
virtual ~NetworkJob() override = default;
// Could fire twice, after Headers and after Trailers!
Function<void(HashMap<ByteString, ByteString, CaseInsensitiveStringTraits> const& response_headers, Optional<u32> response_code)> on_headers_received;
Function<void(bool success)> on_finish;
Function<void(Optional<u64>, u64)> 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(); }
NetworkResponse const* response() const { return m_response.ptr(); }
enum class ShutdownMode {
DetachFromSocket,
CloseSocket,
};
virtual void start(Core::BufferedSocketBase&) = 0;
virtual void shutdown(ShutdownMode) = 0;
virtual void fail(Error error) { did_fail(error); }
void cancel()
{
shutdown(ShutdownMode::DetachFromSocket);
m_error = Error::Cancelled;
}
protected:
NetworkJob(Stream&);
void did_finish(NonnullRefPtr<NetworkResponse>&&);
void did_fail(Error);
void did_progress(Optional<u64> total_size, u64 downloaded);
ErrorOr<size_t> do_write(ReadonlyBytes bytes) { return m_output_stream.write_some(bytes); }
private:
RefPtr<NetworkResponse> m_response;
Stream& m_output_stream;
Error m_error { Error::None };
};
char const* to_string(NetworkJob::Error);
}
template<>
struct AK::Formatter<Core::NetworkJob> : Formatter<Core::EventReceiver> {
};