summaryrefslogtreecommitdiff
path: root/Libraries/LibWeb
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-04-03 22:58:05 +0200
committerAndreas Kling <kling@serenityos.org>2020-04-03 22:58:05 +0200
commit18d45d10822219d0e86233ae112f549512f98452 (patch)
tree9312c2dc98fcd9b177f9a0d495744fc2534695cf /Libraries/LibWeb
parenteeec1c1293705945f6b03fc5f33693e3c8fdc02a (diff)
downloadserenity-18d45d10822219d0e86233ae112f549512f98452.zip
LibWeb: Add ResourceLoader::load_sync()
This function creates a nested event loop and runs a load() operation inside it, returning only once the load has either succeeded or failed. This will be used to implement blocking loads (ew!)
Diffstat (limited to 'Libraries/LibWeb')
-rw-r--r--Libraries/LibWeb/ResourceLoader.cpp19
-rw-r--r--Libraries/LibWeb/ResourceLoader.h1
2 files changed, 20 insertions, 0 deletions
diff --git a/Libraries/LibWeb/ResourceLoader.cpp b/Libraries/LibWeb/ResourceLoader.cpp
index daf2b813eb..47fa1741ee 100644
--- a/Libraries/LibWeb/ResourceLoader.cpp
+++ b/Libraries/LibWeb/ResourceLoader.cpp
@@ -25,6 +25,7 @@
*/
#include <AK/SharedBuffer.h>
+#include <LibCore/EventLoop.h>
#include <LibCore/File.h>
#include <LibProtocol/Client.h>
#include <LibProtocol/Download.h>
@@ -45,6 +46,24 @@ ResourceLoader::ResourceLoader()
{
}
+void ResourceLoader::load_sync(const URL& url, Function<void(const ByteBuffer&)> success_callback, Function<void(const String&)> error_callback)
+{
+ Core::EventLoop loop;
+
+ load(
+ url,
+ [&](auto& data) {
+ success_callback(data);
+ loop.quit(0);
+ },
+ [&](auto& string) {
+ error_callback(string);
+ loop.quit(0);
+ });
+
+ loop.exec();
+}
+
void ResourceLoader::load(const URL& url, Function<void(const ByteBuffer&)> success_callback, Function<void(const String&)> error_callback)
{
if (url.protocol() == "file") {
diff --git a/Libraries/LibWeb/ResourceLoader.h b/Libraries/LibWeb/ResourceLoader.h
index 26f122bbf6..4a98b16591 100644
--- a/Libraries/LibWeb/ResourceLoader.h
+++ b/Libraries/LibWeb/ResourceLoader.h
@@ -42,6 +42,7 @@ public:
static ResourceLoader& the();
void load(const URL&, Function<void(const ByteBuffer&)> success_callback, Function<void(const String&)> error_callback = nullptr);
+ void load_sync(const URL&, Function<void(const ByteBuffer&)> success_callback, Function<void(const String&)> error_callback = nullptr);
Function<void()> on_load_counter_change;