Skip to content

Commit 8e831a3

Browse files
jasnelladuh95
authored andcommitted
src: make minor cleanup to permission checks
Getting the name of the channel is unnecessary. 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 968b2ca commit 8e831a3

1 file changed

Lines changed: 23 additions & 20 deletions

File tree

src/permission/permission.cc

Lines changed: 23 additions & 20 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 "permission/permission_base.h"
1112
#include "v8-template.h"
1213
#include "v8.h"
1314

@@ -247,31 +248,30 @@ void Permission::EnableWarningOnly() {
247248
bool Permission::is_scope_granted(Environment* env,
248249
const PermissionScope permission,
249250
const std::string_view& res) const {
251+
CHECK(permission != PermissionScope::kPermissionsRoot &&
252+
permission != PermissionScope::kPermissionsCount);
250253
auto perm_node = nodes_.find(permission);
251254
bool result = false;
252255
if (perm_node != nodes_.end()) {
253256
result = perm_node->second->is_granted(env, permission, res);
254257
}
255258

256259
if (!result && !publishing_) {
257-
auto channel_name = GetDiagnosticsChannelName(permission);
258-
if (!channel_name.empty()) {
259-
auto ch = GetOrCreateChannel(env, permission);
260-
if (ch && ch->HasSubscribers()) {
261-
publishing_ = true;
262-
v8::Isolate* isolate = env->isolate();
263-
v8::HandleScope handle_scope(isolate);
264-
v8::Local<v8::Context> context = env->context();
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));
273-
publishing_ = false;
274-
}
260+
auto ch = GetOrCreateChannel(env, permission);
261+
if (ch && ch->HasSubscribers()) {
262+
publishing_ = true;
263+
v8::Isolate* isolate = env->isolate();
264+
v8::HandleScope handle_scope(isolate);
265+
v8::Local<v8::Context> context = env->context();
266+
v8::MaybeLocal<v8::Value> values[] = {
267+
PermissionToString(env, permission),
268+
ToV8Value(context, res),
269+
Undefined(isolate),
270+
};
271+
ch->Publish(
272+
env,
273+
GetPermissionDiagnosicsTemplate(env)->NewInstance(context, values));
274+
publishing_ = false;
275275
}
276276
}
277277

@@ -280,6 +280,8 @@ bool Permission::is_scope_granted(Environment* env,
280280

281281
BaseObjectPtr<diagnostics_channel::Channel> Permission::GetOrCreateChannel(
282282
Environment* env, PermissionScope scope) const {
283+
CHECK(scope != PermissionScope::kPermissionsRoot &&
284+
scope != PermissionScope::kPermissionsCount);
283285
auto it = channels_.find(scope);
284286
if (it != channels_.end()) {
285287
// Promote weak ref to strong for the duration of this call.
@@ -310,14 +312,15 @@ void Permission::Apply(Environment* env,
310312
void Permission::Drop(Environment* env,
311313
PermissionScope scope,
312314
const std::string_view& param) {
315+
CHECK(scope != PermissionScope::kPermissionsRoot &&
316+
scope != PermissionScope::kPermissionsCount);
313317
auto permission = nodes_.find(scope);
314318
if (permission != nodes_.end()) {
315319
permission->second->Drop(env, scope, param);
316320
}
317321

318322
// Publish to diagnostics channel so observers can track drops
319-
auto channel_name = GetDiagnosticsChannelName(scope);
320-
if (!channel_name.empty() && !publishing_) {
323+
if (!publishing_) {
321324
auto ch = GetOrCreateChannel(env, scope);
322325
if (ch && ch->HasSubscribers()) {
323326
publishing_ = true;

0 commit comments

Comments
 (0)