1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:47:44 +00:00

LibGUI: Make clipboard-as-bitmap parsing less data-race-y

This encourages the caller to first fetch data and type atomically, and
then parse that, instead of potentially making multiple requests.
This commit is contained in:
Ben Wiederhake 2021-11-20 13:04:38 +01:00 committed by Linus Groh
parent 55526634b6
commit b6419f2cf2
3 changed files with 13 additions and 12 deletions

View file

@ -64,34 +64,34 @@ Clipboard::DataAndType Clipboard::data_and_type() const
return { data.release_value(), type, metadata };
}
RefPtr<Gfx::Bitmap> Clipboard::bitmap() const
RefPtr<Gfx::Bitmap> Clipboard::DataAndType::as_bitmap() const
{
auto clipping = data_and_type();
if (clipping.mime_type != "image/x-serenityos")
if (mime_type != "image/x-serenityos")
return nullptr;
auto width = clipping.metadata.get("width").value_or("0").to_uint();
auto width = metadata.get("width").value_or("0").to_uint();
if (!width.has_value() || width.value() == 0)
return nullptr;
auto height = clipping.metadata.get("height").value_or("0").to_uint();
auto height = metadata.get("height").value_or("0").to_uint();
if (!height.has_value() || height.value() == 0)
return nullptr;
auto scale = clipping.metadata.get("scale").value_or("0").to_uint();
auto scale = metadata.get("scale").value_or("0").to_uint();
if (!scale.has_value() || scale.value() == 0)
return nullptr;
auto pitch = clipping.metadata.get("pitch").value_or("0").to_uint();
auto pitch = metadata.get("pitch").value_or("0").to_uint();
if (!pitch.has_value() || pitch.value() == 0)
return nullptr;
auto format = clipping.metadata.get("format").value_or("0").to_uint();
auto format = metadata.get("format").value_or("0").to_uint();
if (!format.has_value() || format.value() == 0)
return nullptr;
auto clipping_bitmap_or_error = Gfx::Bitmap::try_create_wrapper((Gfx::BitmapFormat)format.value(), { (int)width.value(), (int)height.value() }, scale.value(), pitch.value(), clipping.data.data());
// We won't actually write to the clipping_bitmap, so casting away the const is okay.
auto clipping_data = const_cast<u8*>(data.data());
auto clipping_bitmap_or_error = Gfx::Bitmap::try_create_wrapper((Gfx::BitmapFormat)format.value(), { (int)width.value(), (int)height.value() }, scale.value(), pitch.value(), clipping_data);
if (clipping_bitmap_or_error.is_error())
return nullptr;
auto clipping_bitmap = clipping_bitmap_or_error.release_value_but_fixme_should_propagate_errors();