SharedImageRequest.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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/Error.h>
  8. #include <AK/OwnPtr.h>
  9. #include <AK/URL.h>
  10. #include <LibGfx/Size.h>
  11. #include <LibJS/Heap/Handle.h>
  12. #include <LibJS/Heap/HeapFunction.h>
  13. #include <LibJS/SafeFunction.h>
  14. #include <LibWeb/Forward.h>
  15. namespace Web::HTML {
  16. class SharedImageRequest : public JS::Cell {
  17. JS_CELL(ImageRequest, JS::Cell);
  18. JS_DECLARE_ALLOCATOR(SharedImageRequest);
  19. public:
  20. [[nodiscard]] static JS::NonnullGCPtr<SharedImageRequest> get_or_create(JS::Realm&, JS::NonnullGCPtr<Page>, AK::URL const&);
  21. ~SharedImageRequest();
  22. AK::URL const& url() const { return m_url; }
  23. [[nodiscard]] RefPtr<DecodedImageData const> image_data() const;
  24. [[nodiscard]] JS::GCPtr<Fetch::Infrastructure::FetchController> fetch_controller();
  25. void set_fetch_controller(JS::GCPtr<Fetch::Infrastructure::FetchController>);
  26. void fetch_image(JS::Realm&, JS::NonnullGCPtr<Fetch::Infrastructure::Request>);
  27. void add_callbacks(Function<void()> on_finish, Function<void()> on_fail);
  28. bool is_fetching() const;
  29. bool needs_fetching() const;
  30. virtual void visit_edges(JS::Cell::Visitor&) override;
  31. private:
  32. explicit SharedImageRequest(JS::NonnullGCPtr<Page>, AK::URL, JS::NonnullGCPtr<DOM::Document>);
  33. void handle_successful_fetch(AK::URL const&, StringView mime_type, ByteBuffer data);
  34. void handle_failed_fetch();
  35. enum class State {
  36. New,
  37. Fetching,
  38. Finished,
  39. Failed,
  40. };
  41. State m_state { State::New };
  42. JS::NonnullGCPtr<Page> m_page;
  43. struct Callbacks {
  44. JS::GCPtr<JS::HeapFunction<void()>> on_finish;
  45. JS::GCPtr<JS::HeapFunction<void()>> on_fail;
  46. };
  47. Vector<Callbacks> m_callbacks;
  48. AK::URL m_url;
  49. RefPtr<DecodedImageData const> m_image_data;
  50. JS::GCPtr<Fetch::Infrastructure::FetchController> m_fetch_controller;
  51. JS::GCPtr<DOM::Document> m_document;
  52. };
  53. }