mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-28 22:53:28 +00:00
Handle sessionId in result
Signed-off-by: Francis Bouvier <francis@lightpanda.io>
This commit is contained in:
@@ -52,7 +52,7 @@ fn browserGetVersion(
|
||||
.userAgent = UserAgent,
|
||||
.jsVersion = JsVersion,
|
||||
};
|
||||
return result(alloc, id, Res, res);
|
||||
return result(alloc, id, Res, res, null);
|
||||
}
|
||||
|
||||
fn browserSetDownloadBehavior(
|
||||
@@ -69,5 +69,5 @@ fn browserSetDownloadBehavior(
|
||||
};
|
||||
const params = try getParams(alloc, Params, scanner);
|
||||
std.log.debug("params {any}", .{params});
|
||||
return result(alloc, id, null, null);
|
||||
return result(alloc, id, null, null, null);
|
||||
}
|
||||
|
||||
@@ -78,8 +78,6 @@ fn checkKey(key: []const u8, token: []const u8) !void {
|
||||
if (!std.mem.eql(u8, key, token)) return error.WrongToken;
|
||||
}
|
||||
|
||||
const resultNull = "{{\"id\": {d}, \"result\": {{}}}}";
|
||||
|
||||
// caller owns the slice returned
|
||||
pub fn stringify(alloc: std.mem.Allocator, res: anytype) ![]const u8 {
|
||||
var out = std.ArrayList(u8).init(alloc);
|
||||
@@ -94,20 +92,31 @@ pub fn stringify(alloc: std.mem.Allocator, res: anytype) ![]const u8 {
|
||||
return ret;
|
||||
}
|
||||
|
||||
const resultNull = "{{\"id\": {d}, \"result\": {{}}}}";
|
||||
const resultNullSession = "{{\"id\": {d}, \"result\": {{}}, \"sessionId\": \"{s}\"}}";
|
||||
|
||||
// caller owns the slice returned
|
||||
pub fn result(
|
||||
alloc: std.mem.Allocator,
|
||||
id: u64,
|
||||
comptime T: ?type,
|
||||
res: anytype,
|
||||
sessionID: ?[]const u8,
|
||||
) ![]const u8 {
|
||||
if (T == null) return try std.fmt.allocPrint(alloc, resultNull, .{id});
|
||||
if (T == null) {
|
||||
// No need to stringify a custom JSON msg, just use string templates
|
||||
if (sessionID) |sID| {
|
||||
return try std.fmt.allocPrint(alloc, resultNullSession, .{ id, sID });
|
||||
}
|
||||
return try std.fmt.allocPrint(alloc, resultNull, .{id});
|
||||
}
|
||||
|
||||
const Resp = struct {
|
||||
id: u64,
|
||||
result: T.?,
|
||||
sessionId: ?[]const u8,
|
||||
};
|
||||
const resp = Resp{ .id = id, .result = res };
|
||||
const resp = Resp{ .id = id, .result = res, .sessionId = sessionID };
|
||||
|
||||
return stringify(alloc, resp);
|
||||
}
|
||||
@@ -125,32 +134,32 @@ pub fn getParams(
|
||||
return std.json.innerParse(T, alloc, scanner, options);
|
||||
}
|
||||
|
||||
pub fn getSessionID(
|
||||
alloc: std.mem.Allocator,
|
||||
scanner: *std.json.Scanner,
|
||||
) ![]const u8 {
|
||||
var n = (try scanner.next()).string;
|
||||
pub fn getSessionID(scanner: *std.json.Scanner) !?[]const u8 {
|
||||
|
||||
// if next token is the end of the object, there is no "sessionId"
|
||||
const t = try scanner.next();
|
||||
if (t == .object_end) return null;
|
||||
|
||||
var n = t.string;
|
||||
|
||||
// if next token is "params" ignore them
|
||||
// NOTE: will panic if it's not an empty "params" object
|
||||
// TODO: maybe we should return a custom error here
|
||||
if (std.mem.eql(u8, n, "params")) {
|
||||
// ignore empty params
|
||||
_ = (try scanner.next()).object_begin;
|
||||
_ = (try scanner.next()).object_end;
|
||||
n = (try scanner.next()).string;
|
||||
}
|
||||
try checkKey("sessionId", n);
|
||||
const options = std.json.ParseOptions{
|
||||
.max_value_len = scanner.input.len,
|
||||
.allocate = .alloc_if_needed,
|
||||
};
|
||||
return std.json.innerParse([]const u8, alloc, scanner, options);
|
||||
|
||||
// if next token is not "sessionId" there is no "sessionId"
|
||||
if (!std.mem.eql(u8, n, "sessionId")) return null;
|
||||
|
||||
// parse "sessionId"
|
||||
return (try scanner.next()).string;
|
||||
}
|
||||
|
||||
// Common
|
||||
// ------
|
||||
|
||||
pub const SessionID = "9559320D92474062597D9875C664CAC0";
|
||||
|
||||
pub const SessionIDResp = struct {
|
||||
id: u64,
|
||||
result: struct {} = .{},
|
||||
sessionId: []const u8,
|
||||
};
|
||||
|
||||
@@ -32,7 +32,7 @@ fn setEmulatedMedia(
|
||||
_: *std.json.Scanner,
|
||||
_: *Ctx,
|
||||
) ![]const u8 {
|
||||
return result(alloc, id, null, null);
|
||||
return result(alloc, id, null, null, null);
|
||||
}
|
||||
|
||||
fn setFocusEmulationEnabled(
|
||||
@@ -41,5 +41,5 @@ fn setFocusEmulationEnabled(
|
||||
_: *std.json.Scanner,
|
||||
_: *Ctx,
|
||||
) ![]const u8 {
|
||||
return result(alloc, id, null, null);
|
||||
return result(alloc, id, null, null, null);
|
||||
}
|
||||
|
||||
@@ -31,8 +31,6 @@ fn enable(
|
||||
scanner: *std.json.Scanner,
|
||||
_: *Ctx,
|
||||
) ![]const u8 {
|
||||
return stringify(alloc, cdp.SessionIDResp{
|
||||
.id = id,
|
||||
.sessionId = try cdp.getSessionID(alloc, scanner),
|
||||
});
|
||||
const sessionID = try cdp.getSessionID(scanner);
|
||||
return result(alloc, id, null, null, sessionID);
|
||||
}
|
||||
|
||||
@@ -31,8 +31,6 @@ fn enable(
|
||||
scanner: *std.json.Scanner,
|
||||
_: *Ctx,
|
||||
) ![]const u8 {
|
||||
return stringify(alloc, cdp.SessionIDResp{
|
||||
.id = id,
|
||||
.sessionId = try cdp.getSessionID(alloc, scanner),
|
||||
});
|
||||
const sessionID = try cdp.getSessionID(scanner);
|
||||
return result(alloc, id, null, null, sessionID);
|
||||
}
|
||||
|
||||
@@ -37,10 +37,8 @@ fn enable(
|
||||
scanner: *std.json.Scanner,
|
||||
_: *Ctx,
|
||||
) ![]const u8 {
|
||||
return stringify(alloc, cdp.SessionIDResp{
|
||||
.id = id,
|
||||
.sessionId = try cdp.getSessionID(alloc, scanner),
|
||||
});
|
||||
const sessionID = try cdp.getSessionID(scanner);
|
||||
return result(alloc, id, null, null, sessionID);
|
||||
}
|
||||
|
||||
fn getFrameTree(
|
||||
@@ -50,17 +48,26 @@ fn getFrameTree(
|
||||
_: *Ctx,
|
||||
) ![]const u8 {
|
||||
// TODO: dummy
|
||||
return result(alloc, id, null, null);
|
||||
return result(alloc, id, null, null, null);
|
||||
}
|
||||
|
||||
fn setLifecycleEventsEnabled(
|
||||
alloc: std.mem.Allocator,
|
||||
id: u64,
|
||||
_: *std.json.Scanner,
|
||||
scanner: *std.json.Scanner,
|
||||
_: *Ctx,
|
||||
) ![]const u8 {
|
||||
|
||||
// input
|
||||
const Params = struct {
|
||||
enabled: bool,
|
||||
};
|
||||
_ = try getParams(alloc, Params, scanner);
|
||||
const sessionID = try cdp.getSessionID(scanner);
|
||||
|
||||
// output
|
||||
// TODO: dummy
|
||||
return result(alloc, id, null, null);
|
||||
return result(alloc, id, null, null, sessionID);
|
||||
}
|
||||
|
||||
fn addScriptToEvaluateOnNewDocument(
|
||||
@@ -69,20 +76,18 @@ fn addScriptToEvaluateOnNewDocument(
|
||||
scanner: *std.json.Scanner,
|
||||
_: *Ctx,
|
||||
) ![]const u8 {
|
||||
|
||||
// input
|
||||
const Params = struct {
|
||||
source: []const u8,
|
||||
worldName: ?[]const u8 = null,
|
||||
};
|
||||
_ = try getParams(alloc, Params, scanner);
|
||||
const sessionID = try cdp.getSessionID(scanner);
|
||||
|
||||
// output
|
||||
const Res = struct {
|
||||
id: u64,
|
||||
result: struct {
|
||||
identifier: []const u8 = "1",
|
||||
} = .{},
|
||||
sessionId: []const u8,
|
||||
identifier: []const u8 = "1",
|
||||
};
|
||||
return stringify(alloc, Res{
|
||||
.id = id,
|
||||
.sessionId = try cdp.getSessionID(alloc, scanner),
|
||||
});
|
||||
return result(alloc, id, Res, Res{}, sessionID);
|
||||
}
|
||||
|
||||
@@ -33,10 +33,8 @@ fn enable(
|
||||
scanner: *std.json.Scanner,
|
||||
_: *Ctx,
|
||||
) ![]const u8 {
|
||||
return stringify(alloc, cdp.SessionIDResp{
|
||||
.id = id,
|
||||
.sessionId = try cdp.getSessionID(alloc, scanner),
|
||||
});
|
||||
const sessionID = try cdp.getSessionID(scanner);
|
||||
return result(alloc, id, null, null, sessionID);
|
||||
}
|
||||
|
||||
fn runIfWaitingForDebugger(
|
||||
@@ -45,5 +43,5 @@ fn runIfWaitingForDebugger(
|
||||
_: *std.json.Scanner,
|
||||
_: *Ctx,
|
||||
) ![]const u8 {
|
||||
return result(alloc, id, null, null);
|
||||
return result(alloc, id, null, null, null);
|
||||
}
|
||||
|
||||
@@ -2,9 +2,10 @@ const std = @import("std");
|
||||
|
||||
const server = @import("../server.zig");
|
||||
const Ctx = server.Cmd;
|
||||
const result = @import("cdp.zig").result;
|
||||
const getParams = @import("cdp.zig").getParams;
|
||||
const stringify = @import("cdp.zig").stringify;
|
||||
const cdp = @import("cdp.zig");
|
||||
const result = cdp.result;
|
||||
const getParams = cdp.getParams;
|
||||
const stringify = cdp.stringify;
|
||||
|
||||
const TargetMethods = enum {
|
||||
setAutoAttach,
|
||||
@@ -63,7 +64,7 @@ fn tagetSetAutoAttach(
|
||||
const attached = try stringify(alloc, AttachToTarget{});
|
||||
try server.sendSync(ctx, attached);
|
||||
|
||||
return result(alloc, id, null, null);
|
||||
return result(alloc, id, null, null, null);
|
||||
}
|
||||
|
||||
fn tagetGetTargetInfo(
|
||||
|
||||
Reference in New Issue
Block a user