Skip to content

Commit

Permalink
LibWeb: Implement NavigatorStorage mixin interface
Browse files Browse the repository at this point in the history
Co-authored-by: Tim Flynn <[email protected]>
(cherry picked from commit e3b3041a0c87f4cfd6d7941963452c3c9428b487)
  • Loading branch information
jamierocks authored and nico committed Nov 1, 2024
1 parent 65b9428 commit 6a477c7
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
source_set("StorageAPI") {
configs += [ "//Userland/Libraries/LibWeb:configs" ]
deps = [ "//Userland/Libraries/LibWeb:all_generated" ]
sources = [ "StorageManager.cpp" ]
sources = [
"NavigatorStorage.cpp",
"StorageManager.cpp",
]
}
1 change: 1 addition & 0 deletions Userland/Libraries/LibWeb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ set(SOURCES
ResizeObserver/ResizeObserverSize.cpp
SecureContexts/AbstractOperations.cpp
SRI/SRI.cpp
StorageAPI/NavigatorStorage.cpp
StorageAPI/StorageManager.cpp
Streams/AbstractOperations.cpp
Streams/ByteLengthQueuingStrategy.cpp
Expand Down
1 change: 1 addition & 0 deletions Userland/Libraries/LibWeb/Forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ struct UnderlyingSource;
}

namespace Web::StorageAPI {
class NavigatorStorage;
class StorageManager;
}

Expand Down
7 changes: 6 additions & 1 deletion Userland/Libraries/LibWeb/HTML/Navigator.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <LibWeb/HTML/NavigatorOnLine.h>
#include <LibWeb/HTML/PluginArray.h>
#include <LibWeb/HTML/UserActivation.h>
#include <LibWeb/StorageAPI/NavigatorStorage.h>

namespace Web::HTML {

Expand All @@ -23,7 +24,8 @@ class Navigator : public Bindings::PlatformObject
, public NavigatorConcurrentHardwareMixin
, public NavigatorIDMixin
, public NavigatorLanguageMixin
, public NavigatorOnLineMixin {
, public NavigatorOnLineMixin
, public StorageAPI::NavigatorStorage {
WEB_PLATFORM_OBJECT(Navigator, Bindings::PlatformObject);
JS_DECLARE_ALLOCATOR(Navigator);

Expand Down Expand Up @@ -64,6 +66,9 @@ class Navigator : public Bindings::PlatformObject

virtual void initialize(JS::Realm&) override;

// ^StorageAPI::NavigatorStorage
virtual Bindings::PlatformObject const& this_navigator_storage_object() const override { return *this; }

JS::GCPtr<PluginArray> m_plugin_array;
JS::GCPtr<MimeTypeArray> m_mime_type_array;

Expand Down
2 changes: 2 additions & 0 deletions Userland/Libraries/LibWeb/HTML/Navigator.idl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#import <HTML/NavigatorConcurrentHardware.idl>
#import <HTML/PluginArray.idl>
#import <HTML/UserActivation.idl>
#import <StorageAPI/NavigatorStorage.idl>

// https://html.spec.whatwg.org/multipage/system-state.html#navigator
[Exposed=Window]
Expand Down Expand Up @@ -62,3 +63,4 @@ Navigator includes NavigatorCookies;
Navigator includes NavigatorPlugins;
Navigator includes NavigatorConcurrentHardware;
Navigator includes NavigatorAutomationInformation;
Navigator includes NavigatorStorage;
9 changes: 9 additions & 0 deletions Userland/Libraries/LibWeb/HTML/Scripting/Environments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <LibWeb/HTML/WorkerGlobalScope.h>
#include <LibWeb/Page/Page.h>
#include <LibWeb/SecureContexts/AbstractOperations.h>
#include <LibWeb/StorageAPI/StorageManager.h>

namespace Web::HTML {

Expand Down Expand Up @@ -56,6 +57,7 @@ void EnvironmentSettingsObject::visit_edges(Cell::Visitor& visitor)
visitor.ignore(m_outstanding_rejected_promises_weak_set);
m_realm_execution_context->visit_edges(visitor);
visitor.visit(m_fetch_group);
visitor.visit(m_storage_manager);
}

JS::ExecutionContext& EnvironmentSettingsObject::realm_execution_context()
Expand Down Expand Up @@ -517,4 +519,11 @@ SerializedEnvironmentSettingsObject EnvironmentSettingsObject::serialize()
return object;
}

JS::NonnullGCPtr<StorageAPI::StorageManager> EnvironmentSettingsObject::storage_manager()
{
if (!m_storage_manager)
m_storage_manager = realm().heap().allocate<StorageAPI::StorageManager>(realm(), realm());
return *m_storage_manager;
}

}
6 changes: 6 additions & 0 deletions Userland/Libraries/LibWeb/HTML/Scripting/Environments.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ struct EnvironmentSettingsObject : public Environment {

SerializedEnvironmentSettingsObject serialize();

JS::NonnullGCPtr<StorageAPI::StorageManager> storage_manager();

protected:
explicit EnvironmentSettingsObject(NonnullOwnPtr<JS::ExecutionContext>);

Expand All @@ -143,6 +145,10 @@ struct EnvironmentSettingsObject : public Environment {
// https://fetch.spec.whatwg.org/#concept-fetch-record
// A fetch group holds an ordered list of fetch records
Vector<JS::NonnullGCPtr<Fetch::Infrastructure::FetchRecord>> m_fetch_group;

// https://storage.spec.whatwg.org/#api
// Each environment settings object has an associated StorageManager object.
JS::GCPtr<StorageAPI::StorageManager> m_storage_manager;
};

EnvironmentSettingsObject& incumbent_settings_object();
Expand Down
7 changes: 6 additions & 1 deletion Userland/Libraries/LibWeb/HTML/WorkerNavigator.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
#include <LibWeb/HTML/NavigatorID.h>
#include <LibWeb/HTML/NavigatorLanguage.h>
#include <LibWeb/HTML/NavigatorOnLine.h>
#include <LibWeb/StorageAPI/NavigatorStorage.h>

namespace Web::HTML {

class WorkerNavigator : public Bindings::PlatformObject
, public NavigatorConcurrentHardwareMixin
, public NavigatorIDMixin
, public NavigatorLanguageMixin
, public NavigatorOnLineMixin {
, public NavigatorOnLineMixin
, public StorageAPI::NavigatorStorage {
WEB_PLATFORM_OBJECT(WorkerNavigator, Bindings::PlatformObject);
JS_DECLARE_ALLOCATOR(WorkerNavigator);

Expand All @@ -31,6 +33,9 @@ class WorkerNavigator : public Bindings::PlatformObject
explicit WorkerNavigator(WorkerGlobalScope&);

virtual void initialize(JS::Realm&) override;

// ^StorageAPI::NavigatorStorage
virtual Bindings::PlatformObject const& this_navigator_storage_object() const override { return *this; }
};

}
2 changes: 2 additions & 0 deletions Userland/Libraries/LibWeb/HTML/WorkerNavigator.idl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#import <HTML/NavigatorLanguage.idl>
#import <HTML/NavigatorOnLine.idl>
#import <HTML/NavigatorConcurrentHardware.idl>
#import <StorageAPI/NavigatorStorage.idl>

// https://html.spec.whatwg.org/multipage/workers.html#workernavigator
[Exposed=Worker]
Expand All @@ -11,3 +12,4 @@ WorkerNavigator includes NavigatorID;
WorkerNavigator includes NavigatorLanguage;
WorkerNavigator includes NavigatorOnLine;
WorkerNavigator includes NavigatorConcurrentHardware;
WorkerNavigator includes NavigatorStorage;
22 changes: 22 additions & 0 deletions Userland/Libraries/LibWeb/StorageAPI/NavigatorStorage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2024, Jamie Mansfield <[email protected]>
* Copyright (c) 2024, Tim Flynn <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <LibJS/Runtime/Realm.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/StorageAPI/NavigatorStorage.h>
#include <LibWeb/StorageAPI/StorageManager.h>

namespace Web::StorageAPI {

// https://storage.spec.whatwg.org/#dom-navigatorstorage-storage
JS::NonnullGCPtr<StorageManager> NavigatorStorage::storage()
{
// The storage getter steps are to return this’s relevant settings object’s StorageManager object.
return HTML::relevant_settings_object(this_navigator_storage_object()).storage_manager();
}

}
27 changes: 27 additions & 0 deletions Userland/Libraries/LibWeb/StorageAPI/NavigatorStorage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2024, Jamie Mansfield <[email protected]>
* Copyright (c) 2024, Tim Flynn <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#pragma once

#include <LibJS/Forward.h>
#include <LibJS/Heap/GCPtr.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/Forward.h>

namespace Web::StorageAPI {

class NavigatorStorage {
public:
virtual ~NavigatorStorage() = default;

JS::NonnullGCPtr<StorageManager> storage();

protected:
virtual Bindings::PlatformObject const& this_navigator_storage_object() const = 0;
};

}
7 changes: 7 additions & 0 deletions Userland/Libraries/LibWeb/StorageAPI/NavigatorStorage.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#import <StorageAPI/StorageManager.idl>

// https://storage.spec.whatwg.org/#navigatorstorage
[SecureContext]
interface mixin NavigatorStorage {
[SameObject] readonly attribute StorageManager storage;
};

0 comments on commit 6a477c7

Please sign in to comment.