todo-app/lib/desktop/browser_launcher.dart
Krzysztof kuhy Rudnicki 2bbfe0b663
Some checks failed
dependency freshness / dependencies on newest stable (push) Has been cancelled
file-length / file length <= 250 lines (push) Has been cancelled
md-naming / markdown naming convention (push) Has been cancelled
release-apk / release (APK) (push) Has been cancelled
Desktop: launch Chrome with --password-store=basic so a locked keyring cannot blank the window
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-25 16:31:08 +02:00

89 lines
3.5 KiB
Dart

/// Finding and launching the Chrome window the desktop app runs in.
///
/// Split out of `bin/todo_desktop.dart` for file size. The profile directory
/// is fixed on purpose: the GitHub token (localStorage) and the notes
/// (IndexedDB) are keyed by origin and live in that profile, so a moving
/// profile dir would silently log the user out and hide their notes.
library;
import 'dart:io';
import 'package:path/path.dart' as p;
/// Port must stay fixed: the browser keys `localStorage` (the GitHub token)
/// and IndexedDB (the notes) by origin, so a different port looks like a
/// different app with no token and no notes.
const kWrapperPort = 8730;
/// Browser profile directory for the desktop app.
///
/// Must stay stable: the GitHub token (localStorage) and the notes (IndexedDB)
/// live inside it, so a changing path silently logs the user out and hides
/// their notes. It doubles as the marker for "a window is still attached".
String profileDir(String home) =>
p.join(home, '.local', 'share', 'todo-desktop', 'profile');
/// Returns the first Chrome-family browser on this machine, or ''.
///
/// [override] defaults to `$TODO_BROWSER`; [exists] to a file check.
String findBrowser({String? override, bool Function(String path)? exists}) {
// Ordered by preference, and deliberately broad: this machine runs Thorium
// behind /opt/google/chrome, and has a policy that uninstalls the `chromium`
// package, so assuming any single browser is wrong. TODO_BROWSER overrides.
final candidates = [
override ?? Platform.environment['TODO_BROWSER'] ?? '',
'/opt/google/chrome/chrome',
'/opt/thorium-browser/thorium-browser',
'/usr/bin/google-chrome-stable',
'/usr/bin/chromium',
'/usr/bin/brave',
];
return candidates.firstWhere(
(path) => path.isNotEmpty && (exists ?? (p) => File(p).existsSync())(path),
orElse: () => '',
);
}
/// Command-line arguments for the app window.
List<String> browserArgs(String home) => [
'--app=http://localhost:$kWrapperPort',
'--user-data-dir=${profileDir(home)}',
// Sets WM_CLASS, which the .desktop entry matches on via StartupWMClass.
// Without it the window inherits the browser's class and the taskbar shows
// a browser icon instead of todo's.
'--class=todo',
'--no-first-run',
// Keep Chrome off the GNOME keyring. While the keyring is locked, Chrome
// blocks every non-incognito page load on its first cookie access, and an
// "Unlock Keyring" prompt waits on another workspace: the window stays a
// blank grey. Nothing in this profile needs OS-level encryption (the
// GitHub token is in localStorage, not the password store).
'--password-store=basic',
];
/// Launches the app in a Chrome-family browser with a **stable** profile
/// directory, since the token and notes live in that profile.
///
/// Returns true when the browser ran long enough to have owned the session.
/// [browser] and [start] exist for tests; they default to [findBrowser] and
/// [Process.start].
Future<bool> launchBrowser(
String home, {
String? browser,
Future<Process> Function(String executable, List<String> args)? start,
}) async {
browser ??= findBrowser();
if (browser.isEmpty) {
stderr.writeln(
'No Chrome-family browser found; open '
'http://localhost:$kWrapperPort manually.',
);
return false;
}
final process = await (start ?? Process.start)(browser, browserArgs(home));
final started = DateTime.now();
await process.exitCode;
return DateTime.now().difference(started) > const Duration(seconds: 5);
}