Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass world_id to OnContextCreated and OnContextReleased #3867

Open
magreenblatt opened this issue Jan 7, 2025 · 0 comments
Open

Pass world_id to OnContextCreated and OnContextReleased #3867

magreenblatt opened this issue Jan 7, 2025 · 0 comments
Labels
enhancement Enhancement request

Comments

@magreenblatt
Copy link
Collaborator

Is your feature request related to a problem? Please describe.
Blink may create multiple v8::Context associated with a single blink::WebLocalFrame. For example:

  • The MainWorldScriptContext which the client can use to add bindings on the JS Window object. This is returned via CefFrame::GetV8Context.
  • Other Contexts created for purposes like DevTools access or WebWorkers that the client will likely choose to ignore.

Contexts are grouped into separate Worlds with an associated WorldId value. The MainWorld Context will have a value of 0 (kMainWorldId) whereas other Contexts will have values > 0.

This WorldId value (|world_id| parameter) is currently passed to DidCreateScriptContext and WillReleaseScriptContext but is not forwarded to the client via OnContextCreated and OnContextReleased.

Describe the solution you'd like
Pass the |world_id| parameter to OnContextCreated and OnContextReleased.

Describe alternatives you've considered
The client can currently check frame->GetV8Context()->IsSame(context) in OnContextCreated to implicitly test for the MainWorld Context.

Inside CEF, various World values can also be queried (from blink_glue.cc) using code like:

bool IsMainWorldSciptContext(v8::Local<v8::Context> context) {
  return blink::DOMWrapperWorld::World(context->GetIsolate(), context)
      .IsMainWorld();
}

Or by calling existing methods like WebLocalFrame::GetScriptContextWorldId.

Additional context
A v8::Context may be created for Inspector integration if a DevTools window is opened. This DevTools Context has the same CefBrowser and CefFrame as the MainWorld Context.

This behavior is seen with the V8Test.OnUncaughtExceptionDevTools test failing on MacOS. The DevTools Context is being stored in |test_context_| here when OnContextCreated is called (unexpectedly) for the 2nd time.

[ RUN      ] V8Test.OnUncaughtExceptionDevTools
/Users/marshall/Downloads/cef_binary_133.0.0-master.3115+gaefa6e6+chromium-133.0.6886.0_macosarm64/tests/ceftests/v8_unittest.cc:3095: Failure
Value of: test_context_->IsSame(context)
  Actual: false
Expected: true
/Users/marshall/Downloads/cef_binary_133.0.0-master.3115+gaefa6e6+chromium-133.0.6886.0_macosarm64/tests/ceftests/v8_unittest.cc:3520: Failure
Value of: handler->got_success_
  Actual: false
Expected: true
[  FAILED  ] V8Test.OnUncaughtExceptionDevTools (1501 ms)

Callstack (at M133) for the unexpected DevTools Context creation:

    frame #16: 0x00000001158348dc Chromium Embedded Framework`CefRenderFrameObserver::DidCreateScriptContext(this=<unavailable>, context=v8::Handle<v8::Context> @ x20, world_id=<unavailable>) at render_frame_observer.cc:150:14 [opt]
    frame #17: 0x0000000123984d14 Chromium Embedded Framework`content::RenderFrameImpl::DidCreateScriptContext(this=<unavailable>, context=Local<v8::Context> @ x20, world_id=536870914) at render_frame_impl.cc:4793:14 [opt]
    frame #18: 0x000000012083c1c0 Chromium Embedded Framework`blink::LocalFrameClientImpl::DidCreateScriptContext(this=<unavailable>, context=Local<v8::Context> @ x20, world_id=536870914) at local_frame_client_impl.cc:294:27 [opt]
    frame #19: 0x000000011fe82200 Chromium Embedded Framework`blink::LocalWindowProxy::Initialize(this=0x000001140046cca0) at local_window_proxy.cc:216:27 [opt]
    frame #20: 0x000000011fe92578 Chromium Embedded Framework`blink::ScriptController::WindowProxy(blink::DOMWrapperWorld&) [inlined] blink::WindowProxyManager::GetWindowProxy(this=<unavailable>, world=<unavailable>) at window_proxy_manager.h:53:19 [opt]
    frame #21: 0x000000011fe92568 Chromium Embedded Framework`blink::ScriptController::WindowProxy(blink::DOMWrapperWorld&) [inlined] blink::WindowProxyManagerImplHelper<blink::LocalFrame, blink::LocalWindowProxy>::WindowProxy(this=<unavailable>, world=<unavailable>) at window_proxy_manager.h:91:42 [opt]
    frame #22: 0x000000011fe92568 Chromium Embedded Framework`blink::ScriptController::WindowProxy(this=<unavailable>, world=<unavailable>) at script_controller.cc:113:33 [opt]
    frame #23: 0x000000011fe96d38 Chromium Embedded Framework`blink::ScriptController::CreateNewInspectorIsolatedWorld(this=0x00000114004a0f08, world_name=0x000000016d4fb520) at script_controller.cc:392:3 [opt]
    frame #24: 0x0000000120c06fc8 Chromium Embedded Framework`blink::InspectorPageAgent::EnsureDOMWrapperWorld(this=<unavailable>, frame=0x000001140042dd48, world_name=0x000000016d4fb520, grant_universal_access=true) at inspector_page_agent.cc:1012:37 [opt]
    frame #25: 0x0000000120c02780 Chromium Embedded Framework`blink::InspectorPageAgent::EvaluateScriptOnNewDocument(this=0x00000114004352c8, frame=0x000001140042dd48, script_identifier=0x000000016d4fb828) at inspector_page_agent.cc:1068:39 [opt]
    frame #26: 0x0000000120c02494 Chromium Embedded Framework`blink::InspectorPageAgent::addScriptToEvaluateOnNewDocument(this=0x00000114004352c8, source=<unavailable>, world_name=<unavailable>, include_command_line_api=<unavailable>, runImmediately=<unavailable>, identifier=0x000000016d4fb828) at inspector_page_agent.cc:608:7 [opt]
    frame #27: 0x000000011ab8b964 Chromium Embedded Framework`blink::protocol::Page::DomainDispatcherImpl::addScriptToEvaluateOnNewDocument(this=<unavailable>, dispatchable=<unavailable>) at page.cc:1272:44 [opt]
    frame #28: 0x00000001181f2158 Chromium Embedded Framework`v8_crdtp::UberDispatcher::DispatchResult::Run() [inlined] std::__Cr::__function::__policy_func<void ()>::operator()(this=<unavailable>) const at function.h:718:12 [opt]
    frame #29: 0x00000001181f2154 Chromium Embedded Framework`v8_crdtp::UberDispatcher::DispatchResult::Run() [inlined] std::__Cr::function<void ()>::operator()(this=<unavailable>) const at function.h:991:10 [opt]
    frame #30: 0x00000001181f2154 Chromium Embedded Framework`v8_crdtp::UberDispatcher::DispatchResult::Run(this=0x000000016d4fb9f8) at dispatch.cc:509:3 [opt]
    frame #31: 0x0000000120c63440 Chromium Embedded Framework`blink::DevToolsSession::DispatchProtocolCommandImpl(this=0x0000011400434638, call_id=72, method=<unavailable>, data=(data_ = "\xd8\U00000018Z", size_ = 15161)) at devtools_session.cc:277:59 [opt]
    frame #32: 0x0000000120c631ac Chromium Embedded Framework`blink::DevToolsSession::DispatchProtocolCommand(this=0x0000011400434638, call_id=72, method=0x000000016d4fbb20, message=<unavailable>) at devtools_session.cc:241:10 [opt]
    frame #33: 0x000000011a86dba4 Chromium Embedded Framework`blink::mojom::blink::DevToolsSessionStubDispatch::Accept(impl=0x0000011400434638, message=0x000000016d4fc120) at devtools_agent.mojom-blink.cc:1376:13 [opt]

Note that ScriptController::CreateNewInspectorIsolatedWorld creates a World of type kInspectorIsolated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhancement request
Projects
None yet
Development

No branches or pull requests

1 participant