|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.Linq; |
3 | 4 | using System.Threading.Tasks; |
4 | 5 | using Newtonsoft.Json.Linq; |
5 | 6 | using PuppeteerSharp.Input; |
@@ -39,6 +40,7 @@ namespace PuppeteerSharp |
39 | 40 | public class Frame |
40 | 41 | { |
41 | 42 | private readonly CDPSession _client; |
| 43 | + private readonly List<Frame> _childFrames = new List<Frame>(); |
42 | 44 |
|
43 | 45 | internal string Id { get; set; } |
44 | 46 |
|
@@ -66,15 +68,24 @@ internal Frame(FrameManager frameManager, CDPSession client, Frame parentFrame, |
66 | 68 |
|
67 | 69 | if (parentFrame != null) |
68 | 70 | { |
69 | | - ParentFrame.ChildFrames.Add(this); |
| 71 | + ParentFrame.AddChildFrame(this); |
70 | 72 | } |
71 | 73 | } |
72 | 74 |
|
73 | 75 | #region Properties |
74 | 76 | /// <summary> |
75 | 77 | /// Gets the child frames of the this frame |
76 | 78 | /// </summary> |
77 | | - public List<Frame> ChildFrames { get; } = new List<Frame>(); |
| 79 | + public List<Frame> ChildFrames |
| 80 | + { |
| 81 | + get |
| 82 | + { |
| 83 | + lock (_childFrames) |
| 84 | + { |
| 85 | + return _childFrames.ToList(); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
78 | 89 |
|
79 | 90 | /// <summary> |
80 | 91 | /// Gets the frame's name attribute as specified in the tag |
@@ -495,6 +506,22 @@ public Task ClickAsync(string selector, ClickOptions options = null) |
495 | 506 | public Task TypeAsync(string selector, string text, TypeOptions options = null) |
496 | 507 | => SecondaryWorld.TypeAsync(selector, text, options); |
497 | 508 |
|
| 509 | + internal void AddChildFrame(Frame frame) |
| 510 | + { |
| 511 | + lock (_childFrames) |
| 512 | + { |
| 513 | + _childFrames.Add(frame); |
| 514 | + } |
| 515 | + } |
| 516 | + |
| 517 | + internal void RemoveChildFrame(Frame frame) |
| 518 | + { |
| 519 | + lock (_childFrames) |
| 520 | + { |
| 521 | + _childFrames.Remove(frame); |
| 522 | + } |
| 523 | + } |
| 524 | + |
498 | 525 | internal void OnLoadingStopped() |
499 | 526 | { |
500 | 527 | LifecycleEvents.Add("DOMContentLoaded"); |
@@ -527,7 +554,7 @@ internal void Detach() |
527 | 554 | SecondaryWorld.Detach(); |
528 | 555 | if (ParentFrame != null) |
529 | 556 | { |
530 | | - ParentFrame.ChildFrames.Remove(this); |
| 557 | + ParentFrame.RemoveChildFrame(this); |
531 | 558 | } |
532 | 559 | ParentFrame = null; |
533 | 560 | } |
|
0 commit comments