Skip to content

Commit 968b2ca

Browse files
jasnelladuh95
authored andcommitted
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: #65158 Reviewed-By: Xuguang Mei <meixuguang@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent 18e16e7 commit 968b2ca

4 files changed

Lines changed: 41 additions & 35 deletions

File tree

src/env-inl.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -842,9 +842,8 @@ void Environment::set_process_exit_handler(
842842
#undef VP
843843

844844
#define V(Name, label, _, __) \
845-
inline v8::Local<v8::String> \
846-
IsolateData::Name##_permission_string() const { \
847-
return Name##_permission_string##_.Get(isolate_); \
845+
inline v8::Local<v8::String> IsolateData::Name##_permission_string() const { \
846+
return Name##_permission_string##_.Get(isolate_); \
848847
}
849848
PERMISSIONS(V)
850849
#undef V
@@ -879,9 +878,8 @@ void Environment::set_process_exit_handler(
879878
#undef VP
880879

881880
#define V(Name, label, _, __) \
882-
inline v8::Local<v8::String> \
883-
Environment::Name##_permission_string() const { \
884-
return isolate_data()->Name##_permission_string(); \
881+
inline v8::Local<v8::String> Environment::Name##_permission_string() const { \
882+
return isolate_data()->Name##_permission_string(); \
885883
}
886884
PERMISSIONS(V)
887885
#undef V

src/env.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,10 +424,9 @@ void IsolateData::DeserializeProperties(const IsolateDataSerializeInfo* info) {
424424
info->primitive_values[i++]); \
425425
Local<String> field; \
426426
if (!maybe_field.ToLocal(&field)) { \
427-
fprintf(stderr, \
428-
"Failed to deserialize " #Name "_permission_string\n"); \
427+
fprintf(stderr, "Failed to deserialize " #Name "_permission_string\n"); \
429428
} \
430-
Name##_permission_string##_.Set(isolate_, field); \
429+
Name##_permission_string##_.Set(isolate_, field); \
431430
} while (0);
432431
PERMISSIONS(V)
433432
#undef V

src/env_properties.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@
446446
V(naptr_record_template, v8::DictionaryTemplate) \
447447
V(object_stats_template, v8::DictionaryTemplate) \
448448
V(page_stats_template, v8::DictionaryTemplate) \
449+
V(permission_diagnostic_channel_message, v8::DictionaryTemplate) \
449450
V(pipe_constructor_template, v8::FunctionTemplate) \
450451
V(script_context_constructor_template, v8::FunctionTemplate) \
451452
V(secure_context_constructor_template, v8::FunctionTemplate) \

src/permission/permission.cc

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "node_external_reference.h"
99
#include "node_file.h"
1010

11+
#include "v8-template.h"
1112
#include "v8.h"
1213

1314
#include <memory>
@@ -17,11 +18,13 @@
1718
namespace node {
1819

1920
using v8::Context;
21+
using v8::DictionaryTemplate;
2022
using v8::FunctionCallbackInfo;
2123
using v8::IntegrityLevel;
2224
using v8::Local;
2325
using v8::MaybeLocal;
2426
using v8::Object;
27+
using v8::Undefined;
2528
using v8::Value;
2629

2730
namespace permission {
@@ -51,6 +54,20 @@ constexpr std::string_view GetDiagnosticsChannelName(PermissionScope scope) {
5154
}
5255
}
5356

57+
Local<DictionaryTemplate> GetPermissionDiagnosicsTemplate(Environment* env) {
58+
auto tmpl = env->permission_diagnostic_channel_message();
59+
if (tmpl.IsEmpty()) {
60+
static constexpr std::string_view names[] = {
61+
"permission",
62+
"resource",
63+
"drop",
64+
};
65+
tmpl = DictionaryTemplate::New(env->isolate(), names);
66+
env->set_permission_diagnostic_channel_message(tmpl);
67+
}
68+
return tmpl;
69+
}
70+
5471
// permission.drop('fs.read', '/tmp/')
5572
// permission.drop('child')
5673
static void Drop(const FunctionCallbackInfo<Value>& args) {
@@ -245,17 +262,14 @@ bool Permission::is_scope_granted(Environment* env,
245262
v8::Isolate* isolate = env->isolate();
246263
v8::HandleScope handle_scope(isolate);
247264
v8::Local<v8::Context> context = env->context();
248-
v8::Local<v8::Object> msg =
249-
v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0);
250-
msg->Set(context,
251-
env->permission_string(),
252-
PermissionToString(env, permission))
253-
.Check();
254-
msg->Set(context,
255-
env->resource_string(),
256-
ToV8Value(context, res).ToLocalChecked())
257-
.Check();
258-
ch->Publish(env, msg);
265+
v8::MaybeLocal<v8::Value> values[] = {
266+
PermissionToString(env, permission),
267+
ToV8Value(context, res),
268+
Undefined(isolate),
269+
};
270+
ch->Publish(
271+
env,
272+
GetPermissionDiagnosicsTemplate(env)->NewInstance(context, values));
259273
publishing_ = false;
260274
}
261275
}
@@ -310,21 +324,15 @@ void Permission::Drop(Environment* env,
310324
v8::Isolate* isolate = env->isolate();
311325
v8::HandleScope handle_scope(isolate);
312326
v8::Local<v8::Context> context = env->context();
313-
v8::Local<v8::Object> msg =
314-
v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0);
315-
msg->Set(context,
316-
env->permission_string(),
317-
PermissionToString(env, scope))
318-
.Check();
319-
msg->Set(context,
320-
env->resource_string(),
321-
ToV8Value(context, param).ToLocalChecked())
322-
.Check();
323-
msg->Set(context,
324-
FIXED_ONE_BYTE_STRING(isolate, "drop"),
325-
v8::Boolean::New(isolate, true))
326-
.Check();
327-
ch->Publish(env, msg);
327+
328+
v8::MaybeLocal<v8::Value> values[] = {
329+
PermissionToString(env, scope),
330+
ToV8Value(context, param),
331+
v8::True(isolate),
332+
};
333+
ch->Publish(
334+
env,
335+
GetPermissionDiagnosicsTemplate(env)->NewInstance(context, values));
328336
publishing_ = false;
329337
}
330338
}

0 commit comments

Comments
 (0)