summaryrefslogtreecommitdiff
path: root/Libraries/LibCore/CNetworkJob.h
blob: 2d2bb02778a56ff98fa6c2a5103a6e9e6ae6b5da (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
38
39
40
41
42
43
44
45
46
47
#pragma once

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

class CNetworkResponse;

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

    Function<void(bool success)> on_finish;

    bool is_cancelled() const { return m_error == Error::Cancelled; }
    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 void shutdown() = 0;

    void cancel()
    {
        shutdown();
        m_error = Error::Cancelled;
    }

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

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

const char* to_string(CNetworkJob::Error);