Files
Password-Manager/delphi-backend/Source/PM.SingleInstance.pas
T
Zaki 40b3154a34 feat: MFA tools, single-instance, tray polish, prefs persistence
Session highlights:

- feat(prefs): DPAPI-backed key/value store (PM.UserPrefs) — fixes
  rememberedUsername being lost across reboots due to the random
  ephemeral HTTP port changing the localStorage origin every launch.
  Bridge cmd://prefs/{get,set} round-trips through Delphi.

- feat(tray): icon visible from startup (NIM_ADD at constructor, not
  at first minimize). Tray context menu themed via uxtheme!135
  SetPreferredAppMode so it follows the app's dark/light setting.

- feat(single-instance): named mutex + RegisterWindowMessage broadcast.
  Second launch posts WM_PMSHOW to HWND_BROADCAST and exits; the
  running bridge restores the window from tray. Mutex lives in Local\
  namespace so distinct Windows users can still each run one.

- feat(mfa): Authenticator sidebar view (live TOTP codes for every
  entry with a secret) + standalone TOTP generator modal (paste
  base32 / otpauth:// URI, or generate a random 20-byte secret).

- feat(sidebar): Folders / Tags / Tools sections collapsible with
  chevron toggle. Badge counts stay visible when collapsed. State
  persisted in settings_json (synced across devices).

- feat(autofill): hotkey when vault is locked now restores the app
  and focuses the master password input instead of no-op'ing
  silently. Cleaner UX for the common "I hit Ctrl+Shift+L but the
  vault was locked" path.

- feat(quick-unlock): when enabled, skip lockVault on Windows lock /
  sleep. Rationale: the DPAPI blob already gates access via the
  Windows account, so re-locking on top of the OS lock is redundant.
  Idle auto-lock still fires (separate opt-in).

- fix(quick-unlock): re-sync state.quickUnlockEnabled from DPAPI
  source-of-truth at boot, instead of trusting (now-volatile)
  localStorage.

- docs: CLAUDE.md updated with all new modules, bridge commands,
  and the port-ephemeral pitfall.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-08 21:31:39 +01:00

77 lines
2.1 KiB
ObjectPascal

unit PM.SingleInstance;
{
Single-instance guard.
AcquireOrSignal:
- First instance: creates a named mutex and returns True. Caller proceeds.
- Subsequent instance: detects the mutex, broadcasts WM_PMSHOW so the
running instance restores from tray, returns False. Caller exits.
WM_PMSHOW is a RegisterWindowMessage('PMServer_ShowExisting') — system-
unique, all processes that register the same string get the same ID.
PM.Bridge listens for it on its message-only window.
}
interface
uses
Winapi.Windows, Winapi.Messages;
const
// Mutex name lives in the Local\ namespace → per-user-session, so a
// second user on the same machine (RDP, Switch User) can still launch
// their own instance. The Global\ namespace would block them.
PMSERVER_MUTEX_NAME = 'Local\PMServer.SingleInstance.Mutex';
// System-wide unique message ID, computed once. Bridge + .dpr both call
// this to get the same UINT.
function WM_PMShowMessage: UINT;
// Try to become the single instance. True = we are first; False = another
// instance was already running (we have signalled it and the caller must
// exit immediately).
function AcquireOrSignal: Boolean;
implementation
var
_Mutex: THandle = 0;
_WmShow: UINT = 0;
function WM_PMShowMessage: UINT;
begin
if _WmShow = 0 then
_WmShow := RegisterWindowMessage('PMServer_ShowExisting');
Result := _WmShow;
end;
function AcquireOrSignal: Boolean;
var
LErr: DWORD;
begin
_Mutex := CreateMutex(nil, True, PMSERVER_MUTEX_NAME);
LErr := GetLastError;
if (_Mutex <> 0) and (LErr <> ERROR_ALREADY_EXISTS) then
begin
// We are the first instance. Keep the mutex alive for the process
// lifetime — Windows releases it automatically on exit.
Result := True;
Exit;
end;
// Another instance is already running. Close our handle (it isn't ours)
// and broadcast the show-message to all top-level windows. The running
// bridge picks it up on its message-only window.
if _Mutex <> 0 then
begin
CloseHandle(_Mutex);
_Mutex := 0;
end;
PostMessage(HWND_BROADCAST, WM_PMShowMessage, 0, 0);
Result := False;
end;
end.