From 20bb89de33f33093082e8df9cae4e32be1f0d17a Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Thu, 2 Oct 2025 07:45:50 -0700 Subject: [PATCH] Try fix oauth window creation --- .../src/grants/authorizationCode.ts | 2 +- plugins/auth-oauth2/src/grants/implicit.ts | 6 ++-- src-tauri/src/lib.rs | 4 +-- src-tauri/src/plugin_events.rs | 30 ++++++++++++------- src-tauri/src/window.rs | 28 +++++++++-------- 5 files changed, 42 insertions(+), 28 deletions(-) diff --git a/plugins/auth-oauth2/src/grants/authorizationCode.ts b/plugins/auth-oauth2/src/grants/authorizationCode.ts index fa0677f7..7ba00b7c 100644 --- a/plugins/auth-oauth2/src/grants/authorizationCode.ts +++ b/plugins/auth-oauth2/src/grants/authorizationCode.ts @@ -88,9 +88,9 @@ export async function getAuthorizationCode( const code = await new Promise(async (resolve, reject) => { let foundCode = false; const { close } = await ctx.window.openUrl({ + dataDirKey, url: authorizationUrlStr, label: 'oauth-authorization-url', - dataDirKey, async onClose() { if (!foundCode) { reject(new Error('Authorization window closed')); diff --git a/plugins/auth-oauth2/src/grants/implicit.ts b/plugins/auth-oauth2/src/grants/implicit.ts index d861b472..2ef6836f 100644 --- a/plugins/auth-oauth2/src/grants/implicit.ts +++ b/plugins/auth-oauth2/src/grants/implicit.ts @@ -1,6 +1,6 @@ import type { Context } from '@yaakapp/api'; -import type { AccessToken, AccessTokenRawResponse } from '../store'; -import { getToken, storeToken } from '../store'; +import type { AccessToken, AccessTokenRawResponse} from '../store'; +import { getDataDirKey , getToken, storeToken } from '../store'; import { isTokenExpired } from '../util'; export async function getImplicit( @@ -60,7 +60,9 @@ export async function getImplicit( const newToken = await new Promise(async (resolve, reject) => { let foundAccessToken = false; const authorizationUrlStr = authorizationUrl.toString(); + const dataDirKey = await getDataDirKey(ctx, contextId); const { close } = await ctx.window.openUrl({ + dataDirKey, url: authorizationUrlStr, label: 'oauth-authorization-url', async onClose() { diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 85db6a83..d9ed6f63 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1198,13 +1198,13 @@ async fn cmd_new_child_window( title: &str, inner_size: (f64, f64), ) -> YaakResult<()> { - window::create_child_window(&parent_window, url, label, title, inner_size); + window::create_child_window(&parent_window, url, label, title, inner_size)?; Ok(()) } #[tauri::command] async fn cmd_new_main_window(app_handle: AppHandle, url: &str) -> YaakResult<()> { - window::create_main_window(&app_handle, url); + window::create_main_window(&app_handle, url)?; Ok(()) } diff --git a/src-tauri/src/plugin_events.rs b/src-tauri/src/plugin_events.rs index 126f19f7..11e5ab75 100644 --- a/src-tauri/src/plugin_events.rs +++ b/src-tauri/src/plugin_events.rs @@ -7,16 +7,16 @@ use crate::{ }; use chrono::Utc; use cookie::Cookie; -use log::warn; +use log::{error, warn}; use tauri::{AppHandle, Emitter, Manager, Runtime, State}; use tauri_plugin_clipboard_manager::ClipboardExt; use yaak_models::models::{HttpResponse, Plugin}; use yaak_models::query_manager::QueryManagerExt; use yaak_models::util::UpdateSource; use yaak_plugins::events::{ - Color, DeleteKeyValueResponse, EmptyPayload, FindHttpResponsesResponse, GetCookieValueResponse, - GetHttpRequestByIdResponse, GetKeyValueResponse, Icon, InternalEvent, InternalEventPayload, - ListCookieNamesResponse, PluginWindowContext, RenderGrpcRequestResponse, + Color, DeleteKeyValueResponse, EmptyPayload, ErrorResponse, FindHttpResponsesResponse, + GetCookieValueResponse, GetHttpRequestByIdResponse, GetKeyValueResponse, Icon, InternalEvent, + InternalEventPayload, ListCookieNamesResponse, PluginWindowContext, RenderGrpcRequestResponse, RenderHttpRequestResponse, SendHttpRequestResponse, SetKeyValueResponse, ShowToastRequest, TemplateRenderResponse, WindowNavigateEvent, }; @@ -124,6 +124,7 @@ pub(crate) async fn handle_plugin_event( Some(InternalEventPayload::TemplateRenderResponse(TemplateRenderResponse { data })) } InternalEventPayload::ErrorResponse(resp) => { + error!("Plugin error: {}: {:?}", resp.error, resp); let toast_event = plugin_handle.build_event_to_send( &window_context, &InternalEventPayload::ShowToastRequest(ShowToastRequest { @@ -218,20 +219,29 @@ pub(crate) async fn handle_plugin_event( })) } InternalEventPayload::OpenWindowRequest(req) => { - let label = req.label; let (navigation_tx, mut navigation_rx) = tokio::sync::mpsc::channel(128); let (close_tx, mut close_rx) = tokio::sync::mpsc::channel(128); let win_config = CreateWindowConfig { url: &req.url, - label: &label.clone(), - title: &req.title.unwrap_or_default(), + label: &req.label, + title: &req.title.clone().unwrap_or_default(), navigation_tx: Some(navigation_tx), close_tx: Some(close_tx), - inner_size: req.size.map(|s| (s.width, s.height)), - data_dir_key: req.data_dir_key, + inner_size: req.size.clone().map(|s| (s.width, s.height)), + data_dir_key: req.data_dir_key.clone(), ..Default::default() }; - create_window(app_handle, win_config); + if let Err(e) = create_window(app_handle, win_config) { + let error_event = plugin_handle.build_event_to_send( + &window_context, + &InternalEventPayload::ErrorResponse(ErrorResponse { + error: format!("Failed to create window: {:?}", e), + }), + None, + ); + Box::pin(handle_plugin_event(app_handle, &error_event, plugin_handle)).await; + return; + } { let event_id = event.id.clone(); diff --git a/src-tauri/src/window.rs b/src-tauri/src/window.rs index ee3d4584..90515b58 100644 --- a/src-tauri/src/window.rs +++ b/src-tauri/src/window.rs @@ -6,6 +6,8 @@ use tauri::{ }; use tauri_plugin_opener::OpenerExt; use tokio::sync::mpsc; +use crate::error::Error::GenericError; +use crate::error::Result; const DEFAULT_WINDOW_WIDTH: f64 = 1100.0; const DEFAULT_WINDOW_HEIGHT: f64 = 600.0; @@ -32,9 +34,9 @@ pub(crate) struct CreateWindowConfig<'s> { pub(crate) fn create_window( handle: &AppHandle, config: CreateWindowConfig, -) -> WebviewWindow { +) -> Result> { #[allow(unused_variables)] - let menu = app_menu(handle).unwrap(); + let menu = app_menu(handle)?; // This causes the window to not be clickable (in AppImage), so disable on Linux #[cfg(not(target_os = "linux"))] @@ -55,12 +57,12 @@ pub(crate) fn create_window( #[cfg(not(target_os = "macos"))] { use std::fs; - let dir = handle.path().temp_dir().unwrap().join("yaak_sessions").join(key); - fs::create_dir_all(dir.clone()).unwrap(); + let dir = handle.path().app_data_dir()?.join("window-sessions").join(key); + fs::create_dir_all(dir.clone())?; win_builder = win_builder.data_directory(dir); } - // macOS doesn't support data dir so must use this fn instead + // macOS doesn't support `data_directory()` so must use this fn instead #[cfg(target_os = "macos")] { let hash = md5::compute(key.as_bytes()); @@ -108,11 +110,11 @@ pub(crate) fn create_window( if let Some(w) = handle.webview_windows().get(config.label) { info!("Webview with label {} already exists. Focusing existing", config.label); - w.set_focus().unwrap(); - return w.to_owned(); + w.set_focus()?; + return Ok(w.to_owned()); } - let win = win_builder.build().unwrap(); + let win = win_builder.build()?; if let Some(tx) = config.close_tx { win.on_window_event(move |event| match event { @@ -174,10 +176,10 @@ pub(crate) fn create_window( } }); - win + Ok(win) } -pub(crate) fn create_main_window(handle: &AppHandle, url: &str) -> WebviewWindow { +pub(crate) fn create_main_window(handle: &AppHandle, url: &str) -> Result { let mut counter = 0; let label = loop { let label = format!("{MAIN_WINDOW_PREFIX}{counter}"); @@ -211,7 +213,7 @@ pub(crate) fn create_child_window( label: &str, title: &str, inner_size: (f64, f64), -) -> WebviewWindow { +) -> Result { let app_handle = parent_window.app_handle(); let label = format!("{OTHER_WINDOW_PREFIX}_{label}"); let scale_factor = parent_window.scale_factor().unwrap(); @@ -235,7 +237,7 @@ pub(crate) fn create_child_window( ..Default::default() }; - let child_window = create_window(&app_handle, config); + let child_window = create_window(&app_handle, config)?; // NOTE: These listeners will remain active even when the windows close. Unfortunately, // there's no way to unlisten to events for now, so we just have to be defensive. @@ -272,5 +274,5 @@ pub(crate) fn create_child_window( }); } - child_window + Ok(child_window) }