ResourceLoader.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/DeprecatedString.h>
  9. #include <AK/Function.h>
  10. #include <AK/HashMap.h>
  11. #include <AK/URL.h>
  12. #include <LibCore/Object.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. #endif
  22. #if defined(AK_OS_SERENITY)
  23. # define OS_STRING "SerenityOS"
  24. #elif defined(AK_OS_LINUX)
  25. # define OS_STRING "Linux"
  26. #elif defined(AK_OS_MACOS)
  27. # define OS_STRING "macOS"
  28. #elif defined(AK_OS_WINDOWS)
  29. # define OS_STRING "Windows"
  30. #elif defined(AK_OS_FREEBSD)
  31. # define OS_STRING "FreeBSD"
  32. #elif defined(AK_OS_OPENBSD)
  33. # define OS_STRING "OpenBSD"
  34. #elif defined(AK_OS_NETBSD)
  35. # define OS_STRING "NetBSD"
  36. #elif defined(AK_OS_DRAGONFLY)
  37. # define OS_STRING "DragonFly"
  38. #elif defined(AK_OS_SOLARIS)
  39. # define OS_STRING "SunOS"
  40. #else
  41. # error Unknown OS
  42. #endif
  43. #define BROWSER_NAME "Ladybird"
  44. #define BROWSER_VERSION "1.0"
  45. constexpr auto default_user_agent = "Mozilla/5.0 (" OS_STRING "; " CPU_STRING ") LibWeb+LibJS/1.0 " BROWSER_NAME "/" BROWSER_VERSION ""sv;
  46. class ResourceLoaderConnectorRequest : public RefCounted<ResourceLoaderConnectorRequest> {
  47. public:
  48. virtual ~ResourceLoaderConnectorRequest();
  49. struct CertificateAndKey {
  50. DeprecatedString certificate;
  51. DeprecatedString key;
  52. };
  53. virtual void set_should_buffer_all_input(bool) = 0;
  54. virtual bool stop() = 0;
  55. virtual void stream_into(Stream&) = 0;
  56. Function<void(bool success, u32 total_size, HashMap<DeprecatedString, DeprecatedString, CaseInsensitiveStringTraits> const& response_headers, Optional<u32> response_code, ReadonlyBytes payload)> on_buffered_request_finish;
  57. Function<void(bool success, u32 total_size)> on_finish;
  58. Function<void(Optional<u32> total_size, u32 downloaded_size)> on_progress;
  59. Function<CertificateAndKey()> on_certificate_requested;
  60. protected:
  61. explicit ResourceLoaderConnectorRequest();
  62. };
  63. class ResourceLoaderConnector : public RefCounted<ResourceLoaderConnector> {
  64. public:
  65. virtual ~ResourceLoaderConnector();
  66. virtual void prefetch_dns(AK::URL const&) = 0;
  67. virtual void preconnect(AK::URL const&) = 0;
  68. virtual RefPtr<ResourceLoaderConnectorRequest> start_request(DeprecatedString const& method, AK::URL const&, HashMap<DeprecatedString, DeprecatedString> const& request_headers = {}, ReadonlyBytes request_body = {}, Core::ProxyData const& = {}) = 0;
  69. protected:
  70. explicit ResourceLoaderConnector();
  71. };
  72. class ResourceLoader : public Core::Object {
  73. C_OBJECT_ABSTRACT(ResourceLoader)
  74. public:
  75. static void initialize(RefPtr<ResourceLoaderConnector>);
  76. static ResourceLoader& the();
  77. RefPtr<Resource> load_resource(Resource::Type, LoadRequest&);
  78. void load(LoadRequest&, Function<void(ReadonlyBytes, HashMap<DeprecatedString, DeprecatedString, CaseInsensitiveStringTraits> const& response_headers, Optional<u32> status_code)> success_callback, Function<void(DeprecatedString const&, Optional<u32> status_code)> error_callback = nullptr, Optional<u32> timeout = {}, Function<void()> timeout_callback = nullptr);
  79. void load(const AK::URL&, Function<void(ReadonlyBytes, HashMap<DeprecatedString, DeprecatedString, CaseInsensitiveStringTraits> const& response_headers, Optional<u32> status_code)> success_callback, Function<void(DeprecatedString const&, Optional<u32> status_code)> error_callback = nullptr, Optional<u32> timeout = {}, Function<void()> timeout_callback = nullptr);
  80. ResourceLoaderConnector& connector() { return *m_connector; }
  81. void prefetch_dns(AK::URL const&);
  82. void preconnect(AK::URL const&);
  83. Function<void()> on_load_counter_change;
  84. int pending_loads() const { return m_pending_loads; }
  85. DeprecatedString const& user_agent() const { return m_user_agent; }
  86. void set_user_agent(DeprecatedString const& user_agent) { m_user_agent = user_agent; }
  87. void clear_cache();
  88. void evict_from_cache(LoadRequest const&);
  89. private:
  90. ResourceLoader(NonnullRefPtr<ResourceLoaderConnector>);
  91. static ErrorOr<NonnullRefPtr<ResourceLoader>> try_create(NonnullRefPtr<ResourceLoaderConnector>);
  92. static bool is_port_blocked(int port);
  93. int m_pending_loads { 0 };
  94. HashTable<NonnullRefPtr<ResourceLoaderConnectorRequest>> m_active_requests;
  95. NonnullRefPtr<ResourceLoaderConnector> m_connector;
  96. DeprecatedString m_user_agent;
  97. Optional<Page&> m_page {};
  98. };
  99. }