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