SharedResourceRequest.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (c) 2023, Andreas Kling <andreas@ladybird.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 <LibGC/Function.h>
  10. #include <LibGC/Root.h>
  11. #include <LibGfx/Size.h>
  12. #include <LibURL/URL.h>
  13. #include <LibWeb/Forward.h>
  14. namespace Web::HTML {
  15. class SharedResourceRequest final : public JS::Cell {
  16. GC_CELL(SharedResourceRequest, JS::Cell);
  17. GC_DECLARE_ALLOCATOR(SharedResourceRequest);
  18. public:
  19. [[nodiscard]] static GC::Ref<SharedResourceRequest> get_or_create(JS::Realm&, GC::Ref<Page>, URL::URL const&);
  20. virtual ~SharedResourceRequest() override;
  21. URL::URL const& url() const { return m_url; }
  22. [[nodiscard]] GC::Ptr<DecodedImageData> image_data() const;
  23. [[nodiscard]] GC::Ptr<Fetch::Infrastructure::FetchController> fetch_controller();
  24. void set_fetch_controller(GC::Ptr<Fetch::Infrastructure::FetchController>);
  25. void fetch_resource(JS::Realm&, GC::Ref<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. private:
  30. explicit SharedResourceRequest(GC::Ref<Page>, URL::URL, GC::Ref<DOM::Document>);
  31. virtual void finalize() override;
  32. virtual void visit_edges(JS::Cell::Visitor&) override;
  33. void handle_successful_fetch(URL::URL const&, StringView mime_type, ByteBuffer data);
  34. void handle_failed_fetch();
  35. void handle_successful_resource_load();
  36. enum class State {
  37. New,
  38. Fetching,
  39. Finished,
  40. Failed,
  41. };
  42. State m_state { State::New };
  43. GC::Ref<Page> m_page;
  44. struct Callbacks {
  45. GC::Ptr<GC::Function<void()>> on_finish;
  46. GC::Ptr<GC::Function<void()>> on_fail;
  47. };
  48. Vector<Callbacks> m_callbacks;
  49. URL::URL m_url;
  50. GC::Ptr<DecodedImageData> m_image_data;
  51. GC::Ptr<Fetch::Infrastructure::FetchController> m_fetch_controller;
  52. GC::Ptr<DOM::Document> m_document;
  53. };
  54. }