Files
browser/src/App.zig
Karl Seguin 8e42d63c1c zig: Zig 0.16
Built against https://github.com/lightpanda-io/zig-v8-fork/tree/zig-0.16 but
it doesn't require a new v8 build.

Built against https://github.com/lightpanda-io/boringssl-zig/tree/zig-0.16
since the current fork we point to isn't updated.

A global std.Io instance, lp.io. Way easier this way and requires 0 changes to
our libcurl integration / event loop.

Network code uses a new layer that does what Zig 0.15's posix package used to
do. Again, quicker migration that way. But, as long as we have the global IO,
and given the half-baked nature of networking in std.Io 0.16, this just makes
sense. Things can be migrated as needed.

The std.time.* -> std.Io.Timestamp/Clock/Duration resulted in _a lot_ of
changes. ArrayList = .{} -> ArrayList -> .empty also resulted in a lot of
changes, but that's obviously superficial. As is the trimLeft/trimRight ->
trimStart/trimEnd rename.

Locking adopt the `Uncancelable` variants, e.g. mutex.lockUncancelable() to
preserve the error-free signature (and, because cancellation would be something
we'd have to put more thought into).

std.json.ObjectMap is now unmanaged, so the allocator had to be passed along.
However, there's still a deprecated managed variant of MemoryPool, so I switched
to it (we can do a small follow up PR to move to the unmanaged after).

I tried use_llvm = false, but it locks my computer, consuming RAM until MacOS
gives me a popup I've never seen before, begging me to start killing processes.

Agent and the networking stuff saw the most significant changes.
2026-07-22 13:26:03 +08:00

150 lines
4.8 KiB
Zig

// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const lp = @import("lightpanda");
const Config = @import("Config.zig");
const Snapshot = @import("browser/js/Snapshot.zig");
const Platform = @import("browser/js/Platform.zig");
const Telemetry = @import("telemetry/telemetry.zig").Telemetry;
const Storage = @import("storage/Storage.zig");
const Network = @import("network/Network.zig");
const Watchdog = @import("Watchdog.zig");
pub const ArenaPool = @import("ArenaPool.zig");
const log = lp.log;
const Allocator = std.mem.Allocator;
const App = @This();
network: Network,
config: *const Config,
storage: Storage,
platform: Platform,
snapshot: Snapshot,
telemetry: Telemetry,
watchdog: Watchdog,
allocator: Allocator,
arena_pool: ArenaPool,
app_dir_path: ?[]const u8,
pub fn init(allocator: Allocator, config: *const Config) !*App {
const platform = try Platform.init(config.v8Flags());
errdefer platform.deinit();
const snapshot = try Snapshot.load();
errdefer snapshot.deinit();
var storage = try Storage.init(allocator, config);
errdefer storage.deinit(allocator);
const app = try allocator.create(App);
errdefer allocator.destroy(app);
app.* = .{
.config = config,
.allocator = allocator,
.platform = platform,
.snapshot = snapshot,
.storage = storage,
.network = undefined,
.app_dir_path = undefined,
.telemetry = undefined,
.arena_pool = undefined,
.watchdog = .init(config.watchdogMs()),
};
try app.watchdog.start();
errdefer app.watchdog.deinit();
app.network = try Network.init(allocator, app, config);
errdefer app.network.deinit();
app.app_dir_path = getAndMakeAppDir(allocator);
app.telemetry = try Telemetry.init(app, config.command, config.interactive());
errdefer app.telemetry.deinit(allocator);
app.arena_pool = ArenaPool.init(allocator, .{});
errdefer app.arena_pool.deinit();
return app;
}
pub fn shutdown(self: *const App) bool {
return self.network.shutdown.load(.acquire);
}
pub fn deinit(self: *App) void {
const allocator = self.allocator;
// All browsers are gone by now, so the entry list is empty; this just
// stops the checker thread.
self.watchdog.deinit();
if (self.app_dir_path) |app_dir_path| {
allocator.free(app_dir_path);
self.app_dir_path = null;
}
self.telemetry.deinit(allocator);
self.network.deinit();
self.snapshot.deinit();
self.platform.deinit();
self.arena_pool.deinit();
self.storage.deinit(allocator);
allocator.destroy(self);
}
fn getAndMakeAppDir(allocator: Allocator) ?[]const u8 {
if (@import("builtin").is_test) {
return allocator.dupe(u8, "/tmp") catch unreachable;
}
const app_dir_path = getAppDataDir(allocator, "lightpanda") catch |err| {
log.warn(.app, "get data dir", .{ .err = err });
return null;
};
std.Io.Dir.cwd().createDirPath(lp.io, app_dir_path) catch |err| switch (err) {
else => {
allocator.free(app_dir_path);
log.warn(.app, "create data dir", .{ .err = err, .path = app_dir_path });
return null;
},
};
return app_dir_path;
}
fn getAppDataDir(allocator: Allocator, appname: []const u8) ![]const u8 {
switch (@import("builtin").os.tag) {
.macos, .ios => {
const home = std.c.getenv("HOME") orelse return error.AppDataDirUnavailable;
return std.fs.path.join(allocator, &.{ std.mem.span(home), "Library", "Application Support", appname });
},
else => {
if (std.c.getenv("XDG_DATA_HOME")) |xdg| {
const x = std.mem.span(xdg);
if (x.len > 0) {
return std.fs.path.join(allocator, &.{ x, appname });
}
}
const home = std.c.getenv("HOME") orelse return error.AppDataDirUnavailable;
return std.fs.path.join(allocator, &.{ std.mem.span(home), ".local", "share", appname });
},
}
}