Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 CEF: only load OpenKneeboard API in the main frame and main JS context
Browse files Browse the repository at this point in the history
Might allow additional frames later, especially if same-origin, but my usual approach is to start as restrictive as possible, open up if needed, as it's much easier to open things up than close them down later.

Multiple contexts can exist for the same page - see chromiumembedded/cef#3867

refs #725 - Move from WebView2 to CEF
fredemmott committed Jan 26, 2025
1 parent b6dd6c0 commit 56ff7a2
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/utilities/chromium-worker.cpp
Original file line number Diff line number Diff line change
@@ -123,28 +123,45 @@ class BrowserApp final : public CefApp,
CefRefPtr<CefV8Context> context) override {
OPENKNEEBOARD_TraceLoggingScope("OnContextCreated");

if (!frame->GetV8Context()->IsSame(context)) {
// Secondary context, e.g. dev tools window
//
// See https://github.com/chromiumembedded/cef/issues/3867
return;
}
if (!frame->IsMain()) {
return;
}

const auto window = context->GetGlobal();
if (window->HasValue("OpenKneeboard")) {

if (!context->Enter()) {
return;
}
if (context->Enter()) {
const scope_exit exitContext([context] { context->Exit(); });

CefRefPtr<CefV8Value> ret;
CefRefPtr<CefV8Exception> exception;
context->Eval(
GetOpenKneeboardAPIJS(), "OpenKneeboardAPI.js", 1, ret, exception);
context->Eval(
"new OpenKneeboardAPI()", "OpenKneeboardInit.js", 1, ret, exception);
window->SetValue("OpenKneeboard", ret, V8_PROPERTY_ATTRIBUTE_READONLY);
context->Exit();
}
OPENKNEEBOARD_ALWAYS_ASSERT(window->HasValue("OpenKneeboard"));
}

void OnContextReleased(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame>,
CefRefPtr<CefV8Context>) override {
CefRefPtr<CefFrame> frame,
CefRefPtr<CefV8Context> context) override {
OPENKNEEBOARD_TraceLoggingScope("OnContextReleased");
if (!frame->GetV8Context()->IsSame(context)) {
return;
}
if (!frame->IsMain()) {
return;
}

dprint("OnContextReleased");
const auto id = browser->GetIdentifier();
if (mBrowserData.contains(id)) {

0 comments on commit 56ff7a2

Please sign in to comment.