Skip to content

Lightpanda: the headless browser designed for AI and automation

`Download`: fix `inProgress` regression

Halil Durak98db1ec8101e
diff --git a/src/Notification.zig b/src/Notification.zig
index 3a6f0cd9f..03d2e9aea 100644
--- a/src/Notification.zig
+++ b/src/Notification.zig
@@ -333,8 +333,8 @@ pub const DownloadWillBegin = struct {
content_type: ?[]const u8,
};
-// Emitted by Frame as a download is written to disk: once when it starts
-// (`.in_progress`) and once when the body is fully written (`.completed`).
+// Emitted by Download: once when it starts (`.in_progress`) and once when it
+// ends (`.completed`, or `.canceled` if it failed).
// The CDP browser domain turns this into a `Browser.downloadProgress` event.
pub const DownloadProgress = struct {
guid: []const u8,
diff --git a/src/browser/Download.zig b/src/browser/Download.zig
index 34e0a1618..fa05812aa 100644
--- a/src/browser/Download.zig
+++ b/src/browser/Download.zig
@@ -97,7 +97,7 @@ fn init(frame: *Frame, transfer: *HttpClient.Transfer, maybe_suggestion: ?[]cons
});
}
- return .{
+ const download: Download = .{
.guid = guid,
.file = maybe_file,
.filename = filename,
@@ -107,6 +107,12 @@ fn init(frame: *Frame, transfer: *HttpClient.Transfer, maybe_suggestion: ?[]cons
.total = if (succeeded) transfer.getContentLength() else null,
.frame = frame,
};
+ // Like Chrome, downloadWillBegin is followed by an inProgress
+ // downloadProgress. A failed download is canceled at once instead.
+ if (succeeded) {
+ download.report(.in_progress);
+ }
+ return download;
}
/// Try to initialize a `Download` from a `Transfer`. It's up to caller to
diff --git a/src/server/cdp/domains/browser.zig b/src/server/cdp/domains/browser.zig
index d570d14b5..36906be6f 100644
--- a/src/server/cdp/domains/browser.zig
+++ b/src/server/cdp/domains/browser.zig
@@ -475,6 +475,11 @@ test "cdp.browser: setDownloadBehavior writes an attachment to disk and emits ev
.suggestedFilename = "report.csv",
.contentType = "text/csv",
}, .{});
+ try ctx.expectSentEvent("Browser.downloadProgress", .{
+ .state = "inProgress",
+ .totalBytes = 30,
+ .receivedBytes = 0,
+ }, .{});
try ctx.expectSentEvent("Browser.downloadProgress", .{
.state = "completed",
.totalBytes = 30,
@@ -716,3 +721,29 @@ test "cdp.browser: stream behavior streams an <a download> as LP.downloadData" {
.receivedBytes = 30,
}, .{ .session_id = "SID-DL" });
}
+
+test "cdp.browser: download events leave a detached stream session" {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+
+ const bc = try ctx.loadBrowserContext(.{ .session_id = "SID-DL" });
+
+ try ctx.processMessage(.{
+ .id = 51,
+ .method = "Browser.setDownloadBehavior",
+ .sessionId = "SID-DL",
+ .params = .{ .behavior = "stream", .eventsEnabled = true },
+ });
+ try ctx.expectSentResult(null, .{ .id = 51 });
+ try testing.expectEqualSlices(u8, "SID-DL", bc.download_session_id.?);
+
+ // Events must not keep going to a sessionId the client dropped.
+ try ctx.processMessage(.{
+ .id = 52,
+ .method = "Target.detachFromTarget",
+ .params = .{ .sessionId = "SID-DL" },
+ });
+ try ctx.expectSentResult(null, .{ .id = 52 });
+ try testing.expect(bc.download_session_id == null);
+ try testing.expectEqual(.stream, bc.session.download_behavior);
+}