2
2
// SPDX-License-Identifier: Apache-2.0
3
3
// SPDX-License-Identifier: MIT
4
4
5
+ use std:: collections:: BTreeMap ;
5
6
use std:: fmt:: { Debug , Display } ;
6
- use std:: { collections :: BTreeMap , ops :: Deref } ;
7
+ use std:: sync :: Arc ;
7
8
8
9
use serde:: de:: DeserializeOwned ;
9
10
use state:: TypeMap ;
@@ -335,11 +336,18 @@ impl RuntimeAuthority {
335
336
/// List of allowed and denied objects that match either the command-specific or plugin global scope criterias.
336
337
#[ derive( Debug ) ]
337
338
pub struct ScopeValue < T : ScopeObject > {
338
- allow : Vec < T > ,
339
- deny : Vec < T > ,
339
+ allow : Arc < Vec < T > > ,
340
+ deny : Arc < Vec < T > > ,
340
341
}
341
342
342
343
impl < T : ScopeObject > ScopeValue < T > {
344
+ fn clone ( & self ) -> Self {
345
+ Self {
346
+ allow : self . allow . clone ( ) ,
347
+ deny : self . deny . clone ( ) ,
348
+ }
349
+ }
350
+
343
351
/// What this access scope allows.
344
352
pub fn allows ( & self ) -> & Vec < T > {
345
353
& self . allow
@@ -351,27 +359,11 @@ impl<T: ScopeObject> ScopeValue<T> {
351
359
}
352
360
}
353
361
354
- #[ derive( Debug ) ]
355
- enum OwnedOrRef < ' a , T : Debug > {
356
- Owned ( T ) ,
357
- Ref ( & ' a T ) ,
358
- }
359
-
360
- impl < ' a , T : Debug > Deref for OwnedOrRef < ' a , T > {
361
- type Target = T ;
362
- fn deref ( & self ) -> & Self :: Target {
363
- match self {
364
- Self :: Owned ( t) => t,
365
- Self :: Ref ( r) => r,
366
- }
367
- }
368
- }
369
-
370
362
/// Access scope for a command that can be retrieved directly in the command function.
371
363
#[ derive( Debug ) ]
372
- pub struct CommandScope < ' a , T : ScopeObject > ( OwnedOrRef < ' a , ScopeValue < T > > ) ;
364
+ pub struct CommandScope < T : ScopeObject > ( ScopeValue < T > ) ;
373
365
374
- impl < ' a , T : ScopeObject > CommandScope < ' a , T > {
366
+ impl < T : ScopeObject > CommandScope < T > {
375
367
/// What this access scope allows.
376
368
pub fn allows ( & self ) -> & Vec < T > {
377
369
& self . 0 . allow
@@ -383,33 +375,35 @@ impl<'a, T: ScopeObject> CommandScope<'a, T> {
383
375
}
384
376
}
385
377
386
- impl < ' a , R : Runtime , T : ScopeObject > CommandArg < ' a , R > for CommandScope < ' a , T > {
378
+ impl < ' a , R : Runtime , T : ScopeObject > CommandArg < ' a , R > for CommandScope < T > {
387
379
/// Grabs the [`ResolvedScope`] from the [`CommandItem`] and returns the associated [`CommandScope`].
388
380
fn from_command ( command : CommandItem < ' a , R > ) -> Result < Self , InvokeError > {
389
381
if let Some ( scope_id) = command. acl . as_ref ( ) . and_then ( |resolved| resolved. scope ) {
390
- Ok ( CommandScope ( OwnedOrRef :: Ref (
382
+ Ok ( CommandScope (
391
383
command
392
384
. message
393
385
. webview
394
386
. manager ( )
395
387
. runtime_authority
388
+ . lock ( )
389
+ . unwrap ( )
396
390
. scope_manager
397
391
. get_command_scope_typed ( command. message . webview . app_handle ( ) , & scope_id) ?,
398
- ) ) )
392
+ ) )
399
393
} else {
400
- Ok ( CommandScope ( OwnedOrRef :: Owned ( ScopeValue {
401
- allow : Vec :: new ( ) ,
402
- deny : Vec :: new ( ) ,
403
- } ) ) )
394
+ Ok ( CommandScope ( ScopeValue {
395
+ allow : Default :: default ( ) ,
396
+ deny : Default :: default ( ) ,
397
+ } ) )
404
398
}
405
399
}
406
400
}
407
401
408
402
/// Global access scope that can be retrieved directly in the command function.
409
403
#[ derive( Debug ) ]
410
- pub struct GlobalScope < ' a , T : ScopeObject > ( & ' a ScopeValue < T > ) ;
404
+ pub struct GlobalScope < T : ScopeObject > ( ScopeValue < T > ) ;
411
405
412
- impl < ' a , T : ScopeObject > GlobalScope < ' a , T > {
406
+ impl < T : ScopeObject > GlobalScope < T > {
413
407
/// What this access scope allows.
414
408
pub fn allows ( & self ) -> & Vec < T > {
415
409
& self . 0 . allow
@@ -421,7 +415,7 @@ impl<'a, T: ScopeObject> GlobalScope<'a, T> {
421
415
}
422
416
}
423
417
424
- impl < ' a , R : Runtime , T : ScopeObject > CommandArg < ' a , R > for GlobalScope < ' a , T > {
418
+ impl < ' a , R : Runtime , T : ScopeObject > CommandArg < ' a , R > for GlobalScope < T > {
425
419
/// Grabs the [`ResolvedScope`] from the [`CommandItem`] and returns the associated [`GlobalScope`].
426
420
fn from_command ( command : CommandItem < ' a , R > ) -> Result < Self , InvokeError > {
427
421
command
@@ -437,6 +431,8 @@ impl<'a, R: Runtime, T: ScopeObject> CommandArg<'a, R> for GlobalScope<'a, T> {
437
431
. webview
438
432
. manager ( )
439
433
. runtime_authority
434
+ . lock ( )
435
+ . unwrap ( )
440
436
. scope_manager
441
437
. get_global_scope_typed ( command. message . webview . app_handle ( ) , plugin)
442
438
. map_err ( InvokeError :: from_error)
@@ -476,9 +472,9 @@ impl ScopeManager {
476
472
& self ,
477
473
app : & AppHandle < R > ,
478
474
plugin : & str ,
479
- ) -> crate :: Result < & ScopeValue < T > > {
480
- match self . global_scope_cache . try_get ( ) {
481
- Some ( cached) => Ok ( cached) ,
475
+ ) -> crate :: Result < ScopeValue < T > > {
476
+ match self . global_scope_cache . try_get :: < ScopeValue < T > > ( ) {
477
+ Some ( cached) => Ok ( cached. clone ( ) ) ,
482
478
None => {
483
479
let mut allow: Vec < T > = Vec :: new ( ) ;
484
480
let mut deny: Vec < T > = Vec :: new ( ) ;
@@ -498,9 +494,12 @@ impl ScopeManager {
498
494
}
499
495
}
500
496
501
- let scope = ScopeValue { allow, deny } ;
502
- let _ = self . global_scope_cache . set ( scope) ;
503
- Ok ( self . global_scope_cache . get ( ) )
497
+ let scope = ScopeValue {
498
+ allow : Arc :: new ( allow) ,
499
+ deny : Arc :: new ( deny) ,
500
+ } ;
501
+ self . global_scope_cache . set ( scope. clone ( ) ) ;
502
+ Ok ( scope)
504
503
}
505
504
}
506
505
}
@@ -509,10 +508,10 @@ impl ScopeManager {
509
508
& self ,
510
509
app : & AppHandle < R > ,
511
510
key : & ScopeKey ,
512
- ) -> crate :: Result < & ScopeValue < T > > {
511
+ ) -> crate :: Result < ScopeValue < T > > {
513
512
let cache = self . command_cache . get ( key) . unwrap ( ) ;
514
- match cache. try_get ( ) {
515
- Some ( cached) => Ok ( cached) ,
513
+ match cache. try_get :: < ScopeValue < T > > ( ) {
514
+ Some ( cached) => Ok ( cached. clone ( ) ) ,
516
515
None => {
517
516
let resolved_scope = self
518
517
. command_scope
@@ -535,10 +534,13 @@ impl ScopeManager {
535
534
) ;
536
535
}
537
536
538
- let value = ScopeValue { allow, deny } ;
537
+ let value = ScopeValue {
538
+ allow : Arc :: new ( allow) ,
539
+ deny : Arc :: new ( deny) ,
540
+ } ;
539
541
540
- let _ = cache. set ( value) ;
541
- Ok ( cache . get ( ) )
542
+ let _ = cache. set ( value. clone ( ) ) ;
543
+ Ok ( value )
542
544
}
543
545
}
544
546
}
0 commit comments