mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-12-17 00:38:59 +00:00
77
src/cdp/browser.zig
Normal file
77
src/cdp/browser.zig
Normal file
@@ -0,0 +1,77 @@
|
||||
const std = @import("std");
|
||||
|
||||
const server = @import("../server.zig");
|
||||
const Ctx = server.CmdContext;
|
||||
const SendFn = server.SendFn;
|
||||
const result = @import("cdp.zig").result;
|
||||
const getParams = @import("cdp.zig").getParams;
|
||||
|
||||
const BrowserMethods = enum {
|
||||
getVersion,
|
||||
setDownloadBehavior,
|
||||
};
|
||||
|
||||
pub fn browser(
|
||||
alloc: std.mem.Allocator,
|
||||
id: u64,
|
||||
action: []const u8,
|
||||
scanner: *std.json.Scanner,
|
||||
ctx: *Ctx,
|
||||
comptime sendFn: SendFn,
|
||||
) ![]const u8 {
|
||||
const method = std.meta.stringToEnum(BrowserMethods, action) orelse
|
||||
return error.UnknownBrowserMethod;
|
||||
return switch (method) {
|
||||
.getVersion => browserGetVersion(alloc, id, scanner, ctx, sendFn),
|
||||
.setDownloadBehavior => browserSetDownloadBehavior(alloc, id, scanner, ctx, sendFn),
|
||||
};
|
||||
}
|
||||
|
||||
const ProtocolVersion = "1.3";
|
||||
const Product = "Chrome/124.0.6367.29";
|
||||
const Revision = "@9e6ded5ac1ff5e38d930ae52bd9aec09bd1a68e4";
|
||||
const UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36";
|
||||
const JsVersion = "12.4.254.8";
|
||||
|
||||
fn browserGetVersion(
|
||||
alloc: std.mem.Allocator,
|
||||
id: u64,
|
||||
_: *std.json.Scanner,
|
||||
_: *Ctx,
|
||||
comptime _: SendFn,
|
||||
) ![]const u8 {
|
||||
const Res = struct {
|
||||
protocolVersion: []const u8,
|
||||
product: []const u8,
|
||||
revision: []const u8,
|
||||
userAgent: []const u8,
|
||||
jsVersion: []const u8,
|
||||
};
|
||||
|
||||
const res = Res{
|
||||
.protocolVersion = ProtocolVersion,
|
||||
.product = Product,
|
||||
.revision = Revision,
|
||||
.userAgent = UserAgent,
|
||||
.jsVersion = JsVersion,
|
||||
};
|
||||
return result(alloc, id, Res, res);
|
||||
}
|
||||
|
||||
fn browserSetDownloadBehavior(
|
||||
alloc: std.mem.Allocator,
|
||||
id: u64,
|
||||
scanner: *std.json.Scanner,
|
||||
_: *Ctx,
|
||||
comptime _: SendFn,
|
||||
) ![]const u8 {
|
||||
const Params = struct {
|
||||
behavior: []const u8,
|
||||
browserContextId: ?[]const u8 = null,
|
||||
downloadPath: ?[]const u8 = null,
|
||||
eventsEnabled: ?bool = null,
|
||||
};
|
||||
const params = try getParams(alloc, Params, scanner);
|
||||
std.log.debug("params {any}", .{params});
|
||||
return result(alloc, id, null, null);
|
||||
}
|
||||
87
src/cdp/cdp.zig
Normal file
87
src/cdp/cdp.zig
Normal file
@@ -0,0 +1,87 @@
|
||||
const std = @import("std");
|
||||
|
||||
const server = @import("../server.zig");
|
||||
const Ctx = server.CmdContext;
|
||||
const SendFn = server.SendFn;
|
||||
const browser = @import("browser.zig").browser;
|
||||
const target = @import("target.zig").target;
|
||||
|
||||
const Domains = enum {
|
||||
Browser,
|
||||
Target,
|
||||
};
|
||||
|
||||
// The caller is responsible for calling `free` on the returned slice.
|
||||
pub fn do(
|
||||
alloc: std.mem.Allocator,
|
||||
s: []const u8,
|
||||
ctx: *Ctx,
|
||||
comptime sendFn: SendFn,
|
||||
) ![]const u8 {
|
||||
var scanner = std.json.Scanner.initCompleteInput(alloc, s);
|
||||
defer scanner.deinit();
|
||||
|
||||
std.debug.assert(try scanner.next() == .object_begin);
|
||||
|
||||
try checkKey("id", (try scanner.next()).string);
|
||||
const id = try std.fmt.parseUnsigned(u64, (try scanner.next()).number, 10);
|
||||
|
||||
try checkKey("method", (try scanner.next()).string);
|
||||
const method = (try scanner.next()).string;
|
||||
|
||||
std.log.debug("cmd: id {any}, method {s}\n", .{ id, method });
|
||||
|
||||
var iter = std.mem.splitScalar(u8, method, '.');
|
||||
const domain = std.meta.stringToEnum(Domains, iter.first()) orelse
|
||||
return error.UnknonwDomain;
|
||||
|
||||
return switch (domain) {
|
||||
.Browser => browser(alloc, id, iter.next().?, &scanner, ctx, sendFn),
|
||||
.Target => target(alloc, id, iter.next().?, &scanner, ctx, sendFn),
|
||||
};
|
||||
}
|
||||
|
||||
// Utils
|
||||
// -----
|
||||
|
||||
fn checkKey(key: []const u8, token: []const u8) !void {
|
||||
if (!std.mem.eql(u8, key, token)) return error.WrongToken;
|
||||
}
|
||||
|
||||
const resultNull = "{{\"id\": {d}, \"result\": {{}}}}";
|
||||
|
||||
pub fn result(
|
||||
alloc: std.mem.Allocator,
|
||||
id: u64,
|
||||
comptime T: ?type,
|
||||
res: anytype,
|
||||
) ![]const u8 {
|
||||
if (T == null) return try std.fmt.allocPrint(alloc, resultNull, .{id});
|
||||
|
||||
const Resp = struct {
|
||||
id: u64,
|
||||
result: T.?,
|
||||
};
|
||||
const resp = Resp{ .id = id, .result = res };
|
||||
|
||||
var out = std.ArrayList(u8).init(alloc);
|
||||
defer out.deinit();
|
||||
|
||||
try std.json.stringify(resp, .{}, out.writer());
|
||||
const ret = try alloc.alloc(u8, out.items.len);
|
||||
@memcpy(ret, out.items);
|
||||
return ret;
|
||||
}
|
||||
|
||||
pub fn getParams(
|
||||
alloc: std.mem.Allocator,
|
||||
comptime T: type,
|
||||
scanner: *std.json.Scanner,
|
||||
) !T {
|
||||
try checkKey("params", (try scanner.next()).string);
|
||||
const options = std.json.ParseOptions{
|
||||
.max_value_len = scanner.input.len,
|
||||
.allocate = .alloc_if_needed,
|
||||
};
|
||||
return std.json.innerParse(T, alloc, scanner, options);
|
||||
}
|
||||
100
src/cdp/target.zig
Normal file
100
src/cdp/target.zig
Normal file
@@ -0,0 +1,100 @@
|
||||
const std = @import("std");
|
||||
|
||||
const server = @import("../server.zig");
|
||||
const Ctx = server.CmdContext;
|
||||
const SendFn = server.SendFn;
|
||||
const result = @import("cdp.zig").result;
|
||||
const getParams = @import("cdp.zig").getParams;
|
||||
|
||||
const TargetMethods = enum {
|
||||
setAutoAttach,
|
||||
// attachedToTarget,
|
||||
// getTargetInfo,
|
||||
};
|
||||
|
||||
pub fn target(
|
||||
alloc: std.mem.Allocator,
|
||||
id: u64,
|
||||
action: []const u8,
|
||||
scanner: *std.json.Scanner,
|
||||
ctx: *Ctx,
|
||||
comptime sendFn: SendFn,
|
||||
) ![]const u8 {
|
||||
const method = std.meta.stringToEnum(TargetMethods, action) orelse
|
||||
return error.UnknownTargetMethod;
|
||||
return switch (method) {
|
||||
.setAutoAttach => tagetSetAutoAttach(alloc, id, scanner, ctx, sendFn),
|
||||
// .getTargetInfo => tagetGetTargetInfo(alloc, id, scanner),
|
||||
};
|
||||
}
|
||||
|
||||
fn tagetSetAutoAttach(
|
||||
alloc: std.mem.Allocator,
|
||||
id: u64,
|
||||
scanner: *std.json.Scanner,
|
||||
_: *Ctx,
|
||||
comptime _: SendFn,
|
||||
) ![]const u8 {
|
||||
const Params = struct {
|
||||
autoAttach: bool,
|
||||
waitForDebuggerOnStart: bool,
|
||||
flatten: ?bool = null,
|
||||
};
|
||||
const params = try getParams(alloc, Params, scanner);
|
||||
std.log.debug("params {any}", .{params});
|
||||
return result(alloc, id, null, null);
|
||||
}
|
||||
|
||||
const TargetID = "CFCD6EC01573CF29BB638E9DC0F52DDC";
|
||||
|
||||
fn tagetGetTargetInfo(
|
||||
alloc: std.mem.Allocator,
|
||||
id: u64,
|
||||
scanner: *std.json.Scanner,
|
||||
_: *Ctx,
|
||||
comptime _: SendFn,
|
||||
) ![]const u8 {
|
||||
_ = scanner;
|
||||
|
||||
const TargetInfo = struct {
|
||||
targetId: []const u8,
|
||||
type: []const u8,
|
||||
title: []const u8,
|
||||
url: []const u8,
|
||||
attached: bool,
|
||||
canAccessOpener: bool,
|
||||
|
||||
browserContextId: ?[]const u8 = null,
|
||||
};
|
||||
const targetInfo = TargetInfo{
|
||||
.targetId = TargetID,
|
||||
.type = "page",
|
||||
};
|
||||
_ = targetInfo;
|
||||
return result(alloc, id, null, null);
|
||||
}
|
||||
|
||||
// fn tagetGetTargetInfo(
|
||||
// alloc: std.mem.Allocator,
|
||||
// id: u64,
|
||||
// scanner: *std.json.Scanner,
|
||||
// ) ![]const u8 {
|
||||
// _ = scanner;
|
||||
|
||||
// const TargetInfo = struct {
|
||||
// targetId: []const u8,
|
||||
// type: []const u8,
|
||||
// title: []const u8,
|
||||
// url: []const u8,
|
||||
// attached: bool,
|
||||
// canAccessOpener: bool,
|
||||
|
||||
// browserContextId: ?[]const u8 = null,
|
||||
// };
|
||||
// const targetInfo = TargetInfo{
|
||||
// .targetId = TargetID,
|
||||
// .type = "page",
|
||||
// };
|
||||
// _ = targetInfo;
|
||||
// return result(alloc, id, null, null);
|
||||
// }
|
||||
Reference in New Issue
Block a user