ListOfAvailableImages.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/HTML/DecodedImageData.h>
  7. #include <LibWeb/HTML/ListOfAvailableImages.h>
  8. namespace Web::HTML {
  9. ListOfAvailableImages::ListOfAvailableImages() = default;
  10. ListOfAvailableImages::~ListOfAvailableImages() = default;
  11. bool ListOfAvailableImages::Key::operator==(Key const& other) const
  12. {
  13. return url == other.url && mode == other.mode && origin == other.origin;
  14. }
  15. u32 ListOfAvailableImages::Key::hash() const
  16. {
  17. if (!cached_hash.has_value()) {
  18. u32 url_hash = Traits<AK::URL>::hash(url);
  19. u32 mode_hash = static_cast<u32>(mode);
  20. u32 origin_hash = 0;
  21. if (origin.has_value())
  22. origin_hash = Traits<HTML::Origin>::hash(origin.value());
  23. cached_hash = pair_int_hash(url_hash, pair_int_hash(mode_hash, origin_hash));
  24. }
  25. return cached_hash.value();
  26. }
  27. ErrorOr<NonnullRefPtr<ListOfAvailableImages::Entry>> ListOfAvailableImages::Entry::create(NonnullRefPtr<DecodedImageData> image_data, bool ignore_higher_layer_caching)
  28. {
  29. return adopt_nonnull_ref_or_enomem(new (nothrow) Entry(move(image_data), ignore_higher_layer_caching));
  30. }
  31. ListOfAvailableImages::Entry::Entry(NonnullRefPtr<DecodedImageData> data, bool ignore_higher_layer_caching)
  32. : ignore_higher_layer_caching(ignore_higher_layer_caching)
  33. , image_data(move(data))
  34. {
  35. }
  36. ListOfAvailableImages::Entry::~Entry() = default;
  37. ErrorOr<void> ListOfAvailableImages::add(Key const& key, NonnullRefPtr<DecodedImageData> image_data, bool ignore_higher_layer_caching)
  38. {
  39. auto entry = TRY(Entry::create(move(image_data), ignore_higher_layer_caching));
  40. TRY(m_images.try_set(key, move(entry)));
  41. return {};
  42. }
  43. void ListOfAvailableImages::remove(Key const& key)
  44. {
  45. m_images.remove(key);
  46. }
  47. RefPtr<ListOfAvailableImages::Entry> ListOfAvailableImages::get(Key const& key) const
  48. {
  49. auto it = m_images.find(key);
  50. if (it == m_images.end())
  51. return nullptr;
  52. return it->value;
  53. }
  54. }