LibHTML: Use LibProtocol for HTTP requests :^)

This moves all of the browser networking to ProtocolServer.
This commit is contained in:
Andreas Kling 2019-11-24 14:24:09 +01:00
parent 653e61d9cf
commit 0d2659c0a2
Notes: sideshowbarker 2024-07-19 11:05:52 +09:00
4 changed files with 26 additions and 22 deletions

View file

@ -3,7 +3,7 @@ DEFINES += -DUSERLAND
all: $(APP)
$(APP): $(OBJS)
$(LD) -o $(APP) $(LDFLAGS) $(OBJS) -lmarkdown -lhtml -laudio -lipc -lvt -lpcidb -lgui -ldraw -lthread -lpthread -lcore -lc
$(LD) -o $(APP) $(LDFLAGS) $(OBJS) -lmarkdown -lhtml -laudio -lipc -lvt -lpcidb -lgui -ldraw -lprotocol -lipc -lthread -lpthread -lcore -lc
.cpp.o:
@echo "CXX $<"; $(CXX) $(CXXFLAGS) -o $@ -c $<

View file

@ -25,7 +25,7 @@ DEFINES += -DUSERLAND
all: $(APP)
$(APP): $(OBJS)
$(LD) -o $(APP) $(LDFLAGS) $(OBJS) -lvt -lhtml -lmarkdown -lgui -ldraw -lthread -lpthread -lcore -lc
$(LD) -o $(APP) $(LDFLAGS) $(OBJS) -lvt -lhtml -lprotocol -lipc -lmarkdown -lgui -ldraw -lthread -lpthread -lcore -lc
.cpp.o:
@echo "CXX $<"; $(CXX) $(CXXFLAGS) -o $@ -c $<

View file

@ -1,8 +1,8 @@
#include <LibC/SharedBuffer.h>
#include <LibCore/CFile.h>
#include <LibCore/CHttpJob.h>
#include <LibCore/CHttpRequest.h>
#include <LibCore/CNetworkResponse.h>
#include <LibHTML/ResourceLoader.h>
#include <LibProtocol/Client.h>
#include <LibProtocol/Download.h>
ResourceLoader& ResourceLoader::the()
{
@ -12,6 +12,11 @@ ResourceLoader& ResourceLoader::the()
return *s_the;
}
ResourceLoader::ResourceLoader()
: m_protocol_client(LibProtocol::Client::construct())
{
}
void ResourceLoader::load(const URL& url, Function<void(const ByteBuffer&)> callback)
{
if (url.protocol() == "file") {
@ -31,25 +36,17 @@ void ResourceLoader::load(const URL& url, Function<void(const ByteBuffer&)> call
}
if (url.protocol() == "http") {
CHttpRequest request;
request.set_url(url);
request.set_method(CHttpRequest::Method::GET);
auto job = request.schedule();
auto download = protocol_client().start_download(url.to_string());
download->on_finish = [callback = move(callback)](bool success, const ByteBuffer& payload, auto) {
if (!success) {
dbg() << "HTTP load failed!";
ASSERT_NOT_REACHED();
}
callback(ByteBuffer::copy(payload.data(), payload.size()));
};
++m_pending_loads;
if (on_load_counter_change)
on_load_counter_change();
job->on_finish = [this, job, callback = move(callback)](bool success) {
--m_pending_loads;
if (on_load_counter_change)
on_load_counter_change();
if (!success) {
dbg() << "HTTP job failed!";
ASSERT_NOT_REACHED();
}
auto* response = job->response();
ASSERT(response);
callback(response->payload());
};
return;
}

View file

@ -4,6 +4,10 @@
#include <AK/URL.h>
#include <LibCore/CObject.h>
namespace LibProtocol {
class Client;
}
class ResourceLoader : public CObject {
C_OBJECT(ResourceLoader)
public:
@ -16,7 +20,10 @@ public:
int pending_loads() const { return m_pending_loads; }
private:
ResourceLoader() {}
ResourceLoader();
int m_pending_loads { 0 };
LibProtocol::Client& protocol_client() { return *m_protocol_client; }
RefPtr<LibProtocol::Client> m_protocol_client;
};