|
8 | 8 | #include "node_external_reference.h" |
9 | 9 | #include "node_file.h" |
10 | 10 |
|
| 11 | +#include "v8-template.h" |
11 | 12 | #include "v8.h" |
12 | 13 |
|
13 | 14 | #include <memory> |
|
17 | 18 | namespace node { |
18 | 19 |
|
19 | 20 | using v8::Context; |
| 21 | +using v8::DictionaryTemplate; |
20 | 22 | using v8::FunctionCallbackInfo; |
21 | 23 | using v8::IntegrityLevel; |
22 | 24 | using v8::Local; |
23 | 25 | using v8::MaybeLocal; |
24 | 26 | using v8::Object; |
| 27 | +using v8::Undefined; |
25 | 28 | using v8::Value; |
26 | 29 |
|
27 | 30 | namespace permission { |
@@ -51,6 +54,20 @@ constexpr std::string_view GetDiagnosticsChannelName(PermissionScope scope) { |
51 | 54 | } |
52 | 55 | } |
53 | 56 |
|
| 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 | + |
54 | 71 | // permission.drop('fs.read', '/tmp/') |
55 | 72 | // permission.drop('child') |
56 | 73 | static void Drop(const FunctionCallbackInfo<Value>& args) { |
@@ -245,17 +262,14 @@ bool Permission::is_scope_granted(Environment* env, |
245 | 262 | v8::Isolate* isolate = env->isolate(); |
246 | 263 | v8::HandleScope handle_scope(isolate); |
247 | 264 | 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)); |
259 | 273 | publishing_ = false; |
260 | 274 | } |
261 | 275 | } |
@@ -310,21 +324,15 @@ void Permission::Drop(Environment* env, |
310 | 324 | v8::Isolate* isolate = env->isolate(); |
311 | 325 | v8::HandleScope handle_scope(isolate); |
312 | 326 | 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)); |
328 | 336 | publishing_ = false; |
329 | 337 | } |
330 | 338 | } |
|
0 commit comments