Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Notification.zig
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ pub const PageRemove = struct {};

pub const PageNavigate = struct {
timestamp: u64,
url: []const u8,
url: [:0]const u8,
opts: Page.NavigateOpts,
};

pub const PageNavigated = struct {
timestamp: u64,
url: []const u8,
url: [:0]const u8,
};

pub const PageNetworkIdle = struct {
Expand Down
34 changes: 21 additions & 13 deletions src/browser/Page.zig
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const timestamp = @import("../datetime.zig").timestamp;
const milliTimestamp = @import("../datetime.zig").milliTimestamp;

pub threadlocal var current: *Page = undefined;
var default_url = URL{ ._raw = "about/blank" };
var default_url = URL{ ._raw = "about:blank" };
pub var default_location: Location = Location{ ._url = &default_url };

pub const BUF_SIZE = 1024;
Expand Down Expand Up @@ -201,19 +201,25 @@ fn reset(self: *Page, comptime initializing: bool) !void {
self._factory = Factory.init(self);

self.version = 0;
self.url = "about/blank";
self.url = "about:blank";

self.document = (try self._factory.document(Node.Document.HTMLDocument{ ._proto = undefined })).asDocument();

const storage_bucket = try self._factory.create(storage.Bucket{});
self.window = try self._factory.eventTarget(Window{
._document = self.document,
._storage_bucket = storage_bucket,
._history = History.init(self),
._performance = Performance.init(),
._proto = undefined,
._location = &default_location,
});
if (comptime initializing == true) {
const storage_bucket = try self._factory.create(storage.Bucket{});
self.window = try self._factory.eventTarget(Window{
._document = self.document,
._storage_bucket = storage_bucket,
._history = History.init(self),
._performance = Performance.init(),
._proto = undefined,
._location = &default_location,
});
} else {
self.window._document = self.document;
self.window._location = &default_location;
// TODO reset _custom_elements?
}

self._parse_state = .pre;
self._load_state = .parsing;
Expand All @@ -224,8 +230,10 @@ fn reset(self: *Page, comptime initializing: bool) !void {
self._script_manager = ScriptManager.init(self);
errdefer self._script_manager.deinit();

self.js = try self._session.executor.createContext(self, true, JS.GlobalMissingCallback.init(&self._polyfill_loader));
errdefer self.js.deinit();
if (comptime initializing == true) {
self.js = try self._session.executor.createContext(self, true, JS.GlobalMissingCallback.init(&self._polyfill_loader));
errdefer self.js.deinit();
}

self._element_styles = .{};
self._element_datasets = .{};
Expand Down
34 changes: 30 additions & 4 deletions src/cdp/domains/page.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
const std = @import("std");
const Page = @import("../../browser/Page.zig");
const Notification = @import("../../Notification.zig");
const timestampF = @import("../../datetime.zig").timestamp;

const Allocator = std.mem.Allocator;

Expand Down Expand Up @@ -47,7 +48,7 @@ pub fn processMessage(cmd: anytype) !void {
const Frame = struct {
id: []const u8,
loaderId: []const u8,
url: []const u8,
url: [:0]const u8,
domainAndRegistry: []const u8 = "",
securityOrigin: []const u8,
mimeType: []const u8 = "text/html",
Expand Down Expand Up @@ -82,11 +83,36 @@ fn setLifecycleEventsEnabled(cmd: anytype) !void {
})) orelse return error.InvalidParams;

const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
if (params.enabled) {
try bc.lifecycleEventsEnable();
} else {

if (params.enabled == false) {
bc.lifecycleEventsDisable();
return cmd.sendResult(null, .{});
}

// Enable lifecycle events.
try bc.lifecycleEventsEnable();

// When we enable lifecycle events, we must dispatch events for all
// attached targets.
const page = bc.session.currentPage() orelse return error.PageNotLoaded;

if (page._load_state == .complete) {
const now = timestampF(.monotonic);
const http_client = page._session.browser.http_client;

try sendPageLifecycle(bc, "DOMContentLoaded", now);
try sendPageLifecycle(bc, "load", now);

const http_active = http_client.active;
const total_network_activity = http_active + http_client.intercepted;
if (page._notified_network_almost_idle.check(total_network_activity <= 2)) {
try sendPageLifecycle(bc, "networkAlmostIdle", now);
}
if (page._notified_network_idle.check(total_network_activity == 0)) {
try sendPageLifecycle(bc, "networkIdle", now);
}
}

return cmd.sendResult(null, .{});
}

Expand Down
12 changes: 8 additions & 4 deletions src/cdp/domains/target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ fn disposeBrowserContext(cmd: anytype) !void {

fn createTarget(cmd: anytype) !void {
const params = (try cmd.params(struct {
// url: []const u8,
url: [:0]const u8 = "about:blank",
// width: ?u64 = null,
// height: ?u64 = null,
browserContextId: ?[]const u8 = null,
Expand Down Expand Up @@ -168,7 +168,7 @@ fn createTarget(cmd: anytype) !void {
.targetInfo = TargetInfo{
.attached = false,
.targetId = target_id,
.title = "about:blank",
.title = params.url,
.browserContextId = bc.id,
.url = "about:blank",
},
Expand All @@ -179,6 +179,10 @@ fn createTarget(cmd: anytype) !void {
try doAttachtoTarget(cmd, target_id);
}

try page.navigate(params.url, .{
.reason = .address_bar,
});

try cmd.sendResult(.{
.targetId = target_id,
}, .{});
Expand Down Expand Up @@ -518,7 +522,7 @@ test "cdp.target: createTarget" {
{
var ctx = testing.context();
defer ctx.deinit();
try ctx.processMessage(.{ .id = 10, .method = "Target.createTarget", .params = .{ .url = "about/blank" } });
try ctx.processMessage(.{ .id = 10, .method = "Target.createTarget", .params = .{ .url = "about:blank" } });

// should create a browser context
const bc = ctx.cdp().browser_context.?;
Expand All @@ -530,7 +534,7 @@ test "cdp.target: createTarget" {
defer ctx.deinit();
// active auto attach to get the Target.attachedToTarget event.
try ctx.processMessage(.{ .id = 9, .method = "Target.setAutoAttach", .params = .{ .autoAttach = true, .waitForDebuggerOnStart = false } });
try ctx.processMessage(.{ .id = 10, .method = "Target.createTarget", .params = .{ .url = "about/blank" } });
try ctx.processMessage(.{ .id = 10, .method = "Target.createTarget", .params = .{ .url = "about:blank" } });

// should create a browser context
const bc = ctx.cdp().browser_context.?;
Expand Down
Loading