Skip to content

Commit

Permalink
adjust return type
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Oct 21, 2024
1 parent c80cc3d commit f6d39d5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 23 deletions.
30 changes: 16 additions & 14 deletions crates/tauri/src/ipc/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,17 @@ impl<'a, R: Runtime, T: ScopeObject> CommandArg<'a, R> for CommandScope<T> {
pub struct GlobalScope<T: ScopeObject>(ScopeValue<T>);

impl<T: ScopeObject> GlobalScope<T> {
pub(crate) fn resolve<R: Runtime>(webview: &Webview<R>, plugin: &str) -> crate::Result<Self> {
webview
.manager()
.runtime_authority
.lock()
.unwrap()
.scope_manager
.get_global_scope_typed(webview.app_handle(), plugin)
.map(Self)
}

/// What this access scope allows.
pub fn allows(&self) -> &Vec<Arc<T>> {
&self.0.allow
Expand All @@ -754,20 +765,11 @@ impl<T: ScopeObject> GlobalScope<T> {
impl<'a, R: Runtime, T: ScopeObject> CommandArg<'a, R> for GlobalScope<T> {
/// Grabs the [`ResolvedScope`] from the [`CommandItem`] and returns the associated [`GlobalScope`].
fn from_command(command: CommandItem<'a, R>) -> Result<Self, InvokeError> {
command
.message
.webview
.manager()
.runtime_authority
.lock()
.unwrap()
.scope_manager
.get_global_scope_typed(
command.message.webview.app_handle(),
command.plugin.unwrap_or(APP_ACL_KEY),
)
.map_err(InvokeError::from_error)
.map(GlobalScope)
GlobalScope::resolve(
&command.message.webview,
command.plugin.unwrap_or(APP_ACL_KEY),
)
.map_err(InvokeError::from_error)
}
}

Expand Down
34 changes: 29 additions & 5 deletions crates/tauri/src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ use crate::{
app::{UriSchemeResponder, WebviewEvent},
event::{EmitArgs, EventTarget},
ipc::{
CallbackFn, CommandArg, CommandItem, CommandScope, Invoke, InvokeBody, InvokeError,
InvokeMessage, InvokeResolver, Origin, OwnedInvokeResponder, ScopeObject,
CallbackFn, CommandArg, CommandItem, CommandScope, GlobalScope, Invoke, InvokeBody,
InvokeError, InvokeMessage, InvokeResolver, Origin, OwnedInvokeResponder, ScopeObject,
},
manager::AppManager,
sealed::{ManagerBase, RuntimeOrDispatch},
Expand Down Expand Up @@ -923,7 +923,7 @@ impl<R: Runtime> Webview<R> {
&self,
plugin: &str,
command: &str,
) -> crate::Result<Option<CommandScope<T>>> {
) -> crate::Result<Option<ResolvedScope<T>>> {
let current_url = self.url()?;
let is_local = self.is_local_url(&current_url);
let origin = if is_local {
Expand All @@ -945,8 +945,14 @@ impl<R: Runtime> Webview<R> {
.iter()
.filter_map(|cmd| cmd.scope_id)
.collect::<Vec<_>>();
let scope = CommandScope::resolve(self, scope_ids)?;
Ok(Some(scope))

let command_scope = CommandScope::resolve(self, scope_ids)?;
let global_scope = GlobalScope::resolve(self, plugin)?;

Ok(Some(ResolvedScope {
global_scope,
command_scope,
}))
} else {
Ok(None)
}
Expand Down Expand Up @@ -1773,6 +1779,24 @@ impl<'de, R: Runtime> CommandArg<'de, R> for Webview<R> {
}
}

/// Resolved scope that can be obtained via [`Webview::resolve_command_scope`].
pub struct ResolvedScope<T: ScopeObject> {
command_scope: CommandScope<T>,
global_scope: GlobalScope<T>,
}

impl<T: ScopeObject> ResolvedScope<T> {
/// The global plugin scope.
pub fn global_scope(&self) -> &GlobalScope<T> {
&self.global_scope
}

/// The command-specific scope.
pub fn command_scope(&self) -> &CommandScope<T> {
&self.command_scope
}
}

#[cfg(test)]
mod tests {
#[test]
Expand Down
8 changes: 4 additions & 4 deletions crates/tauri/src/webview/webview_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::{

use crate::{
event::EventTarget,
ipc::{CommandScope, ScopeObject},
ipc::ScopeObject,
runtime::dpi::{PhysicalPosition, PhysicalSize},
window::Monitor,
Emitter, Listener, ResourceTable, Window,
Expand Down Expand Up @@ -49,7 +49,7 @@ use tauri_macros::default_runtime;
#[cfg(windows)]
use windows::Win32::Foundation::HWND;

use super::DownloadEvent;
use super::{DownloadEvent, ResolvedScope};

/// A builder for [`WebviewWindow`], a window that hosts a single webview.
pub struct WebviewWindowBuilder<'a, R: Runtime, M: Manager<R>> {
Expand Down Expand Up @@ -997,7 +997,7 @@ impl<R: Runtime> WebviewWindow<R> {
///
/// If the scope cannot be deserialized to the given type, an error is returned.
///
/// In a command context this can be directly resolved from the command arguments via [CommandScope]:
/// In a command context this can be directly resolved from the command arguments via [crate::ipc::CommandScope]:
///
/// ```
/// use tauri::ipc::CommandScope;
Expand Down Expand Up @@ -1033,7 +1033,7 @@ impl<R: Runtime> WebviewWindow<R> {
&self,
plugin: &str,
command: &str,
) -> crate::Result<Option<CommandScope<T>>> {
) -> crate::Result<Option<ResolvedScope<T>>> {
self.webview.resolve_command_scope(plugin, command)
}
}
Expand Down

0 comments on commit f6d39d5

Please sign in to comment.