ListOfAvailableImages.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 : public JS::Cell {
  16. JS_CELL(ListOfAvailableImages, Cell);
  17. JS_DECLARE_ALLOCATOR(ListOfAvailableImages);
  18. public:
  19. struct Key {
  20. URL url;
  21. HTML::CORSSettingAttribute mode;
  22. Optional<HTML::Origin> origin;
  23. [[nodiscard]] bool operator==(Key const& other) const;
  24. [[nodiscard]] u32 hash() const;
  25. private:
  26. mutable Optional<u32> cached_hash;
  27. };
  28. struct Entry {
  29. Entry(JS::NonnullGCPtr<DecodedImageData> image_data, bool ignore_higher_layer_caching)
  30. : image_data(move(image_data))
  31. , ignore_higher_layer_caching(ignore_higher_layer_caching)
  32. {
  33. }
  34. JS::NonnullGCPtr<DecodedImageData> image_data;
  35. bool ignore_higher_layer_caching { false };
  36. };
  37. ListOfAvailableImages();
  38. ~ListOfAvailableImages();
  39. void add(Key const&, JS::NonnullGCPtr<DecodedImageData>, bool ignore_higher_layer_caching);
  40. void remove(Key const&);
  41. [[nodiscard]] Entry* get(Key const&);
  42. void visit_edges(JS::Cell::Visitor& visitor) override;
  43. private:
  44. HashMap<Key, NonnullOwnPtr<Entry>> m_images;
  45. };
  46. }
  47. namespace AK {
  48. template<>
  49. struct Traits<Web::HTML::ListOfAvailableImages::Key> : public DefaultTraits<Web::HTML::ListOfAvailableImages::Key> {
  50. static unsigned hash(Web::HTML::ListOfAvailableImages::Key const& key)
  51. {
  52. return key.hash();
  53. }
  54. static bool equals(Web::HTML::ListOfAvailableImages::Key const& a, Web::HTML::ListOfAvailableImages::Key const& b)
  55. {
  56. return a == b;
  57. }
  58. };
  59. }