Skip to content

[java][bidi] Add subscription scope - #17925

Open
pujagani wants to merge 1 commit into
SeleniumHQ:trunkfrom
pujagani:subscription-scope-java
Open

[java][bidi] Add subscription scope#17925
pujagani wants to merge 1 commit into
SeleniumHQ:trunkfrom
pujagani:subscription-scope-java

Conversation

@pujagani

Copy link
Copy Markdown
Contributor

🔗 Related Issues

Add subscription scope to allow users to subscribe using user contexts and browsing contexts. Though, currently BiDi spec draft is working on received events to provide these parameters so clients can redirect events to correct subscribers. This will be required eventually for high-level work and for generated modules.

💥 What does this PR do?

🔧 Implementation Notes

Additional builder class is the a Java pattern that fits the best, in case in future more parameters for subscription are added.

🤖 AI assistance

  • No substantial AI assistance used
  • AI assisted (complete below)
    • Tool(s):
    • What was generated:
    • I reviewed all AI output and can explain the change

💡 Additional Considerations

🔄 Types of changes

  • New feature (non-breaking change which adds functionality and tests!)

@selenium-ci selenium-ci added C-java Java Bindings B-devtools Includes everything BiDi or Chrome DevTools related labels Aug 18, 2026
@qodo-code-review

Copy link
Copy Markdown
Contributor

PR Summary by Qodo

Add scoped Java BiDi event subscriptions

✨ Enhancement 🕐 20-40 Minutes

Grey Divider

AI Description

• Add fluent subscription scopes for browsing contexts and user contexts.
• Expose scoped subscriptions through generated BiDi modules and transport handles.
• Include scope fields in session.subscribe while retaining subscription-ID listener routing.
Diagram

sequenceDiagram
  actor Client
  participant Module
  participant Handle
  participant BiDi
  participant Remote as BiDi Remote
  participant Connection
  Client->>Module: subscribe event with scope
  Module->>Handle: forward subscription
  Handle->>BiDi: add scoped listener
  BiDi->>BiDi: merge event and scope
  BiDi->>Remote: session.subscribe parameters
  Remote-->>BiDi: subscription id
  BiDi->>Connection: register listener by id
  BiDi-->>Client: subscription id
Loading
High-Level Assessment

The dedicated fluent scope object is the preferred approach because it avoids proliferating overloads as subscription parameters evolve, preserves existing global subscription behavior, and keeps protocol scope serialization centralized. Delegating combination validation to the remote end also matches the BiDi specification boundary.

Files changed (4) +104 / -0

Enhancement (4) +104 / -0
BiDi.javaSubscribe with serialized context scope +13/-0

Subscribe with serialized context scope

• Adds an internal listener overload that validates a subscription scope, merges its context fields with the event name, and sends the resulting 'session.subscribe' command. The returned subscription ID continues to identify the locally registered handler.

java/src/org/openqa/selenium/bidi/BiDi.java

Handle.javaForward scoped subscriptions to BiDi +4/-0

Forward scoped subscriptions to BiDi

• Adds a package-private scoped subscription overload that delegates module requests to the shared 'BiDi' instance.

java/src/org/openqa/selenium/bidi/Handle.java

Module.javaExpose scoped subscriptions to BiDi modules +13/-0

Expose scoped subscriptions to BiDi modules

• Adds a documented public 'subscribe' overload accepting 'SubscriptionScope', enabling generated and handwritten modules to limit events by browsing or user context.

java/src/org/openqa/selenium/bidi/Module.java

SubscriptionScope.javaDefine fluent BiDi subscription scopes +74/-0

Define fluent BiDi subscription scopes

• Introduces a beta fluent scope object for immutable copies of browsing-context and user-context ID sets. Its package-private serializer omits empty fields and emits the BiDi 'contexts' and 'userContexts' parameters.

java/src/org/openqa/selenium/bidi/SubscriptionScope.java

@qodo-code-review

Copy link
Copy Markdown
Contributor

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (2) 📜 Skill insights (0)

Grey Divider


Action required

1. Scoped callbacks cross-deliver 🐞 Bug ≡ Correctness
Description
The scoped addListener registers only the subscription ID and event, discarding its
SubscriptionScope; Connection then invokes every handler registered for the matching event
method. Consequently, when the same event has subscriptions for different contexts, each handler
receives the other scopes' events (or duplicate callbacks if the remote emits per subscription), so
the new scope API does not provide scoped listener behavior.
Code

java/src/org/openqa/selenium/bidi/BiDi.java[128]

+    connection.addListener(subscriptionId, event, handler);
Evidence
The public API says the subscription applies only to selected contexts, and SubscriptionScope
sends those filters remotely. However, listener registration stores the handler under only its event
and subscription ID, while incoming event dispatch matches solely on the event method and loops over
every handler in that event's map without consulting either the subscription ID or scope.

java/src/org/openqa/selenium/bidi/Module.java[60-70]
java/src/org/openqa/selenium/bidi/SubscriptionScope.java[64-72]
java/src/org/openqa/selenium/bidi/Connection.java[178-187]
java/src/org/openqa/selenium/bidi/Connection.java[364-393]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
Scoped subscriptions discard their scope during listener registration. Event dispatch subsequently invokes all handlers for the event method, causing events to cross browsing-context or user-context subscription boundaries.

## Issue Context
`SubscriptionScope` affects the remote `session.subscribe` request, but incoming events currently carry no routing decision into `Connection`. The implementation must either preserve and enforce scope during dispatch using reliable event metadata, route by protocol-provided subscription metadata, or defer the scoped public API until correct routing is possible.

## Fix Focus Areas
- java/src/org/openqa/selenium/bidi/BiDi.java[120-129]
- java/src/org/openqa/selenium/bidi/Connection.java[178-190]
- java/src/org/openqa/selenium/bidi/Connection.java[347-394]
- java/src/org/openqa/selenium/bidi/SubscriptionScope.java[64-72]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools



Remediation recommended

2. SubscriptionScope constructor lacks Javadoc 📘 Rule violation ✧ Quality
Description
The new public SubscriptionScope class exposes an implicit public no-argument constructor without
constructor Javadoc. Public API constructors must be explicitly documented.
Code

java/src/org/openqa/selenium/bidi/SubscriptionScope.java[37]

+public final class SubscriptionScope {
Evidence
PR Compliance ID 330200 requires Javadoc for every public API constructor. Because
SubscriptionScope is public and declares no constructor, Java generates a public no-argument
constructor for which no constructor-level Javadoc exists.

Rule 330200: Require Javadoc for all public API types and methods
java/src/org/openqa/selenium/bidi/SubscriptionScope.java[37-40]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`SubscriptionScope` currently exposes an undocumented implicit public no-argument constructor.

## Issue Context
Public API constructors must have Javadoc immediately preceding their declarations. Declare the constructor explicitly and document its purpose.

## Fix Focus Areas
- java/src/org/openqa/selenium/bidi/SubscriptionScope.java[37-40]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


3. Scoped subscriptions lack tests 📘 Rule violation ▣ Testability
Description
The PR adds scoped subscription construction and a public subscribe overload without adding or
updating automated tests. Regressions in browsing-context or user-context parameter serialization
would therefore go undetected.
Code

java/src/org/openqa/selenium/bidi/Module.java[R69-70]

+  public final <X> String subscribe(Event<X> event, Consumer<X> handler, SubscriptionScope scope) {
+    return handle.subscribe(event, handler, scope);
Evidence
PR Compliance ID 389273 requires test changes that exercise every new public behavior and contain
meaningful assertions. The changed production files introduce the overload and scope serialization,
while this PR contains no test-file changes and the Java test tree has no SubscriptionScope
coverage.

Rule 389273: Require tests for all new functionality and bug fixes
java/src/org/openqa/selenium/bidi/Module.java[69-70]
java/src/org/openqa/selenium/bidi/SubscriptionScope.java[64-72]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The new scoped subscription API and serialization paths have no corresponding automated tests.

## Issue Context
Add assertions covering browsing-context scopes, user-context scopes, combined scopes, and propagation through the new `Module.subscribe` overload. Tests should fail against the implementation before this PR.

## Fix Focus Areas
- java/src/org/openqa/selenium/bidi/Module.java[69-70]
- java/src/org/openqa/selenium/bidi/SubscriptionScope.java[64-72]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Context
✅ Compliance rules (platform): 18 rules

Grey Divider

Tip of the day
💡 Did you know, you can keep summaries lean with Finding overflow, which tucks the rest behind 'View more'

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

* session.SubscriptionParameters</a>
*/
@Beta
public final class SubscriptionScope {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remediation recommended

1. subscriptionscope constructor lacks javadoc 📘 Rule violation ✧ Quality

The new public SubscriptionScope class exposes an implicit public no-argument constructor without
constructor Javadoc. Public API constructors must be explicitly documented.
Agent Prompt
## Issue description
`SubscriptionScope` currently exposes an undocumented implicit public no-argument constructor.

## Issue Context
Public API constructors must have Javadoc immediately preceding their declarations. Declare the constructor explicitly and document its purpose.

## Fix Focus Areas
- java/src/org/openqa/selenium/bidi/SubscriptionScope.java[37-40]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Comment on lines +69 to +70
public final <X> String subscribe(Event<X> event, Consumer<X> handler, SubscriptionScope scope) {
return handle.subscribe(event, handler, scope);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remediation recommended

2. Scoped subscriptions lack tests 📘 Rule violation ▣ Testability

The PR adds scoped subscription construction and a public subscribe overload without adding or
updating automated tests. Regressions in browsing-context or user-context parameter serialization
would therefore go undetected.
Agent Prompt
## Issue description
The new scoped subscription API and serialization paths have no corresponding automated tests.

## Issue Context
Add assertions covering browsing-context scopes, user-context scopes, combined scopes, and propagation through the new `Module.subscribe` overload. Tests should fail against the implementation before this PR.

## Fix Focus Areas
- java/src/org/openqa/selenium/bidi/Module.java[69-70]
- java/src/org/openqa/selenium/bidi/SubscriptionScope.java[64-72]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Map<String, Object> params = new HashMap<>(scope.toMap());
params.put("events", List.of(event.getMethod()));
String subscriptionId = subscribe(params);
connection.addListener(subscriptionId, event, handler);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Action required

3. Scoped callbacks cross-deliver 🐞 Bug ≡ Correctness

The scoped addListener registers only the subscription ID and event, discarding its
SubscriptionScope; Connection then invokes every handler registered for the matching event
method. Consequently, when the same event has subscriptions for different contexts, each handler
receives the other scopes' events (or duplicate callbacks if the remote emits per subscription), so
the new scope API does not provide scoped listener behavior.
Agent Prompt
## Issue description
Scoped subscriptions discard their scope during listener registration. Event dispatch subsequently invokes all handlers for the event method, causing events to cross browsing-context or user-context subscription boundaries.

## Issue Context
`SubscriptionScope` affects the remote `session.subscribe` request, but incoming events currently carry no routing decision into `Connection`. The implementation must either preserve and enforce scope during dispatch using reliable event metadata, route by protocol-provided subscription metadata, or defer the scoped public API until correct routing is possible.

## Fix Focus Areas
- java/src/org/openqa/selenium/bidi/BiDi.java[120-129]
- java/src/org/openqa/selenium/bidi/Connection.java[178-190]
- java/src/org/openqa/selenium/bidi/Connection.java[347-394]
- java/src/org/openqa/selenium/bidi/SubscriptionScope.java[64-72]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

B-devtools Includes everything BiDi or Chrome DevTools related C-java Java Bindings

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants