From d01b4327fabeabe77a3e57ab5d8a7dfb82eb3c52 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 27 Jul 2021 01:12:53 +0200 Subject: LibGfx: Improve ImageDecoder construction Previously, ImageDecoder::create() would return a NonnullRefPtr and could not "fail", although the returned decoder may be "invalid" which you then had to check anyway. The new interface looks like this: static RefPtr try_create(ReadonlyBytes); This simplifies ImageDecoder since it no longer has to worry about its validity. Client code gets slightly clearer as well. --- Userland/Libraries/LibWeb/Loader/FrameLoader.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'Userland/Libraries/LibWeb/Loader') diff --git a/Userland/Libraries/LibWeb/Loader/FrameLoader.cpp b/Userland/Libraries/LibWeb/Loader/FrameLoader.cpp index 6db5f9b13c..1fc5755223 100644 --- a/Userland/Libraries/LibWeb/Loader/FrameLoader.cpp +++ b/Userland/Libraries/LibWeb/Loader/FrameLoader.cpp @@ -68,7 +68,9 @@ static bool build_text_document(DOM::Document& document, const ByteBuffer& data) static bool build_image_document(DOM::Document& document, const ByteBuffer& data) { - auto image_decoder = Gfx::ImageDecoder::create(data.data(), data.size()); + auto image_decoder = Gfx::ImageDecoder::try_create(data.bytes()); + if (!image_decoder) + return false; auto bitmap = image_decoder->bitmap(); if (!bitmap) return false; @@ -164,7 +166,11 @@ bool FrameLoader::load(const LoadRequest& request, Type type) favicon_url, [this, favicon_url](auto data, auto&, auto) { dbgln("Favicon downloaded, {} bytes from {}", data.size(), favicon_url); - auto decoder = Gfx::ImageDecoder::create(data.data(), data.size()); + auto decoder = Gfx::ImageDecoder::try_create(data); + if (!decoder) { + dbgln("No image decoder plugin for favicon {}", favicon_url); + return; + } auto bitmap = decoder->bitmap(); if (!bitmap) { dbgln("Could not decode favicon {}", favicon_url); -- cgit v1.2.3