@@ -141,53 +141,49 @@ PermissionScope Permission::StringToPermission(std::string_view perm) { |
141 | 141 | #undef V |
142 | 142 |
|
143 | 143 | Permission::Permission() : enabled_(false), warning_only_(false) { |
144 | | - std::shared_ptr<PermissionBase> fs = std::make_shared<FSPermission>(); |
145 | | - std::shared_ptr<PermissionBase> child_p = |
146 | | - std::make_shared<ChildProcessPermission>(); |
147 | | - std::shared_ptr<PermissionBase> worker_t = |
148 | | - std::make_shared<WorkerPermission>(); |
149 | | - std::shared_ptr<PermissionBase> inspector = |
150 | | - std::make_shared<InspectorPermission>(); |
151 | | - std::shared_ptr<PermissionBase> wasi = std::make_shared<WASIPermission>(); |
152 | | - std::shared_ptr<PermissionBase> net = std::make_shared<NetPermission>(); |
153 | | - std::shared_ptr<PermissionBase> addon = std::make_shared<AddonPermission>(); |
154 | | - std::shared_ptr<FFIPermission> ffi = std::make_shared<FFIPermission>(); |
155 | | - std::shared_ptr<PermissionBase> openssl_store = |
156 | | - std::make_shared<OpenSSLStorePermission>(); |
| 144 | + auto fs = std::make_shared<FSPermission>(); |
| 145 | + auto child_p = std::make_shared<ChildProcessPermission>(); |
| 146 | + auto worker_t = std::make_shared<WorkerPermission>(); |
| 147 | + auto inspector = std::make_shared<InspectorPermission>(); |
| 148 | + auto wasi = std::make_shared<WASIPermission>(); |
| 149 | + auto net = std::make_shared<NetPermission>(); |
| 150 | + auto addon = std::make_shared<AddonPermission>(); |
| 151 | + auto ffi = std::make_shared<FFIPermission>(); |
| 152 | + auto openssl_store = std::make_shared<OpenSSLStorePermission>(); |
157 | 153 | #define V(Name, _, __, ___) \ |
158 | | - nodes_.insert(std::make_pair(PermissionScope::k##Name, fs)); |
| 154 | + nodes_[static_cast<size_t>(PermissionScope::k##Name)] = fs; |
159 | 155 | FILESYSTEM_PERMISSIONS(V) |
160 | 156 | #undef V |
161 | 157 | #define V(Name, _, __, ___) \ |
162 | | - nodes_.insert(std::make_pair(PermissionScope::k##Name, child_p)); |
| 158 | + nodes_[static_cast<size_t>(PermissionScope::k##Name)] = child_p; |
163 | 159 | CHILD_PROCESS_PERMISSIONS(V) |
164 | 160 | #undef V |
165 | 161 | #define V(Name, _, __, ___) \ |
166 | | - nodes_.insert(std::make_pair(PermissionScope::k##Name, worker_t)); |
| 162 | + nodes_[static_cast<size_t>(PermissionScope::k##Name)] = worker_t; |
167 | 163 | WORKER_THREADS_PERMISSIONS(V) |
168 | 164 | #undef V |
169 | 165 | #define V(Name, _, __, ___) \ |
170 | | - nodes_.insert(std::make_pair(PermissionScope::k##Name, inspector)); |
| 166 | + nodes_[static_cast<size_t>(PermissionScope::k##Name)] = inspector; |
171 | 167 | INSPECTOR_PERMISSIONS(V) |
172 | 168 | #undef V |
173 | 169 | #define V(Name, _, __, ___) \ |
174 | | - nodes_.insert(std::make_pair(PermissionScope::k##Name, wasi)); |
| 170 | + nodes_[static_cast<size_t>(PermissionScope::k##Name)] = wasi; |
175 | 171 | WASI_PERMISSIONS(V) |
176 | 172 | #undef V |
177 | 173 | #define V(Name, _, __, ___) \ |
178 | | - nodes_.insert(std::make_pair(PermissionScope::k##Name, net)); |
| 174 | + nodes_[static_cast<size_t>(PermissionScope::k##Name)] = net; |
179 | 175 | NET_PERMISSIONS(V) |
180 | 176 | #undef V |
181 | 177 | #define V(Name, _, __, ___) \ |
182 | | - nodes_.insert(std::make_pair(PermissionScope::k##Name, addon)); |
| 178 | + nodes_[static_cast<size_t>(PermissionScope::k##Name)] = addon; |
183 | 179 | ADDON_PERMISSIONS(V) |
184 | 180 | #undef V |
185 | 181 | #define V(Name, _, __, ___) \ |
186 | | - nodes_.insert(std::make_pair(PermissionScope::k##Name, ffi)); |
| 182 | + nodes_[static_cast<size_t>(PermissionScope::k##Name)] = ffi; |
187 | 183 | FFI_PERMISSIONS(V) |
188 | 184 | #undef V |
189 | 185 | #define V(Name, _, __, ___) \ |
190 | | - nodes_.insert(std::make_pair(PermissionScope::k##Name, openssl_store)); |
| 186 | + nodes_[static_cast<size_t>(PermissionScope::k##Name)] = openssl_store; |
191 | 187 | OPENSSL_STORE_PERMISSIONS(V) |
192 | 188 | #undef V |
193 | 189 | } |
@@ -264,10 +260,10 @@ bool Permission::is_scope_granted(Environment* env, |
264 | 260 | std::string_view res) const { |
265 | 261 | CHECK(permission != PermissionScope::kPermissionsRoot && |
266 | 262 | permission != PermissionScope::kPermissionsCount); |
267 | | - auto perm_node = nodes_.find(permission); |
| 263 | + auto& perm_node = nodes_[static_cast<size_t>(permission)]; |
268 | 264 | bool result = false; |
269 | | - if (perm_node != nodes_.end()) { |
270 | | - result = perm_node->second->is_granted(env, permission, res); |
| 265 | + if (perm_node) { |
| 266 | + result = perm_node->is_granted(env, permission, res); |
271 | 267 | } |
272 | 268 |
|
273 | 269 | if (!result && !publishing_) { |
@@ -296,17 +292,13 @@ BaseObjectPtr<diagnostics_channel::Channel> Permission::GetOrCreateChannel( |
296 | 292 | Environment* env, PermissionScope scope) const { |
297 | 293 | CHECK(scope != PermissionScope::kPermissionsRoot && |
298 | 294 | scope != PermissionScope::kPermissionsCount); |
299 | | - auto it = channels_.find(scope); |
300 | | - if (it != channels_.end()) { |
301 | | - // Promote weak ref to strong for the duration of this call. |
302 | | - BaseObjectPtr<diagnostics_channel::Channel> ptr(it->second.get()); |
303 | | - if (ptr) return ptr; |
304 | | - channels_.erase(it); |
305 | | - } |
| 295 | + auto& weak_ch = channels_[static_cast<size_t>(scope)]; |
| 296 | + // Promote weak ref to strong for the duration of this call. |
| 297 | + BaseObjectPtr<diagnostics_channel::Channel> ptr(weak_ch.get()); |
| 298 | + if (ptr) return ptr; |
306 | 299 | auto channel_name = GetDiagnosticsChannelName(scope); |
307 | 300 | if (auto ch = diagnostics_channel::Channel::Get(env, channel_name)) { |
308 | | - channels_.emplace(scope, |
309 | | - BaseObjectWeakPtr<diagnostics_channel::Channel>(ch)); |
| 301 | + weak_ch = BaseObjectWeakPtr<diagnostics_channel::Channel>(ch.get()); |
310 | 302 | return ch; |
311 | 303 | } |
312 | 304 | return {}; |
@@ -315,9 +307,9 @@ BaseObjectPtr<diagnostics_channel::Channel> Permission::GetOrCreateChannel( |
315 | 307 | void Permission::Apply(Environment* env, |
316 | 308 | std::span<const std::string> allow, |
317 | 309 | PermissionScope scope) { |
318 | | - auto permission = nodes_.find(scope); |
319 | | - if (permission != nodes_.end()) { |
320 | | - permission->second->Apply(env, allow, scope); |
| 310 | + auto& perm_node = nodes_[static_cast<size_t>(scope)]; |
| 311 | + if (perm_node) { |
| 312 | + perm_node->Apply(env, allow, scope); |
321 | 313 | } |
322 | 314 | } |
323 | 315 |
|
@@ -326,9 +318,9 @@ void Permission::Drop(Environment* env, |
326 | 318 | std::string_view param) { |
327 | 319 | CHECK(scope != PermissionScope::kPermissionsRoot && |
328 | 320 | scope != PermissionScope::kPermissionsCount); |
329 | | - auto permission = nodes_.find(scope); |
330 | | - if (permission != nodes_.end()) { |
331 | | - permission->second->Drop(env, scope, param); |
| 321 | + auto& perm_node = nodes_[static_cast<size_t>(scope)]; |
| 322 | + if (perm_node) { |
| 323 | + perm_node->Drop(env, scope, param); |
332 | 324 | } |
333 | 325 |
|
334 | 326 | // Publish to diagnostics channel so observers can track drops |
|