refactor: make context bridge's private keys hidden, constexpr string_views (#47586)

* refactor: local functions GetPrivate(), SetPrivate() now take std::string_views

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* refactor: make local keys std::string_views instead of C-style char arrays

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* refactor: make local keys constexpr

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* refactor: move local keys into local anonymous namespace

Co-authored-by: Charles Kerr <charles@charleskerr.com>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
This commit is contained in:
trop[bot] 2025-06-27 14:38:49 -05:00 committed by GitHub
commit 99a224e45b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -42,19 +42,17 @@ content::RenderFrame* GetRenderFrame(v8::Local<v8::Object> value);
namespace api { namespace api {
namespace context_bridge {
const char kProxyFunctionPrivateKey[] = "electron_contextBridge_proxy_fn";
const char kProxyFunctionReceiverPrivateKey[] =
"electron_contextBridge_proxy_fn_receiver";
const char kSupportsDynamicPropertiesPrivateKey[] =
"electron_contextBridge_supportsDynamicProperties";
const char kOriginalFunctionPrivateKey[] = "electron_contextBridge_original_fn";
} // namespace context_bridge
namespace { namespace {
constexpr std::string_view kProxyFunctionPrivateKey =
"electron_contextBridge_proxy_fn";
constexpr std::string_view kProxyFunctionReceiverPrivateKey =
"electron_contextBridge_proxy_fn_receiver";
constexpr std::string_view kSupportsDynamicPropertiesPrivateKey =
"electron_contextBridge_supportsDynamicProperties";
constexpr std::string_view kOriginalFunctionPrivateKey =
"electron_contextBridge_original_fn";
static int kMaxRecursion = 1000; static int kMaxRecursion = 1000;
// Returns true if |maybe| is both a value, and that value is true. // Returns true if |maybe| is both a value, and that value is true.
@ -115,7 +113,7 @@ bool IsPlainArray(const v8::Local<v8::Value>& arr) {
void SetPrivate(v8::Local<v8::Context> context, void SetPrivate(v8::Local<v8::Context> context,
v8::Local<v8::Object> target, v8::Local<v8::Object> target,
const std::string& key, const std::string_view key,
v8::Local<v8::Value> value) { v8::Local<v8::Value> value) {
target target
->SetPrivate( ->SetPrivate(
@ -128,7 +126,7 @@ void SetPrivate(v8::Local<v8::Context> context,
v8::MaybeLocal<v8::Value> GetPrivate(v8::Local<v8::Context> context, v8::MaybeLocal<v8::Value> GetPrivate(v8::Local<v8::Context> context,
v8::Local<v8::Object> target, v8::Local<v8::Object> target,
const std::string& key) { const std::string_view key) {
return target->GetPrivate( return target->GetPrivate(
context, context,
v8::Private::ForApi(context->GetIsolate(), v8::Private::ForApi(context->GetIsolate(),
@ -192,8 +190,8 @@ v8::MaybeLocal<v8::Value> PassValueToOtherContextInner(
// the global handle at the right time. // the global handle at the right time.
if (value->IsFunction()) { if (value->IsFunction()) {
auto func = value.As<v8::Function>(); auto func = value.As<v8::Function>();
v8::MaybeLocal<v8::Value> maybe_original_fn = GetPrivate( v8::MaybeLocal<v8::Value> maybe_original_fn =
source_context, func, context_bridge::kOriginalFunctionPrivateKey); GetPrivate(source_context, func, kOriginalFunctionPrivateKey);
{ {
v8::Context::Scope destination_scope(destination_context); v8::Context::Scope destination_scope(destination_context);
@ -214,13 +212,11 @@ v8::MaybeLocal<v8::Value> PassValueToOtherContextInner(
v8::Local<v8::Object> state = v8::Local<v8::Object> state =
v8::Object::New(destination_context->GetIsolate()); v8::Object::New(destination_context->GetIsolate());
SetPrivate(destination_context, state, SetPrivate(destination_context, state, kProxyFunctionPrivateKey, func);
context_bridge::kProxyFunctionPrivateKey, func); SetPrivate(destination_context, state, kProxyFunctionReceiverPrivateKey,
SetPrivate(destination_context, state,
context_bridge::kProxyFunctionReceiverPrivateKey,
parent_value); parent_value);
SetPrivate(destination_context, state, SetPrivate(destination_context, state,
context_bridge::kSupportsDynamicPropertiesPrivateKey, kSupportsDynamicPropertiesPrivateKey,
gin::ConvertToV8(destination_context->GetIsolate(), gin::ConvertToV8(destination_context->GetIsolate(),
support_dynamic_properties)); support_dynamic_properties));
@ -228,7 +224,7 @@ v8::MaybeLocal<v8::Value> PassValueToOtherContextInner(
.ToLocal(&proxy_func)) .ToLocal(&proxy_func))
return {}; return {};
SetPrivate(destination_context, proxy_func.As<v8::Object>(), SetPrivate(destination_context, proxy_func.As<v8::Object>(),
context_bridge::kOriginalFunctionPrivateKey, func); kOriginalFunctionPrivateKey, func);
object_cache->CacheProxiedObject(value, proxy_func); object_cache->CacheProxiedObject(value, proxy_func);
return v8::MaybeLocal<v8::Value>(proxy_func); return v8::MaybeLocal<v8::Value>(proxy_func);
} }
@ -486,12 +482,11 @@ void ProxyFunctionWrapper(const v8::FunctionCallbackInfo<v8::Value>& info) {
// Pull the original function and its context off of the data private key // Pull the original function and its context off of the data private key
v8::MaybeLocal<v8::Value> sdp_value = v8::MaybeLocal<v8::Value> sdp_value =
GetPrivate(calling_context, data, GetPrivate(calling_context, data, kSupportsDynamicPropertiesPrivateKey);
context_bridge::kSupportsDynamicPropertiesPrivateKey); v8::MaybeLocal<v8::Value> maybe_func =
v8::MaybeLocal<v8::Value> maybe_func = GetPrivate( GetPrivate(calling_context, data, kProxyFunctionPrivateKey);
calling_context, data, context_bridge::kProxyFunctionPrivateKey); v8::MaybeLocal<v8::Value> maybe_recv =
v8::MaybeLocal<v8::Value> maybe_recv = GetPrivate( GetPrivate(calling_context, data, kProxyFunctionReceiverPrivateKey);
calling_context, data, context_bridge::kProxyFunctionReceiverPrivateKey);
v8::Local<v8::Value> func_value; v8::Local<v8::Value> func_value;
if (sdp_value.IsEmpty() || maybe_func.IsEmpty() || maybe_recv.IsEmpty() || if (sdp_value.IsEmpty() || maybe_func.IsEmpty() || maybe_recv.IsEmpty() ||
!gin::ConvertFromV8(args.isolate(), sdp_value.ToLocalChecked(), !gin::ConvertFromV8(args.isolate(), sdp_value.ToLocalChecked(),