diff --git a/Userland/Libraries/LibWeb/HTML/Navigator.h b/Userland/Libraries/LibWeb/HTML/Navigator.h index e48061e096f..a9b1e116d73 100644 --- a/Userland/Libraries/LibWeb/HTML/Navigator.h +++ b/Userland/Libraries/LibWeb/HTML/Navigator.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -23,6 +24,7 @@ namespace Web::HTML { class Navigator : public Bindings::PlatformObject , public NavigatorBeaconMixin , public NavigatorConcurrentHardwareMixin + , public NavigatorDeviceMemoryMixin , public NavigatorIDMixin , public NavigatorLanguageMixin , public NavigatorOnLineMixin diff --git a/Userland/Libraries/LibWeb/HTML/Navigator.idl b/Userland/Libraries/LibWeb/HTML/Navigator.idl index 94f31502a87..e93f60d8cdd 100644 --- a/Userland/Libraries/LibWeb/HTML/Navigator.idl +++ b/Userland/Libraries/LibWeb/HTML/Navigator.idl @@ -1,10 +1,11 @@ #import #import #import +#import +#import #import #import #import -#import #import #import #import @@ -72,3 +73,4 @@ Navigator includes NavigatorPlugins; Navigator includes NavigatorConcurrentHardware; Navigator includes NavigatorAutomationInformation; Navigator includes NavigatorStorage; +Navigator includes NavigatorDeviceMemory; diff --git a/Userland/Libraries/LibWeb/HTML/NavigatorDeviceMemory.h b/Userland/Libraries/LibWeb/HTML/NavigatorDeviceMemory.h new file mode 100644 index 00000000000..4a2615e7b93 --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/NavigatorDeviceMemory.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2024, Jelle Raaijmakers + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#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 signicant 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/Userland/Libraries/LibWeb/HTML/NavigatorDeviceMemory.idl b/Userland/Libraries/LibWeb/HTML/NavigatorDeviceMemory.idl new file mode 100644 index 00000000000..4402200fffc --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/NavigatorDeviceMemory.idl @@ -0,0 +1,5 @@ +// https://www.w3.org/TR/device-memory/#sec-device-memory-js-api +[SecureContext, Exposed=(Window,Worker)] +interface mixin NavigatorDeviceMemory { + readonly attribute double deviceMemory; +}; diff --git a/Userland/Libraries/LibWeb/HTML/WorkerNavigator.h b/Userland/Libraries/LibWeb/HTML/WorkerNavigator.h index 51d7dc5f9d8..7848e1d62e9 100644 --- a/Userland/Libraries/LibWeb/HTML/WorkerNavigator.h +++ b/Userland/Libraries/LibWeb/HTML/WorkerNavigator.h @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -20,6 +21,7 @@ namespace Web::HTML { class WorkerNavigator : public Bindings::PlatformObject , public NavigatorConcurrentHardwareMixin + , public NavigatorDeviceMemoryMixin , public NavigatorIDMixin , public NavigatorLanguageMixin , public NavigatorOnLineMixin diff --git a/Userland/Libraries/LibWeb/HTML/WorkerNavigator.idl b/Userland/Libraries/LibWeb/HTML/WorkerNavigator.idl index 86bccd7652d..bba17e0e9aa 100644 --- a/Userland/Libraries/LibWeb/HTML/WorkerNavigator.idl +++ b/Userland/Libraries/LibWeb/HTML/WorkerNavigator.idl @@ -1,7 +1,8 @@ +#import +#import #import #import #import -#import #import #import @@ -20,3 +21,4 @@ WorkerNavigator includes NavigatorLanguage; WorkerNavigator includes NavigatorOnLine; WorkerNavigator includes NavigatorConcurrentHardware; WorkerNavigator includes NavigatorStorage; +WorkerNavigator includes NavigatorDeviceMemory;