mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-12-16 16:28:58 +00:00
Merge pull request #1171 from lightpanda-io/cdp-lifecycle
support url on createTarget and send lifecycle events
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
|
||||
const std = @import("std");
|
||||
const Page = @import("../../browser/page.zig").Page;
|
||||
const timestampF = @import("../../datetime.zig").timestamp;
|
||||
const Notification = @import("../../notification.zig").Notification;
|
||||
|
||||
const Allocator = std.mem.Allocator;
|
||||
@@ -82,11 +83,33 @@ 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) {
|
||||
try sendPageLifecycle(bc, "DOMContentLoaded", timestampF());
|
||||
try sendPageLifecycle(bc, "load", timestampF());
|
||||
|
||||
const http_active = page.http_client.active;
|
||||
const total_network_activity = http_active + page.http_client.intercepted;
|
||||
if (page.notified_network_almost_idle.check(total_network_activity <= 2)) {
|
||||
try sendPageLifecycle(bc, "networkAlmostIdle", timestampF());
|
||||
}
|
||||
if (page.notified_network_idle.check(total_network_activity == 0)) {
|
||||
try sendPageLifecycle(bc, "networkIdle", timestampF());
|
||||
}
|
||||
}
|
||||
|
||||
return cmd.sendResult(null, .{});
|
||||
}
|
||||
|
||||
|
||||
@@ -109,7 +109,7 @@ fn disposeBrowserContext(cmd: anytype) !void {
|
||||
|
||||
fn createTarget(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
// url: []const u8,
|
||||
url: []const u8 = "about:blank",
|
||||
// width: ?u64 = null,
|
||||
// height: ?u64 = null,
|
||||
browserContextId: ?[]const u8 = null,
|
||||
@@ -167,7 +167,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",
|
||||
},
|
||||
@@ -178,6 +178,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,
|
||||
}, .{});
|
||||
@@ -517,7 +521,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.?;
|
||||
@@ -529,7 +533,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.?;
|
||||
|
||||
Reference in New Issue
Block a user