SharedImageRequest.h 1.7 KB

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