diff --git a/Libraries/LibIPC/AutoCloseFileDescriptor.cpp b/Libraries/LibIPC/AutoCloseFileDescriptor.cpp new file mode 100644 index 00000000000..95355052d5f --- /dev/null +++ b/Libraries/LibIPC/AutoCloseFileDescriptor.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2025, the Ladybird developers. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +namespace IPC { + +AutoCloseFileDescriptor::AutoCloseFileDescriptor(int fd) + : m_fd(fd) +{ +} + +AutoCloseFileDescriptor::~AutoCloseFileDescriptor() +{ + if (m_fd != -1) + (void)Core::System::close(m_fd); +} + +} diff --git a/Libraries/LibIPC/AutoCloseFileDescriptor.h b/Libraries/LibIPC/AutoCloseFileDescriptor.h index 6a2a34820dc..58d39be7e0e 100644 --- a/Libraries/LibIPC/AutoCloseFileDescriptor.h +++ b/Libraries/LibIPC/AutoCloseFileDescriptor.h @@ -7,34 +7,19 @@ #pragma once #include -#include namespace IPC { class AutoCloseFileDescriptor : public RefCounted { public: - AutoCloseFileDescriptor(int fd) - : m_fd(fd) - { - } - - ~AutoCloseFileDescriptor() - { - if (m_fd != -1) - (void)Core::System::close(m_fd); - } + explicit AutoCloseFileDescriptor(int fd); + ~AutoCloseFileDescriptor(); int value() const { return m_fd; } - - int take_fd() - { - int fd = m_fd; - m_fd = -1; - return fd; - } + int take_fd() { return exchange(m_fd, -1); } private: - int m_fd; + int m_fd { -1 }; }; } diff --git a/Libraries/LibIPC/CMakeLists.txt b/Libraries/LibIPC/CMakeLists.txt index 74a934e7f62..8d995268941 100644 --- a/Libraries/LibIPC/CMakeLists.txt +++ b/Libraries/LibIPC/CMakeLists.txt @@ -1,4 +1,5 @@ set(SOURCES + AutoCloseFileDescriptor.cpp Connection.cpp Decoder.cpp Encoder.cpp diff --git a/Libraries/LibIPC/File.cpp b/Libraries/LibIPC/File.cpp index 317b82d05f6..31daa54d00d 100644 --- a/Libraries/LibIPC/File.cpp +++ b/Libraries/LibIPC/File.cpp @@ -1,16 +1,65 @@ /* * Copyright (c) 2020, Sergey Bugaev * Copyright (c) 2021, Andreas Kling - * Copyright (c) 2023, Tim Flynn + * Copyright (c) 2023-2025, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ +#include +#include #include #include namespace IPC { +File File::adopt_file(NonnullOwnPtr file) +{ + return File(file->leak_fd()); +} + +File File::adopt_fd(int fd) +{ + return File(fd); +} + +ErrorOr File::clone_fd(int fd) +{ + int new_fd = TRY(Core::System::dup(fd)); + return File(new_fd); +} + +File::File(int fd) + : m_fd(fd) +{ +} + +File::File(File&& other) + : m_fd(exchange(other.m_fd, -1)) +{ +} + +File& File::operator=(File&& other) +{ + if (this != &other) { + m_fd = exchange(other.m_fd, -1); + } + return *this; +} + +File::~File() +{ + if (m_fd != -1) + (void)Core::System::close(m_fd); +} + +// FIXME: IPC::Files transferred over the wire always set O_CLOEXEC during decoding. Perhaps we should add an option to +// allow the receiver to decide whether to make it O_CLOEXEC or not. Or an attribute in the .ipc file? +ErrorOr File::clear_close_on_exec() +{ + return Core::System::set_close_on_exec(m_fd, false); +} + template<> ErrorOr decode(Decoder& decoder) { diff --git a/Libraries/LibIPC/File.h b/Libraries/LibIPC/File.h index c91eeb6a6c0..d0b311a1b65 100644 --- a/Libraries/LibIPC/File.h +++ b/Libraries/LibIPC/File.h @@ -9,8 +9,7 @@ #include #include -#include -#include +#include namespace IPC { @@ -20,62 +19,24 @@ class File { public: File() = default; - static File adopt_file(NonnullOwnPtr file) - { - return File(file->leak_fd()); - } + static File adopt_file(NonnullOwnPtr file); + static File adopt_fd(int fd); + static ErrorOr clone_fd(int fd); - static File adopt_fd(int fd) - { - return File(fd); - } + File(File&& other); + File& operator=(File&& other); - static ErrorOr clone_fd(int fd) - { - int new_fd = TRY(Core::System::dup(fd)); - return File(new_fd); - } - - File(File&& other) - : m_fd(exchange(other.m_fd, -1)) - { - } - - File& operator=(File&& other) - { - if (this != &other) { - m_fd = exchange(other.m_fd, -1); - } - return *this; - } - - ~File() - { - if (m_fd != -1) - (void)Core::System::close(m_fd); - } + ~File(); int fd() const { return m_fd; } - // NOTE: This is 'const' since generated IPC messages expose all parameters by const reference. - [[nodiscard]] int take_fd() const - { - return exchange(m_fd, -1); - } + // This is 'const' since generated IPC messages expose all parameters by const reference. + [[nodiscard]] int take_fd() const { return exchange(m_fd, -1); } - // FIXME: IPC::Files transferred over the wire are always set O_CLOEXEC during decoding. - // Perhaps we should add an option to IPC::File to allow the receiver to decide whether to - // make it O_CLOEXEC or not. Or an attribute in the .ipc file? - ErrorOr clear_close_on_exec() - { - return Core::System::set_close_on_exec(m_fd, false); - } + ErrorOr clear_close_on_exec(); private: - explicit File(int fd) - : m_fd(fd) - { - } + explicit File(int fd); mutable int m_fd { -1 }; }; diff --git a/Libraries/LibIPC/SingleServer.h b/Libraries/LibIPC/SingleServer.h index a238f00295b..04a2478af6d 100644 --- a/Libraries/LibIPC/SingleServer.h +++ b/Libraries/LibIPC/SingleServer.h @@ -6,7 +6,6 @@ #pragma once -#include #include #include diff --git a/Libraries/LibRequests/Request.cpp b/Libraries/LibRequests/Request.cpp index 73cdb63b32c..15a0d2d3e41 100644 --- a/Libraries/LibRequests/Request.cpp +++ b/Libraries/LibRequests/Request.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt index 0293ce17406..b635a4b1429 100644 --- a/Libraries/LibWeb/CMakeLists.txt +++ b/Libraries/LibWeb/CMakeLists.txt @@ -592,6 +592,8 @@ set(SOURCES HTML/NavigationTransition.cpp HTML/Navigator.cpp HTML/NavigatorBeacon.cpp + HTML/NavigatorConcurrentHardware.cpp + HTML/NavigatorDeviceMemory.cpp HTML/NavigatorID.cpp HTML/Numbers.cpp HTML/OffscreenCanvas.cpp diff --git a/Libraries/LibWeb/HTML/NavigatorConcurrentHardware.cpp b/Libraries/LibWeb/HTML/NavigatorConcurrentHardware.cpp new file mode 100644 index 00000000000..c5ccee160cb --- /dev/null +++ b/Libraries/LibWeb/HTML/NavigatorConcurrentHardware.cpp @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2022, Andrew Kaster + * Copyright (c) 2024, Shannon Booth + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +namespace Web::HTML { + +// https://html.spec.whatwg.org/multipage/workers.html#dom-navigator-hardwareconcurrency +WebIDL::UnsignedLongLong NavigatorConcurrentHardwareMixin::hardware_concurrency() +{ + return Core::System::hardware_concurrency(); +} + +} diff --git a/Libraries/LibWeb/HTML/NavigatorConcurrentHardware.h b/Libraries/LibWeb/HTML/NavigatorConcurrentHardware.h index ff0c303159b..bc5144e4c20 100644 --- a/Libraries/LibWeb/HTML/NavigatorConcurrentHardware.h +++ b/Libraries/LibWeb/HTML/NavigatorConcurrentHardware.h @@ -7,15 +7,13 @@ #pragma once -#include #include namespace Web::HTML { class NavigatorConcurrentHardwareMixin { public: - // https://html.spec.whatwg.org/multipage/workers.html#dom-navigator-hardwareconcurrency - static WebIDL::UnsignedLongLong hardware_concurrency() { return Core::System::hardware_concurrency(); } + static WebIDL::UnsignedLongLong hardware_concurrency(); }; } diff --git a/Libraries/LibWeb/HTML/NavigatorDeviceMemory.cpp b/Libraries/LibWeb/HTML/NavigatorDeviceMemory.cpp new file mode 100644 index 00000000000..7e1ec77b5ac --- /dev/null +++ b/Libraries/LibWeb/HTML/NavigatorDeviceMemory.cpp @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2024, Jelle Raaijmakers + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include + +namespace Web::HTML { + +// https://www.w3.org/TR/device-memory/#computing-device-memory-value +WebIDL::Double NavigatorDeviceMemoryMixin::device_memory() const +{ + // The value is calculated by using the actual device memory in MiB then rounding it to the nearest number where + // only the most significant bit can be set and the rest are zeros (nearest power of two). + auto memory_in_bytes = Core::System::physical_memory_bytes(); + auto memory_in_mib = memory_in_bytes / MiB; + auto required_bits = AK::count_required_bits(memory_in_mib); + auto lower_memory_in_mib = static_cast(1) << (required_bits - 1); + auto upper_memory_in_mib = static_cast(1) << required_bits; + auto rounded_memory_in_mib = upper_memory_in_mib - memory_in_mib <= memory_in_mib - lower_memory_in_mib + ? upper_memory_in_mib + : lower_memory_in_mib; + + // Then dividing that number by 1024.0 to get the value in GiB. + auto memory_in_gib = static_cast(rounded_memory_in_mib) / 1024.0; + + // An upper bound and a lower bound should be set on the list of values. + return AK::clamp(memory_in_gib, 1.0, 4.0); +} + +} diff --git a/Libraries/LibWeb/HTML/NavigatorDeviceMemory.h b/Libraries/LibWeb/HTML/NavigatorDeviceMemory.h index 83eca4e041e..52fbbe51e46 100644 --- a/Libraries/LibWeb/HTML/NavigatorDeviceMemory.h +++ b/Libraries/LibWeb/HTML/NavigatorDeviceMemory.h @@ -6,35 +6,13 @@ #pragma once -#include -#include #include namespace Web::HTML { class NavigatorDeviceMemoryMixin { public: - // https://www.w3.org/TR/device-memory/#computing-device-memory-value - WebIDL::Double device_memory() const - { - // The value is calculated by using the actual device memory in MiB then rounding it to the - // nearest number where only the most significant bit can be set and the rest are zeros - // (nearest power of two). - auto memory_in_bytes = Core::System::physical_memory_bytes(); - auto memory_in_mib = memory_in_bytes / MiB; - auto required_bits = AK::count_required_bits(memory_in_mib); - auto lower_memory_in_mib = static_cast(1) << (required_bits - 1); - auto upper_memory_in_mib = static_cast(1) << required_bits; - auto rounded_memory_in_mib = upper_memory_in_mib - memory_in_mib <= memory_in_mib - lower_memory_in_mib - ? upper_memory_in_mib - : lower_memory_in_mib; - - // Then dividing that number by 1024.0 to get the value in GiB. - auto memory_in_gib = static_cast(rounded_memory_in_mib) / 1024.0; - - // An upper bound and a lower bound should be set on the list of values. - return AK::clamp(memory_in_gib, 1.0, 4.0); - } + WebIDL::Double device_memory() const; }; } diff --git a/Libraries/LibWeb/Loader/ResourceLoader.cpp b/Libraries/LibWeb/Loader/ResourceLoader.cpp index cb31a9922c0..42cf2f9bea6 100644 --- a/Libraries/LibWeb/Loader/ResourceLoader.cpp +++ b/Libraries/LibWeb/Loader/ResourceLoader.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include diff --git a/Libraries/LibWebView/HelperProcess.cpp b/Libraries/LibWebView/HelperProcess.cpp index 2481db46514..50658cde2fd 100644 --- a/Libraries/LibWebView/HelperProcess.cpp +++ b/Libraries/LibWebView/HelperProcess.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include diff --git a/Libraries/LibWebView/Process.cpp b/Libraries/LibWebView/Process.cpp index 9759ec1d325..aaa83c312e3 100644 --- a/Libraries/LibWebView/Process.cpp +++ b/Libraries/LibWebView/Process.cpp @@ -8,7 +8,9 @@ #include #include #include +#include #include + #if defined(AK_OS_WINDOWS) # include # include diff --git a/Services/ImageDecoder/ConnectionFromClient.cpp b/Services/ImageDecoder/ConnectionFromClient.cpp index ed5d1f65f24..dbaf1a769e3 100644 --- a/Services/ImageDecoder/ConnectionFromClient.cpp +++ b/Services/ImageDecoder/ConnectionFromClient.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include diff --git a/Services/RequestServer/ConnectionFromClient.cpp b/Services/RequestServer/ConnectionFromClient.cpp index 52b2cf523ed..f816fb14758 100644 --- a/Services/RequestServer/ConnectionFromClient.cpp +++ b/Services/RequestServer/ConnectionFromClient.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include diff --git a/Tests/LibWeb/test-web/Fixture.cpp b/Tests/LibWeb/test-web/Fixture.cpp index 599619ed43f..b39dc188aed 100644 --- a/Tests/LibWeb/test-web/Fixture.cpp +++ b/Tests/LibWeb/test-web/Fixture.cpp @@ -11,6 +11,7 @@ #include #include #include +#include namespace TestWeb { diff --git a/Tests/LibWeb/test-web/main.cpp b/Tests/LibWeb/test-web/main.cpp index 25102e24770..9bf9e8f94ba 100644 --- a/Tests/LibWeb/test-web/main.cpp +++ b/Tests/LibWeb/test-web/main.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include