summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-10-08 19:37:15 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-10-08 19:37:15 +0200
commit3be6d1aff0b68692cc905c656e15206854fcf76d (patch)
treec18bd2f8a3bdbbc11fb68a995a2669f990a09a9e /Libraries
parent3fdc595e0c730edf1feb49a0488319008ef495b6 (diff)
downloadserenity-3be6d1aff0b68692cc905c656e15206854fcf76d.zip
LibHTML: Add ResourceLoader to support protocol-agnostic URL loading
We now support loading both file:// and http:// URLs. Feel free to visit http://www.serenityos.org/ and enjoy the fancy good times. :^)
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibHTML/DOM/HTMLImageElement.cpp17
-rw-r--r--Libraries/LibHTML/HtmlView.cpp24
-rw-r--r--Libraries/LibHTML/Makefile.shared1
-rw-r--r--Libraries/LibHTML/ResourceLoader.cpp50
-rw-r--r--Libraries/LibHTML/ResourceLoader.h14
5 files changed, 88 insertions, 18 deletions
diff --git a/Libraries/LibHTML/DOM/HTMLImageElement.cpp b/Libraries/LibHTML/DOM/HTMLImageElement.cpp
index 846318fd20..b2b8e33733 100644
--- a/Libraries/LibHTML/DOM/HTMLImageElement.cpp
+++ b/Libraries/LibHTML/DOM/HTMLImageElement.cpp
@@ -1,7 +1,9 @@
+#include <LibDraw/PNGLoader.h>
#include <LibHTML/CSS/StyleResolver.h>
#include <LibHTML/DOM/Document.h>
#include <LibHTML/DOM/HTMLImageElement.h>
#include <LibHTML/Layout/LayoutImage.h>
+#include <LibHTML/ResourceLoader.h>
HTMLImageElement::HTMLImageElement(Document& document, const String& tag_name)
: HTMLElement(document, tag_name)
@@ -21,12 +23,15 @@ void HTMLImageElement::parse_attribute(const String& name, const String& value)
void HTMLImageElement::load_image(const String& src)
{
URL src_url = document().complete_url(src);
- if (src_url.protocol() == "file") {
- m_bitmap = GraphicsBitmap::load_from_file(src_url.path());
- } else {
- // FIXME: Implement! This whole thing should be at a different layer though..
- ASSERT_NOT_REACHED();
- }
+ ResourceLoader::the().load(src_url, [this](auto data) {
+ if (data.is_null()) {
+ dbg() << "HTMLImageElement: Failed to load " << this->src();
+ return;
+ }
+
+ m_bitmap = load_png_from_memory(data.data(), data.size());
+ document().invalidate_layout();
+ });
}
int HTMLImageElement::preferred_width() const
diff --git a/Libraries/LibHTML/HtmlView.cpp b/Libraries/LibHTML/HtmlView.cpp
index 2a6c540bdd..ec471c18f1 100644
--- a/Libraries/LibHTML/HtmlView.cpp
+++ b/Libraries/LibHTML/HtmlView.cpp
@@ -10,6 +10,7 @@
#include <LibHTML/Layout/LayoutNode.h>
#include <LibHTML/Parser/HTMLParser.h>
#include <LibHTML/RenderingContext.h>
+#include <LibHTML/ResourceLoader.h>
#include <stdio.h>
HtmlView::HtmlView(GWidget* parent)
@@ -174,19 +175,18 @@ void HtmlView::load(const URL& url)
if (on_load_start)
on_load_start(url);
- auto f = CFile::construct();
- f->set_filename(url.path());
- if (!f->open(CIODevice::OpenMode::ReadOnly)) {
- dbg() << "HtmlView::load: Error: " << f->error_string();
- return;
- }
+ ResourceLoader::the().load(url, [=](auto data) {
+ if (data.is_null()) {
+ dbg() << "Load failed!";
+ ASSERT_NOT_REACHED();
+ }
- auto html = f->read_all();
- auto document = parse_html(html, url);
- document->normalize();
+ auto document = parse_html(data, url);
+ document->normalize();
- set_document(document);
+ set_document(document);
- if (on_title_change)
- on_title_change(document->title());
+ if (on_title_change)
+ on_title_change(document->title());
+ });
}
diff --git a/Libraries/LibHTML/Makefile.shared b/Libraries/LibHTML/Makefile.shared
index 202ca6b808..8b59ee68bf 100644
--- a/Libraries/LibHTML/Makefile.shared
+++ b/Libraries/LibHTML/Makefile.shared
@@ -37,6 +37,7 @@ LIBHTML_OBJS = \
Layout/BoxModelMetrics.o \
Layout/LineBox.o \
Layout/LineBoxFragment.o \
+ ResourceLoader.o \
HtmlView.o \
Frame.o \
Dump.o
diff --git a/Libraries/LibHTML/ResourceLoader.cpp b/Libraries/LibHTML/ResourceLoader.cpp
new file mode 100644
index 0000000000..8345c6ca6b
--- /dev/null
+++ b/Libraries/LibHTML/ResourceLoader.cpp
@@ -0,0 +1,50 @@
+#include <LibCore/CFile.h>
+#include <LibCore/CHttpJob.h>
+#include <LibCore/CHttpRequest.h>
+#include <LibCore/CNetworkResponse.h>
+#include <LibHTML/ResourceLoader.h>
+
+ResourceLoader& ResourceLoader::the()
+{
+ static ResourceLoader* s_the;
+ if (!s_the)
+ s_the = new ResourceLoader;
+ return *s_the;
+}
+
+void ResourceLoader::load(const URL& url, Function<void(const ByteBuffer&)> callback)
+{
+ if (url.protocol() == "file") {
+ auto f = CFile::construct();
+ f->set_filename(url.path());
+ if (!f->open(CIODevice::OpenMode::ReadOnly)) {
+ dbg() << "HtmlView::load: Error: " << f->error_string();
+ callback({});
+ return;
+ }
+
+ auto data = f->read_all();
+ callback(data);
+ return;
+ }
+
+ if (url.protocol() == "http") {
+ CHttpRequest request;
+ request.set_url(url);
+ request.set_method(CHttpRequest::Method::GET);
+ auto job = request.schedule();
+ job->on_finish = [job, callback = move(callback)](bool success) {
+ if (!success) {
+ dbg() << "HTTP job failed!";
+ ASSERT_NOT_REACHED();
+ }
+ auto* response = job->response();
+ ASSERT(response);
+ callback(response->payload());
+ };
+ return;
+ }
+
+ dbg() << "Unimplemented protocol: " << url.protocol();
+ ASSERT_NOT_REACHED();
+}
diff --git a/Libraries/LibHTML/ResourceLoader.h b/Libraries/LibHTML/ResourceLoader.h
new file mode 100644
index 0000000000..44df58c751
--- /dev/null
+++ b/Libraries/LibHTML/ResourceLoader.h
@@ -0,0 +1,14 @@
+#pragma once
+
+#include <AK/Function.h>
+#include <AK/URL.h>
+
+class ResourceLoader {
+public:
+ static ResourceLoader& the();
+
+ void load(const URL&, Function<void(const ByteBuffer&)>);
+
+private:
+ ResourceLoader() {}
+};