SharedImageRequest.h 2.0 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/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 final : 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>, URL const&);
  21. virtual ~SharedImageRequest() override;
  22. URL const& url() const { return m_url; }
  23. [[nodiscard]] JS::GCPtr<DecodedImageData> 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. private:
  31. explicit SharedImageRequest(JS::NonnullGCPtr<Page>, URL, JS::NonnullGCPtr<DOM::Document>);
  32. virtual void finalize() override;
  33. virtual void visit_edges(JS::Cell::Visitor&) override;
  34. void handle_successful_fetch(URL const&, StringView mime_type, ByteBuffer data);
  35. void handle_failed_fetch();
  36. enum class State {
  37. New,
  38. Fetching,
  39. Finished,
  40. Failed,
  41. };
  42. State m_state { State::New };
  43. JS::NonnullGCPtr<Page> m_page;
  44. struct Callbacks {
  45. JS::GCPtr<JS::HeapFunction<void()>> on_finish;
  46. JS::GCPtr<JS::HeapFunction<void()>> on_fail;
  47. };
  48. Vector<Callbacks> m_callbacks;
  49. URL m_url;
  50. JS::GCPtr<DecodedImageData> m_image_data;
  51. JS::GCPtr<Fetch::Infrastructure::FetchController> m_fetch_controller;
  52. JS::GCPtr<DOM::Document> m_document;
  53. };
  54. }