SharedImageRequest.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. public:
  19. [[nodiscard]] static JS::NonnullGCPtr<SharedImageRequest> get_or_create(JS::Realm&, Page&, AK::URL const&);
  20. ~SharedImageRequest();
  21. AK::URL const& url() const { return m_url; }
  22. [[nodiscard]] RefPtr<DecodedImageData const> image_data() const;
  23. [[nodiscard]] JS::GCPtr<Fetch::Infrastructure::FetchController> fetch_controller();
  24. void set_fetch_controller(JS::GCPtr<Fetch::Infrastructure::FetchController>);
  25. void fetch_image(JS::Realm&, JS::NonnullGCPtr<Fetch::Infrastructure::Request>);
  26. void add_callbacks(Function<void()> on_finish, Function<void()> on_fail);
  27. bool is_fetching() const;
  28. bool needs_fetching() const;
  29. virtual void visit_edges(JS::Cell::Visitor&) override;
  30. private:
  31. explicit SharedImageRequest(Page&, AK::URL, JS::NonnullGCPtr<DOM::Document>);
  32. void handle_successful_fetch(AK::URL const&, StringView mime_type, ByteBuffer data);
  33. void handle_failed_fetch();
  34. enum class State {
  35. New,
  36. Fetching,
  37. Finished,
  38. Failed,
  39. };
  40. State m_state { State::New };
  41. Page& m_page;
  42. struct Callbacks {
  43. JS::GCPtr<JS::HeapFunction<void()>> on_finish;
  44. JS::GCPtr<JS::HeapFunction<void()>> on_fail;
  45. };
  46. Vector<Callbacks> m_callbacks;
  47. AK::URL m_url;
  48. RefPtr<DecodedImageData const> m_image_data;
  49. JS::GCPtr<Fetch::Infrastructure::FetchController> m_fetch_controller;
  50. JS::GCPtr<DOM::Document> m_document;
  51. };
  52. }