diff options
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibCore/CHttpJob.cpp | 11 | ||||
-rw-r--r-- | Libraries/LibCore/CHttpJob.h | 1 | ||||
-rw-r--r-- | Libraries/LibCore/CNetworkJob.cpp | 6 | ||||
-rw-r--r-- | Libraries/LibCore/CNetworkJob.h | 9 | ||||
-rw-r--r-- | Libraries/LibCore/ObjectPtr.h | 2 |
5 files changed, 27 insertions, 2 deletions
diff --git a/Libraries/LibCore/CHttpJob.cpp b/Libraries/LibCore/CHttpJob.cpp index 472aae01d9..6ddde0475a 100644 --- a/Libraries/LibCore/CHttpJob.cpp +++ b/Libraries/LibCore/CHttpJob.cpp @@ -26,6 +26,8 @@ void CHttpJob::on_socket_connected() return deferred_invoke([this](auto&) { did_fail(CNetworkJob::Error::TransmissionFailed); }); m_socket->on_ready_to_read = [&] { + if (is_cancelled()) + return; if (m_state == State::InStatus) { if (!m_socket->can_read_line()) return; @@ -125,3 +127,12 @@ void CHttpJob::start() if (!success) return did_fail(CNetworkJob::Error::ConnectionFailed); } + +void CHttpJob::shutdown() +{ + if (!m_socket) + return; + m_socket->on_ready_to_read = nullptr; + m_socket->on_connected = nullptr; + m_socket = nullptr; +} diff --git a/Libraries/LibCore/CHttpJob.h b/Libraries/LibCore/CHttpJob.h index d0aa714920..3e3b758977 100644 --- a/Libraries/LibCore/CHttpJob.h +++ b/Libraries/LibCore/CHttpJob.h @@ -13,6 +13,7 @@ public: virtual ~CHttpJob() override; virtual void start() override; + virtual void shutdown() override; private: void on_socket_connected(); diff --git a/Libraries/LibCore/CNetworkJob.cpp b/Libraries/LibCore/CNetworkJob.cpp index 6522b355a7..4334cb67a7 100644 --- a/Libraries/LibCore/CNetworkJob.cpp +++ b/Libraries/LibCore/CNetworkJob.cpp @@ -16,7 +16,7 @@ void CNetworkJob::did_finish(NonnullRefPtr<CNetworkResponse>&& response) dbg() << *this << " job did_finish!"; ASSERT(on_finish); on_finish(true); - delete_later(); + shutdown(); } void CNetworkJob::did_fail(Error error) @@ -25,7 +25,7 @@ void CNetworkJob::did_fail(Error error) dbgprintf("%s{%p} job did_fail! error: %u (%s)\n", class_name(), this, (unsigned)error, to_string(error)); ASSERT(on_finish); on_finish(false); - delete_later(); + shutdown(); } const char* to_string(CNetworkJob::Error error) @@ -37,6 +37,8 @@ const char* to_string(CNetworkJob::Error error) return "ConnectionFailed"; case CNetworkJob::Error::TransmissionFailed: return "TransmissionFailed"; + case CNetworkJob::Error::Cancelled: + return "Cancelled"; default: return "(Unknown error)"; } diff --git a/Libraries/LibCore/CNetworkJob.h b/Libraries/LibCore/CNetworkJob.h index a0638dd46d..2d2bb02778 100644 --- a/Libraries/LibCore/CNetworkJob.h +++ b/Libraries/LibCore/CNetworkJob.h @@ -13,17 +13,26 @@ public: 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(); diff --git a/Libraries/LibCore/ObjectPtr.h b/Libraries/LibCore/ObjectPtr.h index a34c8f8224..98a4cd2bf0 100644 --- a/Libraries/LibCore/ObjectPtr.h +++ b/Libraries/LibCore/ObjectPtr.h @@ -76,6 +76,8 @@ public: T* ptr() const { return m_ptr; } T* leak_ptr() { return exchange(m_ptr, nullptr); } + operator bool() const { return !!m_ptr; } + private: T* m_ptr { nullptr }; }; |