mirror of
https://github.com/RGBCube/serenity
synced 2025-05-16 19:15:07 +00:00
Browser: Add error page
Add a simple HTML error page that gets loaded into the HtmlView when loading the page fails. Closes #1210 and #1516
This commit is contained in:
parent
b1365d94f4
commit
021d78f8f7
5 changed files with 79 additions and 28 deletions
|
@ -26,9 +26,9 @@
|
|||
|
||||
#include <AK/SharedBuffer.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibWeb/ResourceLoader.h>
|
||||
#include <LibProtocol/Client.h>
|
||||
#include <LibProtocol/Download.h>
|
||||
#include <LibWeb/ResourceLoader.h>
|
||||
|
||||
namespace Web {
|
||||
|
||||
|
@ -45,36 +45,37 @@ ResourceLoader::ResourceLoader()
|
|||
{
|
||||
}
|
||||
|
||||
void ResourceLoader::load(const URL& url, Function<void(const ByteBuffer&)> callback)
|
||||
void ResourceLoader::load(const URL& url, Function<void(const ByteBuffer&)> success_callback, Function<void(const String&)> error_callback)
|
||||
{
|
||||
if (url.protocol() == "file") {
|
||||
auto f = Core::File::construct();
|
||||
f->set_filename(url.path());
|
||||
if (!f->open(Core::IODevice::OpenMode::ReadOnly)) {
|
||||
dbg() << "ResourceLoader::load: Error: " << f->error_string();
|
||||
callback({});
|
||||
if (error_callback)
|
||||
error_callback(f->error_string());
|
||||
return;
|
||||
}
|
||||
|
||||
auto data = f->read_all();
|
||||
deferred_invoke([data = move(data), callback = move(callback)](auto&) {
|
||||
callback(data);
|
||||
deferred_invoke([data = move(data), success_callback = move(success_callback)](auto&) {
|
||||
success_callback(data);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (url.protocol() == "http") {
|
||||
auto download = protocol_client().start_download(url.to_string());
|
||||
download->on_finish = [this, callback = move(callback)](bool success, const ByteBuffer& payload, auto) {
|
||||
download->on_finish = [this, success_callback = move(success_callback), error_callback = move(error_callback)](bool success, const ByteBuffer& payload, auto) {
|
||||
--m_pending_loads;
|
||||
if (on_load_counter_change)
|
||||
on_load_counter_change();
|
||||
if (!success) {
|
||||
dbg() << "HTTP load failed!";
|
||||
callback({});
|
||||
if (error_callback)
|
||||
error_callback("HTTP load failed");
|
||||
return;
|
||||
}
|
||||
callback(ByteBuffer::copy(payload.data(), payload.size()));
|
||||
success_callback(ByteBuffer::copy(payload.data(), payload.size()));
|
||||
};
|
||||
++m_pending_loads;
|
||||
if (on_load_counter_change)
|
||||
|
@ -82,8 +83,8 @@ void ResourceLoader::load(const URL& url, Function<void(const ByteBuffer&)> call
|
|||
return;
|
||||
}
|
||||
|
||||
dbg() << "Unimplemented protocol: " << url.protocol();
|
||||
ASSERT_NOT_REACHED();
|
||||
if (error_callback)
|
||||
error_callback(String::format("Protocol not implemented: %s", url.protocol().characters()));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue