Skip to content

Commit

Permalink
LibWeb: Implement navigator.deviceMemory
Browse files Browse the repository at this point in the history
Fixes at least 2 subtests in wpt/device-memory.
  • Loading branch information
gmta authored and awesomekling committed Oct 8, 2024
1 parent 6189d1a commit 69f11fc
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Userland/Libraries/LibWeb/HTML/Navigator.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <LibWeb/HTML/MimeTypeArray.h>
#include <LibWeb/HTML/NavigatorBeacon.h>
#include <LibWeb/HTML/NavigatorConcurrentHardware.h>
#include <LibWeb/HTML/NavigatorDeviceMemory.h>
#include <LibWeb/HTML/NavigatorID.h>
#include <LibWeb/HTML/NavigatorLanguage.h>
#include <LibWeb/HTML/NavigatorOnLine.h>
Expand All @@ -23,6 +24,7 @@ namespace Web::HTML {
class Navigator : public Bindings::PlatformObject
, public NavigatorBeaconMixin
, public NavigatorConcurrentHardwareMixin
, public NavigatorDeviceMemoryMixin
, public NavigatorIDMixin
, public NavigatorLanguageMixin
, public NavigatorOnLineMixin
Expand Down
4 changes: 3 additions & 1 deletion Userland/Libraries/LibWeb/HTML/Navigator.idl
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#import <Clipboard/Clipboard.idl>
#import <HTML/MimeTypeArray.idl>
#import <HTML/NavigatorBeacon.idl>
#import <HTML/NavigatorConcurrentHardware.idl>
#import <HTML/NavigatorDeviceMemory.idl>
#import <HTML/NavigatorID.idl>
#import <HTML/NavigatorLanguage.idl>
#import <HTML/NavigatorOnLine.idl>
#import <HTML/NavigatorConcurrentHardware.idl>
#import <HTML/PluginArray.idl>
#import <HTML/ServiceWorkerContainer.idl>
#import <HTML/UserActivation.idl>
Expand Down Expand Up @@ -72,3 +73,4 @@ Navigator includes NavigatorPlugins;
Navigator includes NavigatorConcurrentHardware;
Navigator includes NavigatorAutomationInformation;
Navigator includes NavigatorStorage;
Navigator includes NavigatorDeviceMemory;
40 changes: 40 additions & 0 deletions Userland/Libraries/LibWeb/HTML/NavigatorDeviceMemory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2024, Jelle Raaijmakers <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#pragma once

#include <AK/BuiltinWrappers.h>
#include <LibCore/System.h>
#include <LibWeb/WebIDL/Types.h>

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<u64>(1) << (required_bits - 1);
auto upper_memory_in_mib = static_cast<u64>(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<WebIDL::Double>(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);
}
};

}
5 changes: 5 additions & 0 deletions Userland/Libraries/LibWeb/HTML/NavigatorDeviceMemory.idl
Original file line number Diff line number Diff line change
@@ -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;
};
2 changes: 2 additions & 0 deletions Userland/Libraries/LibWeb/HTML/WorkerNavigator.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/HTML/NavigatorConcurrentHardware.h>
#include <LibWeb/HTML/NavigatorDeviceMemory.h>
#include <LibWeb/HTML/NavigatorID.h>
#include <LibWeb/HTML/NavigatorLanguage.h>
#include <LibWeb/HTML/NavigatorOnLine.h>
Expand All @@ -20,6 +21,7 @@ namespace Web::HTML {

class WorkerNavigator : public Bindings::PlatformObject
, public NavigatorConcurrentHardwareMixin
, public NavigatorDeviceMemoryMixin
, public NavigatorIDMixin
, public NavigatorLanguageMixin
, public NavigatorOnLineMixin
Expand Down
4 changes: 3 additions & 1 deletion Userland/Libraries/LibWeb/HTML/WorkerNavigator.idl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#import <HTML/NavigatorConcurrentHardware.idl>
#import <HTML/NavigatorDeviceMemory.idl>
#import <HTML/NavigatorID.idl>
#import <HTML/NavigatorLanguage.idl>
#import <HTML/NavigatorOnLine.idl>
#import <HTML/NavigatorConcurrentHardware.idl>
#import <MediaCapabilitiesAPI/MediaCapabilities.idl>
#import <StorageAPI/NavigatorStorage.idl>

Expand All @@ -20,3 +21,4 @@ WorkerNavigator includes NavigatorLanguage;
WorkerNavigator includes NavigatorOnLine;
WorkerNavigator includes NavigatorConcurrentHardware;
WorkerNavigator includes NavigatorStorage;
WorkerNavigator includes NavigatorDeviceMemory;

0 comments on commit 69f11fc

Please sign in to comment.