Skip to content

Commit a7e0110

Browse files
Merge pull request #1260 from lightpanda-io/cdp-lifecycle-backport
support url on createTarget and send lifecycle events
2 parents e98bb16 + 3769715 commit a7e0110

File tree

4 files changed

+61
-23
lines changed

4 files changed

+61
-23
lines changed

src/Notification.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ pub const PageRemove = struct {};
109109

110110
pub const PageNavigate = struct {
111111
timestamp: u64,
112-
url: []const u8,
112+
url: [:0]const u8,
113113
opts: Page.NavigateOpts,
114114
};
115115

116116
pub const PageNavigated = struct {
117117
timestamp: u64,
118-
url: []const u8,
118+
url: [:0]const u8,
119119
};
120120

121121
pub const PageNetworkIdle = struct {

src/browser/Page.zig

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const timestamp = @import("../datetime.zig").timestamp;
6363
const milliTimestamp = @import("../datetime.zig").milliTimestamp;
6464

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

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

203203
self.version = 0;
204-
self.url = "about/blank";
204+
self.url = "about:blank";
205205

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

208-
const storage_bucket = try self._factory.create(storage.Bucket{});
209-
self.window = try self._factory.eventTarget(Window{
210-
._document = self.document,
211-
._storage_bucket = storage_bucket,
212-
._history = History.init(self),
213-
._performance = Performance.init(),
214-
._proto = undefined,
215-
._location = &default_location,
216-
});
208+
if (comptime initializing == true) {
209+
const storage_bucket = try self._factory.create(storage.Bucket{});
210+
self.window = try self._factory.eventTarget(Window{
211+
._document = self.document,
212+
._storage_bucket = storage_bucket,
213+
._history = History.init(self),
214+
._performance = Performance.init(),
215+
._proto = undefined,
216+
._location = &default_location,
217+
});
218+
} else {
219+
self.window._document = self.document;
220+
self.window._location = &default_location;
221+
// TODO reset _custom_elements?
222+
}
217223

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

227-
self.js = try self._session.executor.createContext(self, true, JS.GlobalMissingCallback.init(&self._polyfill_loader));
228-
errdefer self.js.deinit();
233+
if (comptime initializing == true) {
234+
self.js = try self._session.executor.createContext(self, true, JS.GlobalMissingCallback.init(&self._polyfill_loader));
235+
errdefer self.js.deinit();
236+
}
229237

230238
self._element_styles = .{};
231239
self._element_datasets = .{};

src/cdp/domains/page.zig

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
const std = @import("std");
2020
const Page = @import("../../browser/Page.zig");
2121
const Notification = @import("../../Notification.zig");
22+
const timestampF = @import("../../datetime.zig").timestamp;
2223

2324
const Allocator = std.mem.Allocator;
2425

@@ -47,7 +48,7 @@ pub fn processMessage(cmd: anytype) !void {
4748
const Frame = struct {
4849
id: []const u8,
4950
loaderId: []const u8,
50-
url: []const u8,
51+
url: [:0]const u8,
5152
domainAndRegistry: []const u8 = "",
5253
securityOrigin: []const u8,
5354
mimeType: []const u8 = "text/html",
@@ -82,11 +83,36 @@ 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+
const now = timestampF(.monotonic);
101+
const http_client = page._session.browser.http_client;
102+
103+
try sendPageLifecycle(bc, "DOMContentLoaded", now);
104+
try sendPageLifecycle(bc, "load", now);
105+
106+
const http_active = http_client.active;
107+
const total_network_activity = http_active + http_client.intercepted;
108+
if (page._notified_network_almost_idle.check(total_network_activity <= 2)) {
109+
try sendPageLifecycle(bc, "networkAlmostIdle", now);
110+
}
111+
if (page._notified_network_idle.check(total_network_activity == 0)) {
112+
try sendPageLifecycle(bc, "networkIdle", now);
113+
}
114+
}
115+
90116
return cmd.sendResult(null, .{});
91117
}
92118

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: [:0]const u8 = "about:blank",
113113
// width: ?u64 = null,
114114
// height: ?u64 = null,
115115
browserContextId: ?[]const u8 = null,
@@ -168,7 +168,7 @@ fn createTarget(cmd: anytype) !void {
168168
.targetInfo = TargetInfo{
169169
.attached = false,
170170
.targetId = target_id,
171-
.title = "about:blank",
171+
.title = params.url,
172172
.browserContextId = bc.id,
173173
.url = "about:blank",
174174
},
@@ -179,6 +179,10 @@ fn createTarget(cmd: anytype) !void {
179179
try doAttachtoTarget(cmd, target_id);
180180
}
181181

182+
try page.navigate(params.url, .{
183+
.reason = .address_bar,
184+
});
185+
182186
try cmd.sendResult(.{
183187
.targetId = target_id,
184188
}, .{});
@@ -518,7 +522,7 @@ test "cdp.target: createTarget" {
518522
{
519523
var ctx = testing.context();
520524
defer ctx.deinit();
521-
try ctx.processMessage(.{ .id = 10, .method = "Target.createTarget", .params = .{ .url = "about/blank" } });
525+
try ctx.processMessage(.{ .id = 10, .method = "Target.createTarget", .params = .{ .url = "about:blank" } });
522526

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

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

0 commit comments

Comments
 (0)