ensureDeepLinkProtocolRegistered function

Future<void> ensureDeepLinkProtocolRegistered()

Auto-register the protocol handler when missing or stale.

Implementation

Future<void> ensureDeepLinkProtocolRegistered() async {
  final neomagePath = await _resolveNeomagePath();
  if (await isProtocolHandlerCurrent(neomagePath)) return;

  // Check failure backoff
  final configHome =
      Platform.environment['MAGE_CONFIG_HOME'] ??
      p.join(_homeDir(), '.neomage');
  final failureMarkerPath = p.join(configHome, '.deep-link-register-failed');
  try {
    final stat = await File(failureMarkerPath).stat();
    if (DateTime.now().millisecondsSinceEpoch -
            stat.modified.millisecondsSinceEpoch <
        failureBackoffMs) {
      return;
    }
  } catch (_) {
    // Marker absent — proceed
  }

  try {
    await registerProtocolHandler(neomagePath);
    _logDebug(
      'Auto-registered $deepLinkProtocol:// deep link protocol handler',
    );
    try {
      await File(failureMarkerPath).delete();
    } catch (_) {}
  } catch (e) {
    _logDebug('Failed to auto-register deep link protocol handler: $e');
    try {
      await File(failureMarkerPath).writeAsString('');
    } catch (_) {}
  }
}