|
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 { |
@@ -55,6 +58,20 @@ constexpr std::string_view GetDiagnosticsChannelName(PermissionScope scope) { |
55 | 58 | } |
56 | 59 | } |
57 | 60 |
|
| 61 | +Local<DictionaryTemplate> GetPermissionDiagnosicsTemplate(Environment* env) { |
| 62 | + auto tmpl = env->permission_diagnostic_channel_message(); |
| 63 | + if (tmpl.IsEmpty()) { |
| 64 | + static constexpr std::string_view names[] = { |
| 65 | + "permission", |
| 66 | + "resource", |
| 67 | + "drop", |
| 68 | + }; |
| 69 | + tmpl = DictionaryTemplate::New(env->isolate(), names); |
| 70 | + env->set_permission_diagnostic_channel_message(tmpl); |
| 71 | + } |
| 72 | + return tmpl; |
| 73 | +} |
| 74 | + |
58 | 75 | // permission.drop('fs.read', '/tmp/') |
59 | 76 | // permission.drop('child') |
60 | 77 | static void Drop(const FunctionCallbackInfo<Value>& args) { |
@@ -259,17 +276,14 @@ bool Permission::is_scope_granted(Environment* env, |
259 | 276 | v8::Isolate* isolate = env->isolate(); |
260 | 277 | v8::HandleScope handle_scope(isolate); |
261 | 278 | v8::Local<v8::Context> context = env->context(); |
262 | | - v8::Local<v8::Object> msg = |
263 | | - v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0); |
264 | | - msg->Set(context, |
265 | | - env->permission_string(), |
266 | | - PermissionToString(env, permission)) |
267 | | - .Check(); |
268 | | - msg->Set(context, |
269 | | - env->resource_string(), |
270 | | - ToV8Value(context, res).ToLocalChecked()) |
271 | | - .Check(); |
272 | | - ch->Publish(env, msg); |
| 279 | + v8::MaybeLocal<v8::Value> values[] = { |
| 280 | + PermissionToString(env, permission), |
| 281 | + ToV8Value(context, res), |
| 282 | + Undefined(isolate), |
| 283 | + }; |
| 284 | + ch->Publish( |
| 285 | + env, |
| 286 | + GetPermissionDiagnosicsTemplate(env)->NewInstance(context, values)); |
273 | 287 | publishing_ = false; |
274 | 288 | } |
275 | 289 | } |
@@ -324,21 +338,15 @@ void Permission::Drop(Environment* env, |
324 | 338 | v8::Isolate* isolate = env->isolate(); |
325 | 339 | v8::HandleScope handle_scope(isolate); |
326 | 340 | v8::Local<v8::Context> context = env->context(); |
327 | | - v8::Local<v8::Object> msg = |
328 | | - v8::Object::New(isolate, v8::Null(isolate), nullptr, nullptr, 0); |
329 | | - msg->Set(context, |
330 | | - env->permission_string(), |
331 | | - PermissionToString(env, scope)) |
332 | | - .Check(); |
333 | | - msg->Set(context, |
334 | | - env->resource_string(), |
335 | | - ToV8Value(context, param).ToLocalChecked()) |
336 | | - .Check(); |
337 | | - msg->Set(context, |
338 | | - FIXED_ONE_BYTE_STRING(isolate, "drop"), |
339 | | - v8::Boolean::New(isolate, true)) |
340 | | - .Check(); |
341 | | - ch->Publish(env, msg); |
| 341 | + |
| 342 | + v8::MaybeLocal<v8::Value> values[] = { |
| 343 | + PermissionToString(env, scope), |
| 344 | + ToV8Value(context, param), |
| 345 | + v8::True(isolate), |
| 346 | + }; |
| 347 | + ch->Publish( |
| 348 | + env, |
| 349 | + GetPermissionDiagnosicsTemplate(env)->NewInstance(context, values)); |
342 | 350 | publishing_ = false; |
343 | 351 | } |
344 | 352 | } |
|
0 commit comments