ResourceLoader.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Dex♪ <dexes.ttp@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/ByteString.h>
  9. #include <AK/Function.h>
  10. #include <AK/HashMap.h>
  11. #include <AK/URL.h>
  12. #include <LibCore/EventReceiver.h>
  13. #include <LibCore/Proxy.h>
  14. #include <LibWeb/Loader/Resource.h>
  15. #include <LibWeb/Page/Page.h>
  16. namespace Web {
  17. #if ARCH(X86_64)
  18. # define CPU_STRING "x86_64"
  19. #elif ARCH(AARCH64)
  20. # define CPU_STRING "AArch64"
  21. #elif ARCH(I386)
  22. # define CPU_STRING "x86"
  23. #elif ARCH(RISCV64)
  24. # define CPU_STRING "RISC-V 64"
  25. #else
  26. # error Unknown architecture
  27. #endif
  28. #if defined(AK_OS_SERENITY)
  29. # define OS_STRING "SerenityOS"
  30. #elif defined(AK_OS_ANDROID)
  31. # define OS_STRING "Android 10"
  32. #elif defined(AK_OS_LINUX)
  33. # define OS_STRING "Linux"
  34. #elif defined(AK_OS_MACOS)
  35. # define OS_STRING "macOS"
  36. #elif defined(AK_OS_WINDOWS)
  37. # define OS_STRING "Windows"
  38. #elif defined(AK_OS_FREEBSD)
  39. # define OS_STRING "FreeBSD"
  40. #elif defined(AK_OS_OPENBSD)
  41. # define OS_STRING "OpenBSD"
  42. #elif defined(AK_OS_NETBSD)
  43. # define OS_STRING "NetBSD"
  44. #elif defined(AK_OS_DRAGONFLY)
  45. # define OS_STRING "DragonFly"
  46. #elif defined(AK_OS_SOLARIS)
  47. # define OS_STRING "SunOS"
  48. #elif defined(AK_OS_HAIKU)
  49. # define OS_STRING "Haiku"
  50. #elif defined(AK_OS_GNU_HURD)
  51. # define OS_STRING "GNU/Hurd"
  52. #else
  53. # error Unknown OS
  54. #endif
  55. #define BROWSER_NAME "Ladybird"
  56. #define BROWSER_VERSION "1.0"
  57. constexpr auto default_user_agent = "Mozilla/5.0 (" OS_STRING "; " CPU_STRING ") " BROWSER_NAME "/" BROWSER_VERSION ""sv;
  58. constexpr auto default_platform = OS_STRING " " CPU_STRING ""sv;
  59. class ResourceLoaderConnectorRequest : public RefCounted<ResourceLoaderConnectorRequest> {
  60. public:
  61. virtual ~ResourceLoaderConnectorRequest();
  62. struct CertificateAndKey {
  63. ByteString certificate;
  64. ByteString key;
  65. };
  66. virtual void set_should_buffer_all_input(bool) = 0;
  67. virtual bool stop() = 0;
  68. virtual void stream_into(Stream&) = 0;
  69. Function<void(bool success, u64 total_size, HashMap<ByteString, ByteString, CaseInsensitiveStringTraits> const& response_headers, Optional<u32> response_code, ReadonlyBytes payload)> on_buffered_request_finish;
  70. Function<void(bool success, u64 total_size)> on_finish;
  71. Function<void(Optional<u64> total_size, u64 downloaded_size)> on_progress;
  72. Function<CertificateAndKey()> on_certificate_requested;
  73. protected:
  74. explicit ResourceLoaderConnectorRequest();
  75. };
  76. class ResourceLoaderConnector : public RefCounted<ResourceLoaderConnector> {
  77. public:
  78. virtual ~ResourceLoaderConnector();
  79. virtual void prefetch_dns(AK::URL const&) = 0;
  80. virtual void preconnect(AK::URL const&) = 0;
  81. virtual RefPtr<ResourceLoaderConnectorRequest> start_request(ByteString const& method, AK::URL const&, HashMap<ByteString, ByteString> const& request_headers = {}, ReadonlyBytes request_body = {}, Core::ProxyData const& = {}) = 0;
  82. protected:
  83. explicit ResourceLoaderConnector();
  84. };
  85. class ResourceLoader : public Core::EventReceiver {
  86. C_OBJECT_ABSTRACT(ResourceLoader)
  87. public:
  88. static void initialize(RefPtr<ResourceLoaderConnector>);
  89. static ResourceLoader& the();
  90. RefPtr<Resource> load_resource(Resource::Type, LoadRequest&);
  91. using SuccessCallback = Function<void(ReadonlyBytes, HashMap<ByteString, ByteString, CaseInsensitiveStringTraits> const& response_headers, Optional<u32> status_code)>;
  92. using ErrorCallback = Function<void(ByteString const&, Optional<u32> status_code, ReadonlyBytes payload, HashMap<ByteString, ByteString, CaseInsensitiveStringTraits> const& response_headers)>;
  93. using TimeoutCallback = Function<void()>;
  94. void load(LoadRequest&, SuccessCallback success_callback, ErrorCallback error_callback = nullptr, Optional<u32> timeout = {}, TimeoutCallback timeout_callback = nullptr);
  95. ResourceLoaderConnector& connector() { return *m_connector; }
  96. void prefetch_dns(AK::URL const&);
  97. void preconnect(AK::URL const&);
  98. Function<void()> on_load_counter_change;
  99. int pending_loads() const { return m_pending_loads; }
  100. String const& user_agent() const { return m_user_agent; }
  101. void set_user_agent(String user_agent) { m_user_agent = move(user_agent); }
  102. String const& platform() const { return m_platform; }
  103. void set_platform(String platform) { m_platform = move(platform); }
  104. void clear_cache();
  105. void evict_from_cache(LoadRequest const&);
  106. private:
  107. ResourceLoader(NonnullRefPtr<ResourceLoaderConnector>);
  108. static ErrorOr<NonnullRefPtr<ResourceLoader>> try_create(NonnullRefPtr<ResourceLoaderConnector>);
  109. static bool is_port_blocked(int port);
  110. int m_pending_loads { 0 };
  111. HashTable<NonnullRefPtr<ResourceLoaderConnectorRequest>> m_active_requests;
  112. NonnullRefPtr<ResourceLoaderConnector> m_connector;
  113. String m_user_agent;
  114. String m_platform;
  115. Optional<JS::GCPtr<Page>> m_page {};
  116. };
  117. }