mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-13 01:59:14 +03:00
LibCore: Convert CHttpJob to ObjectPtr
This commit is contained in:
parent
953cb4e436
commit
6b347747f2
Notes:
sideshowbarker
2024-07-19 12:02:33 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/6b347747f2f
@ -21,10 +21,10 @@ void BoardListModel::update()
|
||||
CHttpRequest request;
|
||||
request.set_url("http://a.4cdn.org/boards.json");
|
||||
|
||||
auto* job = request.schedule();
|
||||
m_pending_job = request.schedule();
|
||||
|
||||
job->on_finish = [job, this](bool success) {
|
||||
auto* response = job->response();
|
||||
m_pending_job->on_finish = [this](bool success) {
|
||||
auto* response = m_pending_job->response();
|
||||
dbg() << "Board list download finished, success=" << success << ", response=" << response;
|
||||
|
||||
if (!success)
|
||||
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <AK/JsonArray.h>
|
||||
#include <LibCore/CHttpJob.h>
|
||||
#include <LibGUI/GModel.h>
|
||||
|
||||
class BoardListModel final : public GModel {
|
||||
@ -24,4 +25,5 @@ private:
|
||||
BoardListModel();
|
||||
|
||||
JsonArray m_boards;
|
||||
ObjectPtr<CHttpJob> m_pending_job;
|
||||
};
|
||||
|
@ -29,13 +29,13 @@ void ThreadCatalogModel::update()
|
||||
CHttpRequest request;
|
||||
request.set_url(String::format("http://a.4cdn.org/%s/catalog.json", m_board.characters()));
|
||||
|
||||
auto* job = request.schedule();
|
||||
m_pending_job = request.schedule();
|
||||
|
||||
if (on_load_started)
|
||||
on_load_started();
|
||||
|
||||
job->on_finish = [job, this](bool success) {
|
||||
auto* response = job->response();
|
||||
m_pending_job->on_finish = [this](bool success) {
|
||||
auto* response = m_pending_job->response();
|
||||
dbg() << "Catalog download finished, success=" << success << ", response=" << response;
|
||||
|
||||
if (!success) {
|
||||
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <AK/JsonArray.h>
|
||||
#include <LibCore/CHttpJob.h>
|
||||
#include <LibGUI/GModel.h>
|
||||
|
||||
class ThreadCatalogModel final : public GModel {
|
||||
@ -36,4 +37,5 @@ private:
|
||||
|
||||
String m_board { "g" };
|
||||
JsonArray m_catalog;
|
||||
ObjectPtr<CHttpJob> m_pending_job;
|
||||
};
|
||||
|
@ -18,7 +18,7 @@ int main(int argc, char** argv)
|
||||
return;
|
||||
}
|
||||
auto& response = static_cast<const CHttpResponse&>(*job->response());
|
||||
printf("%s{%p}: on_receive: code=%d\n", job->class_name(), job, response.code());
|
||||
printf("%s{%p}: on_receive: code=%d\n", job->class_name(), job.ptr(), response.code());
|
||||
//printf("payload:\n");
|
||||
//printf("%s", response.payload().pointer());
|
||||
printf("payload was %d bytes\n", response.payload().size());
|
||||
|
@ -10,9 +10,9 @@ CHttpRequest::~CHttpRequest()
|
||||
{
|
||||
}
|
||||
|
||||
CNetworkJob* CHttpRequest::schedule()
|
||||
ObjectPtr<CNetworkJob> CHttpRequest::schedule()
|
||||
{
|
||||
auto* job = new CHttpJob(*this);
|
||||
auto job = CHttpJob::construct(*this);
|
||||
job->start();
|
||||
return job;
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
#include <AK/String.h>
|
||||
#include <AK/URL.h>
|
||||
#include <LibCore/ObjectPtr.h>
|
||||
|
||||
class CNetworkJob;
|
||||
|
||||
@ -26,7 +27,7 @@ public:
|
||||
String method_name() const;
|
||||
ByteBuffer to_raw_request() const;
|
||||
|
||||
CNetworkJob* schedule();
|
||||
ObjectPtr<CNetworkJob> schedule();
|
||||
|
||||
private:
|
||||
URL m_url;
|
||||
|
@ -8,22 +8,46 @@ template<typename T>
|
||||
class ObjectPtr {
|
||||
public:
|
||||
ObjectPtr() {}
|
||||
ObjectPtr(T* ptr) : m_ptr(ptr) {}
|
||||
ObjectPtr(T& ptr) : m_ptr(&ptr) {}
|
||||
ObjectPtr(T* ptr)
|
||||
: m_ptr(ptr)
|
||||
{
|
||||
}
|
||||
ObjectPtr(T& ptr)
|
||||
: m_ptr(&ptr)
|
||||
{
|
||||
}
|
||||
~ObjectPtr()
|
||||
{
|
||||
if (m_ptr && !m_ptr->parent())
|
||||
delete m_ptr;
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
ObjectPtr(U* ptr)
|
||||
: m_ptr(static_cast<T*>(ptr))
|
||||
{
|
||||
}
|
||||
|
||||
ObjectPtr(const ObjectPtr& other)
|
||||
: m_ptr(other.m_ptr)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
ObjectPtr(const ObjectPtr<U>& other)
|
||||
: m_ptr(static_cast<T*>(const_cast<ObjectPtr<U>&>(other).ptr()))
|
||||
{
|
||||
}
|
||||
|
||||
ObjectPtr(ObjectPtr&& other)
|
||||
{
|
||||
m_ptr = exchange(other.m_ptr, nullptr);
|
||||
m_ptr = other.leak_ptr();
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
ObjectPtr(const ObjectPtr<U>&& other)
|
||||
{
|
||||
m_ptr = static_cast<T*>(const_cast<ObjectPtr<U>&>(other).leak_ptr());
|
||||
}
|
||||
|
||||
ObjectPtr& operator=(const ObjectPtr& other)
|
||||
@ -49,6 +73,9 @@ public:
|
||||
T& operator*() { return *m_ptr; }
|
||||
const T& operator*() const { return *m_ptr; }
|
||||
|
||||
T* ptr() const { return m_ptr; }
|
||||
T* leak_ptr() { return exchange(m_ptr, nullptr); }
|
||||
|
||||
private:
|
||||
T* m_ptr { nullptr };
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user