Skip to content

Commit d8dd94c

Browse files
Merge pull request #569 from lightpanda-io/make_cdp_less_generic
Make CDP less generic.
2 parents 8fbf559 + b0b3e92 commit d8dd94c

File tree

4 files changed

+19
-209
lines changed

4 files changed

+19
-209
lines changed

src/browser/browser.zig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ pub const Browser = struct {
5858
http_client: *http.Client,
5959
session_pool: SessionPool,
6060
page_arena: std.heap.ArenaAllocator,
61-
pub const EnvType = Env;
6261

6362
const SessionPool = std.heap.MemoryPool(Session);
6463

@@ -97,7 +96,7 @@ pub const Browser = struct {
9796
return session;
9897
}
9998

100-
fn closeSession(self: *Browser) void {
99+
pub fn closeSession(self: *Browser) void {
101100
if (self.session) |session| {
102101
session.deinit();
103102
self.session_pool.destroy(session);

src/cdp/cdp.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ const Allocator = std.mem.Allocator;
2121
const json = std.json;
2222

2323
const App = @import("../app.zig").App;
24+
const Env = @import("../browser/env.zig").Env;
2425
const asUint = @import("../str/parser.zig").asUint;
26+
const Browser = @import("../browser/browser.zig").Browser;
27+
const Session = @import("../browser/browser.zig").Session;
2528
const Incrementing = @import("../id.zig").Incrementing;
2629
const Notification = @import("../notification.zig").Notification;
2730

@@ -32,8 +35,6 @@ pub const LOADER_ID = "LOADERID24DD2FD56CF1EF33C965C79C";
3235

3336
pub const CDP = CDPT(struct {
3437
const Client = *@import("../server.zig").Client;
35-
const Browser = @import("../browser/browser.zig").Browser;
36-
const Session = @import("../browser/browser.zig").Session;
3738
});
3839

3940
const SessionIdGen = Incrementing(u32, "SID");
@@ -69,8 +70,6 @@ pub fn CDPT(comptime TypeProvider: type) type {
6970
message_arena: std.heap.ArenaAllocator,
7071

7172
const Self = @This();
72-
pub const Browser = TypeProvider.Browser;
73-
pub const Session = TypeProvider.Session;
7473

7574
pub fn init(app: *App, client: TypeProvider.Client) !Self {
7675
const allocator = app.allocator;
@@ -247,6 +246,7 @@ pub fn CDPT(comptime TypeProvider: type) type {
247246
return false;
248247
}
249248
bc.deinit();
249+
self.browser.closeSession();
250250
self.browser_context_pool.destroy(bc);
251251
self.browser_context = null;
252252
return true;
@@ -282,7 +282,7 @@ pub fn BrowserContext(comptime CDP_T: type) type {
282282
// all intents and purpose, from CDP's point of view our Browser and
283283
// our Session more or less maps to a BrowserContext. THIS HAS ZERO
284284
// RELATION TO SESSION_ID
285-
session: *CDP_T.Session,
285+
session: *Session,
286286

287287
// Points to the session arena
288288
arena: Allocator,
@@ -309,7 +309,7 @@ pub fn BrowserContext(comptime CDP_T: type) type {
309309
node_registry: Node.Registry,
310310
node_search_list: Node.Search.List,
311311

312-
isolated_world: ?IsolatedWorld(CDP_T.Browser.EnvType),
312+
isolated_world: ?IsolatedWorld(Env),
313313

314314
const Self = @This();
315315

src/cdp/domains/target.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ test "cdp.target: createTarget" {
522522
try testing.expectEqual(true, bc.target_id != null);
523523
try testing.expectEqual(
524524
\\{"isDefault":true,"type":"default","frameId":"TID-1"}
525-
, bc.session.page.?.aux_data);
525+
, bc.session.aux_data);
526526

527527
try ctx.expectSentResult(.{ .targetId = bc.target_id.? }, .{ .id = 10 });
528528
try ctx.expectSentEvent("Target.targetCreated", .{ .targetInfo = .{ .url = "about:blank", .title = "about:blank", .attached = false, .type = "page", .canAccessOpener = false, .browserContextId = "BID-9", .targetId = bc.target_id.? } }, .{});

src/cdp/testing.zig

Lines changed: 11 additions & 200 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
const std = @import("std");
2020
const json = std.json;
2121
const Allocator = std.mem.Allocator;
22+
const ArenaAllocator = std.heap.ArenaAllocator;
2223

2324
const Testing = @This();
2425

@@ -36,196 +37,6 @@ pub const expectEqualSlices = base.expectEqualSlices;
3637

3738
pub const Document = @import("../testing.zig").Document;
3839

39-
const Browser = struct {
40-
session: ?*Session = null,
41-
arena: std.heap.ArenaAllocator,
42-
env: Env,
43-
pub const EnvType = Env;
44-
45-
pub fn init(app: *App) !Browser {
46-
return .{
47-
.arena = std.heap.ArenaAllocator.init(app.allocator),
48-
.env = Env{},
49-
};
50-
}
51-
52-
pub fn deinit(self: *Browser) void {
53-
self.arena.deinit();
54-
}
55-
56-
pub fn newSession(self: *Browser, ctx: anytype) !*Session {
57-
_ = ctx;
58-
if (self.session != null) {
59-
return error.MockBrowserSessionAlreadyExists;
60-
}
61-
const arena = self.arena.allocator();
62-
const executor = arena.create(Env.Executor) catch unreachable;
63-
self.session = try arena.create(Session);
64-
self.session.?.* = .{
65-
.page = null,
66-
.arena = self.arena,
67-
.executor = executor,
68-
.inspector = .{},
69-
.state = 0,
70-
};
71-
return self.session.?;
72-
}
73-
74-
pub fn hasSession(self: *const Browser, session_id: []const u8) bool {
75-
const session = self.session orelse return false;
76-
return std.mem.eql(u8, session.id, session_id);
77-
}
78-
79-
pub fn runMicrotasks(_: *const Browser) void {}
80-
};
81-
82-
const Session = struct {
83-
page: ?Page = null,
84-
arena: std.heap.ArenaAllocator,
85-
executor: *Env.Executor,
86-
inspector: Inspector,
87-
state: i32,
88-
89-
pub fn currentPage(self: *Session) ?*Page {
90-
return &(self.page orelse return null);
91-
}
92-
93-
pub fn createPage(self: *Session, aux_data: ?[]const u8) !*Page {
94-
if (self.page != null) {
95-
return error.MockBrowserPageAlreadyExists;
96-
}
97-
self.page = .{
98-
.session = self,
99-
.url = URL.parse("https://lightpanda.io/", null) catch unreachable,
100-
.aux_data = try self.arena.allocator().dupe(u8, aux_data orelse ""),
101-
};
102-
return &self.page.?;
103-
}
104-
105-
pub fn removePage(self: *Session) void {
106-
self.page = null;
107-
}
108-
109-
pub fn callInspector(self: *Session, msg: []const u8) void {
110-
_ = self;
111-
_ = msg;
112-
}
113-
};
114-
115-
const Env = struct {
116-
pub const Executor = MockExecutor;
117-
pub fn startExecutor(self: *Env, comptime Global: type, state: anytype, module_loader: anytype, kind: anytype) !*Executor {
118-
_ = self;
119-
_ = Global;
120-
_ = state;
121-
_ = module_loader;
122-
_ = kind;
123-
return error.MockExecutor;
124-
}
125-
pub fn stopExecutor(self: *Env, executor: *Executor) void {
126-
_ = self;
127-
_ = executor;
128-
}
129-
};
130-
const MockExecutor = struct {
131-
context: Context,
132-
133-
pub fn startScope(self: *MockExecutor, global: anytype) !void {
134-
_ = self;
135-
_ = global;
136-
}
137-
pub fn endScope(self: *MockExecutor) void {
138-
_ = self;
139-
}
140-
};
141-
const Context = struct {
142-
pub fn debugContextId(self: Context) i32 {
143-
_ = self;
144-
return 0;
145-
}
146-
};
147-
148-
const Inspector = struct {
149-
pub fn getRemoteObject(
150-
self: *const Inspector,
151-
executor: *Env.Executor,
152-
group: []const u8,
153-
value: anytype,
154-
) !RemoteObject {
155-
_ = self;
156-
_ = executor;
157-
_ = group;
158-
_ = value;
159-
return RemoteObject{};
160-
}
161-
pub fn getNodePtr(self: Inspector, alloc: std.mem.Allocator, object_id: []const u8) !?*anyopaque {
162-
_ = self;
163-
_ = object_id;
164-
return try alloc.create(i32);
165-
}
166-
pub fn contextCreated(
167-
self: *const Inspector,
168-
executor: *const Env.Executor,
169-
name: []const u8,
170-
origin: []const u8,
171-
aux_data: ?[]const u8,
172-
is_default_context: bool,
173-
) void {
174-
_ = self;
175-
_ = executor;
176-
_ = name;
177-
_ = origin;
178-
_ = aux_data;
179-
_ = is_default_context;
180-
}
181-
};
182-
183-
const RemoteObject = struct {
184-
pub fn deinit(self: RemoteObject) void {
185-
_ = self;
186-
}
187-
pub fn getType(self: RemoteObject, alloc: std.mem.Allocator) ![:0]const u8 {
188-
_ = self;
189-
_ = alloc;
190-
return "TheType";
191-
}
192-
pub fn getSubtype(self: RemoteObject, alloc: std.mem.Allocator) ![:0]const u8 {
193-
_ = self;
194-
_ = alloc;
195-
return "TheSubtype";
196-
}
197-
pub fn getClassName(self: RemoteObject, alloc: std.mem.Allocator) ![:0]const u8 {
198-
_ = self;
199-
_ = alloc;
200-
return "TheClassName";
201-
}
202-
pub fn getDescription(self: RemoteObject, alloc: std.mem.Allocator) ![:0]const u8 {
203-
_ = self;
204-
_ = alloc;
205-
return "TheDescription";
206-
}
207-
pub fn getObjectId(self: RemoteObject, alloc: std.mem.Allocator) ![:0]const u8 {
208-
_ = self;
209-
_ = alloc;
210-
return "TheObjectId";
211-
}
212-
};
213-
214-
const Page = struct {
215-
session: *Session,
216-
url: ?URL = null,
217-
aux_data: []const u8 = "",
218-
doc: ?*parser.Document = null,
219-
220-
pub fn navigate(_: *Page, url: URL, opts: anytype) !void {
221-
_ = url;
222-
_ = opts;
223-
}
224-
225-
const MouseEvent = @import("../browser/browser.zig").Page.MouseEvent;
226-
pub fn mouseEvent(_: *Page, _: MouseEvent) !void {}
227-
};
228-
22940
const Client = struct {
23041
allocator: Allocator,
23142
sent: std.ArrayListUnmanaged(json.Value) = .{},
@@ -246,19 +57,22 @@ const Client = struct {
24657
const value = try json.parseFromSliceLeaky(json.Value, self.allocator, serialized, .{});
24758
try self.sent.append(self.allocator, value);
24859
}
60+
61+
pub fn sendJSONRaw(self: *Client, _: ArenaAllocator, buf: std.ArrayListUnmanaged(u8)) !void {
62+
const value = try json.parseFromSliceLeaky(json.Value, self.allocator, buf.items, .{});
63+
try self.sent.append(self.allocator, value);
64+
}
24965
};
25066

25167
const TestCDP = main.CDPT(struct {
252-
pub const Browser = Testing.Browser;
253-
pub const Session = Testing.Session;
25468
pub const Client = *Testing.Client;
25569
});
25670

25771
const TestContext = struct {
25872
app: *App,
25973
client: ?Client = null,
26074
cdp_: ?TestCDP = null,
261-
arena: std.heap.ArenaAllocator,
75+
arena: ArenaAllocator,
26276

26377
pub fn deinit(self: *TestContext) void {
26478
if (self.cdp_) |*c| {
@@ -273,7 +87,7 @@ const TestContext = struct {
27387
self.client = Client.init(self.arena.allocator());
27488
// Don't use the arena here. We want to detect leaks in CDP.
27589
// The arena is only for test-specific stuff
276-
self.cdp_ = try TestCDP.init(self.app, &self.client.?);
90+
self.cdp_ = TestCDP.init(self.app, &self.client.?) catch unreachable;
27791
}
27892
return &self.cdp_.?;
27993
}
@@ -286,11 +100,8 @@ const TestContext = struct {
286100
};
287101
pub fn loadBrowserContext(self: *TestContext, opts: BrowserContextOpts) !*main.BrowserContext(TestCDP) {
288102
var c = self.cdp();
289-
c.browser.session = null;
290-
291103
if (c.browser_context) |bc| {
292-
bc.deinit();
293-
c.browser_context = null;
104+
_ = c.disposeBrowserContext(bc.id);
294105
}
295106

296107
_ = try c.createBrowserContext();
@@ -410,7 +221,7 @@ const TestContext = struct {
410221
pub fn context() TestContext {
411222
return .{
412223
.app = App.init(std.testing.allocator, .{ .run_mode = .serve }) catch unreachable,
413-
.arena = std.heap.ArenaAllocator.init(std.testing.allocator),
224+
.arena = ArenaAllocator.init(std.testing.allocator),
414225
};
415226
}
416227

@@ -420,7 +231,7 @@ pub fn context() TestContext {
420231
// json and check if the two are equal.
421232
// Except serializing to JSON isn't deterministic.
422233
// So we serialize the JSON then we deserialize to json.Value. And then we can
423-
// compare our anytype expection with the json.Value that we captured
234+
// compare our anytype expectation with the json.Value that we captured
424235

425236
fn compareExpectedToSent(expected: []const u8, actual: json.Value) !bool {
426237
const expected_value = try std.json.parseFromSlice(json.Value, std.testing.allocator, expected, .{});

0 commit comments

Comments
 (0)