Skip to content

Comments

[WebDriver BiDi] Add realm created and destroyed events with lifecycle tracking#1

Open
sergedeh wants to merge 1 commit intomainfrom
WEBG-12
Open

[WebDriver BiDi] Add realm created and destroyed events with lifecycle tracking#1
sergedeh wants to merge 1 commit intomainfrom
WEBG-12

Conversation

@sergedeh
Copy link
Owner

[WebDriver BiDi] Add realm created and destroyed events with lifecycle tracking
https://bugs.webkit.org/show_bug.cgi?id=304062

Reviewed by NOBODY (OOPS!).

Implements the script.realmCreated and script.realmDestroyed events for WebDriver BiDi
to track realm lifecycle. This enables proper monitoring of browsing context realms,
dedicated worker realms, and other execution contexts as they are created and destroyed.

The implementation adds:

  • script.realmCreated event emission when realms are initialized
  • script.realmDestroyed event emission when realms are torn down
  • script.getRealms command to query active realms
  • Realm lifecycle instrumentation in WindowProxy and FrameLoader
  • Source/WebCore/automation/AutomationInstrumentation.cpp:
    (WebCore::AutomationInstrumentation::hasClient): Added
    (WebCore::AutomationInstrumentation::scriptRealmCreated): Added
    (WebCore::AutomationInstrumentation::scriptRealmDestroyed): Added

  • Source/WebCore/automation/AutomationInstrumentation.h:
    Added realm lifecycle notification infrastructure

  • Source/WebCore/bindings/js/WindowProxy.cpp:
    (WebCore::WindowProxy::destroyJSWindowProxy): Notify realm destroyed
    (WebCore::WindowProxy::createJSWindowProxy): Notify realm created
    (WebCore::WindowProxy::setDOMWindow): Notify realm destroyed and created during navigation

  • Source/WebCore/loader/FrameLoader.cpp:
    (WebCore::FrameLoader::checkCompleted): Ensure realm creation during automation

  • Source/WebKit/UIProcess/Automation/BidiScriptAgent.cpp:
    (WebKit::BidiScriptAgent::getRealms): Added
    (WebKit::BidiScriptAgent::notifyRealmCreated): Added
    (WebKit::BidiScriptAgent::notifyRealmDestroyed): Added

  • Source/WebKit/UIProcess/Automation/BidiScriptAgent.h:
    Added realm event handling methods and RealmInfo tracking

  • Source/WebKit/UIProcess/Automation/WebAutomationSession.h:
    Added realm lifecycle integration

  • Source/WebKit/UIProcess/Automation/WebDriverBidiProcessor.h:
    Added realm event dispatching

  • Source/WebKit/UIProcess/Automation/protocol/BidiScript.json:
    Added RealmType enum and getRealms command definition

  • Source/WebKit/UIProcess/WebPageProxy.cpp:
    (WebKit::WebPageProxy::scriptRealmWasCreated): Added
    (WebKit::WebPageProxy::scriptRealmWasDestroyed): Added

  • Source/WebKit/UIProcess/WebPageProxy.h:
    Added realm notification methods

  • Source/WebKit/UIProcess/WebPageProxy.messages.in:
    Added ScriptRealmWasCreated and ScriptRealmWasDestroyed messages

  • Source/WebKit/WebProcess/Automation/WebAutomationSessionProxy.cpp:
    (WebKit::WebAutomationSessionProxy::scriptRealmCreated): Added
    (WebKit::WebAutomationSessionProxy::scriptRealmDestroyed): Added

  • Source/WebKit/WebProcess/Automation/WebAutomationSessionProxy.h:
    Added realm notification infrastructure

  • WebDriverTests/TestExpectations.json:
    Updated test expectations for realm lifecycle tests

@sergedeh sergedeh self-assigned this Dec 15, 2025
@sergedeh sergedeh force-pushed the WEBG-12 branch 4 times, most recently from 79c97f6 to 967f1d2 Compare December 22, 2025 15:34
…yed events

https://bugs.webkit.org/show_bug.cgi?id=304062
rdar://166875337

Reviewed by NOBODY (OOPS!).

This patch implements the script.realmCreated and script.realmDestroyed events
as specified in the W3C WebDriver BiDi specification. These events notify BiDi
clients when JavaScript execution realms are created and destroyed in browsing
contexts, enabling proper tracking of realm lifecycle for script execution.

Key architecture and design decisions:

1. **Async message passing with sendWithAsyncReply barrier**: The implementation
   uses sendWithAsyncReply from WebContent process to UI process to ensure
   proper message ordering and prevent race conditions between realm creation/
   destruction notifications and subsequent script.evaluate/callFunction commands.
   The completion handler acts as a synchronization barrier.

2. **Origin resolution helper**: Added resolveOriginForRealm() helper function
   in WindowProxy.cpp to properly resolve the realm's origin. This handles edge
   cases like initial about:blank documents where the document may not exist yet,
   falling back to the document loader URL when necessary.

3. **Single source of truth for realm IDs**: Realm IDs are generated using
   m_realmNavigationCounters (a counter-based scheme: realm-{context},
   realm-{context}-1, etc.) shared between event notifications and getRealms
   queries. This ensures perfect consistency between realm events and queries.

4. **Event emission on realm lifecycle transitions**: Events are emitted from
   WindowProxy::createJSWindowProxy() for creation and WindowProxy::setDOMWindow()
   for the destroy-then-create sequence during navigations.

Test updates:
* Marked test_existing_realm[window] and test_existing_realm[tab] as flaky
  due to timing issues in parallel test execution.

* Source/WebCore/automation/AutomationInstrumentation.cpp:
(WebCore::AutomationInstrumentation::scriptRealmCreated): Added.
(WebCore::AutomationInstrumentation::scriptRealmDestroyed): Added.

* Source/WebCore/automation/AutomationInstrumentation.h:
Added scriptRealmCreated and scriptRealmDestroyed methods.

* Source/WebCore/bindings/js/WindowProxy.cpp:
(WebCore::resolveOriginForRealm): Added helper to resolve realm origin.
(WebCore::WindowProxy::createJSWindowProxy): Notify realm creation.
(WebCore::WindowProxy::setDOMWindow): Notify realm destruction and re-creation.

* Source/WebKit/UIProcess/Automation/BidiScriptAgent.cpp:
(WebKit::BidiScriptAgent::generateRealmIdForFrame): Simplified to use only m_realmNavigationCounters.
(WebKit::BidiScriptAgent::notifyRealmCreated): Emit script.realmCreated event.
(WebKit::BidiScriptAgent::notifyRealmDestroyed): Emit script.realmDestroyed event.
(WebKit::BidiScriptAgent::hasRealmForContext): Track active realms.

* Source/WebKit/UIProcess/Automation/BidiScriptAgent.h:
Added realm tracking state (m_activeRealms, m_realmNavigationCounters).
Removed redundant m_frameRealmCounters and m_frameRealmCache.

* Source/WebKit/UIProcess/Automation/WebAutomationSession.cpp:
(WebKit::WebAutomationSession::createBrowsingContext): Simplified process message send.
(WebKit::WebAutomationSession::setWindowFrameOfBrowsingContext): Fixed trailing whitespace.

* Source/WebKit/UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::scriptRealmWasCreated): Handle realm creation from WebContent process.
(WebKit::WebPageProxy::scriptRealmWasDestroyed): Handle realm destruction from WebContent process.

* Source/WebKit/UIProcess/WebPageProxy.messages.in:
Added ScriptRealmWasCreated and ScriptRealmWasDestroyed messages.

* Source/WebKit/WebProcess/Automation/WebAutomationSessionProxy.cpp:
(WebKit::WebAutomationSessionProxy::notifyScriptRealmCreated): Send creation notification with barrier.
(WebKit::WebAutomationSessionProxy::notifyScriptRealmDestroyed): Send destruction notification with barrier.

* Source/WebKit/WebProcess/Automation/WebAutomationSessionProxy.messages.in:
Added EnsureRealmForInitialEmptyDocument message.

* WebDriverTests/TestExpectations.json:
Marked test_existing_realm tests as flaky (PASS/FAIL).
sergedeh pushed a commit that referenced this pull request Jan 22, 2026
https://bugs.webkit.org/show_bug.cgi?id=304917
rdar://167529315

Reviewed by Marcus Plutowski.

This patch implements chain of compare with ARM64 ccmp / ccmn.
Let's say,

    if (x0 == 0 && x1 == 1) {
        // target-block
    }

Then this will be compiled via LLVM into wasm. And this will create
BitAnd(Equal(a, 0), Equal(b, 1)).
Then ARM64 ccmp can handle it as follows.

    cmp x0, #0
    ccmp x1, #1, #0, eq  // cmp x1, #1 when flag is eq and override, otherwise set #0 to flag
    b.eq target-block

This reduces small weird basic blocks and reduces prediction miss, and
reduces code size.

We introduce CompareOnFlags, CompareConditionallyOnFlags, and
BranchOnFlags Air opcodes. All of them are annotated as /effects since
this relies on the current flag, and they are not producing register
output while it has side effects. This ensures that we are not removing
these instructions, and we are not hoisting etc. randomly.

We introduced V8's compare chain detection mechanism and using it for
chained cmp code generation.

Tests: Source/JavaScriptCore/b3/testb3_1.cpp
       Source/JavaScriptCore/b3/testb3_8.cpp

* Source/JavaScriptCore/assembler/MacroAssemblerARM64.h:
(JSC::MacroAssemblerARM64::compareOnFlags32):
(JSC::MacroAssemblerARM64::compareOnFlags64):
(JSC::MacroAssemblerARM64::compareOnFlagsFloat):
(JSC::MacroAssemblerARM64::compareOnFlagsDouble):
(JSC::MacroAssemblerARM64::compareConditionallyOnFlags32):
(JSC::MacroAssemblerARM64::compareConditionallyOnFlags64):
(JSC::MacroAssemblerARM64::compareConditionallyOnFlagsFloat):
(JSC::MacroAssemblerARM64::compareConditionallyOnFlagsDouble):
(JSC::MacroAssemblerARM64::branchOnFlags):
* Source/JavaScriptCore/b3/B3LowerToAir.cpp:
* Source/JavaScriptCore/b3/air/AirOpcode.opcodes:
* Source/JavaScriptCore/b3/air/AirOptimizeBlockOrder.cpp:
(JSC::B3::Air::optimizeBlockOrder):
* Source/JavaScriptCore/b3/testb3.h:
* Source/JavaScriptCore/b3/testb3_1.cpp:
(run):
* Source/JavaScriptCore/b3/testb3_8.cpp:
(testCCmpAnd32):
(testCCmpAnd64):
(testCCmpOr32):
(testCCmpOr64):
(testCCmpAndAnd32):
(testCCmpOrOr32):
(testCCmpAndOr32):
(testCCmnAnd32WithNegativeImm):
(testCCmnAnd64WithNegativeImm):
(testCCmpWithLargePositiveImm):
(testCCmpWithLargeNegativeImm):
(testCCmpSmartOperandOrdering32):
(testCCmpSmartOperandOrdering64):
(testCCmpOperandCommutation32):
(testCCmpOperandCommutation64):
(testCCmpCombinedOptimizations):
(testCCmpZeroRegisterOptimization32):
(testCCmpZeroRegisterOptimization64):
(testCCmpMixedAndOr32):
(testCCmpMixedOrAnd32):
(testCCmpNegatedAnd32):
(testCCmpNegatedOr32):
(testCCmpMixedWidth32And64):
(testCCmpMixedWidth64And32):

Canonical link: https://commits.webkit.org/305493@main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant