mirror of
https://github.com/nodejs/node.git
synced 2026-11-21 12:59:16 -05:00
src: use DictionaryTemplate for permission diag channel message
Since DiagnosticChannel permission messages always have the same shape and should be as low cost as possible, use a cached DictionaryTemplate for creating them Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: https://github.com/nodejs/node/pull/65158 Reviewed-By: Xuguang Mei <meixuguang@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
This commit is contained in:
1 parent
79720e98c1
commit
9dc2f0d86d
4 files changed
+41
-35
No files matched your search
+4
-6
@@ -842,9 +842,8 @@ void Environment::set_process_exit_handler(
|
||||
#undef VP
|
||||
|
||||
#define V(Name, label, _, __) \
|
||||
inline v8::Local<v8::String> \
|
||||
IsolateData::Name##_permission_string() const { \
|
||||
return Name##_permission_string##_.Get(isolate_); \
|
||||
inline v8::Local<v8::String> IsolateData::Name##_permission_string() const { \
|
||||
return Name##_permission_string##_.Get(isolate_); \
|
||||
}
|
||||
PERMISSIONS(V)
|
||||
#undef V
|
||||
@@ -879,9 +878,8 @@ void Environment::set_process_exit_handler(
|
||||
#undef VP
|
||||
|
||||
#define V(Name, label, _, __) \
|
||||
inline v8::Local<v8::String> \
|
||||
Environment::Name##_permission_string() const { \
|
||||
return isolate_data()->Name##_permission_string(); \
|
||||
inline v8::Local<v8::String> Environment::Name##_permission_string() const { \
|
||||
return isolate_data()->Name##_permission_string(); \
|
||||
}
|
||||
PERMISSIONS(V)
|
||||
#undef V
|
||||
|
||||
+2
-3
@@ -432,10 +432,9 @@ void IsolateData::DeserializeProperties(const IsolateDataSerializeInfo* info) {
|
||||
info->primitive_values[i++]); \
|
||||
Local<String> field; \
|
||||
if (!maybe_field.ToLocal(&field)) { \
|
||||
fprintf(stderr, \
|
||||
"Failed to deserialize " #Name "_permission_string\n"); \
|
||||
fprintf(stderr, "Failed to deserialize " #Name "_permission_string\n"); \
|
||||
} \
|
||||
Name##_permission_string##_.Set(isolate_, field); \
|
||||
Name##_permission_string##_.Set(isolate_, field); \
|
||||
} while (0);
|
||||
PERMISSIONS(V)
|
||||
#undef V
|
||||
|
||||
@@ -463,6 +463,7 @@
|
||||
V(naptr_record_template, v8::DictionaryTemplate) \
|
||||
V(object_stats_template, v8::DictionaryTemplate) \
|
||||
V(page_stats_template, v8::DictionaryTemplate) \
|
||||
V(permission_diagnostic_channel_message, v8::DictionaryTemplate) \
|
||||
V(pipe_constructor_template, v8::FunctionTemplate) \
|
||||
V(script_context_constructor_template, v8::FunctionTemplate) \
|
||||
V(secure_context_constructor_template, v8::FunctionTemplate) \
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "node_external_reference.h"
|
||||
#include "node_file.h"
|
||||
|
||||
#include "v8-template.h"
|
||||
#include "v8.h"
|
||||
|
||||
#include <memory>
|
||||
@@ -17,11 +18,13 @@
|
||||
namespace node {
|
||||
|
||||
using v8::Context;
|
||||
using v8::DictionaryTemplate;
|
||||
using v8::FunctionCallbackInfo;
|
||||
using v8::IntegrityLevel;
|
||||
using v8::Local;
|
||||
using v8::MaybeLocal;
|
||||
using v8::Object;
|
||||
using v8::Undefined;
|
||||
using v8::Value;
|
||||
|
||||
namespace permission {
|
||||
@@ -55,6 +58,20 @@ constexpr std::string_view GetDiagnosticsChannelName(PermissionScope scope) {
|
||||
}
|
||||
}
|
||||
|
||||
Local<DictionaryTemplate> GetPermissionDiagnosicsTemplate(Environment* env) {
|
||||
auto tmpl = env->permission_diagnostic_channel_message();
|
||||
if (tmpl.IsEmpty()) {
|
||||
static constexpr std::string_view names[] = {
|
||||
"permission",
|
||||
"resource",
|
||||
"drop",
|
||||
};
|
||||
tmpl = DictionaryTemplate::New(env->isolate(), names);
|
||||
env->set_permission_diagnostic_channel_message(tmpl);
|
||||
}
|
||||
return tmpl;
|
||||
}
|
||||
|
||||
// permission.drop('fs.read', '/tmp/')
|
||||
// permission.drop('child')
|
||||
static void Drop(const FunctionCallbackInfo<Value>& args) {
|
||||
@@ -259,17 +276,14 @@ bool Permission::is_scope_granted(Environment* env,
|
||||
v8::Isolate* isolate = env->isolate();
|
||||
v8::HandleScope handle_scope(isolate);
|
||||
v8::Local<v8::Context> context = env->context();
|
||||
v8::Local<v8::Object> msg =
|
||||
v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0);
|
||||
msg->Set(context,
|
||||
env->permission_string(),
|
||||
PermissionToString(env, permission))
|
||||
.Check();
|
||||
msg->Set(context,
|
||||
env->resource_string(),
|
||||
ToV8Value(context, res).ToLocalChecked())
|
||||
.Check();
|
||||
ch->Publish(env, msg);
|
||||
v8::MaybeLocal<v8::Value> values[] = {
|
||||
PermissionToString(env, permission),
|
||||
ToV8Value(context, res),
|
||||
Undefined(isolate),
|
||||
};
|
||||
ch->Publish(
|
||||
env,
|
||||
GetPermissionDiagnosicsTemplate(env)->NewInstance(context, values));
|
||||
publishing_ = false;
|
||||
}
|
||||
}
|
||||
@@ -324,21 +338,15 @@ void Permission::Drop(Environment* env,
|
||||
v8::Isolate* isolate = env->isolate();
|
||||
v8::HandleScope handle_scope(isolate);
|
||||
v8::Local<v8::Context> context = env->context();
|
||||
v8::Local<v8::Object> msg =
|
||||
v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0);
|
||||
msg->Set(context,
|
||||
env->permission_string(),
|
||||
PermissionToString(env, scope))
|
||||
.Check();
|
||||
msg->Set(context,
|
||||
env->resource_string(),
|
||||
ToV8Value(context, param).ToLocalChecked())
|
||||
.Check();
|
||||
msg->Set(context,
|
||||
FIXED_ONE_BYTE_STRING(isolate, "drop"),
|
||||
v8::Boolean::New(isolate, true))
|
||||
.Check();
|
||||
ch->Publish(env, msg);
|
||||
|
||||
v8::MaybeLocal<v8::Value> values[] = {
|
||||
PermissionToString(env, scope),
|
||||
ToV8Value(context, param),
|
||||
v8::True(isolate),
|
||||
};
|
||||
ch->Publish(
|
||||
env,
|
||||
GetPermissionDiagnosicsTemplate(env)->NewInstance(context, values));
|
||||
publishing_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user