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:
parent
1754c55c05
commit
99a224e45b
1 changed files with 22 additions and 27 deletions
|
@ -42,19 +42,17 @@ content::RenderFrame* GetRenderFrame(v8::Local<v8::Object> value);
|
|||
|
||||
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 {
|
||||
|
||||
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;
|
||||
|
||||
// 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,
|
||||
v8::Local<v8::Object> target,
|
||||
const std::string& key,
|
||||
const std::string_view key,
|
||||
v8::Local<v8::Value> value) {
|
||||
target
|
||||
->SetPrivate(
|
||||
|
@ -128,7 +126,7 @@ void SetPrivate(v8::Local<v8::Context> context,
|
|||
|
||||
v8::MaybeLocal<v8::Value> GetPrivate(v8::Local<v8::Context> context,
|
||||
v8::Local<v8::Object> target,
|
||||
const std::string& key) {
|
||||
const std::string_view key) {
|
||||
return target->GetPrivate(
|
||||
context,
|
||||
v8::Private::ForApi(context->GetIsolate(),
|
||||
|
@ -192,8 +190,8 @@ v8::MaybeLocal<v8::Value> PassValueToOtherContextInner(
|
|||
// the global handle at the right time.
|
||||
if (value->IsFunction()) {
|
||||
auto func = value.As<v8::Function>();
|
||||
v8::MaybeLocal<v8::Value> maybe_original_fn = GetPrivate(
|
||||
source_context, func, context_bridge::kOriginalFunctionPrivateKey);
|
||||
v8::MaybeLocal<v8::Value> maybe_original_fn =
|
||||
GetPrivate(source_context, func, kOriginalFunctionPrivateKey);
|
||||
|
||||
{
|
||||
v8::Context::Scope destination_scope(destination_context);
|
||||
|
@ -214,13 +212,11 @@ v8::MaybeLocal<v8::Value> PassValueToOtherContextInner(
|
|||
|
||||
v8::Local<v8::Object> state =
|
||||
v8::Object::New(destination_context->GetIsolate());
|
||||
SetPrivate(destination_context, state,
|
||||
context_bridge::kProxyFunctionPrivateKey, func);
|
||||
SetPrivate(destination_context, state,
|
||||
context_bridge::kProxyFunctionReceiverPrivateKey,
|
||||
SetPrivate(destination_context, state, kProxyFunctionPrivateKey, func);
|
||||
SetPrivate(destination_context, state, kProxyFunctionReceiverPrivateKey,
|
||||
parent_value);
|
||||
SetPrivate(destination_context, state,
|
||||
context_bridge::kSupportsDynamicPropertiesPrivateKey,
|
||||
kSupportsDynamicPropertiesPrivateKey,
|
||||
gin::ConvertToV8(destination_context->GetIsolate(),
|
||||
support_dynamic_properties));
|
||||
|
||||
|
@ -228,7 +224,7 @@ v8::MaybeLocal<v8::Value> PassValueToOtherContextInner(
|
|||
.ToLocal(&proxy_func))
|
||||
return {};
|
||||
SetPrivate(destination_context, proxy_func.As<v8::Object>(),
|
||||
context_bridge::kOriginalFunctionPrivateKey, func);
|
||||
kOriginalFunctionPrivateKey, func);
|
||||
object_cache->CacheProxiedObject(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
|
||||
v8::MaybeLocal<v8::Value> sdp_value =
|
||||
GetPrivate(calling_context, data,
|
||||
context_bridge::kSupportsDynamicPropertiesPrivateKey);
|
||||
v8::MaybeLocal<v8::Value> maybe_func = GetPrivate(
|
||||
calling_context, data, context_bridge::kProxyFunctionPrivateKey);
|
||||
v8::MaybeLocal<v8::Value> maybe_recv = GetPrivate(
|
||||
calling_context, data, context_bridge::kProxyFunctionReceiverPrivateKey);
|
||||
GetPrivate(calling_context, data, kSupportsDynamicPropertiesPrivateKey);
|
||||
v8::MaybeLocal<v8::Value> maybe_func =
|
||||
GetPrivate(calling_context, data, kProxyFunctionPrivateKey);
|
||||
v8::MaybeLocal<v8::Value> maybe_recv =
|
||||
GetPrivate(calling_context, data, kProxyFunctionReceiverPrivateKey);
|
||||
v8::Local<v8::Value> func_value;
|
||||
if (sdp_value.IsEmpty() || maybe_func.IsEmpty() || maybe_recv.IsEmpty() ||
|
||||
!gin::ConvertFromV8(args.isolate(), sdp_value.ToLocalChecked(),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue