forked from microsoft/playwright-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrowserContext.cs
947 lines (817 loc) · 37.7 KB
/
BrowserContext.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
/*
* MIT License
*
* Copyright (c) Microsoft Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Playwright.Helpers;
using Microsoft.Playwright.Transport;
using Microsoft.Playwright.Transport.Channels;
using Microsoft.Playwright.Transport.Protocol;
namespace Microsoft.Playwright.Core;
internal class BrowserContext : ChannelOwner, IBrowserContext
{
private readonly TaskCompletionSource<bool> _closeTcs = new();
private readonly Dictionary<string, Delegate> _bindings = new();
private readonly BrowserContextInitializer _initializer;
private readonly Tracing _tracing;
private readonly Clock _clock;
internal readonly HashSet<IPage> _backgroundPages = new();
internal readonly APIRequestContext _request;
private readonly Dictionary<string, HarRecorder> _harRecorders = new();
internal readonly List<IWorker> _serviceWorkers = new();
private readonly List<WebSocketRouteHandler> _webSocketRoutes = new();
private List<RouteHandler> _routes = new();
internal readonly List<Page> _pages = new();
private readonly Browser _browser;
private readonly List<HarRouter> _harRouters = new();
private string _closeReason;
internal TimeoutSettings _timeoutSettings = new();
internal BrowserContext(ChannelOwner parent, string guid, BrowserContextInitializer initializer) : base(parent, guid)
{
_browser = parent as Browser;
_browser?._contexts.Add(this);
_tracing = initializer.Tracing;
_clock = new Clock(this);
_request = initializer.RequestContext;
_initializer = initializer;
}
private event EventHandler<IRequest> _requestImpl;
private event EventHandler<IResponse> _responseImpl;
private event EventHandler<IRequest> _requestFinishedImpl;
private event EventHandler<IRequest> _requestFailedImpl;
private event EventHandler<IConsoleMessage> _consoleImpl;
private event EventHandler<IDialog> _dialogImpl;
public event EventHandler<IBrowserContext> Close;
public event EventHandler<IConsoleMessage> Console
{
add => this._consoleImpl = UpdateEventHandler("console", this._consoleImpl, value, true);
remove => this._consoleImpl = UpdateEventHandler("console", this._consoleImpl, value, false);
}
public event EventHandler<IDialog> Dialog
{
add => this._dialogImpl = UpdateEventHandler("dialog", this._dialogImpl, value, true);
remove => this._dialogImpl = UpdateEventHandler("dialog", this._dialogImpl, value, false);
}
public event EventHandler<IPage> Page;
public event EventHandler<IPage> BackgroundPage;
public event EventHandler<IWebError> WebError;
public event EventHandler<IRequest> Request
{
add => this._requestImpl = UpdateEventHandler("request", this._requestImpl, value, true);
remove => this._requestImpl = UpdateEventHandler("request", this._requestImpl, value, false);
}
public event EventHandler<IResponse> Response
{
add => this._responseImpl = UpdateEventHandler("response", this._responseImpl, value, true);
remove => this._responseImpl = UpdateEventHandler("response", this._responseImpl, value, false);
}
public event EventHandler<IRequest> RequestFinished
{
add => this._requestFinishedImpl = UpdateEventHandler("requestFinished", this._requestFinishedImpl, value, true);
remove => this._requestFinishedImpl = UpdateEventHandler("requestFinished", this._requestFinishedImpl, value, false);
}
public event EventHandler<IRequest> RequestFailed
{
add => this._requestFailedImpl = UpdateEventHandler("requestFailed", this._requestFailedImpl, value, true);
remove => this._requestFailedImpl = UpdateEventHandler("requestFailed", this._requestFailedImpl, value, false);
}
public event EventHandler<IWorker> ServiceWorker;
public ITracing Tracing => _tracing;
public IClock Clock => _clock;
public IBrowser Browser => _browser;
public IReadOnlyList<IPage> Pages => _pages;
internal Page OwnerPage { get; set; }
internal bool IsChromium => _initializer.IsChromium;
internal BrowserNewContextOptions Options { get; set; }
internal bool CloseWasCalled { get; private set; }
public IAPIRequestContext APIRequest => _request;
public IReadOnlyList<IWorker> ServiceWorkers => _serviceWorkers;
public IReadOnlyList<IPage> BackgroundPages => _backgroundPages.ToList();
internal override void OnMessage(string method, JsonElement? serverParams)
{
switch (method)
{
case "close":
OnClose();
break;
case "backgroundPage":
{
var page = serverParams?.GetProperty("page").ToObject<Page>(_connection.DefaultJsonSerializerOptions);
_backgroundPages.Add(page);
BackgroundPage?.Invoke(this, page);
break;
}
case "bindingCall":
Channel_BindingCall(
serverParams?.GetProperty("binding").ToObject<BindingCall>(_connection.DefaultJsonSerializerOptions));
break;
case "dialog":
OnDialog(serverParams?.GetProperty("dialog").ToObject<Dialog>(_connection.DefaultJsonSerializerOptions));
break;
case "console":
var consoleMessage = new ConsoleMessage(serverParams?.ToObject<BrowserContextConsoleEvent>(_connection.DefaultJsonSerializerOptions));
_consoleImpl?.Invoke(this, consoleMessage);
if (consoleMessage.Page != null)
{
(consoleMessage.Page as Page).FireConsole(consoleMessage);
}
break;
case "route":
var route = serverParams?.GetProperty("route").ToObject<Route>(_connection.DefaultJsonSerializerOptions);
Channel_Route(this, route);
break;
case "webSocketRoute":
var webSocketRoute = serverParams?.GetProperty("webSocketRoute").ToObject<WebSocketRoute>(_connection.DefaultJsonSerializerOptions);
_ = OnWebSocketRouteAsync(webSocketRoute).ConfigureAwait(false);
break;
case "page":
Channel_OnPage(
this,
serverParams?.GetProperty("page").ToObject<Page>(_connection.DefaultJsonSerializerOptions));
break;
case "pageError":
{
var error = serverParams?.GetProperty("error").ToObject<SerializedError>(_connection.DefaultJsonSerializerOptions);
var pageObject = serverParams?.GetProperty("page").ToObject<Page>(_connection.DefaultJsonSerializerOptions);
var parsedError = string.IsNullOrEmpty(error.Error.Stack) ? $"{error.Error.Name}: {error.Error.Message}" : error.Error.Stack;
WebError?.Invoke(this, new WebError(pageObject, parsedError));
pageObject?.FirePageError(parsedError);
break;
}
case "serviceWorker":
{
var serviceWorker = serverParams?.GetProperty("worker").ToObject<Worker>(_connection.DefaultJsonSerializerOptions);
((Worker)serviceWorker).Context = this;
_serviceWorkers.Add(serviceWorker);
ServiceWorker?.Invoke(this, serviceWorker);
break;
}
case "request":
{
var e = serverParams?.ToObject<BrowserContextChannelRequestEventArgs>(_connection.DefaultJsonSerializerOptions);
_requestImpl?.Invoke(this, e.Request);
e.Page?.FireRequest(e.Request);
break;
}
case "requestFinished":
{
var e = serverParams?.ToObject<BrowserContextChannelRequestEventArgs>(_connection.DefaultJsonSerializerOptions);
e.Request.SetResponseEndTiming(e.ResponseEndTiming);
e.Request.Sizes = e.RequestSizes;
_requestFinishedImpl?.Invoke(this, e.Request);
e.Page?.FireRequestFinished(e.Request);
e.Response?.ReportFinished();
break;
}
case "requestFailed":
{
var e = serverParams?.ToObject<BrowserContextChannelRequestEventArgs>(_connection.DefaultJsonSerializerOptions);
e.Request.Failure = e.FailureText;
e.Request.SetResponseEndTiming(e.ResponseEndTiming);
_requestFailedImpl?.Invoke(this, e.Request);
e.Page?.FireRequestFailed(e.Request);
e.Response?.ReportFinished(e.FailureText);
}
break;
case "response":
{
var e = serverParams?.ToObject<BrowserContextChannelResponseEventArgs>(_connection.DefaultJsonSerializerOptions);
_responseImpl?.Invoke(this, e.Response);
e.Page?.FireResponse(e.Response);
}
break;
}
}
internal void OnDialog(IDialog dialog)
{
bool hasListeners = _dialogImpl?.GetInvocationList().Length > 0 || ((dialog?.Page as Page)?.HasDialogListenersAttached() ?? false);
if (!hasListeners)
{
// Although we do similar handling on the server side, we still need this logic
// on the client side due to a possible race condition between two async calls:
// a) removing "dialog" listener subscription (client->server)
// b) actual "dialog" event (server->client)
if ("beforeunload".Equals(dialog.Type, StringComparison.Ordinal))
{
dialog.AcceptAsync().IgnoreException();
}
else
{
dialog.DismissAsync().IgnoreException();
}
}
else
{
_dialogImpl?.Invoke(this, dialog);
(dialog.Page as Page)?.FireDialog(dialog);
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
public Task AddCookiesAsync(IEnumerable<Cookie> cookies) => SendMessageToServerAsync(
"addCookies",
new Dictionary<string, object>
{
["cookies"] = cookies,
});
[MethodImpl(MethodImplOptions.NoInlining)]
public Task AddInitScriptAsync(string script = null, string scriptPath = null)
{
if (string.IsNullOrEmpty(script))
{
script = ScriptsHelper.EvaluationScript(script, scriptPath, true);
}
return SendMessageToServerAsync(
"addInitScript",
new Dictionary<string, object>
{
["source"] = script,
});
}
[MethodImpl(MethodImplOptions.NoInlining)]
public async Task ClearCookiesAsync(BrowserContextClearCookiesOptions options = default)
{
var @params = new Dictionary<string, object>
{
["name"] = options?.Name ?? options?.NameString,
["nameRegexSource"] = options?.NameRegex?.ToString(),
["nameRegexFlags"] = options?.NameRegex?.Options.GetInlineFlags(),
["domain"] = options?.Domain ?? options?.DomainString,
["domainRegexSource"] = options?.DomainRegex?.ToString(),
["domainRegexFlags"] = options?.DomainRegex?.Options.GetInlineFlags(),
["path"] = options?.Path ?? options?.PathString,
["pathRegexSource"] = options?.PathRegex?.ToString(),
["pathRegexFlags"] = options?.PathRegex?.Options.GetInlineFlags(),
};
await SendMessageToServerAsync("clearCookies", @params).ConfigureAwait(false);
}
[MethodImpl(MethodImplOptions.NoInlining)]
public Task ClearPermissionsAsync() => SendMessageToServerAsync("clearPermissions");
[MethodImpl(MethodImplOptions.NoInlining)]
public async Task CloseAsync(BrowserContextCloseOptions options = default)
{
if (CloseWasCalled)
{
return;
}
_closeReason = options?.Reason;
CloseWasCalled = true;
await WrapApiCallAsync(
async () =>
{
await _request.DisposeAsync(options?.Reason).ConfigureAwait(false);
},
true).ConfigureAwait(false);
await WrapApiCallAsync(
async () =>
{
foreach (var harRecorder in _harRecorders)
{
Artifact artifact = (await SendMessageToServerAsync(
"harExport",
new Dictionary<string, object>
{
["harId"] = harRecorder.Key,
}).ConfigureAwait(false)).GetObject<Artifact>("artifact", _connection);
// Server side will compress artifact if content is attach or if file is .zip.
var isCompressed = harRecorder.Value.Content == HarContentPolicy.Attach || harRecorder.Value.Path.EndsWith(".zip", StringComparison.Ordinal);
var needCompressed = harRecorder.Value.Path.EndsWith(".zip", StringComparison.Ordinal);
if (isCompressed && !needCompressed)
{
await artifact.SaveAsAsync(harRecorder.Value.Path + ".tmp").ConfigureAwait(false);
await _connection.LocalUtils.HarUnzipAsync(harRecorder.Value.Path + ".tmp", harRecorder.Value.Path).ConfigureAwait(false);
}
else
{
await artifact.SaveAsAsync(harRecorder.Value.Path).ConfigureAwait(false);
}
await artifact.DeleteAsync().ConfigureAwait(false);
}
},
true).ConfigureAwait(false);
await SendMessageToServerAsync("close", new Dictionary<string, object>
{
["reason"] = options?.Reason,
}).ConfigureAwait(false);
await _closeTcs.Task.ConfigureAwait(false);
}
internal void SetOptions(BrowserNewContextOptions contextOptions, string tracesDir)
{
Options = contextOptions;
if (!string.IsNullOrEmpty(Options?.RecordHarPath))
{
_harRecorders.Add(string.Empty, new() { Path = Options.RecordHarPath, Content = Options.RecordHarContent });
}
_tracing._tracesDir = tracesDir;
}
[MethodImpl(MethodImplOptions.NoInlining)]
public async Task<IReadOnlyList<BrowserContextCookiesResult>> CookiesAsync(IEnumerable<string> urls = null) => (await SendMessageToServerAsync(
"cookies",
new Dictionary<string, object>
{
["urls"] = urls?.ToArray() ?? Array.Empty<string>(),
}).ConfigureAwait(false))?.GetProperty("cookies").ToObject<IReadOnlyList<BrowserContextCookiesResult>>();
[MethodImpl(MethodImplOptions.NoInlining)]
public Task ExposeBindingAsync(string name, Action callback, BrowserContextExposeBindingOptions options = default)
#pragma warning disable CS0612 // Type or member is obsolete
=> ExposeBindingAsync(name, callback, handle: options?.Handle ?? false);
#pragma warning restore CS0612 // Type or member is obsolete
[MethodImpl(MethodImplOptions.NoInlining)]
public Task ExposeBindingAsync(string name, Action<BindingSource> callback)
=> ExposeBindingAsync(name, (Delegate)callback);
[MethodImpl(MethodImplOptions.NoInlining)]
public Task ExposeBindingAsync<T>(string name, Action<BindingSource, T> callback)
=> ExposeBindingAsync(name, (Delegate)callback);
[MethodImpl(MethodImplOptions.NoInlining)]
public Task ExposeBindingAsync<TResult>(string name, Func<BindingSource, TResult> callback)
=> ExposeBindingAsync(name, (Delegate)callback);
[MethodImpl(MethodImplOptions.NoInlining)]
public Task ExposeBindingAsync<TResult>(string name, Func<BindingSource, IJSHandle, TResult> callback)
=> ExposeBindingAsync(name, callback, true);
[MethodImpl(MethodImplOptions.NoInlining)]
public Task ExposeBindingAsync<T, TResult>(string name, Func<BindingSource, T, TResult> callback)
=> ExposeBindingAsync(name, (Delegate)callback);
[MethodImpl(MethodImplOptions.NoInlining)]
public Task ExposeBindingAsync<T1, T2, TResult>(string name, Func<BindingSource, T1, T2, TResult> callback)
=> ExposeBindingAsync(name, (Delegate)callback);
[MethodImpl(MethodImplOptions.NoInlining)]
public Task ExposeBindingAsync<T1, T2, T3, TResult>(string name, Func<BindingSource, T1, T2, T3, TResult> callback)
=> ExposeBindingAsync(name, (Delegate)callback);
[MethodImpl(MethodImplOptions.NoInlining)]
public Task ExposeBindingAsync<T1, T2, T3, T4, TResult>(string name, Func<BindingSource, T1, T2, T3, T4, TResult> callback)
=> ExposeBindingAsync(name, (Delegate)callback);
[MethodImpl(MethodImplOptions.NoInlining)]
public Task ExposeFunctionAsync(string name, Action callback)
=> ExposeBindingAsync(name, (BindingSource _) => callback());
[MethodImpl(MethodImplOptions.NoInlining)]
public Task ExposeFunctionAsync<T>(string name, Action<T> callback)
=> ExposeBindingAsync(name, (BindingSource _, T t) => callback(t));
[MethodImpl(MethodImplOptions.NoInlining)]
public Task ExposeFunctionAsync<TResult>(string name, Func<TResult> callback)
=> ExposeBindingAsync(name, (BindingSource _) => callback());
[MethodImpl(MethodImplOptions.NoInlining)]
public Task ExposeFunctionAsync<T, TResult>(string name, Func<T, TResult> callback)
=> ExposeBindingAsync(name, (BindingSource _, T t) => callback(t));
[MethodImpl(MethodImplOptions.NoInlining)]
public Task ExposeFunctionAsync<T1, T2, TResult>(string name, Func<T1, T2, TResult> callback)
=> ExposeBindingAsync(name, (BindingSource _, T1 t1, T2 t2) => callback(t1, t2));
[MethodImpl(MethodImplOptions.NoInlining)]
public Task ExposeFunctionAsync<T1, T2, T3, TResult>(string name, Func<T1, T2, T3, TResult> callback)
=> ExposeBindingAsync(name, (BindingSource _, T1 t1, T2 t2, T3 t3) => callback(t1, t2, t3));
[MethodImpl(MethodImplOptions.NoInlining)]
public Task ExposeFunctionAsync<T1, T2, T3, T4, TResult>(string name, Func<T1, T2, T3, T4, TResult> callback)
=> ExposeBindingAsync(name, (BindingSource _, T1 t1, T2 t2, T3 t3, T4 t4) => callback(t1, t2, t3, t4));
[MethodImpl(MethodImplOptions.NoInlining)]
public Task GrantPermissionsAsync(IEnumerable<string> permissions, BrowserContextGrantPermissionsOptions options = default)
=> SendMessageToServerAsync("grantPermissions", new Dictionary<string, object>
{
["permissions"] = permissions?.ToArray(),
["origin"] = options?.Origin,
});
[MethodImpl(MethodImplOptions.NoInlining)]
public async Task<ICDPSession> NewCDPSessionAsync(IPage page)
=> await SendMessageToServerAsync<CDPSession>(
"newCDPSession",
new Dictionary<string, object>
{
["page"] = new { guid = (page as Page).Guid },
}).ConfigureAwait(false);
[MethodImpl(MethodImplOptions.NoInlining)]
public async Task<ICDPSession> NewCDPSessionAsync(IFrame frame)
=> await SendMessageToServerAsync<CDPSession>(
"newCDPSession",
new Dictionary<string, object>
{
["frame"] = new { guid = (frame as Frame).Guid },
}).ConfigureAwait(false);
[MethodImpl(MethodImplOptions.NoInlining)]
public async Task<IPage> NewPageAsync()
{
if (OwnerPage != null)
{
throw new PlaywrightException("Please use Browser.NewContextAsync()");
}
return await SendMessageToServerAsync<Page>("newPage").ConfigureAwait(false);
}
[MethodImpl(MethodImplOptions.NoInlining)]
public Task RouteAsync(string globMatch, Func<IRoute, Task> handler, BrowserContextRouteOptions options = null)
=> RouteAsync(globMatch, null, null, handler, options);
[MethodImpl(MethodImplOptions.NoInlining)]
public Task RouteAsync(string globMatch, Action<IRoute> handler, BrowserContextRouteOptions options = null)
=> RouteAsync(globMatch, null, null, handler, options);
[MethodImpl(MethodImplOptions.NoInlining)]
public Task RouteAsync(Regex reMatch, Action<IRoute> handler, BrowserContextRouteOptions options = null)
=> RouteAsync(null, reMatch, null, handler, options);
[MethodImpl(MethodImplOptions.NoInlining)]
public Task RouteAsync(Regex reMatch, Func<IRoute, Task> handler, BrowserContextRouteOptions options = null)
=> RouteAsync(null, reMatch, null, handler, options);
[MethodImpl(MethodImplOptions.NoInlining)]
public Task RouteAsync(Func<string, bool> funcMatch, Action<IRoute> handler, BrowserContextRouteOptions options = null)
=> RouteAsync(null, null, funcMatch, handler, options);
[MethodImpl(MethodImplOptions.NoInlining)]
public Task RouteAsync(Func<string, bool> funcMatch, Func<IRoute, Task> handler, BrowserContextRouteOptions options = null)
=> RouteAsync(null, null, funcMatch, handler, options);
[MethodImpl(MethodImplOptions.NoInlining)]
public Task SetExtraHTTPHeadersAsync(IEnumerable<KeyValuePair<string, string>> headers)
=> SendMessageToServerAsync(
"setExtraHTTPHeaders",
new Dictionary<string, object>
{
["headers"] = headers.Select(kv => new HeaderEntry { Name = kv.Key, Value = kv.Value }),
});
[MethodImpl(MethodImplOptions.NoInlining)]
public Task SetGeolocationAsync(Geolocation geolocation) => SendMessageToServerAsync(
"setGeolocation",
new Dictionary<string, object>
{
["geolocation"] = geolocation,
});
[MethodImpl(MethodImplOptions.NoInlining)]
public Task SetOfflineAsync(bool offline) => SendMessageToServerAsync(
"setOffline",
new Dictionary<string, object>
{
["offline"] = offline,
});
[MethodImpl(MethodImplOptions.NoInlining)]
public async Task<string> StorageStateAsync(BrowserContextStorageStateOptions options = default)
{
string state = JsonSerializer.Serialize(
await SendMessageToServerAsync<object>("storageState").ConfigureAwait(false),
JsonExtensions.DefaultJsonSerializerOptions);
if (!string.IsNullOrEmpty(options?.Path))
{
File.WriteAllText(options?.Path, state);
}
return state;
}
[MethodImpl(MethodImplOptions.NoInlining)]
public async Task UnrouteAllAsync(BrowserContextUnrouteAllOptions options = default)
{
await UnrouteInternalAsync(_routes, new(), options?.Behavior).ConfigureAwait(false);
DisposeHarRouters();
}
[MethodImpl(MethodImplOptions.NoInlining)]
public Task UnrouteAsync(string globMatch, Action<IRoute> handler)
=> UnrouteAsync(globMatch, null, null, handler);
[MethodImpl(MethodImplOptions.NoInlining)]
public Task UnrouteAsync(string globMatch, Func<IRoute, Task> handler)
=> UnrouteAsync(globMatch, null, null, handler);
[MethodImpl(MethodImplOptions.NoInlining)]
public Task UnrouteAsync(Regex reMatch, Action<IRoute> handler)
=> UnrouteAsync(null, reMatch, null, handler);
[MethodImpl(MethodImplOptions.NoInlining)]
public Task UnrouteAsync(Regex reMatch, Func<IRoute, Task> handler)
=> UnrouteAsync(null, reMatch, null, handler);
[MethodImpl(MethodImplOptions.NoInlining)]
public Task UnrouteAsync(Func<string, bool> funcMatch, Action<IRoute> handler)
=> UnrouteAsync(null, null, funcMatch, handler);
[MethodImpl(MethodImplOptions.NoInlining)]
public Task UnrouteAsync(Func<string, bool> funcMatch, Func<IRoute, Task> handler)
=> UnrouteAsync(null, null, funcMatch, handler);
internal string _effectiveCloseReason()
{
return _closeReason ?? _browser._closeReason;
}
internal async Task<T> InnerWaitForEventAsync<T>(PlaywrightEvent<T> playwrightEvent, Func<Task> action = default, Func<T, bool> predicate = default, float? timeout = default)
{
if (playwrightEvent == null)
{
throw new ArgumentException("Page event is required", nameof(playwrightEvent));
}
timeout = _timeoutSettings.Timeout(timeout);
using var waiter = new Waiter(this, $"context.WaitForEventAsync(\"{playwrightEvent.Name}\")");
waiter.RejectOnTimeout(Convert.ToInt32(timeout, CultureInfo.InvariantCulture), $"Timeout {timeout}ms exceeded while waiting for event \"{playwrightEvent.Name}\"");
if (playwrightEvent.Name != BrowserContextEvent.Close.Name)
{
waiter.RejectOnEvent<IBrowserContext>(this, BrowserContextEvent.Close.Name, () => new TargetClosedException(_effectiveCloseReason()));
}
var result = waiter.WaitForEventAsync(this, playwrightEvent.Name, predicate);
if (action != null)
{
await WrapApiBoundaryAsync(() => waiter.CancelWaitOnExceptionAsync(result, action)).ConfigureAwait(false);
}
return await result.ConfigureAwait(false);
}
[MethodImpl(MethodImplOptions.NoInlining)]
public Task<IPage> WaitForPageAsync(BrowserContextWaitForPageOptions options = default)
=> InnerWaitForEventAsync(BrowserContextEvent.Page, null, options?.Predicate, options?.Timeout);
[MethodImpl(MethodImplOptions.NoInlining)]
public Task<IPage> RunAndWaitForPageAsync(Func<Task> action, BrowserContextRunAndWaitForPageOptions options = default)
=> InnerWaitForEventAsync(BrowserContextEvent.Page, action, options?.Predicate, options?.Timeout);
[MethodImpl(MethodImplOptions.NoInlining)]
public Task<IConsoleMessage> WaitForConsoleMessageAsync(BrowserContextWaitForConsoleMessageOptions options = default)
=> InnerWaitForEventAsync(PageEvent.Console, null, options?.Predicate, options?.Timeout);
[MethodImpl(MethodImplOptions.NoInlining)]
public Task<IConsoleMessage> RunAndWaitForConsoleMessageAsync(Func<Task> action, BrowserContextRunAndWaitForConsoleMessageOptions options = default)
=> InnerWaitForEventAsync(PageEvent.Console, action, options?.Predicate, options?.Timeout);
[MethodImpl(MethodImplOptions.NoInlining)]
public ValueTask DisposeAsync() => new(CloseAsync());
[MethodImpl(MethodImplOptions.NoInlining)]
public void SetDefaultNavigationTimeout(float timeout) => SetDefaultNavigationTimeoutImpl(timeout);
internal void SetDefaultNavigationTimeoutImpl(float? timeout)
{
_timeoutSettings.SetDefaultNavigationTimeout(timeout);
WrapApiCallAsync(
() => SendMessageToServerAsync(
"setDefaultNavigationTimeoutNoReply",
new Dictionary<string, object>
{
["timeout"] = timeout,
}),
true).IgnoreException();
}
[MethodImpl(MethodImplOptions.NoInlining)]
public void SetDefaultTimeout(float timeout) => SetDefaultTimeoutImpl(timeout);
internal void SetDefaultTimeoutImpl(float? timeout)
{
_timeoutSettings.SetDefaultTimeout(timeout);
WrapApiCallAsync(
() => SendMessageToServerAsync(
"setDefaultTimeoutNoReply",
new Dictionary<string, object>
{
["timeout"] = timeout,
}),
true).IgnoreException();
}
internal async Task OnRouteAsync(Route route)
{
route._context = this;
var page = route._request.SafePage;
var routeHandlers = _routes.ToArray();
foreach (var routeHandler in routeHandlers)
{
// If the page or the context was closed we stall all requests right away.
if (page?.CloseWasCalled == true || CloseWasCalled)
{
return;
}
if (!routeHandler.Matches(route.Request.Url))
{
continue;
}
if (!_routes.Contains(routeHandler))
{
continue;
}
if (routeHandler.WillExpire())
{
_routes.Remove(routeHandler);
}
var handled = await routeHandler.HandleAsync(route).ConfigureAwait(false);
if (_routes.Count == 0)
{
await UpdateInterceptionAsync().ConfigureAwait(false);
}
if (handled)
{
return;
}
}
try
{
await route.InnerContinueAsync(true /* isFallback */).ConfigureAwait(false);
}
catch
{
// If the page is closed or UnrouteAll() was called without waiting and interception disabled,
// the method will throw an error - silence it.
}
}
internal async Task OnWebSocketRouteAsync(WebSocketRoute webSocketRoute)
{
var routeHandler = _webSocketRoutes.Find(route => route.Matches(webSocketRoute.Url));
if (routeHandler != null)
{
await routeHandler.HandleAsync(webSocketRoute).ConfigureAwait(false);
}
else
{
webSocketRoute.ConnectToServer();
}
}
internal bool UrlMatches(string url, string globMatch)
=> new URLMatch()
{
glob = globMatch,
baseURL = Options.BaseURL,
}.Match(url);
private Task RouteAsync(string globMatch, Regex reMatch, Func<string, bool> funcMatch, Delegate handler, BrowserContextRouteOptions options)
=> RouteAsync(new()
{
urlMatcher = new URLMatch()
{
glob = globMatch,
re = reMatch,
func = funcMatch,
baseURL = Options.BaseURL,
},
Handler = handler,
Times = options?.Times,
});
private Task RouteAsync(RouteHandler setting)
{
_routes.Insert(0, setting);
return UpdateInterceptionAsync();
}
private async Task UnrouteAsync(string globMatch, Regex reMatch, Func<string, bool> funcMatch, Delegate handler)
{
var removed = new List<RouteHandler>();
var remaining = new List<RouteHandler>();
foreach (var routeHandler in _routes)
{
if (routeHandler.urlMatcher.Equals(globMatch, reMatch, funcMatch, Options.BaseURL) && (handler == null || routeHandler.Handler == handler))
{
removed.Add(routeHandler);
}
else
{
remaining.Add(routeHandler);
}
}
await UnrouteInternalAsync(removed, remaining, UnrouteBehavior.Default).ConfigureAwait(false);
}
private async Task UnrouteInternalAsync(List<RouteHandler> removed, List<RouteHandler> remaining, UnrouteBehavior? behavior)
{
_routes = remaining;
await UpdateInterceptionAsync().ConfigureAwait(false);
if (behavior == null || behavior == UnrouteBehavior.Default)
{
return;
}
var tasks = removed.Select(routeHandler => routeHandler.StopAsync(behavior.Value));
await Task.WhenAll(tasks).ConfigureAwait(false);
}
private async Task UpdateInterceptionAsync()
{
var patterns = RouteHandler.PrepareInterceptionPatterns(_routes);
await SendMessageToServerAsync(
"setNetworkInterceptionPatterns",
new Dictionary<string, object>
{
["patterns"] = patterns,
}).ConfigureAwait(false);
}
internal void OnClose()
{
if (Browser != null)
{
((Browser)Browser)._contexts.Remove(this);
}
DisposeHarRouters();
_tracing.ResetStackCounter();
Close?.Invoke(this, this);
_closeTcs.TrySetResult(true);
}
private void Channel_OnPage(object sender, Page page)
{
page.Context = this;
_pages.Add(page);
Page?.Invoke(this, page);
if (page.Opener?.IsClosed == false)
{
page.Opener.NotifyPopup(page);
}
}
private void Channel_BindingCall(BindingCall bindingCall)
{
if (_bindings.TryGetValue(bindingCall.Name, out var binding))
{
_ = bindingCall.CallAsync(binding);
}
}
private void Channel_Route(object sender, Route route) => _ = OnRouteAsync(route).ConfigureAwait(false);
private async Task ExposeBindingAsync(string name, Delegate callback, bool handle = false)
{
foreach (var page in _pages)
{
if (page.Bindings.ContainsKey(name))
{
throw new PlaywrightException($"Function \"{name}\" has been already registered in one of the pages");
}
}
if (_bindings.ContainsKey(name))
{
throw new PlaywrightException($"Function \"{name}\" has been already registered");
}
_bindings.Add(name, callback);
await SendMessageToServerAsync(
"exposeBinding",
new Dictionary<string, object>
{
["name"] = name,
["needsHandle"] = handle,
}).ConfigureAwait(false);
}
private HarContentPolicy? RouteFromHarUpdateContentPolicyToHarContentPolicy(RouteFromHarUpdateContentPolicy? policy)
{
switch (policy)
{
case RouteFromHarUpdateContentPolicy.Attach:
return HarContentPolicy.Attach;
case RouteFromHarUpdateContentPolicy.Embed:
return HarContentPolicy.Embed;
default:
return null;
}
}
internal async Task RecordIntoHarAsync(string har, Page page, BrowserContextRouteFromHAROptions options)
{
var contentPolicy = RouteFromHarUpdateContentPolicyToHarContentPolicy(options?.UpdateContent);
var harId = (await SendMessageToServerAsync("harStart", new Dictionary<string, object>
{
{ "page", page },
{ "options", Core.Browser.PrepareHarOptions(contentPolicy ?? HarContentPolicy.Attach, options.UpdateMode ?? HarMode.Minimal, har, null, options.Url, options.UrlString, options.UrlRegex) },
}).ConfigureAwait(false)).GetString("harId", false);
_harRecorders.Add(harId, new() { Path = har, Content = contentPolicy ?? HarContentPolicy.Attach });
}
[MethodImpl(MethodImplOptions.NoInlining)]
public async Task RouteFromHARAsync(string har, BrowserContextRouteFromHAROptions options = null)
{
if (options?.Update == true)
{
await RecordIntoHarAsync(har, null, options).ConfigureAwait(false);
return;
}
var harRouter = await HarRouter.CreateAsync(_connection.LocalUtils, har, options?.NotFound ?? HarNotFound.Abort, new()
{
Url = options?.Url,
UrlRegex = options?.UrlRegex,
UrlString = options?.UrlString,
}).ConfigureAwait(false);
_harRouters.Add(harRouter);
await harRouter.AddContextRouteAsync(this).ConfigureAwait(false);
}
private void DisposeHarRouters()
{
foreach (var router in _harRouters)
{
router.Dispose();
}
_harRouters.Clear();
}
[MethodImpl(MethodImplOptions.NoInlining)]
public Task RouteWebSocketAsync(string url, Action<IWebSocketRoute> handler)
=> RouteWebSocketAsync(url, null, null, handler);
[MethodImpl(MethodImplOptions.NoInlining)]
public Task RouteWebSocketAsync(Regex url, Action<IWebSocketRoute> handler)
=> RouteWebSocketAsync(null, url, null, handler);
[MethodImpl(MethodImplOptions.NoInlining)]
public Task RouteWebSocketAsync(Func<string, bool> url, Action<IWebSocketRoute> handler)
=> RouteWebSocketAsync(null, null, url, handler);
private Task RouteWebSocketAsync(string globMatch, Regex reMatch, Func<string, bool> funcMatch, Delegate handler)
{
_webSocketRoutes.Insert(0, new WebSocketRouteHandler()
{
urlMatcher = new URLMatch()
{
baseURL = Options.BaseURL,
glob = globMatch,
re = reMatch,
func = funcMatch,
},
Handler = handler,
});
return UpdateWebSocketInterceptionAsync();
}
private async Task UpdateWebSocketInterceptionAsync()
{
var patterns = WebSocketRouteHandler.PrepareInterceptionPatterns(_webSocketRoutes);
await SendMessageToServerAsync("setWebSocketInterceptionPatterns", new Dictionary<string, object>
{
["patterns"] = patterns,
}).ConfigureAwait(false);
}
}
internal class HarRecorder
{
internal string Path { get; set; }
internal HarContentPolicy? Content { get; set; }
}