storage: first implementation of webstorage API

This commit is contained in:
Pierre Tachoire
2024-04-24 11:53:04 +02:00
parent 2a94e5a69e
commit 3c5d601622
7 changed files with 272 additions and 0 deletions

View File

@@ -4,6 +4,8 @@ const parser = @import("../netsurf.zig");
const EventTarget = @import("../dom/event_target.zig").EventTarget;
const storage = @import("../storage/storage.zig");
// https://dom.spec.whatwg.org/#interface-window-extensions
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#window
pub const Window = struct {
@@ -17,6 +19,8 @@ pub const Window = struct {
document: ?*parser.DocumentHTML = null,
target: []const u8,
storageShelf: ?*storage.Shelf = null,
pub fn create(target: ?[]const u8) Window {
return Window{
.target = target orelse "",
@@ -27,6 +31,10 @@ pub const Window = struct {
self.document = doc;
}
pub fn setStorageShelf(self: *Window, shelf: *storage.Shelf) void {
self.storageShelf = shelf;
}
pub fn get_window(self: *Window) *Window {
return self;
}
@@ -46,4 +54,14 @@ pub const Window = struct {
pub fn get_name(self: *Window) []const u8 {
return self.target;
}
pub fn get_localStorage(self: *Window) !*storage.Bottle {
if (self.storageShelf == null) return parser.DOMError.NotSupported;
return &self.storageShelf.?.bucket.local;
}
pub fn get_sessionStorage(self: *Window) !*storage.Bottle {
if (self.storageShelf == null) return parser.DOMError.NotSupported;
return &self.storageShelf.?.bucket.session;
}
};