ListOfAvailableImages.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/HashMap.h>
  8. #include <AK/URL.h>
  9. #include <LibJS/Heap/Cell.h>
  10. #include <LibWeb/Forward.h>
  11. #include <LibWeb/HTML/CORSSettingAttribute.h>
  12. #include <LibWeb/HTML/Origin.h>
  13. namespace Web::HTML {
  14. // https://html.spec.whatwg.org/multipage/images.html#list-of-available-images
  15. class ListOfAvailableImages {
  16. public:
  17. struct Key {
  18. AK::URL url;
  19. HTML::CORSSettingAttribute mode;
  20. Optional<HTML::Origin> origin;
  21. [[nodiscard]] bool operator==(Key const& other) const;
  22. [[nodiscard]] u32 hash() const;
  23. private:
  24. mutable Optional<u32> cached_hash;
  25. };
  26. struct Entry final : public RefCounted<Entry> {
  27. static ErrorOr<NonnullRefPtr<Entry>> create(NonnullRefPtr<DecodedImageData>, bool ignore_higher_layer_caching);
  28. ~Entry();
  29. bool ignore_higher_layer_caching { false };
  30. NonnullRefPtr<DecodedImageData> image_data;
  31. private:
  32. Entry(NonnullRefPtr<DecodedImageData>, bool ignore_higher_layer_caching);
  33. };
  34. ListOfAvailableImages();
  35. ~ListOfAvailableImages();
  36. ErrorOr<void> add(Key const&, NonnullRefPtr<DecodedImageData>, bool ignore_higher_layer_caching);
  37. void remove(Key const&);
  38. [[nodiscard]] RefPtr<Entry> get(Key const&) const;
  39. private:
  40. HashMap<Key, NonnullRefPtr<Entry>> m_images;
  41. };
  42. }
  43. namespace AK {
  44. template<>
  45. struct Traits<Web::HTML::ListOfAvailableImages::Key> : public GenericTraits<Web::HTML::ListOfAvailableImages::Key> {
  46. static unsigned hash(Web::HTML::ListOfAvailableImages::Key const& key)
  47. {
  48. return key.hash();
  49. }
  50. static bool equals(Web::HTML::ListOfAvailableImages::Key const& a, Web::HTML::ListOfAvailableImages::Key const& b)
  51. {
  52. return a == b;
  53. }
  54. };
  55. }