diff --git a/dotnet/src/webdriver/BiDi/Broker.cs b/dotnet/src/webdriver/BiDi/Broker.cs index f7e23c7d2f076..8ba9b3c8d7f56 100644 --- a/dotnet/src/webdriver/BiDi/Broker.cs +++ b/dotnet/src/webdriver/BiDi/Broker.cs @@ -145,23 +145,21 @@ public async Task ExecuteCommandAsync(TCommand comma return (TResult)await tcs.Task.ConfigureAwait(false); } - public async Task SubscribeAsync(string eventName, Action action, SubscriptionOptions? options, JsonTypeInfo jsonTypeInfo) + public Task SubscribeAsync(string eventName, Action action, SubscriptionOptions? options, JsonTypeInfo jsonTypeInfo) where TEventArgs : EventArgs { - _eventTypesMap[eventName] = jsonTypeInfo; - - var handlers = _eventHandlers.GetOrAdd(eventName, (a) => []); - - var subscribeResult = await _bidi.SessionModule.SubscribeAsync([eventName], new() { Contexts = options?.Contexts, UserContexts = options?.UserContexts }).ConfigureAwait(false); - var eventHandler = new SyncEventHandler(eventName, action); + return SubscribeAsync(eventName, eventHandler, options, jsonTypeInfo); + } - handlers.Add(eventHandler); - - return new Subscription(subscribeResult.Subscription, this, eventHandler); + public Task SubscribeAsync(string eventName, Func func, SubscriptionOptions? options, JsonTypeInfo jsonTypeInfo) + where TEventArgs : EventArgs + { + var eventHandler = new AsyncEventHandler(eventName, func); + return SubscribeAsync(eventName, eventHandler, options, jsonTypeInfo); } - public async Task SubscribeAsync(string eventName, Func func, SubscriptionOptions? options, JsonTypeInfo jsonTypeInfo) + private async Task SubscribeAsync(string eventName, EventHandler eventHandler, SubscriptionOptions? options, JsonTypeInfo jsonTypeInfo) where TEventArgs : EventArgs { _eventTypesMap[eventName] = jsonTypeInfo; @@ -170,8 +168,6 @@ public async Task SubscribeAsync(string eventName, Fun var subscribeResult = await _bidi.SessionModule.SubscribeAsync([eventName], new() { Contexts = options?.Contexts, UserContexts = options?.UserContexts }).ConfigureAwait(false); - var eventHandler = new AsyncEventHandler(eventName, func); - handlers.Add(eventHandler); return new Subscription(subscribeResult.Subscription, this, eventHandler); diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs index cb53464c2a247..b03af60084a0b 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs @@ -128,222 +128,342 @@ public Task GetTreeAsync(ContextGetTreeOptions? options = null) public Task OnNavigationStartedAsync(Func handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnNavigationStartedAsync(async e => - { - if (Equals(e.Context)) - { - await handler(e).ConfigureAwait(false); - } - }, ContextSubscriptionOptions.WithContext(options, this)); + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return BiDi.BrowsingContext.OnNavigationStartedAsync( + e => HandleNavigationStartedAsync(e, handler), + ContextSubscriptionOptions.WithContext(options, this)); } public Task OnNavigationStartedAsync(Action handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnNavigationStartedAsync(e => - { - if (Equals(e.Context)) - { - handler(e); - } - }, ContextSubscriptionOptions.WithContext(options, this)); + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return BiDi.BrowsingContext.OnNavigationStartedAsync( + e => HandleNavigationStarted(e, handler), + ContextSubscriptionOptions.WithContext(options, this)); } public Task OnFragmentNavigatedAsync(Func handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnFragmentNavigatedAsync(async e => - { - if (Equals(e.Context)) - { - await handler(e).ConfigureAwait(false); - } - }, ContextSubscriptionOptions.WithContext(options, this)); + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return BiDi.BrowsingContext.OnFragmentNavigatedAsync( + e => HandleFragmentNavigatedAsync(e, handler), + ContextSubscriptionOptions.WithContext(options, this)); } public Task OnFragmentNavigatedAsync(Action handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnFragmentNavigatedAsync(e => - { - if (Equals(e.Context)) - { - handler(e); - } - }, ContextSubscriptionOptions.WithContext(options, this)); + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return BiDi.BrowsingContext.OnFragmentNavigatedAsync( + e => HandleFragmentNavigated(e, handler), + ContextSubscriptionOptions.WithContext(options, this)); } public Task OnHistoryUpdatedAsync(Func handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnHistoryUpdatedAsync(async e => - { - if (Equals(e.Context)) - { - await handler(e).ConfigureAwait(false); - } - }, ContextSubscriptionOptions.WithContext(options, this)); + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return BiDi.BrowsingContext.OnHistoryUpdatedAsync( + e => HandleHistoryUpdatedAsync(e, handler), + ContextSubscriptionOptions.WithContext(options, this)); } public Task OnHistoryUpdatedAsync(Action handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnHistoryUpdatedAsync(e => - { - if (Equals(e.Context)) - { - handler(e); - } - }, ContextSubscriptionOptions.WithContext(options, this)); + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return BiDi.BrowsingContext.OnHistoryUpdatedAsync( + e => HandleHistoryUpdated(e, handler), + ContextSubscriptionOptions.WithContext(options, this)); } public Task OnDomContentLoadedAsync(Func handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnDomContentLoadedAsync(async e => - { - if (Equals(e.Context)) - { - await handler(e).ConfigureAwait(false); - } - }, ContextSubscriptionOptions.WithContext(options, this)); + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return BiDi.BrowsingContext.OnDomContentLoadedAsync( + e => HandleDomContentLoadedAsync(e, handler), + ContextSubscriptionOptions.WithContext(options, this)); } public Task OnDomContentLoadedAsync(Action handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnDomContentLoadedAsync(e => - { - if (Equals(e.Context)) - { - handler(e); - } - }, ContextSubscriptionOptions.WithContext(options, this)); + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return BiDi.BrowsingContext.OnDomContentLoadedAsync( + e => HandleDomContentLoaded(e, handler), + ContextSubscriptionOptions.WithContext(options, this)); } public Task OnLoadAsync(Action handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnLoadAsync(e => - { - if (Equals(e.Context)) - { - handler(e); - } - }, ContextSubscriptionOptions.WithContext(options, this)); + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return BiDi.BrowsingContext.OnLoadAsync( + e => HandleLoad(e, handler), + ContextSubscriptionOptions.WithContext(options, this)); } public Task OnLoadAsync(Func handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnLoadAsync(async e => - { - if (Equals(e.Context)) - { - await handler(e).ConfigureAwait(false); - } - }, ContextSubscriptionOptions.WithContext(options, this)); + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return BiDi.BrowsingContext.OnLoadAsync( + e => HandleLoadAsync(e, handler), + ContextSubscriptionOptions.WithContext(options, this)); } public Task OnDownloadWillBeginAsync(Action handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnDownloadWillBeginAsync(e => - { - if (Equals(e.Context)) - { - handler(e); - } - }, ContextSubscriptionOptions.WithContext(options, this)); + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return BiDi.BrowsingContext.OnDownloadWillBeginAsync( + e => HandleDownloadWillBegin(e, handler), + ContextSubscriptionOptions.WithContext(options, this)); } public Task OnDownloadWillBeginAsync(Func handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnDownloadWillBeginAsync(async e => - { - if (Equals(e.Context)) - { - await handler(e).ConfigureAwait(false); - } - }, ContextSubscriptionOptions.WithContext(options, this)); + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return BiDi.BrowsingContext.OnDownloadWillBeginAsync( + e => HandleDownloadWillBeginAsync(e, handler), + ContextSubscriptionOptions.WithContext(options, this)); } public Task OnDownloadEndAsync(Action handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnDownloadEndAsync(e => - { - if (Equals(e.Context)) - { - handler(e); - } - }, ContextSubscriptionOptions.WithContext(options, this)); + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return BiDi.BrowsingContext.OnDownloadEndAsync( + e => HandleDownloadEnd(e, handler), + ContextSubscriptionOptions.WithContext(options, this)); } public Task OnDownloadEndAsync(Func handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnDownloadEndAsync(async e => - { - if (Equals(e.Context)) - { - await handler(e).ConfigureAwait(false); - } - }, ContextSubscriptionOptions.WithContext(options, this)); + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return BiDi.BrowsingContext.OnDownloadEndAsync( + e => HandleDownloadEndAsync(e, handler), + ContextSubscriptionOptions.WithContext(options, this)); } public Task OnNavigationAbortedAsync(Action handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnNavigationAbortedAsync(e => - { - if (Equals(e.Context)) - { - handler(e); - } - }, ContextSubscriptionOptions.WithContext(options, this)); + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return BiDi.BrowsingContext.OnNavigationAbortedAsync( + e => HandleNavigationAborted(e, handler), + ContextSubscriptionOptions.WithContext(options, this)); } public Task OnNavigationAbortedAsync(Func handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnNavigationAbortedAsync(async e => - { - if (Equals(e.Context)) - { - await handler(e).ConfigureAwait(false); - } - }, ContextSubscriptionOptions.WithContext(options, this)); + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return BiDi.BrowsingContext.OnNavigationAbortedAsync( + e => HandleNavigationAbortedAsync(e, handler), + ContextSubscriptionOptions.WithContext(options, this)); } public Task OnNavigationFailedAsync(Action handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnNavigationFailedAsync(e => - { - if (Equals(e.Context)) - { - handler(e); - } - }, ContextSubscriptionOptions.WithContext(options, this)); + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return BiDi.BrowsingContext.OnNavigationFailedAsync( + e => HandleNavigationFailed(e, handler), + ContextSubscriptionOptions.WithContext(options, this)); } public Task OnNavigationFailedAsync(Func handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnNavigationFailedAsync(async e => - { - if (Equals(e.Context)) - { - await handler(e).ConfigureAwait(false); - } - }, ContextSubscriptionOptions.WithContext(options, this)); + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return BiDi.BrowsingContext.OnNavigationFailedAsync( + e => HandleNavigationFailedAsync(e, handler), + ContextSubscriptionOptions.WithContext(options, this)); } public Task OnNavigationCommittedAsync(Action handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnNavigationCommittedAsync(e => - { - if (Equals(e.Context)) - { - handler(e); - } - }, ContextSubscriptionOptions.WithContext(options, this)); + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return BiDi.BrowsingContext.OnNavigationCommittedAsync( + e => HandleNavigationCommitted(e, handler), + ContextSubscriptionOptions.WithContext(options, this)); } public Task OnNavigationCommittedAsync(Func handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnNavigationCommittedAsync(async e => + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return BiDi.BrowsingContext.OnNavigationCommittedAsync( + e => HandleNavigationCommittedAsync(e, handler), + ContextSubscriptionOptions.WithContext(options, this)); + } + + private async Task HandleNavigationStartedAsync(NavigationInfo e, Func handler) + { + if (Equals(e.Context)) + { + await handler(e).ConfigureAwait(false); + } + } + + private void HandleNavigationStarted(NavigationInfo e, Action handler) + { + if (Equals(e.Context)) + { + handler(e); + } + } + + private async Task HandleFragmentNavigatedAsync(NavigationInfo e, Func handler) + { + if (Equals(e.Context)) + { + await handler(e).ConfigureAwait(false); + } + } + + private void HandleFragmentNavigated(NavigationInfo e, Action handler) + { + if (Equals(e.Context)) + { + handler(e); + } + } + + private async Task HandleHistoryUpdatedAsync(HistoryUpdatedEventArgs e, Func handler) + { + if (Equals(e.Context)) + { + await handler(e).ConfigureAwait(false); + } + } + + private void HandleHistoryUpdated(HistoryUpdatedEventArgs e, Action handler) + { + if (Equals(e.Context)) + { + handler(e); + } + } + + private async Task HandleDomContentLoadedAsync(NavigationInfo e, Func handler) + { + if (Equals(e.Context)) + { + await handler(e).ConfigureAwait(false); + } + } + + private void HandleDomContentLoaded(NavigationInfo e, Action handler) + { + if (Equals(e.Context)) + { + handler(e); + } + } + + private void HandleLoad(NavigationInfo e, Action handler) + { + if (Equals(e.Context)) + { + handler(e); + } + } + + private async Task HandleLoadAsync(NavigationInfo e, Func handler) + { + if (Equals(e.Context)) + { + await handler(e).ConfigureAwait(false); + } + } + + private void HandleDownloadWillBegin(DownloadWillBeginEventArgs e, Action handler) + { + if (Equals(e.Context)) + { + handler(e); + } + } + + private async Task HandleDownloadWillBeginAsync(DownloadWillBeginEventArgs e, Func handler) + { + if (Equals(e.Context)) + { + await handler(e).ConfigureAwait(false); + } + } + + private void HandleDownloadEnd(DownloadEndEventArgs e, Action handler) + { + if (Equals(e.Context)) + { + handler(e); + } + } + + private async Task HandleDownloadEndAsync(DownloadEndEventArgs e, Func handler) + { + if (Equals(e.Context)) + { + await handler(e).ConfigureAwait(false); + } + } + + private void HandleNavigationAborted(NavigationInfo e, Action handler) + { + if (Equals(e.Context)) + { + handler(e); + } + } + + private async Task HandleNavigationAbortedAsync(NavigationInfo e, Func handler) + { + if (Equals(e.Context)) + { + await handler(e).ConfigureAwait(false); + } + } + + private void HandleNavigationFailed(NavigationInfo e, Action handler) + { + if (Equals(e.Context)) + { + handler(e); + } + } + + private async Task HandleNavigationFailedAsync(NavigationInfo e, Func handler) + { + if (Equals(e.Context)) + { + await handler(e).ConfigureAwait(false); + } + } + + private void HandleNavigationCommitted(NavigationInfo e, Action handler) + { + if (Equals(e.Context)) + { + handler(e); + } + } + + private async Task HandleNavigationCommittedAsync(NavigationInfo e, Func handler) + { + if (Equals(e.Context)) { - if (Equals(e.Context)) - { - await handler(e).ConfigureAwait(false); - } - }, ContextSubscriptionOptions.WithContext(options, this)); + await handler(e).ConfigureAwait(false); + } } public bool Equals(BrowsingContext? other) diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextInputModule.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextInputModule.cs index da4666a6f3bfc..d8e991ce8c730 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextInputModule.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextInputModule.cs @@ -43,23 +43,35 @@ public Task SetFilesAsync(Script.ISharedReference element, IEnum public Task OnFileDialogOpenedAsync(Func handler, ContextSubscriptionOptions? options = null) { - return inputModule.OnFileDialogOpenedAsync(async e => - { - if (context.Equals(e.Context)) - { - await handler(e).ConfigureAwait(false); - } - }, ContextSubscriptionOptions.WithContext(options, context)); + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return inputModule.OnFileDialogOpenedAsync( + e => HandleFileDialogOpenedAsync(e, handler), + ContextSubscriptionOptions.WithContext(options, context)); } public Task OnFileDialogOpenedAsync(Action handler, ContextSubscriptionOptions? options = null) { - return inputModule.OnFileDialogOpenedAsync(e => + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return inputModule.OnFileDialogOpenedAsync( + e => HandleFileDialogOpened(e, handler), + ContextSubscriptionOptions.WithContext(options, context)); + } + + private async Task HandleFileDialogOpenedAsync(FileDialogInfo e, Func handler) + { + if (context.Equals(e.Context)) + { + await handler(e).ConfigureAwait(false); + } + } + + private void HandleFileDialogOpened(FileDialogInfo e, Action handler) + { + if (context.Equals(e.Context)) { - if (context.Equals(e.Context)) - { - handler(e); - } - }, ContextSubscriptionOptions.WithContext(options, context)); + handler(e); + } } } diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextLogModule.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextLogModule.cs index 0327a48044dca..08296bd783fb0 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextLogModule.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextLogModule.cs @@ -27,23 +27,35 @@ public sealed class BrowsingContextLogModule(BrowsingContext context, LogModule { public Task OnEntryAddedAsync(Func handler, ContextSubscriptionOptions? options = null) { - return logModule.OnEntryAddedAsync(async args => - { - if (context.Equals(args.Source.Context)) - { - await handler(args).ConfigureAwait(false); - } - }, new SubscriptionOptions() { Timeout = options?.Timeout }); // special case, don't scope to context, awaiting https://github.com/w3c/webdriver-bidi/issues/1032 + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return logModule.OnEntryAddedAsync( + e => HandleEntryAddedAsync(e, handler), + ContextSubscriptionOptions.WithContext(options, context)); } public Task OnEntryAddedAsync(Action handler, ContextSubscriptionOptions? options = null) { - return logModule.OnEntryAddedAsync(args => + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return logModule.OnEntryAddedAsync( + e => HandleEntryAdded(e, handler), + ContextSubscriptionOptions.WithContext(options, context)); + } + + private async Task HandleEntryAddedAsync(Log.LogEntry e, Func handler) + { + if (context.Equals(e.Source.Context)) + { + await handler(e).ConfigureAwait(false); + } + } + + private void HandleEntryAdded(Log.LogEntry e, Action handler) + { + if (context.Equals(e.Source.Context)) { - if (context.Equals(args.Source.Context)) - { - handler(args); - } - }, new SubscriptionOptions() { Timeout = options?.Timeout }); // special case, don't scope to context, awaiting https://github.com/w3c/webdriver-bidi/issues/1032 + handler(e); + } } } diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextNetworkModule.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextNetworkModule.cs index d3fdd49e31e8b..3517674956b91 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextNetworkModule.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextNetworkModule.cs @@ -92,112 +92,172 @@ public Task SetCacheBehaviorAsync(CacheBehavior behavior public Task OnBeforeRequestSentAsync(Func handler, ContextSubscriptionOptions? options = null) { - return networkModule.OnBeforeRequestSentAsync(async e => - { - if (context.Equals(e.Context)) - { - await handler(e).ConfigureAwait(false); - } - }, ContextSubscriptionOptions.WithContext(options, context)); + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return networkModule.OnBeforeRequestSentAsync( + e => HandleBeforeRequestSentAsync(e, handler), + ContextSubscriptionOptions.WithContext(options, context)); } public Task OnBeforeRequestSentAsync(Action handler, ContextSubscriptionOptions? options = null) { - return networkModule.OnBeforeRequestSentAsync(e => - { - if (context.Equals(e.Context)) - { - handler(e); - } - }, ContextSubscriptionOptions.WithContext(options, context)); + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return networkModule.OnBeforeRequestSentAsync( + e => HandleBeforeRequestSent(e, handler), + ContextSubscriptionOptions.WithContext(options, context)); } public Task OnResponseStartedAsync(Func handler, ContextSubscriptionOptions? options = null) { - return networkModule.OnResponseStartedAsync(async e => - { - if (context.Equals(e.Context)) - { - await handler(e).ConfigureAwait(false); - } - }, ContextSubscriptionOptions.WithContext(options, context)); + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return networkModule.OnResponseStartedAsync(e + => HandleResponseStartedAsync(e, handler), + ContextSubscriptionOptions.WithContext(options, context)); } public Task OnResponseStartedAsync(Action handler, ContextSubscriptionOptions? options = null) { - return networkModule.OnResponseStartedAsync(e => - { - if (context.Equals(e.Context)) - { - handler(e); - } - }, ContextSubscriptionOptions.WithContext(options, context)); + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return networkModule.OnResponseStartedAsync( + e => HandleResponseStarted(e, handler), + ContextSubscriptionOptions.WithContext(options, context)); } public Task OnResponseCompletedAsync(Func handler, ContextSubscriptionOptions? options = null) { - return networkModule.OnResponseCompletedAsync(async e => - { - if (context.Equals(e.Context)) - { - await handler(e).ConfigureAwait(false); - } - }, ContextSubscriptionOptions.WithContext(options, context)); + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return networkModule.OnResponseCompletedAsync( + e => HandleResponseCompletedAsync(e, handler), + ContextSubscriptionOptions.WithContext(options, context)); } public Task OnResponseCompletedAsync(Action handler, ContextSubscriptionOptions? options = null) { - return networkModule.OnResponseCompletedAsync(e => - { - if (context.Equals(e.Context)) - { - handler(e); - } - }, ContextSubscriptionOptions.WithContext(options, context)); + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return networkModule.OnResponseCompletedAsync( + e => HandleResponseCompleted(e, handler), + ContextSubscriptionOptions.WithContext(options, context)); } public Task OnFetchErrorAsync(Func handler, ContextSubscriptionOptions? options = null) { - return networkModule.OnFetchErrorAsync(async e => - { - if (context.Equals(e.Context)) - { - await handler(e).ConfigureAwait(false); - } - }, ContextSubscriptionOptions.WithContext(options, context)); + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return networkModule.OnFetchErrorAsync( + e => HandleFetchErrorAsync(e, handler), + ContextSubscriptionOptions.WithContext(options, context)); } public Task OnFetchErrorAsync(Action handler, ContextSubscriptionOptions? options = null) { - return networkModule.OnFetchErrorAsync(e => - { - if (context.Equals(e.Context)) - { - handler(e); - } - }, ContextSubscriptionOptions.WithContext(options, context)); + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return networkModule.OnFetchErrorAsync( + e => HandleFetchError(e, handler), + ContextSubscriptionOptions.WithContext(options, context)); } public Task OnAuthRequiredAsync(Func handler, ContextSubscriptionOptions? options = null) { - return networkModule.OnAuthRequiredAsync(async e => - { - if (context.Equals(e.Context)) - { - await handler(e).ConfigureAwait(false); - } - }, ContextSubscriptionOptions.WithContext(options, context)); + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return networkModule.OnAuthRequiredAsync( + e => HandleAuthRequiredAsync(e, handler), + ContextSubscriptionOptions.WithContext(options, context)); } public Task OnAuthRequiredAsync(Action handler, ContextSubscriptionOptions? options = null) { - return networkModule.OnAuthRequiredAsync(e => + if (handler is null) throw new ArgumentNullException(nameof(handler)); + + return networkModule.OnAuthRequiredAsync( + e => HandleAuthRequired(e, handler), + ContextSubscriptionOptions.WithContext(options, context)); + } + + private async Task HandleBeforeRequestSentAsync(BeforeRequestSentEventArgs e, Func handler) + { + if (context.Equals(e.Context)) + { + await handler(e).ConfigureAwait(false); + } + } + + private void HandleBeforeRequestSent(BeforeRequestSentEventArgs e, Action handler) + { + if (context.Equals(e.Context)) + { + handler(e); + } + } + + private async Task HandleResponseStartedAsync(ResponseStartedEventArgs e, Func handler) + { + if (context.Equals(e.Context)) + { + await handler(e).ConfigureAwait(false); + } + } + + private void HandleResponseStarted(ResponseStartedEventArgs e, Action handler) + { + if (context.Equals(e.Context)) + { + handler(e); + } + } + + private async Task HandleResponseCompletedAsync(ResponseCompletedEventArgs e, Func handler) + { + if (context.Equals(e.Context)) + { + await handler(e).ConfigureAwait(false); + } + } + + private void HandleResponseCompleted(ResponseCompletedEventArgs e, Action handler) + { + if (context.Equals(e.Context)) + { + handler(e); + } + } + + private async Task HandleFetchErrorAsync(FetchErrorEventArgs e, Func handler) + { + if (context.Equals(e.Context)) + { + await handler(e).ConfigureAwait(false); + } + } + + private void HandleFetchError(FetchErrorEventArgs e, Action handler) + { + if (context.Equals(e.Context)) + { + handler(e); + } + } + + private async Task HandleAuthRequiredAsync(AuthRequiredEventArgs e, Func handler) + { + if (context.Equals(e.Context)) + { + await handler(e).ConfigureAwait(false); + } + } + + private void HandleAuthRequired(AuthRequiredEventArgs e, Action handler) + { + if (context.Equals(e.Context)) { - if (context.Equals(e.Context)) - { - handler(e); - } - }, ContextSubscriptionOptions.WithContext(options, context)); + handler(e); + } } } diff --git a/dotnet/src/webdriver/BiDi/EventHandler.cs b/dotnet/src/webdriver/BiDi/EventHandler.cs index b5d7ca9b3feac..12cde327e3601 100644 --- a/dotnet/src/webdriver/BiDi/EventHandler.cs +++ b/dotnet/src/webdriver/BiDi/EventHandler.cs @@ -32,7 +32,7 @@ internal abstract class EventHandler(string eventName) internal class AsyncEventHandler(string eventName, Func func) : EventHandler(eventName) where TEventArgs : EventArgs { - private readonly Func _func = func; + private readonly Func _func = func ?? throw new ArgumentNullException(nameof(func), "Async event handler function cannot be null."); public override async ValueTask InvokeAsync(EventArgs args) { @@ -43,7 +43,7 @@ public override async ValueTask InvokeAsync(EventArgs args) internal class SyncEventHandler(string eventName, Action action) : EventHandler(eventName) where TEventArgs : EventArgs { - private readonly Action _action = action; + private readonly Action _action = action ?? throw new ArgumentNullException(nameof(action), "Sync event handler action cannot be null."); public override ValueTask InvokeAsync(EventArgs args) {