diff --git a/dotnet/src/webdriver/BiDi/Broker.cs b/dotnet/src/webdriver/BiDi/Broker.cs index 3e1b49783f16b..49de432fb391b 100644 --- a/dotnet/src/webdriver/BiDi/Broker.cs +++ b/dotnet/src/webdriver/BiDi/Broker.cs @@ -69,18 +69,18 @@ public Broker(ITransport transport, BiDi bidi) _processingTask = Task.Run(ProcessMessagesAsync); } - public async Task SubscribeAsync(string eventName, Action action, Func factory, SubscriptionOptions? options, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken) + public async Task SubscribeAsync(Event descriptor, Action action, SubscriptionOptions? options, CancellationToken cancellationToken) where TEventArgs : EventArgs { ValueTask InvokeAction(EventArgs args) { action((TEventArgs)args); return default; } - return await SubscribeAsync(eventName, InvokeAction, (bidi, ep) => factory(bidi, (TEventParams)ep), jsonTypeInfo, options, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(descriptor.Name, InvokeAction, (bidi, ep) => descriptor.Factory(bidi, (TEventParams)ep), descriptor.JsonTypeInfo, options, cancellationToken).ConfigureAwait(false); } - public async Task SubscribeAsync(string eventName, Func func, Func factory, SubscriptionOptions? options, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken) + public async Task SubscribeAsync(Event descriptor, Func func, SubscriptionOptions? options, CancellationToken cancellationToken) where TEventArgs : EventArgs { ValueTask InvokeFunc(EventArgs args) => new(func((TEventArgs)args)); - return await SubscribeAsync(eventName, InvokeFunc, (bidi, ep) => factory(bidi, (TEventParams)ep), jsonTypeInfo, options, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(descriptor.Name, InvokeFunc, (bidi, ep) => descriptor.Factory(bidi, (TEventParams)ep), descriptor.JsonTypeInfo, options, cancellationToken).ConfigureAwait(false); } private async Task SubscribeAsync(string eventName, Func handler, Func argsFactory, JsonTypeInfo jsonTypeInfo, SubscriptionOptions? options, CancellationToken cancellationToken) diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs index da8ddf663238a..6ba3c353cbd7e 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs @@ -60,6 +60,81 @@ internal sealed class BrowsingContextModule : Module, IBrowsingContextModule private static readonly Command HandleUserPromptCommand = new( "browsingContext.handleUserPrompt", Default.HandleUserPromptParameters, Default.HandleUserPromptResult); + private static readonly Event NavigationStartedEvent = new( + "browsingContext.navigationStarted", + static (bidi, p) => new NavigationStartedEventArgs(bidi, p.Context, p.Navigation, p.Timestamp, p.Url, p.UserContext), + Default.NavigationInfo); + + private static readonly Event FragmentNavigatedEvent = new( + "browsingContext.fragmentNavigated", + static (bidi, p) => new FragmentNavigatedEventArgs(bidi, p.Context, p.Navigation, p.Timestamp, p.Url, p.UserContext), + Default.NavigationInfo); + + private static readonly Event HistoryUpdatedEvent = new( + "browsingContext.historyUpdated", + static (bidi, p) => new HistoryUpdatedEventArgs(bidi, p.Context, p.Timestamp, p.Url, p.UserContext), + Default.HistoryUpdatedParameters); + + private static readonly Event DomContentLoadedEvent = new( + "browsingContext.domContentLoaded", + static (bidi, p) => new DomContentLoadedEventArgs(bidi, p.Context, p.Navigation, p.Timestamp, p.Url, p.UserContext), + Default.NavigationInfo); + + private static readonly Event LoadEvent = new( + "browsingContext.load", + static (bidi, p) => new LoadEventArgs(bidi, p.Context, p.Navigation, p.Timestamp, p.Url, p.UserContext), + Default.NavigationInfo); + + private static readonly Event DownloadWillBeginEvent = new( + "browsingContext.downloadWillBegin", + static (bidi, p) => new DownloadWillBeginEventArgs(bidi, p.SuggestedFilename, p.Context, p.Navigation, p.Timestamp, p.Url), + Default.DownloadWillBeginParams); + + private static readonly Event DownloadEndEvent = new( + "browsingContext.downloadEnd", + static (bidi, p) => p switch + { + DownloadCanceledParams c => new DownloadCanceledEventArgs(bidi, c.Context, c.Navigation, c.Timestamp, c.Url), + DownloadCompleteParams c => new DownloadCompleteEventArgs(bidi, c.Filepath, c.Context, c.Navigation, c.Timestamp, c.Url), + _ => throw new BiDiException($"Unknown {nameof(DownloadEndParams)} type: {p.GetType()}") + }, + Default.DownloadEndParams); + + private static readonly Event NavigationAbortedEvent = new( + "browsingContext.navigationAborted", + static (bidi, p) => new NavigationAbortedEventArgs(bidi, p.Context, p.Navigation, p.Timestamp, p.Url, p.UserContext), + Default.NavigationInfo); + + private static readonly Event NavigationFailedEvent = new( + "browsingContext.navigationFailed", + static (bidi, p) => new NavigationFailedEventArgs(bidi, p.Context, p.Navigation, p.Timestamp, p.Url, p.UserContext), + Default.NavigationInfo); + + private static readonly Event NavigationCommittedEvent = new( + "browsingContext.navigationCommitted", + static (bidi, p) => new NavigationCommittedEventArgs(bidi, p.Context, p.Navigation, p.Timestamp, p.Url, p.UserContext), + Default.NavigationInfo); + + private static readonly Event ContextCreatedEvent = new( + "browsingContext.contextCreated", + static (bidi, p) => new ContextCreatedEventArgs(bidi, p.Children, p.ClientWindow, p.Context, p.OriginalOpener, p.Url, p.UserContext, p.Parent), + Default.Info); + + private static readonly Event ContextDestroyedEvent = new( + "browsingContext.contextDestroyed", + static (bidi, p) => new ContextDestroyedEventArgs(bidi, p.Children, p.ClientWindow, p.Context, p.OriginalOpener, p.Url, p.UserContext, p.Parent), + Default.Info); + + private static readonly Event UserPromptOpenedEvent = new( + "browsingContext.userPromptOpened", + static (bidi, p) => new UserPromptOpenedEventArgs(bidi, p.Context, p.Handler, p.Message, p.Type, p.UserContext, p.DefaultValue), + Default.UserPromptOpenedParameters); + + private static readonly Event UserPromptClosedEvent = new( + "browsingContext.userPromptClosed", + static (bidi, p) => new UserPromptClosedEventArgs(bidi, p.Context, p.Accepted, p.Type, p.UserContext, p.UserText), + Default.UserPromptClosedParameters); + public async Task CreateAsync(ContextType type, CreateOptions? options = null, CancellationToken cancellationToken = default) { var @params = new CreateParameters(type, options?.ReferenceContext, options?.Background, options?.UserContext); @@ -146,176 +221,143 @@ public async Task HandleUserPromptAsync(BrowsingContext public async Task OnNavigationStartedAsync(Func handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("browsingContext.navigationStarted", handler, CreateNavigationStartedEventArgs, options, Default.NavigationInfo, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(NavigationStartedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnNavigationStartedAsync(Action handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("browsingContext.navigationStarted", handler, CreateNavigationStartedEventArgs, options, Default.NavigationInfo, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(NavigationStartedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnFragmentNavigatedAsync(Func handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("browsingContext.fragmentNavigated", handler, CreateFragmentNavigatedEventArgs, options, Default.NavigationInfo, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(FragmentNavigatedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnFragmentNavigatedAsync(Action handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("browsingContext.fragmentNavigated", handler, CreateFragmentNavigatedEventArgs, options, Default.NavigationInfo, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(FragmentNavigatedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnHistoryUpdatedAsync(Func handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("browsingContext.historyUpdated", handler, CreateHistoryUpdatedEventArgs, options, Default.HistoryUpdatedParameters, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(HistoryUpdatedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnHistoryUpdatedAsync(Action handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("browsingContext.historyUpdated", handler, CreateHistoryUpdatedEventArgs, options, Default.HistoryUpdatedParameters, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(HistoryUpdatedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnDomContentLoadedAsync(Func handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("browsingContext.domContentLoaded", handler, CreateDomContentLoadedEventArgs, options, Default.NavigationInfo, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(DomContentLoadedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnDomContentLoadedAsync(Action handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("browsingContext.domContentLoaded", handler, CreateDomContentLoadedEventArgs, options, Default.NavigationInfo, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(DomContentLoadedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnLoadAsync(Func handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("browsingContext.load", handler, CreateLoadEventArgs, options, Default.NavigationInfo, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(LoadEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnLoadAsync(Action handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("browsingContext.load", handler, CreateLoadEventArgs, options, Default.NavigationInfo, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(LoadEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnDownloadWillBeginAsync(Func handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("browsingContext.downloadWillBegin", handler, CreateDownloadWillBeginEventArgs, options, Default.DownloadWillBeginParams, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(DownloadWillBeginEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnDownloadWillBeginAsync(Action handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("browsingContext.downloadWillBegin", handler, CreateDownloadWillBeginEventArgs, options, Default.DownloadWillBeginParams, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(DownloadWillBeginEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnDownloadEndAsync(Func handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("browsingContext.downloadEnd", handler, CreateDownloadEndEventArgs, options, Default.DownloadEndParams, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(DownloadEndEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnDownloadEndAsync(Action handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("browsingContext.downloadEnd", handler, CreateDownloadEndEventArgs, options, Default.DownloadEndParams, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(DownloadEndEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnNavigationAbortedAsync(Func handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("browsingContext.navigationAborted", handler, CreateNavigationAbortedEventArgs, options, Default.NavigationInfo, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(NavigationAbortedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnNavigationAbortedAsync(Action handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("browsingContext.navigationAborted", handler, CreateNavigationAbortedEventArgs, options, Default.NavigationInfo, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(NavigationAbortedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnNavigationFailedAsync(Func handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("browsingContext.navigationFailed", handler, CreateNavigationFailedEventArgs, options, Default.NavigationInfo, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(NavigationFailedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnNavigationFailedAsync(Action handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("browsingContext.navigationFailed", handler, CreateNavigationFailedEventArgs, options, Default.NavigationInfo, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(NavigationFailedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnNavigationCommittedAsync(Func handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("browsingContext.navigationCommitted", handler, CreateNavigationCommittedEventArgs, options, Default.NavigationInfo, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(NavigationCommittedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnNavigationCommittedAsync(Action handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("browsingContext.navigationCommitted", handler, CreateNavigationCommittedEventArgs, options, Default.NavigationInfo, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(NavigationCommittedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnContextCreatedAsync(Func handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("browsingContext.contextCreated", handler, CreateContextCreatedEventArgs, options, Default.Info, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(ContextCreatedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnContextCreatedAsync(Action handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("browsingContext.contextCreated", handler, CreateContextCreatedEventArgs, options, Default.Info, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(ContextCreatedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnContextDestroyedAsync(Func handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("browsingContext.contextDestroyed", handler, CreateContextDestroyedEventArgs, options, Default.Info, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(ContextDestroyedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnContextDestroyedAsync(Action handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("browsingContext.contextDestroyed", handler, CreateContextDestroyedEventArgs, options, Default.Info, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(ContextDestroyedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnUserPromptOpenedAsync(Func handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("browsingContext.userPromptOpened", handler, CreateUserPromptOpenedEventArgs, options, Default.UserPromptOpenedParameters, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(UserPromptOpenedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnUserPromptOpenedAsync(Action handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("browsingContext.userPromptOpened", handler, CreateUserPromptOpenedEventArgs, options, Default.UserPromptOpenedParameters, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(UserPromptOpenedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnUserPromptClosedAsync(Func handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("browsingContext.userPromptClosed", handler, CreateUserPromptClosedEventArgs, options, Default.UserPromptClosedParameters, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(UserPromptClosedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnUserPromptClosedAsync(Action handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("browsingContext.userPromptClosed", handler, CreateUserPromptClosedEventArgs, options, Default.UserPromptClosedParameters, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(UserPromptClosedEvent, handler, options, cancellationToken).ConfigureAwait(false); } - - private static NavigationStartedEventArgs CreateNavigationStartedEventArgs(IBiDi bidi, NavigationInfo p) => new(bidi, p.Context, p.Navigation, p.Timestamp, p.Url, p.UserContext); - - private static FragmentNavigatedEventArgs CreateFragmentNavigatedEventArgs(IBiDi bidi, NavigationInfo p) => new(bidi, p.Context, p.Navigation, p.Timestamp, p.Url, p.UserContext); - - private static DomContentLoadedEventArgs CreateDomContentLoadedEventArgs(IBiDi bidi, NavigationInfo p) => new(bidi, p.Context, p.Navigation, p.Timestamp, p.Url, p.UserContext); - - private static LoadEventArgs CreateLoadEventArgs(IBiDi bidi, NavigationInfo p) => new(bidi, p.Context, p.Navigation, p.Timestamp, p.Url, p.UserContext); - - private static NavigationAbortedEventArgs CreateNavigationAbortedEventArgs(IBiDi bidi, NavigationInfo p) => new(bidi, p.Context, p.Navigation, p.Timestamp, p.Url, p.UserContext); - - private static NavigationFailedEventArgs CreateNavigationFailedEventArgs(IBiDi bidi, NavigationInfo p) => new(bidi, p.Context, p.Navigation, p.Timestamp, p.Url, p.UserContext); - - private static NavigationCommittedEventArgs CreateNavigationCommittedEventArgs(IBiDi bidi, NavigationInfo p) => new(bidi, p.Context, p.Navigation, p.Timestamp, p.Url, p.UserContext); - - private static HistoryUpdatedEventArgs CreateHistoryUpdatedEventArgs(IBiDi bidi, HistoryUpdatedParameters p) => new(bidi, p.Context, p.Timestamp, p.Url, p.UserContext); - - private static DownloadWillBeginEventArgs CreateDownloadWillBeginEventArgs(IBiDi bidi, DownloadWillBeginParams p) => new(bidi, p.SuggestedFilename, p.Context, p.Navigation, p.Timestamp, p.Url); - - private static DownloadEndEventArgs CreateDownloadEndEventArgs(IBiDi bidi, DownloadEndParams p) => p switch - { - DownloadCanceledParams c => new DownloadCanceledEventArgs(bidi, c.Context, c.Navigation, c.Timestamp, c.Url), - DownloadCompleteParams c => new DownloadCompleteEventArgs(bidi, c.Filepath, c.Context, c.Navigation, c.Timestamp, c.Url), - _ => throw new BiDiException($"Unknown {nameof(DownloadEndParams)} type: {p.GetType()}") - }; - - private static ContextCreatedEventArgs CreateContextCreatedEventArgs(IBiDi bidi, Info p) => new(bidi, p.Children, p.ClientWindow, p.Context, p.OriginalOpener, p.Url, p.UserContext, p.Parent); - - private static ContextDestroyedEventArgs CreateContextDestroyedEventArgs(IBiDi bidi, Info p) => new(bidi, p.Children, p.ClientWindow, p.Context, p.OriginalOpener, p.Url, p.UserContext, p.Parent); - - private static UserPromptOpenedEventArgs CreateUserPromptOpenedEventArgs(IBiDi bidi, UserPromptOpenedParameters p) => new(bidi, p.Context, p.Handler, p.Message, p.Type, p.UserContext, p.DefaultValue); - - private static UserPromptClosedEventArgs CreateUserPromptClosedEventArgs(IBiDi bidi, UserPromptClosedParameters p) => new(bidi, p.Context, p.Accepted, p.Type, p.UserContext, p.UserText); } [JsonSerializable(typeof(ActivateParameters))] diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/ContextCreatedEventArgs.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/ContextCreatedEvent.cs similarity index 93% rename from dotnet/src/webdriver/BiDi/BrowsingContext/ContextCreatedEventArgs.cs rename to dotnet/src/webdriver/BiDi/BrowsingContext/ContextCreatedEvent.cs index f44ff21f6009b..cbac360a96af9 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/ContextCreatedEventArgs.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/ContextCreatedEvent.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/ContextDestroyedEventArgs.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/ContextDestroyedEvent.cs similarity index 93% rename from dotnet/src/webdriver/BiDi/BrowsingContext/ContextDestroyedEventArgs.cs rename to dotnet/src/webdriver/BiDi/BrowsingContext/ContextDestroyedEvent.cs index 225786a76e6a3..3b4c7a986dd63 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/ContextDestroyedEventArgs.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/ContextDestroyedEvent.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/DomContentLoadedEventArgs.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/DomContentLoadedEvent.cs similarity index 93% rename from dotnet/src/webdriver/BiDi/BrowsingContext/DomContentLoadedEventArgs.cs rename to dotnet/src/webdriver/BiDi/BrowsingContext/DomContentLoadedEvent.cs index ff08994cdbeac..446f58cadda93 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/DomContentLoadedEventArgs.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/DomContentLoadedEvent.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/DownloadEndEventArgs.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/DownloadEndEvent.cs similarity index 97% rename from dotnet/src/webdriver/BiDi/BrowsingContext/DownloadEndEventArgs.cs rename to dotnet/src/webdriver/BiDi/BrowsingContext/DownloadEndEvent.cs index 6524d5a94504a..ad47fa6f571ed 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/DownloadEndEventArgs.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/DownloadEndEvent.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/DownloadWillBeginEventArgs.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/DownloadWillBeginEvent.cs similarity index 94% rename from dotnet/src/webdriver/BiDi/BrowsingContext/DownloadWillBeginEventArgs.cs rename to dotnet/src/webdriver/BiDi/BrowsingContext/DownloadWillBeginEvent.cs index fed2fe885180b..485c4533e3f35 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/DownloadWillBeginEventArgs.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/DownloadWillBeginEvent.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/FragmentNavigatedEventArgs.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/FragmentNavigatedEvent.cs similarity index 93% rename from dotnet/src/webdriver/BiDi/BrowsingContext/FragmentNavigatedEventArgs.cs rename to dotnet/src/webdriver/BiDi/BrowsingContext/FragmentNavigatedEvent.cs index 8297ad76d82ae..2eaa74f0fac51 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/FragmentNavigatedEventArgs.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/FragmentNavigatedEvent.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/HistoryUpdatedEventArgs.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/HistoryUpdatedEvent.cs similarity index 94% rename from dotnet/src/webdriver/BiDi/BrowsingContext/HistoryUpdatedEventArgs.cs rename to dotnet/src/webdriver/BiDi/BrowsingContext/HistoryUpdatedEvent.cs index b2adbbddbdda8..a44f13bbb3b05 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/HistoryUpdatedEventArgs.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/HistoryUpdatedEvent.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/LoadEventArgs.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/LoadEvent.cs similarity index 94% rename from dotnet/src/webdriver/BiDi/BrowsingContext/LoadEventArgs.cs rename to dotnet/src/webdriver/BiDi/BrowsingContext/LoadEvent.cs index ff9a799090780..b4041b7e8026c 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/LoadEventArgs.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/LoadEvent.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/NavigationAbortedEventArgs.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/NavigationAbortedEvent.cs similarity index 93% rename from dotnet/src/webdriver/BiDi/BrowsingContext/NavigationAbortedEventArgs.cs rename to dotnet/src/webdriver/BiDi/BrowsingContext/NavigationAbortedEvent.cs index 4ecd7d3392b1d..e80e2e533f0b6 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/NavigationAbortedEventArgs.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/NavigationAbortedEvent.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/NavigationCommittedEventArgs.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/NavigationCommittedEvent.cs similarity index 93% rename from dotnet/src/webdriver/BiDi/BrowsingContext/NavigationCommittedEventArgs.cs rename to dotnet/src/webdriver/BiDi/BrowsingContext/NavigationCommittedEvent.cs index b15241d58de79..00b153d8290ac 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/NavigationCommittedEventArgs.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/NavigationCommittedEvent.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/NavigationEventArgs.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/NavigationEvent.cs similarity index 93% rename from dotnet/src/webdriver/BiDi/BrowsingContext/NavigationEventArgs.cs rename to dotnet/src/webdriver/BiDi/BrowsingContext/NavigationEvent.cs index 6d7aa19394cd0..fcf5fa906cf45 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/NavigationEventArgs.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/NavigationEvent.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/NavigationFailedEventArgs.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/NavigationFailedEvent.cs similarity index 93% rename from dotnet/src/webdriver/BiDi/BrowsingContext/NavigationFailedEventArgs.cs rename to dotnet/src/webdriver/BiDi/BrowsingContext/NavigationFailedEvent.cs index d831ad55183f6..466ba5072983f 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/NavigationFailedEventArgs.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/NavigationFailedEvent.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/NavigationStartedEventArgs.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/NavigationStartedEvent.cs similarity index 93% rename from dotnet/src/webdriver/BiDi/BrowsingContext/NavigationStartedEventArgs.cs rename to dotnet/src/webdriver/BiDi/BrowsingContext/NavigationStartedEvent.cs index 2300008f233bd..a559621da9e97 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/NavigationStartedEventArgs.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/NavigationStartedEvent.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/UserPromptClosedEventArgs.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/UserPromptClosedEvent.cs similarity index 94% rename from dotnet/src/webdriver/BiDi/BrowsingContext/UserPromptClosedEventArgs.cs rename to dotnet/src/webdriver/BiDi/BrowsingContext/UserPromptClosedEvent.cs index 40b12fe9b69a2..ce860c6e97e33 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/UserPromptClosedEventArgs.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/UserPromptClosedEvent.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/UserPromptOpenedEventArgs.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/UserPromptOpenedEvent.cs similarity index 94% rename from dotnet/src/webdriver/BiDi/BrowsingContext/UserPromptOpenedEventArgs.cs rename to dotnet/src/webdriver/BiDi/BrowsingContext/UserPromptOpenedEvent.cs index 2dea8436242a6..a40c594b396ef 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/UserPromptOpenedEventArgs.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/UserPromptOpenedEvent.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information diff --git a/dotnet/src/webdriver/BiDi/EventArgs.cs b/dotnet/src/webdriver/BiDi/Event.cs similarity index 73% rename from dotnet/src/webdriver/BiDi/EventArgs.cs rename to dotnet/src/webdriver/BiDi/Event.cs index 9b78d23ef06e2..80b46abd45654 100644 --- a/dotnet/src/webdriver/BiDi/EventArgs.cs +++ b/dotnet/src/webdriver/BiDi/Event.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information @@ -17,6 +17,14 @@ // under the License. // +using System.Text.Json.Serialization.Metadata; + namespace OpenQA.Selenium.BiDi; +public readonly record struct Event( + string Name, + Func Factory, + JsonTypeInfo JsonTypeInfo) + where TEventArgs : EventArgs; + public abstract record EventArgs(IBiDi BiDi); diff --git a/dotnet/src/webdriver/BiDi/Input/FileDialogOpenedEventArgs.cs b/dotnet/src/webdriver/BiDi/Input/FileDialogOpenedEvent.cs similarity index 93% rename from dotnet/src/webdriver/BiDi/Input/FileDialogOpenedEventArgs.cs rename to dotnet/src/webdriver/BiDi/Input/FileDialogOpenedEvent.cs index 3e3c88218bbb2..cf94b8f283339 100644 --- a/dotnet/src/webdriver/BiDi/Input/FileDialogOpenedEventArgs.cs +++ b/dotnet/src/webdriver/BiDi/Input/FileDialogOpenedEvent.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information diff --git a/dotnet/src/webdriver/BiDi/Input/InputModule.cs b/dotnet/src/webdriver/BiDi/Input/InputModule.cs index 1673fa74ce5a1..547b656c4c767 100644 --- a/dotnet/src/webdriver/BiDi/Input/InputModule.cs +++ b/dotnet/src/webdriver/BiDi/Input/InputModule.cs @@ -33,6 +33,11 @@ internal sealed class InputModule : Module, IInputModule private static readonly Command SetFilesCommand = new( "input.setFiles", Default.SetFilesParameters, Default.SetFilesResult); + private static readonly Event FileDialogOpenedEvent = new( + "input.fileDialogOpened", + static (bidi, p) => new FileDialogOpenedEventArgs(bidi, p.Context, p.UserContext, p.Multiple, p.Element), + Default.FileDialogInfo); + public async Task PerformActionsAsync(BrowsingContext.BrowsingContext context, IEnumerable actions, PerformActionsOptions? options = null, CancellationToken cancellationToken = default) { var @params = new PerformActionsParameters(context, actions); @@ -56,17 +61,12 @@ public async Task SetFilesAsync(BrowsingContext.BrowsingContext public async Task OnFileDialogOpenedAsync(Func handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("input.fileDialogOpened", handler, CreateFileDialogOpenedEventArgs, options, Default.FileDialogInfo, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(FileDialogOpenedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnFileDialogOpenedAsync(Action handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("input.fileDialogOpened", handler, CreateFileDialogOpenedEventArgs, options, Default.FileDialogInfo, cancellationToken).ConfigureAwait(false); - } - - private static FileDialogOpenedEventArgs CreateFileDialogOpenedEventArgs(IBiDi bidi, FileDialogInfo p) - { - return new FileDialogOpenedEventArgs(bidi, p.Context, p.UserContext, p.Multiple, p.Element); + return await SubscribeAsync(FileDialogOpenedEvent, handler, options, cancellationToken).ConfigureAwait(false); } } diff --git a/dotnet/src/webdriver/BiDi/Log/EntryAddedEventArgs.cs b/dotnet/src/webdriver/BiDi/Log/EntryAddedEvent.cs similarity index 98% rename from dotnet/src/webdriver/BiDi/Log/EntryAddedEventArgs.cs rename to dotnet/src/webdriver/BiDi/Log/EntryAddedEvent.cs index 74c5ea43b3745..383abfc04a6a2 100644 --- a/dotnet/src/webdriver/BiDi/Log/EntryAddedEventArgs.cs +++ b/dotnet/src/webdriver/BiDi/Log/EntryAddedEvent.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information diff --git a/dotnet/src/webdriver/BiDi/Log/LogModule.cs b/dotnet/src/webdriver/BiDi/Log/LogModule.cs index 342441fb2cdd9..05eb965055ee2 100644 --- a/dotnet/src/webdriver/BiDi/Log/LogModule.cs +++ b/dotnet/src/webdriver/BiDi/Log/LogModule.cs @@ -24,23 +24,26 @@ namespace OpenQA.Selenium.BiDi.Log; internal sealed class LogModule : Module, ILogModule { + private static readonly Event EntryAddedEvent = new( + "log.entryAdded", + static (bidi, p) => p switch + { + ConsoleLogEntry c => new ConsoleEntryAddedEventArgs(bidi, c.Level, c.Source, c.Text, c.Timestamp, c.Method, c.Args) { StackTrace = c.StackTrace }, + JavascriptLogEntry j => new JavascriptEntryAddedEventArgs(bidi, j.Level, j.Source, j.Text, j.Timestamp) { StackTrace = j.StackTrace }, + GenericLogEntry g => new GenericEntryAddedEventArgs(bidi, g.Type, g.Level, g.Source, g.Text, g.Timestamp) { StackTrace = g.StackTrace }, + _ => throw new BiDiException($"Unknown {nameof(LogEntry)} type: {p.GetType()}") + }, + Default.LogEntry); + public async Task OnEntryAddedAsync(Func handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("log.entryAdded", handler, CreateEntryAddedEventArgs, options, Default.LogEntry, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(EntryAddedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnEntryAddedAsync(Action handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("log.entryAdded", handler, CreateEntryAddedEventArgs, options, Default.LogEntry, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(EntryAddedEvent, handler, options, cancellationToken).ConfigureAwait(false); } - - private static EntryAddedEventArgs CreateEntryAddedEventArgs(IBiDi bidi, LogEntry p) => p switch - { - ConsoleLogEntry c => new ConsoleEntryAddedEventArgs(bidi, c.Level, c.Source, c.Text, c.Timestamp, c.Method, c.Args) { StackTrace = c.StackTrace }, - JavascriptLogEntry j => new JavascriptEntryAddedEventArgs(bidi, j.Level, j.Source, j.Text, j.Timestamp) { StackTrace = j.StackTrace }, - GenericLogEntry g => new GenericEntryAddedEventArgs(bidi, g.Type, g.Level, g.Source, g.Text, g.Timestamp) { StackTrace = g.StackTrace }, - _ => throw new BiDiException($"Unknown {nameof(LogEntry)} type: {p.GetType()}") - }; } #region https://github.com/dotnet/runtime/issues/72604 Script.RemoteValue type dependency diff --git a/dotnet/src/webdriver/BiDi/Module.cs b/dotnet/src/webdriver/BiDi/Module.cs index 934406b253f0a..4eeb6821953e0 100644 --- a/dotnet/src/webdriver/BiDi/Module.cs +++ b/dotnet/src/webdriver/BiDi/Module.cs @@ -17,8 +17,6 @@ // under the License. // -using System.Text.Json.Serialization.Metadata; - namespace OpenQA.Selenium.BiDi; public abstract class Module @@ -32,16 +30,16 @@ protected Task ExecuteAsync(Command SubscribeAsync(string name, Action action, Func factory, SubscriptionOptions? options, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken) + protected Task SubscribeAsync(Event descriptor, Action action, SubscriptionOptions? options, CancellationToken cancellationToken) where TEventArgs : EventArgs { - return Broker.SubscribeAsync(name, action, factory, options, jsonTypeInfo, cancellationToken); + return Broker.SubscribeAsync(descriptor, action, options, cancellationToken); } - protected Task SubscribeAsync(string name, Func func, Func factory, SubscriptionOptions? options, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken) + protected Task SubscribeAsync(Event descriptor, Func func, SubscriptionOptions? options, CancellationToken cancellationToken) where TEventArgs : EventArgs { - return Broker.SubscribeAsync(name, func, factory, options, jsonTypeInfo, cancellationToken); + return Broker.SubscribeAsync(descriptor, func, options, cancellationToken); } internal static TModule Create(IBiDi bidi, Broker broker) diff --git a/dotnet/src/webdriver/BiDi/Network/AuthRequiredEventArgs.cs b/dotnet/src/webdriver/BiDi/Network/AuthRequiredEvent.cs similarity index 95% rename from dotnet/src/webdriver/BiDi/Network/AuthRequiredEventArgs.cs rename to dotnet/src/webdriver/BiDi/Network/AuthRequiredEvent.cs index 7e26c9b4a4823..073674e6bd3ef 100644 --- a/dotnet/src/webdriver/BiDi/Network/AuthRequiredEventArgs.cs +++ b/dotnet/src/webdriver/BiDi/Network/AuthRequiredEvent.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information diff --git a/dotnet/src/webdriver/BiDi/Network/BeforeRequestSentEventArgs.cs b/dotnet/src/webdriver/BiDi/Network/BeforeRequestSentEvent.cs similarity index 95% rename from dotnet/src/webdriver/BiDi/Network/BeforeRequestSentEventArgs.cs rename to dotnet/src/webdriver/BiDi/Network/BeforeRequestSentEvent.cs index 693eedd3b0318..e9ddbe9d8de19 100644 --- a/dotnet/src/webdriver/BiDi/Network/BeforeRequestSentEventArgs.cs +++ b/dotnet/src/webdriver/BiDi/Network/BeforeRequestSentEvent.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information diff --git a/dotnet/src/webdriver/BiDi/Network/FetchErrorEventArgs.cs b/dotnet/src/webdriver/BiDi/Network/FetchErrorEvent.cs similarity index 95% rename from dotnet/src/webdriver/BiDi/Network/FetchErrorEventArgs.cs rename to dotnet/src/webdriver/BiDi/Network/FetchErrorEvent.cs index fc255124fa205..b7057f720f367 100644 --- a/dotnet/src/webdriver/BiDi/Network/FetchErrorEventArgs.cs +++ b/dotnet/src/webdriver/BiDi/Network/FetchErrorEvent.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information diff --git a/dotnet/src/webdriver/BiDi/Network/NetworkModule.cs b/dotnet/src/webdriver/BiDi/Network/NetworkModule.cs index 6b2613ce25234..b74bfb3510736 100644 --- a/dotnet/src/webdriver/BiDi/Network/NetworkModule.cs +++ b/dotnet/src/webdriver/BiDi/Network/NetworkModule.cs @@ -61,6 +61,31 @@ internal sealed partial class NetworkModule : Module, INetworkModule private static readonly Command ContinueWithAuthCommand = new( "network.continueWithAuth", Default.ContinueWithAuthParameters, Default.ContinueWithAuthResult); + private static readonly Event BeforeRequestSentEvent = new( + "network.beforeRequestSent", + static (bidi, p) => new BeforeRequestSentEventArgs(bidi, p.Context, p.IsBlocked, p.Navigation, p.RedirectCount, p.Request, p.Timestamp, p.Initiator, p.UserContext, p.Intercepts), + Default.BeforeRequestSentParameters); + + private static readonly Event ResponseStartedEvent = new( + "network.responseStarted", + static (bidi, p) => new ResponseStartedEventArgs(bidi, p.Context, p.IsBlocked, p.Navigation, p.RedirectCount, p.Request, p.Timestamp, p.Response, p.UserContext, p.Intercepts), + Default.ResponseStartedParameters); + + private static readonly Event ResponseCompletedEvent = new( + "network.responseCompleted", + static (bidi, p) => new ResponseCompletedEventArgs(bidi, p.Context, p.IsBlocked, p.Navigation, p.RedirectCount, p.Request, p.Timestamp, p.Response, p.UserContext, p.Intercepts), + Default.ResponseCompletedParameters); + + private static readonly Event FetchErrorEvent = new( + "network.fetchError", + static (bidi, p) => new FetchErrorEventArgs(bidi, p.Context, p.IsBlocked, p.Navigation, p.RedirectCount, p.Request, p.Timestamp, p.ErrorText, p.UserContext, p.Intercepts), + Default.FetchErrorParameters); + + private static readonly Event AuthRequiredEvent = new( + "network.authRequired", + static (bidi, p) => new AuthRequiredEventArgs(bidi, p.Context, p.IsBlocked, p.Navigation, p.RedirectCount, p.Request, p.Timestamp, p.UserContext, p.Intercepts, p.Response), + Default.AuthRequiredParameters); + public async Task AddDataCollectorAsync(IEnumerable dataTypes, int maxEncodedDataSize, AddDataCollectorOptions? options = null, CancellationToken cancellationToken = default) { var @params = new AddDataCollectorParameters(dataTypes, maxEncodedDataSize, options?.CollectorType, options?.Contexts, options?.UserContexts); @@ -157,68 +182,53 @@ public async Task ContinueWithAuthAsync(Request request, public async Task OnBeforeRequestSentAsync(Func handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("network.beforeRequestSent", handler, CreateBeforeRequestSentEventArgs, options, Default.BeforeRequestSentParameters, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(BeforeRequestSentEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnBeforeRequestSentAsync(Action handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("network.beforeRequestSent", handler, CreateBeforeRequestSentEventArgs, options, Default.BeforeRequestSentParameters, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(BeforeRequestSentEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnResponseStartedAsync(Func handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("network.responseStarted", handler, CreateResponseStartedEventArgs, options, Default.ResponseStartedParameters, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(ResponseStartedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnResponseStartedAsync(Action handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("network.responseStarted", handler, CreateResponseStartedEventArgs, options, Default.ResponseStartedParameters, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(ResponseStartedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnResponseCompletedAsync(Func handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("network.responseCompleted", handler, CreateResponseCompletedEventArgs, options, Default.ResponseCompletedParameters, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(ResponseCompletedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnResponseCompletedAsync(Action handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("network.responseCompleted", handler, CreateResponseCompletedEventArgs, options, Default.ResponseCompletedParameters, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(ResponseCompletedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnFetchErrorAsync(Func handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("network.fetchError", handler, CreateFetchErrorEventArgs, options, Default.FetchErrorParameters, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(FetchErrorEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnFetchErrorAsync(Action handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("network.fetchError", handler, CreateFetchErrorEventArgs, options, Default.FetchErrorParameters, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(FetchErrorEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnAuthRequiredAsync(Func handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("network.authRequired", handler, CreateAuthRequiredEventArgs, options, Default.AuthRequiredParameters, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(AuthRequiredEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnAuthRequiredAsync(Action handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("network.authRequired", handler, CreateAuthRequiredEventArgs, options, Default.AuthRequiredParameters, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(AuthRequiredEvent, handler, options, cancellationToken).ConfigureAwait(false); } - - private static BeforeRequestSentEventArgs CreateBeforeRequestSentEventArgs(IBiDi bidi, BeforeRequestSentParameters p) - => new(bidi, p.Context, p.IsBlocked, p.Navigation, p.RedirectCount, p.Request, p.Timestamp, p.Initiator, p.UserContext, p.Intercepts); - - private static ResponseStartedEventArgs CreateResponseStartedEventArgs(IBiDi bidi, ResponseStartedParameters p) - => new(bidi, p.Context, p.IsBlocked, p.Navigation, p.RedirectCount, p.Request, p.Timestamp, p.Response, p.UserContext, p.Intercepts); - - private static ResponseCompletedEventArgs CreateResponseCompletedEventArgs(IBiDi bidi, ResponseCompletedParameters p) - => new(bidi, p.Context, p.IsBlocked, p.Navigation, p.RedirectCount, p.Request, p.Timestamp, p.Response, p.UserContext, p.Intercepts); - - private static FetchErrorEventArgs CreateFetchErrorEventArgs(IBiDi bidi, FetchErrorParameters p) - => new(bidi, p.Context, p.IsBlocked, p.Navigation, p.RedirectCount, p.Request, p.Timestamp, p.ErrorText, p.UserContext, p.Intercepts); - - private static AuthRequiredEventArgs CreateAuthRequiredEventArgs(IBiDi bidi, AuthRequiredParameters p) - => new(bidi, p.Context, p.IsBlocked, p.Navigation, p.RedirectCount, p.Request, p.Timestamp, p.UserContext, p.Intercepts, p.Response); } [JsonSerializable(typeof(AddDataCollectorParameters))] diff --git a/dotnet/src/webdriver/BiDi/Network/ResponseCompletedEventArgs.cs b/dotnet/src/webdriver/BiDi/Network/ResponseCompletedEvent.cs similarity index 95% rename from dotnet/src/webdriver/BiDi/Network/ResponseCompletedEventArgs.cs rename to dotnet/src/webdriver/BiDi/Network/ResponseCompletedEvent.cs index 78fc1d46a3cde..3cdb670c25f54 100644 --- a/dotnet/src/webdriver/BiDi/Network/ResponseCompletedEventArgs.cs +++ b/dotnet/src/webdriver/BiDi/Network/ResponseCompletedEvent.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information diff --git a/dotnet/src/webdriver/BiDi/Network/ResponseStartedEventArgs.cs b/dotnet/src/webdriver/BiDi/Network/ResponseStartedEvent.cs similarity index 95% rename from dotnet/src/webdriver/BiDi/Network/ResponseStartedEventArgs.cs rename to dotnet/src/webdriver/BiDi/Network/ResponseStartedEvent.cs index a6f81db8fc1ee..cfe543c07b0c9 100644 --- a/dotnet/src/webdriver/BiDi/Network/ResponseStartedEventArgs.cs +++ b/dotnet/src/webdriver/BiDi/Network/ResponseStartedEvent.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information diff --git a/dotnet/src/webdriver/BiDi/Script/MessageEventArgs.cs b/dotnet/src/webdriver/BiDi/Script/MessageEvent.cs similarity index 93% rename from dotnet/src/webdriver/BiDi/Script/MessageEventArgs.cs rename to dotnet/src/webdriver/BiDi/Script/MessageEvent.cs index 96c72723f991b..4a2f412bf1ac1 100644 --- a/dotnet/src/webdriver/BiDi/Script/MessageEventArgs.cs +++ b/dotnet/src/webdriver/BiDi/Script/MessageEvent.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information diff --git a/dotnet/src/webdriver/BiDi/Script/RealmDestroyedEventArgs.cs b/dotnet/src/webdriver/BiDi/Script/RealmDestroyedEvent.cs similarity index 92% rename from dotnet/src/webdriver/BiDi/Script/RealmDestroyedEventArgs.cs rename to dotnet/src/webdriver/BiDi/Script/RealmDestroyedEvent.cs index 707ebcb77d152..455de3dce3c55 100644 --- a/dotnet/src/webdriver/BiDi/Script/RealmDestroyedEventArgs.cs +++ b/dotnet/src/webdriver/BiDi/Script/RealmDestroyedEvent.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information diff --git a/dotnet/src/webdriver/BiDi/Script/RealmInfoEventArgs.cs b/dotnet/src/webdriver/BiDi/Script/RealmInfoEvent.cs similarity index 96% rename from dotnet/src/webdriver/BiDi/Script/RealmInfoEventArgs.cs rename to dotnet/src/webdriver/BiDi/Script/RealmInfoEvent.cs index 0d8663d020ad2..c4db4386f1e75 100644 --- a/dotnet/src/webdriver/BiDi/Script/RealmInfoEventArgs.cs +++ b/dotnet/src/webdriver/BiDi/Script/RealmInfoEvent.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information diff --git a/dotnet/src/webdriver/BiDi/Script/ScriptModule.cs b/dotnet/src/webdriver/BiDi/Script/ScriptModule.cs index 3f6353dd68c2b..ef64775ca2515 100644 --- a/dotnet/src/webdriver/BiDi/Script/ScriptModule.cs +++ b/dotnet/src/webdriver/BiDi/Script/ScriptModule.cs @@ -43,6 +43,32 @@ internal sealed class ScriptModule : Module, IScriptModule private static readonly Command RemovePreloadScriptCommand = new( "script.removePreloadScript", Default.RemovePreloadScriptParameters, Default.RemovePreloadScriptResult); + private static readonly Event MessageEvent = new( + "script.message", + static (bidi, p) => new MessageEventArgs(bidi, p.Channel, p.Data, p.Source), + Default.MessageParameters); + + private static readonly Event RealmCreatedEvent = new( + "script.realmCreated", + static (bidi, p) => p switch + { + WindowRealmInfo w => new WindowRealmCreatedEventArgs(bidi, w.Realm, w.Origin, w.Context, w.UserContext, w.Sandbox), + DedicatedWorkerRealmInfo d => new DedicatedWorkerRealmCreatedEventArgs(bidi, d.Realm, d.Origin, d.Owners), + SharedWorkerRealmInfo s => new SharedWorkerRealmCreatedEventArgs(bidi, s.Realm, s.Origin), + ServiceWorkerRealmInfo s => new ServiceWorkerRealmCreatedEventArgs(bidi, s.Realm, s.Origin), + WorkerRealmInfo w => new WorkerRealmCreatedEventArgs(bidi, w.Realm, w.Origin), + PaintWorkletRealmInfo p2 => new PaintWorkletRealmCreatedEventArgs(bidi, p2.Realm, p2.Origin), + AudioWorkletRealmInfo a => new AudioWorkletRealmCreatedEventArgs(bidi, a.Realm, a.Origin), + WorkletRealmInfo w => new WorkletRealmCreatedEventArgs(bidi, w.Realm, w.Origin), + _ => throw new BiDiException($"Unknown {nameof(RealmInfo)} type: {p.GetType()}") + }, + Default.RealmInfo); + + private static readonly Event RealmDestroyedEvent = new( + "script.realmDestroyed", + static (bidi, p) => new RealmDestroyedEventArgs(bidi, p.Realm), + Default.RealmDestroyedParameters); + public async Task EvaluateAsync([StringSyntax(StringSyntaxConstants.JavaScript)] string expression, bool awaitPromise, Target target, EvaluateOptions? options = null, CancellationToken cancellationToken = default) { var @params = new EvaluateParameters(expression, target, awaitPromise, options?.ResultOwnership, options?.SerializationOptions, options?.UserActivation); @@ -101,52 +127,33 @@ public async Task RemovePreloadScriptAsync(PreloadScr public async Task OnMessageAsync(Func handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("script.message", handler, CreateMessageEventArgs, options, Default.MessageParameters, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(MessageEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnMessageAsync(Action handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("script.message", handler, CreateMessageEventArgs, options, Default.MessageParameters, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(MessageEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnRealmCreatedAsync(Func handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("script.realmCreated", handler, CreateRealmCreatedEventArgs, options, Default.RealmInfo, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(RealmCreatedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnRealmCreatedAsync(Action handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("script.realmCreated", handler, CreateRealmCreatedEventArgs, options, Default.RealmInfo, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(RealmCreatedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnRealmDestroyedAsync(Func handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("script.realmDestroyed", handler, CreateRealmDestroyedEventArgs, options, Default.RealmDestroyedParameters, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(RealmDestroyedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnRealmDestroyedAsync(Action handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("script.realmDestroyed", handler, CreateRealmDestroyedEventArgs, options, Default.RealmDestroyedParameters, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(RealmDestroyedEvent, handler, options, cancellationToken).ConfigureAwait(false); } - - private static MessageEventArgs CreateMessageEventArgs(IBiDi bidi, MessageParameters p) - => new(bidi, p.Channel, p.Data, p.Source); - - private static RealmDestroyedEventArgs CreateRealmDestroyedEventArgs(IBiDi bidi, RealmDestroyedParameters p) - => new(bidi, p.Realm); - - private static RealmCreatedEventArgs CreateRealmCreatedEventArgs(IBiDi bidi, RealmInfo p) => p switch - { - WindowRealmInfo w => new WindowRealmCreatedEventArgs(bidi, w.Realm, w.Origin, w.Context, w.UserContext, w.Sandbox), - DedicatedWorkerRealmInfo d => new DedicatedWorkerRealmCreatedEventArgs(bidi, d.Realm, d.Origin, d.Owners), - SharedWorkerRealmInfo s => new SharedWorkerRealmCreatedEventArgs(bidi, s.Realm, s.Origin), - ServiceWorkerRealmInfo s => new ServiceWorkerRealmCreatedEventArgs(bidi, s.Realm, s.Origin), - WorkerRealmInfo w => new WorkerRealmCreatedEventArgs(bidi, w.Realm, w.Origin), - PaintWorkletRealmInfo p2 => new PaintWorkletRealmCreatedEventArgs(bidi, p2.Realm, p2.Origin), - AudioWorkletRealmInfo a => new AudioWorkletRealmCreatedEventArgs(bidi, a.Realm, a.Origin), - WorkletRealmInfo w => new WorkletRealmCreatedEventArgs(bidi, w.Realm, w.Origin), - _ => throw new BiDiException($"Unknown {nameof(RealmInfo)} type: {p.GetType()}") - }; } #region https://github.com/dotnet/runtime/issues/72604 diff --git a/dotnet/src/webdriver/BiDi/Speculation/PrefetchStatusUpdatedEventArgs.cs b/dotnet/src/webdriver/BiDi/Speculation/PrefetchStatusUpdatedEvent.cs similarity index 93% rename from dotnet/src/webdriver/BiDi/Speculation/PrefetchStatusUpdatedEventArgs.cs rename to dotnet/src/webdriver/BiDi/Speculation/PrefetchStatusUpdatedEvent.cs index d47e8307a6408..52b1bb320a34d 100644 --- a/dotnet/src/webdriver/BiDi/Speculation/PrefetchStatusUpdatedEventArgs.cs +++ b/dotnet/src/webdriver/BiDi/Speculation/PrefetchStatusUpdatedEvent.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information diff --git a/dotnet/src/webdriver/BiDi/Speculation/SpeculationModule.cs b/dotnet/src/webdriver/BiDi/Speculation/SpeculationModule.cs index 6243014142982..c30bbbc0b9a11 100644 --- a/dotnet/src/webdriver/BiDi/Speculation/SpeculationModule.cs +++ b/dotnet/src/webdriver/BiDi/Speculation/SpeculationModule.cs @@ -24,18 +24,20 @@ namespace OpenQA.Selenium.BiDi.Speculation; internal sealed class SpeculationModule : Module, ISpeculationModule { + private static readonly Event PrefetchStatusUpdatedEvent = new( + "speculation.prefetchStatusUpdated", + static (bidi, p) => new PrefetchStatusUpdatedEventArgs(bidi, p.Context, p.Url, p.Status), + Default.PrefetchStatusUpdatedParameters); + public async Task OnPrefetchStatusUpdatedAsync(Func handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("speculation.prefetchStatusUpdated", handler, CreatePrefetchStatusUpdatedEventArgs, options, Default.PrefetchStatusUpdatedParameters, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(PrefetchStatusUpdatedEvent, handler, options, cancellationToken).ConfigureAwait(false); } public async Task OnPrefetchStatusUpdatedAsync(Action handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default) { - return await SubscribeAsync("speculation.prefetchStatusUpdated", handler, CreatePrefetchStatusUpdatedEventArgs, options, Default.PrefetchStatusUpdatedParameters, cancellationToken).ConfigureAwait(false); + return await SubscribeAsync(PrefetchStatusUpdatedEvent, handler, options, cancellationToken).ConfigureAwait(false); } - - private static PrefetchStatusUpdatedEventArgs CreatePrefetchStatusUpdatedEventArgs(IBiDi bidi, PrefetchStatusUpdatedParameters p) - => new(bidi, p.Context, p.Url, p.Status); } [JsonSerializable(typeof(PrefetchStatusUpdatedParameters))]