Skip to content

Commit c064dcf

Browse files
authored
[dotnet] [bidi] Fix events subscription possibilities (#16603)
1 parent 0c17ff1 commit c064dcf

File tree

7 files changed

+124
-138
lines changed

7 files changed

+124
-138
lines changed

dotnet/src/webdriver/BiDi/Broker.cs

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -162,26 +162,13 @@ public async Task<Subscription> SubscribeAsync<TEventArgs>(string eventName, Act
162162

163163
var handlers = _eventHandlers.GetOrAdd(eventName, (a) => []);
164164

165-
if (options is BrowsingContextsSubscriptionOptions browsingContextsOptions)
166-
{
167-
var subscribeResult = await _bidi.SessionModule.SubscribeAsync([eventName], new() { Contexts = browsingContextsOptions.Contexts }).ConfigureAwait(false);
168-
169-
var eventHandler = new SyncEventHandler<TEventArgs>(eventName, action, browsingContextsOptions?.Contexts);
165+
var subscribeResult = await _bidi.SessionModule.SubscribeAsync([eventName], new() { Contexts = options?.Contexts, UserContexts = options?.UserContexts }).ConfigureAwait(false);
170166

171-
handlers.Add(eventHandler);
167+
var eventHandler = new SyncEventHandler<TEventArgs>(eventName, action, options?.Contexts);
172168

173-
return new Subscription(subscribeResult.Subscription, this, eventHandler);
174-
}
175-
else
176-
{
177-
var subscribeResult = await _bidi.SessionModule.SubscribeAsync([eventName]).ConfigureAwait(false);
169+
handlers.Add(eventHandler);
178170

179-
var eventHandler = new SyncEventHandler<TEventArgs>(eventName, action);
180-
181-
handlers.Add(eventHandler);
182-
183-
return new Subscription(subscribeResult.Subscription, this, eventHandler);
184-
}
171+
return new Subscription(subscribeResult.Subscription, this, eventHandler);
185172
}
186173

187174
public async Task<Subscription> SubscribeAsync<TEventArgs>(string eventName, Func<TEventArgs, Task> func, SubscriptionOptions? options, JsonTypeInfo<TEventArgs> jsonTypeInfo)
@@ -191,26 +178,13 @@ public async Task<Subscription> SubscribeAsync<TEventArgs>(string eventName, Fun
191178

192179
var handlers = _eventHandlers.GetOrAdd(eventName, (a) => []);
193180

194-
if (options is BrowsingContextsSubscriptionOptions browsingContextsOptions)
195-
{
196-
var subscribeResult = await _bidi.SessionModule.SubscribeAsync([eventName], new() { Contexts = browsingContextsOptions.Contexts }).ConfigureAwait(false);
197-
198-
var eventHandler = new AsyncEventHandler<TEventArgs>(eventName, func, browsingContextsOptions.Contexts);
181+
var subscribeResult = await _bidi.SessionModule.SubscribeAsync([eventName], new() { Contexts = options?.Contexts, UserContexts = options?.UserContexts }).ConfigureAwait(false);
199182

200-
handlers.Add(eventHandler);
183+
var eventHandler = new AsyncEventHandler<TEventArgs>(eventName, func, options?.Contexts);
201184

202-
return new Subscription(subscribeResult.Subscription, this, eventHandler);
203-
}
204-
else
205-
{
206-
var subscribeResult = await _bidi.SessionModule.SubscribeAsync([eventName]).ConfigureAwait(false);
185+
handlers.Add(eventHandler);
207186

208-
var eventHandler = new AsyncEventHandler<TEventArgs>(eventName, func);
209-
210-
handlers.Add(eventHandler);
211-
212-
return new Subscription(subscribeResult.Subscription, this, eventHandler);
213-
}
187+
return new Subscription(subscribeResult.Subscription, this, eventHandler);
214188
}
215189

216190
public async Task UnsubscribeAsync(Subscription subscription)

dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -116,104 +116,104 @@ public Task<GetTreeResult> GetTreeAsync(BrowsingContextGetTreeOptions? options =
116116
return BiDi.BrowsingContext.GetTreeAsync(getTreeOptions);
117117
}
118118

119-
public Task<Subscription> OnNavigationStartedAsync(Func<NavigationInfo, Task> handler, SubscriptionOptions? options = null)
119+
public Task<Subscription> OnNavigationStartedAsync(Func<NavigationInfo, Task> handler, ContextSubscriptionOptions? options = null)
120120
{
121-
return BiDi.BrowsingContext.OnNavigationStartedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
121+
return BiDi.BrowsingContext.OnNavigationStartedAsync(handler, options.WithContext(this));
122122
}
123123

124-
public Task<Subscription> OnNavigationStartedAsync(Action<NavigationInfo> handler, SubscriptionOptions? options = null)
124+
public Task<Subscription> OnNavigationStartedAsync(Action<NavigationInfo> handler, ContextSubscriptionOptions? options = null)
125125
{
126-
return BiDi.BrowsingContext.OnNavigationStartedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
126+
return BiDi.BrowsingContext.OnNavigationStartedAsync(handler, options.WithContext(this));
127127
}
128128

129-
public Task<Subscription> OnFragmentNavigatedAsync(Func<NavigationInfo, Task> handler, SubscriptionOptions? options = null)
129+
public Task<Subscription> OnFragmentNavigatedAsync(Func<NavigationInfo, Task> handler, ContextSubscriptionOptions? options = null)
130130
{
131-
return BiDi.BrowsingContext.OnFragmentNavigatedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
131+
return BiDi.BrowsingContext.OnFragmentNavigatedAsync(handler, options.WithContext(this));
132132
}
133133

134-
public Task<Subscription> OnFragmentNavigatedAsync(Action<NavigationInfo> handler, SubscriptionOptions? options = null)
134+
public Task<Subscription> OnFragmentNavigatedAsync(Action<NavigationInfo> handler, ContextSubscriptionOptions? options = null)
135135
{
136-
return BiDi.BrowsingContext.OnFragmentNavigatedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
136+
return BiDi.BrowsingContext.OnFragmentNavigatedAsync(handler, options.WithContext(this));
137137
}
138138

139-
public Task<Subscription> OnHistoryUpdatedAsync(Func<HistoryUpdatedEventArgs, Task> handler, SubscriptionOptions? options = null)
139+
public Task<Subscription> OnHistoryUpdatedAsync(Func<HistoryUpdatedEventArgs, Task> handler, ContextSubscriptionOptions? options = null)
140140
{
141-
return BiDi.BrowsingContext.OnHistoryUpdatedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
141+
return BiDi.BrowsingContext.OnHistoryUpdatedAsync(handler, options.WithContext(this));
142142
}
143143

144-
public Task<Subscription> OnHistoryUpdatedAsync(Action<HistoryUpdatedEventArgs> handler, SubscriptionOptions? options = null)
144+
public Task<Subscription> OnHistoryUpdatedAsync(Action<HistoryUpdatedEventArgs> handler, ContextSubscriptionOptions? options = null)
145145
{
146-
return BiDi.BrowsingContext.OnHistoryUpdatedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
146+
return BiDi.BrowsingContext.OnHistoryUpdatedAsync(handler, options.WithContext(this));
147147
}
148148

149-
public Task<Subscription> OnDomContentLoadedAsync(Func<NavigationInfo, Task> handler, SubscriptionOptions? options = null)
149+
public Task<Subscription> OnDomContentLoadedAsync(Func<NavigationInfo, Task> handler, ContextSubscriptionOptions? options = null)
150150
{
151-
return BiDi.BrowsingContext.OnDomContentLoadedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
151+
return BiDi.BrowsingContext.OnDomContentLoadedAsync(handler, options.WithContext(this));
152152
}
153153

154-
public Task<Subscription> OnDomContentLoadedAsync(Action<NavigationInfo> handler, SubscriptionOptions? options = null)
154+
public Task<Subscription> OnDomContentLoadedAsync(Action<NavigationInfo> handler, ContextSubscriptionOptions? options = null)
155155
{
156-
return BiDi.BrowsingContext.OnDomContentLoadedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
156+
return BiDi.BrowsingContext.OnDomContentLoadedAsync(handler, options.WithContext(this));
157157
}
158158

159-
public Task<Subscription> OnLoadAsync(Action<NavigationInfo> handler, SubscriptionOptions? options = null)
159+
public Task<Subscription> OnLoadAsync(Action<NavigationInfo> handler, ContextSubscriptionOptions? options = null)
160160
{
161-
return BiDi.BrowsingContext.OnLoadAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
161+
return BiDi.BrowsingContext.OnLoadAsync(handler, options.WithContext(this));
162162
}
163163

164-
public Task<Subscription> OnLoadAsync(Func<NavigationInfo, Task> handler, SubscriptionOptions? options = null)
164+
public Task<Subscription> OnLoadAsync(Func<NavigationInfo, Task> handler, ContextSubscriptionOptions? options = null)
165165
{
166-
return BiDi.BrowsingContext.OnLoadAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
166+
return BiDi.BrowsingContext.OnLoadAsync(handler, options.WithContext(this));
167167
}
168168

169-
public Task<Subscription> OnDownloadWillBeginAsync(Action<DownloadWillBeginEventArgs> handler, SubscriptionOptions? options = null)
169+
public Task<Subscription> OnDownloadWillBeginAsync(Action<DownloadWillBeginEventArgs> handler, ContextSubscriptionOptions? options = null)
170170
{
171-
return BiDi.BrowsingContext.OnDownloadWillBeginAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
171+
return BiDi.BrowsingContext.OnDownloadWillBeginAsync(handler, options.WithContext(this));
172172
}
173173

174-
public Task<Subscription> OnDownloadWillBeginAsync(Func<DownloadWillBeginEventArgs, Task> handler, SubscriptionOptions? options = null)
174+
public Task<Subscription> OnDownloadWillBeginAsync(Func<DownloadWillBeginEventArgs, Task> handler, ContextSubscriptionOptions? options = null)
175175
{
176-
return BiDi.BrowsingContext.OnDownloadWillBeginAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
176+
return BiDi.BrowsingContext.OnDownloadWillBeginAsync(handler, options.WithContext(this));
177177
}
178178

179-
public Task<Subscription> OnDownloadEndAsync(Action<DownloadEndEventArgs> handler, SubscriptionOptions? options = null)
179+
public Task<Subscription> OnDownloadEndAsync(Action<DownloadEndEventArgs> handler, ContextSubscriptionOptions? options = null)
180180
{
181-
return BiDi.BrowsingContext.OnDownloadEndAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
181+
return BiDi.BrowsingContext.OnDownloadEndAsync(handler, options.WithContext(this));
182182
}
183183

184-
public Task<Subscription> OnDownloadEndAsync(Func<DownloadEndEventArgs, Task> handler, SubscriptionOptions? options = null)
184+
public Task<Subscription> OnDownloadEndAsync(Func<DownloadEndEventArgs, Task> handler, ContextSubscriptionOptions? options = null)
185185
{
186-
return BiDi.BrowsingContext.OnDownloadEndAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
186+
return BiDi.BrowsingContext.OnDownloadEndAsync(handler, options.WithContext(this));
187187
}
188188

189-
public Task<Subscription> OnNavigationAbortedAsync(Action<NavigationInfo> handler, SubscriptionOptions? options = null)
189+
public Task<Subscription> OnNavigationAbortedAsync(Action<NavigationInfo> handler, ContextSubscriptionOptions? options = null)
190190
{
191-
return BiDi.BrowsingContext.OnNavigationAbortedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
191+
return BiDi.BrowsingContext.OnNavigationAbortedAsync(handler, options.WithContext(this));
192192
}
193193

194-
public Task<Subscription> OnNavigationAbortedAsync(Func<NavigationInfo, Task> handler, SubscriptionOptions? options = null)
194+
public Task<Subscription> OnNavigationAbortedAsync(Func<NavigationInfo, Task> handler, ContextSubscriptionOptions? options = null)
195195
{
196-
return BiDi.BrowsingContext.OnNavigationAbortedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
196+
return BiDi.BrowsingContext.OnNavigationAbortedAsync(handler, options.WithContext(this));
197197
}
198198

199-
public Task<Subscription> OnNavigationFailedAsync(Action<NavigationInfo> handler, SubscriptionOptions? options = null)
199+
public Task<Subscription> OnNavigationFailedAsync(Action<NavigationInfo> handler, ContextSubscriptionOptions? options = null)
200200
{
201-
return BiDi.BrowsingContext.OnNavigationFailedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
201+
return BiDi.BrowsingContext.OnNavigationFailedAsync(handler, options.WithContext(this));
202202
}
203203

204-
public Task<Subscription> OnNavigationFailedAsync(Func<NavigationInfo, Task> handler, SubscriptionOptions? options = null)
204+
public Task<Subscription> OnNavigationFailedAsync(Func<NavigationInfo, Task> handler, ContextSubscriptionOptions? options = null)
205205
{
206-
return BiDi.BrowsingContext.OnNavigationFailedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
206+
return BiDi.BrowsingContext.OnNavigationFailedAsync(handler, options.WithContext(this));
207207
}
208208

209-
public Task<Subscription> OnNavigationCommittedAsync(Action<NavigationInfo> handler, SubscriptionOptions? options = null)
209+
public Task<Subscription> OnNavigationCommittedAsync(Action<NavigationInfo> handler, ContextSubscriptionOptions? options = null)
210210
{
211-
return BiDi.BrowsingContext.OnNavigationCommittedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
211+
return BiDi.BrowsingContext.OnNavigationCommittedAsync(handler, options.WithContext(this));
212212
}
213213

214-
public Task<Subscription> OnNavigationCommittedAsync(Func<NavigationInfo, Task> handler, SubscriptionOptions? options = null)
214+
public Task<Subscription> OnNavigationCommittedAsync(Func<NavigationInfo, Task> handler, ContextSubscriptionOptions? options = null)
215215
{
216-
return BiDi.BrowsingContext.OnNavigationCommittedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
216+
return BiDi.BrowsingContext.OnNavigationCommittedAsync(handler, options.WithContext(this));
217217
}
218218

219219
public override bool Equals(object? obj)

dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextLogModule.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,32 @@
1818
// </copyright>
1919

2020
using OpenQA.Selenium.BiDi.Log;
21-
using System.Threading.Tasks;
2221
using System;
22+
using System.Threading.Tasks;
2323

2424
namespace OpenQA.Selenium.BiDi.BrowsingContext;
2525

2626
public sealed class BrowsingContextLogModule(BrowsingContext context, LogModule logModule)
2727
{
28-
public Task<Subscription> OnEntryAddedAsync(Func<Log.LogEntry, Task> handler, SubscriptionOptions? options = null)
28+
public Task<Subscription> OnEntryAddedAsync(Func<Log.LogEntry, Task> handler, ContextSubscriptionOptions? options = null)
2929
{
3030
return logModule.OnEntryAddedAsync(async args =>
3131
{
3232
if (args.Source.Context?.Equals(context) is true)
3333
{
3434
await handler(args).ConfigureAwait(false);
3535
}
36-
}, options);
36+
}, new SubscriptionOptions() { Timeout = options?.Timeout }); // special case, don't scope to context, awaiting https://github.com/w3c/webdriver-bidi/issues/1032
3737
}
3838

39-
public Task<Subscription> OnEntryAddedAsync(Action<Log.LogEntry> handler, SubscriptionOptions? options = null)
39+
public Task<Subscription> OnEntryAddedAsync(Action<Log.LogEntry> handler, ContextSubscriptionOptions? options = null)
4040
{
4141
return logModule.OnEntryAddedAsync(args =>
4242
{
4343
if (args.Source.Context?.Equals(context) is true)
4444
{
4545
handler(args);
4646
}
47-
}, options);
47+
}, new SubscriptionOptions() { Timeout = options?.Timeout }); // special case, don't scope to context, awaiting https://github.com/w3c/webdriver-bidi/issues/1032
4848
}
4949
}

0 commit comments

Comments
 (0)