summaryrefslogtreecommitdiff
path: root/LibCore/CNetworkJob.h
blob: 4b6b479c8fbc4b6f199d7021f0cd86ae1effe86e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#pragma once

#include <LibCore/CObject.h>
#include <AK/Function.h>

class CNetworkResponse;

class CNetworkJob : public CObject {
public:
    enum class Error {
        None,
        ConnectionFailed,
        TransmissionFailed,
        ProtocolFailed,
    };
    virtual ~CNetworkJob() override;

    Function<void(bool success)> on_finish;

    bool has_error() const { return m_error != Error::None; }
    Error error() const { return m_error; }
    CNetworkResponse* response() { return m_response.ptr(); }
    const CNetworkResponse* response() const { return m_response.ptr(); }

    virtual void start() = 0;

    virtual const char* class_name() const override { return "CNetworkJob"; }

protected:
    CNetworkJob();
    void did_finish(Retained<CNetworkResponse>&&);
    void did_fail(Error);

private:
    RetainPtr<CNetworkResponse> m_response;
    Error m_error { Error::None };
};