Skip to content

Commit b58ff2c

Browse files
Merge pull request #1171 from lightpanda-io/cdp-lifecycle
support url on createTarget and send lifecycle events
2 parents b2e4183 + b5ef841 commit b58ff2c

File tree

4 files changed

+53
-13
lines changed

4 files changed

+53
-13
lines changed

src/browser/page.zig

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,14 +549,31 @@ pub const Page = struct {
549549
.body = opts.body != null,
550550
});
551551

552-
// if the url is about:blank, nothing to do.
552+
// if the url is about:blank, we load an empty HTML document in the
553+
// page and dispatch the events.
553554
if (std.mem.eql(u8, "about:blank", request_url)) {
554555
const html_doc = try parser.documentHTMLParseFromStr("");
555556
try self.setDocument(html_doc);
556557

558+
// Assume we parsed the document.
559+
// It's important to force a reset during the following navigation.
560+
self.mode = .parsed;
561+
557562
// We do not processHTMLDoc here as we know we don't have any scripts
558563
// This assumption may be false when CDP Page.addScriptToEvaluateOnNewDocument is implemented
559-
try HTMLDocument.documentIsComplete(self.window.document, self);
564+
self.documentIsComplete();
565+
566+
self.session.browser.notification.dispatch(.page_navigate, &.{
567+
.opts = opts,
568+
.url = request_url,
569+
.timestamp = timestamp(),
570+
});
571+
572+
self.session.browser.notification.dispatch(.page_navigated, &.{
573+
.url = request_url,
574+
.timestamp = timestamp(),
575+
});
576+
560577
return;
561578
}
562579

src/cdp/domains/page.zig

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
const std = @import("std");
2020
const Page = @import("../../browser/page.zig").Page;
21+
const timestampF = @import("../../datetime.zig").timestamp;
2122
const Notification = @import("../../notification.zig").Notification;
2223

2324
const Allocator = std.mem.Allocator;
@@ -82,11 +83,33 @@ fn setLifecycleEventsEnabled(cmd: anytype) !void {
8283
})) orelse return error.InvalidParams;
8384

8485
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
85-
if (params.enabled) {
86-
try bc.lifecycleEventsEnable();
87-
} else {
86+
87+
if (params.enabled == false) {
8888
bc.lifecycleEventsDisable();
89+
return cmd.sendResult(null, .{});
8990
}
91+
92+
// Enable lifecycle events.
93+
try bc.lifecycleEventsEnable();
94+
95+
// When we enable lifecycle events, we must dispatch events for all
96+
// attached targets.
97+
const page = bc.session.currentPage() orelse return error.PageNotLoaded;
98+
99+
if (page.load_state == .complete) {
100+
try sendPageLifecycle(bc, "DOMContentLoaded", timestampF());
101+
try sendPageLifecycle(bc, "load", timestampF());
102+
103+
const http_active = page.http_client.active;
104+
const total_network_activity = http_active + page.http_client.intercepted;
105+
if (page.notified_network_almost_idle.check(total_network_activity <= 2)) {
106+
try sendPageLifecycle(bc, "networkAlmostIdle", timestampF());
107+
}
108+
if (page.notified_network_idle.check(total_network_activity == 0)) {
109+
try sendPageLifecycle(bc, "networkIdle", timestampF());
110+
}
111+
}
112+
90113
return cmd.sendResult(null, .{});
91114
}
92115

src/cdp/domains/target.zig

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ fn disposeBrowserContext(cmd: anytype) !void {
109109

110110
fn createTarget(cmd: anytype) !void {
111111
const params = (try cmd.params(struct {
112-
// url: []const u8,
112+
url: []const u8 = "about:blank",
113113
// width: ?u64 = null,
114114
// height: ?u64 = null,
115115
browserContextId: ?[]const u8 = null,
@@ -167,7 +167,7 @@ fn createTarget(cmd: anytype) !void {
167167
.targetInfo = TargetInfo{
168168
.attached = false,
169169
.targetId = target_id,
170-
.title = "about:blank",
170+
.title = params.url,
171171
.browserContextId = bc.id,
172172
.url = "about:blank",
173173
},
@@ -178,6 +178,10 @@ fn createTarget(cmd: anytype) !void {
178178
try doAttachtoTarget(cmd, target_id);
179179
}
180180

181+
try page.navigate(params.url, .{
182+
.reason = .address_bar,
183+
});
184+
181185
try cmd.sendResult(.{
182186
.targetId = target_id,
183187
}, .{});
@@ -517,7 +521,7 @@ test "cdp.target: createTarget" {
517521
{
518522
var ctx = testing.context();
519523
defer ctx.deinit();
520-
try ctx.processMessage(.{ .id = 10, .method = "Target.createTarget", .params = .{ .url = "about/blank" } });
524+
try ctx.processMessage(.{ .id = 10, .method = "Target.createTarget", .params = .{ .url = "about:blank" } });
521525

522526
// should create a browser context
523527
const bc = ctx.cdp().browser_context.?;
@@ -529,7 +533,7 @@ test "cdp.target: createTarget" {
529533
defer ctx.deinit();
530534
// active auto attach to get the Target.attachedToTarget event.
531535
try ctx.processMessage(.{ .id = 9, .method = "Target.setAutoAttach", .params = .{ .autoAttach = true, .waitForDebuggerOnStart = false } });
532-
try ctx.processMessage(.{ .id = 10, .method = "Target.createTarget", .params = .{ .url = "about/blank" } });
536+
try ctx.processMessage(.{ .id = 10, .method = "Target.createTarget", .params = .{ .url = "about:blank" } });
533537

534538
// should create a browser context
535539
const bc = ctx.cdp().browser_context.?;

src/http/Client.zig

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,6 @@ pub fn restoreOriginalProxy(self: *Client) !void {
338338

339339
// Enable TLS verification on all connections.
340340
pub fn enableTlsVerify(self: *const Client) !void {
341-
try self.ensureNoActiveConnection();
342-
343341
for (self.handles.handles) |*h| {
344342
const easy = h.conn.easy;
345343

@@ -355,8 +353,6 @@ pub fn enableTlsVerify(self: *const Client) !void {
355353

356354
// Disable TLS verification on all connections.
357355
pub fn disableTlsVerify(self: *const Client) !void {
358-
try self.ensureNoActiveConnection();
359-
360356
for (self.handles.handles) |*h| {
361357
const easy = h.conn.easy;
362358

0 commit comments

Comments
 (0)