Compare commits

...

1 Commits

Author SHA1 Message Date
Shelley Vohr
90f1c37a78 fix: InspectorFrontendHost override in embedded windows 2026-02-17 15:20:40 +01:00
3 changed files with 29 additions and 17 deletions

View File

@@ -23,6 +23,9 @@ export default contextBridge;
export const internalContextBridge = { export const internalContextBridge = {
contextIsolationEnabled: process.contextIsolated, contextIsolationEnabled: process.contextIsolated,
tryOverrideGlobalValueFromIsolatedWorld: (keys: string[], value: any) => {
return binding._overrideGlobalValueFromIsolatedWorld(keys, value, true, true);
},
overrideGlobalValueFromIsolatedWorld: (keys: string[], value: any) => { overrideGlobalValueFromIsolatedWorld: (keys: string[], value: any) => {
return binding._overrideGlobalValueFromIsolatedWorld(keys, value, false); return binding._overrideGlobalValueFromIsolatedWorld(keys, value, false);
}, },

View File

@@ -11,14 +11,12 @@ const { contextIsolationEnabled } = internalContextBridge;
* 1) Use menu API to show context menu. * 1) Use menu API to show context menu.
*/ */
window.onload = function () { window.onload = function () {
if (window.InspectorFrontendHost) { if (contextIsolationEnabled) {
if (contextIsolationEnabled) { internalContextBridge.tryOverrideGlobalValueFromIsolatedWorld([
internalContextBridge.overrideGlobalValueFromIsolatedWorld([ 'InspectorFrontendHost', 'showContextMenuAtPoint'
'InspectorFrontendHost', 'showContextMenuAtPoint' ], createMenu);
], createMenu); } else {
} else { window.InspectorFrontendHost!.showContextMenuAtPoint = createMenu;
window.InspectorFrontendHost.showContextMenuAtPoint = createMenu;
}
} }
}; };

View File

@@ -5,6 +5,7 @@
#include "shell/renderer/api/electron_api_context_bridge.h" #include "shell/renderer/api/electron_api_context_bridge.h"
#include <memory> #include <memory>
#include <optional>
#include <set> #include <set>
#include <string> #include <string>
#include <utility> #include <utility>
@@ -826,13 +827,18 @@ void ExposeAPIInWorld(v8::Isolate* isolate,
ExposeAPI(isolate, source_context, target_isolate, target_context, key, api); ExposeAPI(isolate, source_context, target_isolate, target_context, key, api);
} }
gin_helper::Dictionary TraceKeyPath(const gin_helper::Dictionary& start, std::optional<gin_helper::Dictionary> TraceKeyPath(
const std::vector<std::string>& key_path) { const gin_helper::Dictionary& start,
const std::vector<std::string>& key_path,
bool allow_silent_failure) {
gin_helper::Dictionary current = start; gin_helper::Dictionary current = start;
for (size_t i = 0; i < key_path.size() - 1; i++) { for (size_t i = 0; i < key_path.size() - 1; i++) {
CHECK(current.Get(key_path[i], &current)) if (!current.Get(key_path[i], &current)) {
<< "Failed to get property '" << key_path[i] << "' at index " << i if (allow_silent_failure)
<< " in key path"; return std::nullopt;
CHECK(false) << "Failed to get property '" << key_path[i] << "' at index "
<< i << " in key path";
}
} }
return current; return current;
} }
@@ -841,7 +847,8 @@ void OverrideGlobalValueFromIsolatedWorld(
v8::Isolate* isolate, v8::Isolate* isolate,
const std::vector<std::string>& key_path, const std::vector<std::string>& key_path,
v8::Local<v8::Object> value, v8::Local<v8::Object> value,
bool support_dynamic_properties) { bool support_dynamic_properties,
bool allow_silent_failure) {
if (key_path.empty()) if (key_path.empty())
return; return;
@@ -853,7 +860,11 @@ void OverrideGlobalValueFromIsolatedWorld(
gin_helper::Dictionary global(isolate, main_context->Global()); gin_helper::Dictionary global(isolate, main_context->Global());
const std::string final_key = key_path[key_path.size() - 1]; const std::string final_key = key_path[key_path.size() - 1];
gin_helper::Dictionary target_object = TraceKeyPath(global, key_path); auto maybe_target_object =
TraceKeyPath(global, key_path, allow_silent_failure);
if (!maybe_target_object.has_value())
return;
gin_helper::Dictionary target_object = maybe_target_object.value();
{ {
v8::Context::Scope main_context_scope(main_context); v8::Context::Scope main_context_scope(main_context);
@@ -886,8 +897,8 @@ bool OverrideGlobalPropertyFromIsolatedWorld(
gin_helper::Dictionary global(isolate, main_context->Global()); gin_helper::Dictionary global(isolate, main_context->Global());
const std::string final_key = key_path[key_path.size() - 1]; const std::string final_key = key_path[key_path.size() - 1];
v8::Local<v8::Object> target_object = auto target_dict = TraceKeyPath(global, key_path, false);
TraceKeyPath(global, key_path).GetHandle(); v8::Local<v8::Object> target_object = target_dict.value().GetHandle();
{ {
v8::Context::Scope main_context_scope(main_context); v8::Context::Scope main_context_scope(main_context);