Parcourir la source

LibHTML: Use LibProtocol for HTTP requests :^)

This moves all of the browser networking to ProtocolServer.
Andreas Kling il y a 5 ans
Parent
commit
0d2659c0a2

+ 1 - 1
Applications/Makefile.common

@@ -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 $<

+ 1 - 1
DevTools/HackStudio/Makefile

@@ -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 $<

+ 15 - 18
Libraries/LibHTML/ResourceLoader.cpp

@@ -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();
-        ++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();
+        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 job failed!";
+                dbg() << "HTTP load failed!";
                 ASSERT_NOT_REACHED();
             }
-            auto* response = job->response();
-            ASSERT(response);
-            callback(response->payload());
+            callback(ByteBuffer::copy(payload.data(), payload.size()));
         };
+        ++m_pending_loads;
+        if (on_load_counter_change)
+            on_load_counter_change();
         return;
     }
 

+ 8 - 1
Libraries/LibHTML/ResourceLoader.h

@@ -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;
 };