@@ -58,6 +58,7 @@ pub const Browser = struct {
58
58
allocator : Allocator ,
59
59
http_client : * http.Client ,
60
60
page_arena : ArenaAllocator ,
61
+ notification : * Notification ,
61
62
62
63
pub fn init (app : * App ) ! Browser {
63
64
const allocator = app .allocator ;
@@ -67,11 +68,15 @@ pub const Browser = struct {
67
68
});
68
69
errdefer env .deinit ();
69
70
71
+ const notification = try Notification .init (allocator , app .notification );
72
+ errdefer notification .deinit ();
73
+
70
74
return .{
71
75
.app = app ,
72
76
.env = env ,
73
77
.session = null ,
74
78
.allocator = allocator ,
79
+ .notification = notification ,
75
80
.http_client = & app .http_client ,
76
81
.page_arena = ArenaAllocator .init (allocator ),
77
82
};
@@ -81,13 +86,14 @@ pub const Browser = struct {
81
86
self .closeSession ();
82
87
self .env .deinit ();
83
88
self .page_arena .deinit ();
89
+ self .notification .deinit ();
84
90
}
85
91
86
- pub fn newSession (self : * Browser , ctx : anytype ) ! * Session {
92
+ pub fn newSession (self : * Browser ) ! * Session {
87
93
self .closeSession ();
88
94
self .session = @as (Session , undefined );
89
95
const session = & self .session .? ;
90
- try Session .init (session , self , ctx );
96
+ try Session .init (session , self );
91
97
return session ;
92
98
}
93
99
@@ -119,32 +125,14 @@ pub const Session = struct {
119
125
120
126
page : ? Page = null ,
121
127
122
- // recipient of notification, passed as the first parameter to notify
123
- notify_ctx : * anyopaque ,
124
- notify_func : * const fn (ctx : * anyopaque , notification : * const Notification ) anyerror ! void ,
125
-
126
- fn init (self : * Session , browser : * Browser , ctx : anytype ) ! void {
127
- const ContextT = @TypeOf (ctx );
128
- const ContextStruct = switch (@typeInfo (ContextT )) {
129
- .@"struct" = > ContextT ,
130
- .pointer = > | ptr | ptr .child ,
131
- .void = > NoopContext ,
132
- else = > @compileError ("invalid context type" ),
133
- };
134
-
135
- // ctx can be void, to be able to store it in our *anyopaque field, we
136
- // need to play a little game.
137
- const any_ctx : * anyopaque = if (@TypeOf (ctx ) == void ) @constCast (@ptrCast (&{})) else ctx ;
138
-
128
+ fn init (self : * Session , browser : * Browser ) ! void {
139
129
var executor = try browser .env .newExecutor ();
140
130
errdefer executor .deinit ();
141
131
142
132
const allocator = browser .app .allocator ;
143
133
self .* = .{
144
134
.browser = browser ,
145
135
.executor = executor ,
146
- .notify_ctx = any_ctx ,
147
- .notify_func = ContextStruct .notify ,
148
136
.arena = ArenaAllocator .init (allocator ),
149
137
.storage_shed = storage .Shed .init (allocator ),
150
138
.cookie_jar = storage .CookieJar .init (allocator ),
@@ -213,12 +201,6 @@ pub const Session = struct {
213
201
.reason = .anchor ,
214
202
});
215
203
}
216
-
217
- fn notify (self : * const Session , notification : * const Notification ) void {
218
- self .notify_func (self .notify_ctx , notification ) catch | err | {
219
- log .err ("notify {}: {}" , .{ std .meta .activeTag (notification .* ), err });
220
- };
221
- }
222
204
};
223
205
224
206
// Page navigates to an url.
@@ -350,20 +332,15 @@ pub const Page = struct {
350
332
// redirect)
351
333
self .url = request_url ;
352
334
353
- session .browser .app .telemetry .record (.{ .navigate = .{
354
- .proxy = false ,
355
- .tls = std .ascii .eqlIgnoreCase (request_url .scheme (), "https" ),
356
- } });
357
-
358
335
// load the data
359
336
var request = try self .newHTTPRequest (.GET , & self .url , .{ .navigation = true });
360
337
defer request .deinit ();
361
338
362
- session .notify (&.{ . page_navigate = .{
339
+ session .browser . notification . dispatch ( . page_navigate, & .{
363
340
.url = & self .url ,
364
341
.reason = opts .reason ,
365
342
.timestamp = timestamp (),
366
- } } );
343
+ });
367
344
368
345
var response = try request .sendSync (.{});
369
346
@@ -399,10 +376,10 @@ pub const Page = struct {
399
376
self .raw_data = arr .items ;
400
377
}
401
378
402
- session .notify (&.{ . page_navigated = .{
379
+ session .browser . notification . dispatch ( . page_navigated, & .{
403
380
.url = & self .url ,
404
381
.timestamp = timestamp (),
405
- } } );
382
+ });
406
383
}
407
384
408
385
// https://html.spec.whatwg.org/#read-html
0 commit comments