@@ -340,6 +340,8 @@ pub fn BrowserContext(comptime CDP_T: type) type {
340340 self .node_search_list = Node .Search .List .init (allocator , & self .node_registry );
341341 errdefer self .deinit ();
342342
343+ try cdp .browser .notification .register (.page_remove , self , onPageRemove );
344+ try cdp .browser .notification .register (.page_created , self , onPageCreated );
343345 try cdp .browser .notification .register (.page_navigate , self , onPageNavigate );
344346 try cdp .browser .notification .register (.page_navigated , self , onPageNavigated );
345347 }
@@ -365,7 +367,7 @@ pub fn BrowserContext(comptime CDP_T: type) type {
365367 self .node_search_list .reset ();
366368 }
367369
368- pub fn createIsolatedWorld (self : * Self , page : * Page ) ! void {
370+ pub fn createIsolatedWorld (self : * Self , page : * Page , world_name : [] const u8 , grant_universal_access : bool ) ! * IsolatedWorld {
369371 if (self .isolated_world != null ) {
370372 return error .CurrentlyOnly1IsolatedWorldSupported ;
371373 }
@@ -374,19 +376,14 @@ pub fn BrowserContext(comptime CDP_T: type) type {
374376 errdefer executor .deinit ();
375377
376378 self .isolated_world = .{
377- .name = "" ,
379+ .name = try self . arena . dupe ( u8 , world_name ) ,
378380 .scope = undefined ,
379381 .executor = executor ,
380- .grant_universal_access = true ,
382+ .grant_universal_access = grant_universal_access ,
381383 };
382- var world = & self .isolated_world .? ;
383-
384- // The isolate world must share at least some of the state with the related page, specifically the DocumentHTML
385- // (assuming grantUniveralAccess will be set to True!).
386- // We just created the world and the page. The page's state lives in the session, but is update on navigation.
387- // This also means this pointer becomes invalid after removePage untill a new page is created.
388- // Currently we have only 1 page/frame and thus also only 1 state in the isolate world.
389- world .scope = try world .executor .startScope (& page .window , & page .state , {}, false );
384+ const world = & self .isolated_world .? ;
385+ try world .createContext (page );
386+ return world ;
390387 }
391388
392389 pub fn nodeWriter (self : * Self , node : * const Node , opts : Node.Writer.Opts ) Node.Writer {
@@ -403,6 +400,16 @@ pub fn BrowserContext(comptime CDP_T: type) type {
403400 return if (raw_url .len == 0 ) null else raw_url ;
404401 }
405402
403+ pub fn onPageRemove (ctx : * anyopaque , _ : Notification.PageRemove ) ! void {
404+ const self : * Self = @alignCast (@ptrCast (ctx ));
405+ return @import ("domains/page.zig" ).pageRemove (self );
406+ }
407+
408+ pub fn onPageCreated (ctx : * anyopaque , page : * Page ) ! void {
409+ const self : * Self = @alignCast (@ptrCast (ctx ));
410+ return @import ("domains/page.zig" ).pageCreated (self , page );
411+ }
412+
406413 pub fn onPageNavigate (ctx : * anyopaque , data : * const Notification.PageNavigate ) ! void {
407414 const self : * Self = @alignCast (@ptrCast (ctx ));
408415 return @import ("domains/page.zig" ).pageNavigate (self , data );
@@ -506,12 +513,26 @@ pub fn BrowserContext(comptime CDP_T: type) type {
506513/// An object id is unique across all contexts, different object ids can refer to the same Node in different contexts.
507514const IsolatedWorld = struct {
508515 name : []const u8 ,
509- scope : * Env.Scope ,
516+ scope : ? * Env.Scope ,
510517 executor : Env.Executor ,
511518 grant_universal_access : bool ,
512519
513520 pub fn deinit (self : * IsolatedWorld ) void {
514521 self .executor .deinit ();
522+ self .scope = null ;
523+ }
524+ pub fn removeContext (self : * IsolatedWorld ) void {
525+ self .executor .endScope ();
526+ self .scope = null ;
527+ }
528+
529+ // The isolate world must share at least some of the state with the related page, specifically the DocumentHTML
530+ // (assuming grantUniveralAccess will be set to True!).
531+ // We just created the world and the page. The page's state lives in the session, but is update on navigation.
532+ // This also means this pointer becomes invalid after removePage untill a new page is created.
533+ // Currently we have only 1 page/frame and thus also only 1 state in the isolate world.
534+ pub fn createContext (self : * IsolatedWorld , page : * Page ) ! void {
535+ self .scope = try self .executor .startScope (& page .window , & page .state , {}, false );
515536 }
516537};
517538
0 commit comments