restartApp method

Future<void> restartApp()

Hands the staged update to the native installer or restart helper.

Implementation

Future<void> restartApp() async {
  final stagingPath = _stagingPath;
  if (stagingPath == null || stagingPath.isEmpty) {
    throw StateError("No downloaded update is ready to install.");
  }

  _state = const UpdateInstalling();
  _diagnosticsRecorder.record(
    stage: UpdateDiagnosticStage.install,
    level: UpdateDiagnosticLevel.info,
    message: "Install handoff started for $stagingPath",
  );
  emitUpdateTelemetry(
    telemetry,
    UpdateTelemetryEvent.installScheduled(
      stagingPath: stagingPath,
      version: _activeDescriptor?.version,
      channel: _activeDescriptor?.channel,
      platform: _activeDescriptor?.platform,
    ),
  );
  notifyListeners();

  try {
    await _writePendingRecoveryMarker(stagingPath);
    await DesktopUpdaterPlatform.instance.installUpdate(
      stagingPath: stagingPath,
      allowUnsignedMacOSUpdates: allowUnsignedMacOSUpdates,
      diagnosticsLogPath: diagnosticsLogPath,
    );
    final cleanupReport = _buildCleanupReport(
      stagingPath: stagingPath,
      cleanupAttempted: false,
    );
    _recordCleanupReport(cleanupReport);
    _state = UpdateInstalling(cleanupReport: cleanupReport);
    notifyListeners();
  } on Object catch (error) {
    await _clearPendingRecoveryMarker();
    _recordCleanupReport(
      _buildCleanupReport(
        stagingPath: stagingPath,
        cleanupAttempted: false,
        cleanupSucceeded: false,
        errorText: error.toString(),
      ),
    );
    _diagnosticsRecorder.record(
      stage: UpdateDiagnosticStage.install,
      level: UpdateDiagnosticLevel.error,
      message: "Install failed.",
      error: error,
    );
    _state = UpdateFailed(
      error,
      report: _buildProblemReport(
        error,
        updateVersion: _activeDescriptor?.version,
        stagingPath: stagingPath,
      ),
    );
    emitUpdateTelemetry(
      telemetry,
      UpdateTelemetryEvent.installFailed(
        stagingPath: stagingPath,
        version: _activeDescriptor?.version,
        channel: _activeDescriptor?.channel,
        platform: _activeDescriptor?.platform,
        error: error,
      ),
    );
    notifyListeners();
    rethrow;
  }
}