2025-11-09 12:05:07 +01:00
using ElectronNET.API.Entities ;
2019-05-20 01:08:26 +02:00
using System ;
2020-07-08 11:16:00 +02:00
using System.Collections.Generic ;
2019-05-20 01:08:26 +02:00
using System.Threading.Tasks ;
2025-11-09 03:50:24 +01:00
2025-11-05 14:14:37 +00:00
// ReSharper disable InconsistentNaming
2019-05-20 01:08:26 +02:00
namespace ElectronNET.API
{
/// <summary>
/// Enable apps to automatically update themselves. Based on electron-updater.
/// </summary>
2025-11-12 10:43:32 +00:00
public sealed class AutoUpdater : ApiBase
2019-05-20 01:08:26 +02:00
{
2025-11-12 10:43:32 +00:00
protected override SocketTaskEventNameTypes SocketTaskEventNameType = > SocketTaskEventNameTypes . DashesLowerFirst ;
protected override SocketTaskMessageNameTypes SocketTaskMessageNameType = > SocketTaskMessageNameTypes . DashesLowerFirst ;
protected override SocketEventNameTypes SocketEventNameType = > SocketEventNameTypes . DashedLower ;
2019-05-23 23:10:45 +02:00
/// <summary>
/// Whether to automatically download an update when it is found. (Default is true)
/// </summary>
public bool AutoDownload
{
get
{
2025-11-15 10:09:51 +01:00
return Task . Run ( ( ) = > this . InvokeAsync < bool > ( ) ) . Result ;
2019-05-23 23:10:45 +02:00
}
set
{
BridgeConnector . Socket . Emit ( "autoUpdater-autoDownload-set" , value ) ;
}
}
/// <summary>
/// Whether to automatically install a downloaded update on app quit (if `QuitAndInstall` was not called before).
///
/// Applicable only on Windows and Linux.
/// </summary>
public bool AutoInstallOnAppQuit
{
get
{
2025-11-15 10:09:51 +01:00
return Task . Run ( ( ) = > this . InvokeAsync < bool > ( ) ) . Result ;
2019-05-23 23:10:45 +02:00
}
set
{
BridgeConnector . Socket . Emit ( "autoUpdater-autoInstallOnAppQuit-set" , value ) ;
}
}
/// <summary>
/// *GitHub provider only.* Whether to allow update to pre-release versions.
/// Defaults to "true" if application version contains prerelease components (e.g. "0.12.1-alpha.1", here "alpha" is a prerelease component), otherwise "false".
///
/// If "true", downgrade will be allowed("allowDowngrade" will be set to "true").
/// </summary>
public bool AllowPrerelease
{
get
{
2025-11-15 10:09:51 +01:00
return Task . Run ( ( ) = > this . InvokeAsync < bool > ( ) ) . Result ;
2019-05-23 23:10:45 +02:00
}
set
{
BridgeConnector . Socket . Emit ( "autoUpdater-allowPrerelease-set" , value ) ;
}
}
/// <summary>
/// *GitHub provider only.*
/// Get all release notes (from current version to latest), not just the latest (Default is false).
/// </summary>
public bool FullChangelog
{
get
{
2025-11-15 10:09:51 +01:00
return Task . Run ( ( ) = > this . InvokeAsync < bool > ( ) ) . Result ;
2019-05-23 23:10:45 +02:00
}
set
{
BridgeConnector . Socket . Emit ( "autoUpdater-fullChangelog-set" , value ) ;
}
}
/// <summary>
/// Whether to allow version downgrade (when a user from the beta channel wants to go back to the stable channel).
/// Taken in account only if channel differs (pre-release version component in terms of semantic versioning).
/// Default is false.
/// </summary>
public bool AllowDowngrade
{
get
{
2025-11-15 10:09:51 +01:00
return Task . Run ( ( ) = > this . InvokeAsync < bool > ( ) ) . Result ;
2019-05-23 23:10:45 +02:00
}
set
{
BridgeConnector . Socket . Emit ( "autoUpdater-allowDowngrade-set" , value ) ;
}
}
/// <summary>
/// For test only.
/// </summary>
public string UpdateConfigPath
{
get
{
2025-11-15 10:09:51 +01:00
return Task . Run ( ( ) = > this . InvokeAsync < string > ( ) ) . Result ;
2019-05-23 23:10:45 +02:00
}
}
2020-07-08 11:16:00 +02:00
/// <summary>
/// The current application version
/// </summary>
public Task < SemVer > CurrentVersionAsync
{
get
{
2025-11-15 10:09:51 +01:00
return Task . Run ( ( ) = > this . InvokeAsync < SemVer > ( ) ) ;
2020-07-08 11:16:00 +02:00
}
}
2019-05-23 23:10:45 +02:00
/// <summary>
/// Get the update channel. Not applicable for GitHub.
/// Doesn’ t return channel from the update configuration, only if was previously set.
/// </summary>
2020-07-08 11:16:00 +02:00
[Obsolete("Use the asynchronous version ChannelAsync instead")]
2019-05-23 23:10:45 +02:00
public string Channel
2020-07-08 11:16:00 +02:00
{
get
{
2020-08-21 12:13:38 +02:00
return ChannelAsync . Result ;
2020-07-08 11:16:00 +02:00
}
}
/// <summary>
/// Get the update channel. Not applicable for GitHub.
/// Doesn’ t return channel from the update configuration, only if was previously set.
/// </summary>
public Task < string > ChannelAsync
2019-05-23 23:10:45 +02:00
{
get
{
2025-11-15 10:09:51 +01:00
return Task . Run ( ( ) = > this . InvokeAsync < string > ( ) ) ;
2025-11-12 10:43:32 +00:00
}
}
2019-05-23 23:10:45 +02:00
2025-11-12 10:43:32 +00:00
/// <summary>
/// Set the update channel. Not applicable for GitHub.
/// </summary>
public string SetChannel
{
set
{
BridgeConnector . Socket . Emit ( "autoUpdater-channel-set" , value ) ;
2020-07-08 11:16:00 +02:00
}
}
/// <summary>
/// The request headers.
/// </summary>
public Task < Dictionary < string , string > > RequestHeadersAsync
{
get
{
2025-11-15 10:09:51 +01:00
return Task . Run ( ( ) = > this . InvokeAsync < Dictionary < string , string > > ( ) ) ;
2019-05-23 23:10:45 +02:00
}
}
2019-05-23 02:59:30 +02:00
/// <summary>
2020-07-08 11:16:00 +02:00
/// The request headers.
2019-05-23 02:59:30 +02:00
/// </summary>
2020-07-08 11:16:00 +02:00
public Dictionary < string , string > RequestHeaders
{
set
{
2025-11-09 12:05:07 +01:00
BridgeConnector . Socket . Emit ( "autoUpdater-requestHeaders-set" , value ) ;
2020-07-08 11:16:00 +02:00
}
}
2020-08-21 12:13:38 +02:00
/// <summary>
/// Emitted when there is an error while updating.
/// </summary>
public event Action < string > OnError
2019-05-23 02:59:30 +02:00
{
2025-11-12 10:43:32 +00:00
add = > AddEvent ( value , GetHashCode ( ) ) ;
remove = > RemoveEvent ( value , GetHashCode ( ) ) ;
2019-05-23 02:59:30 +02:00
}
/// <summary>
/// Emitted when checking if an update has started.
/// </summary>
public event Action OnCheckingForUpdate
{
2025-11-12 10:43:32 +00:00
add = > AddEvent ( value , GetHashCode ( ) ) ;
remove = > RemoveEvent ( value , GetHashCode ( ) ) ;
2019-05-23 02:59:30 +02:00
}
/// <summary>
/// Emitted when there is an available update.
/// The update is downloaded automatically if AutoDownload is true.
/// </summary>
public event Action < UpdateInfo > OnUpdateAvailable
{
2025-11-12 10:43:32 +00:00
add = > AddEvent ( value , GetHashCode ( ) ) ;
remove = > RemoveEvent ( value , GetHashCode ( ) ) ;
2019-05-23 02:59:30 +02:00
}
/// <summary>
/// Emitted when there is no available update.
/// </summary>
public event Action < UpdateInfo > OnUpdateNotAvailable
{
2025-11-12 10:43:32 +00:00
add = > AddEvent ( value , GetHashCode ( ) ) ;
remove = > RemoveEvent ( value , GetHashCode ( ) ) ;
2019-05-23 02:59:30 +02:00
}
/// <summary>
/// Emitted on download progress.
/// </summary>
public event Action < ProgressInfo > OnDownloadProgress
{
2025-11-12 10:43:32 +00:00
add = > AddEvent ( value , GetHashCode ( ) ) ;
remove = > RemoveEvent ( value , GetHashCode ( ) ) ;
2019-05-23 02:59:30 +02:00
}
/// <summary>
/// Emitted on download complete.
/// </summary>
public event Action < UpdateInfo > OnUpdateDownloaded
{
2025-11-12 10:43:32 +00:00
add = > AddEvent ( value , GetHashCode ( ) ) ;
remove = > RemoveEvent ( value , GetHashCode ( ) ) ;
2019-05-23 02:59:30 +02:00
}
2019-05-20 01:08:26 +02:00
private static AutoUpdater _autoUpdater ;
private static object _syncRoot = new object ( ) ;
2025-11-09 03:50:24 +01:00
internal AutoUpdater ( )
{
}
2019-05-20 01:08:26 +02:00
internal static AutoUpdater Instance
{
get
{
if ( _autoUpdater = = null )
{
lock ( _syncRoot )
{
if ( _autoUpdater = = null )
{
_autoUpdater = new AutoUpdater ( ) ;
}
}
}
return _autoUpdater ;
}
}
/// <summary>
/// Asks the server whether there is an update.
/// </summary>
/// <returns></returns>
public Task < UpdateCheckResult > CheckForUpdatesAsync ( )
{
var taskCompletionSource = new TaskCompletionSource < UpdateCheckResult > ( ) ;
string guid = Guid . NewGuid ( ) . ToString ( ) ;
2025-11-12 10:43:32 +00:00
BridgeConnector . Socket . Once < UpdateCheckResult > ( "autoUpdater-checkForUpdates-completed" + guid , ( result ) = >
2020-07-08 11:16:00 +02:00
{
try
{
2025-11-12 10:43:32 +00:00
BridgeConnector . Socket . Off ( "autoUpdater-checkForUpdatesError" + guid ) ;
2025-11-09 15:46:33 +01:00
taskCompletionSource . SetResult ( result ) ;
2020-07-08 11:16:00 +02:00
}
catch ( Exception ex )
{
taskCompletionSource . SetException ( ex ) ;
}
} ) ;
2025-11-12 10:43:32 +00:00
BridgeConnector . Socket . Once < string > ( "autoUpdater-checkForUpdatesError" + guid , ( result ) = >
2019-05-20 01:08:26 +02:00
{
2025-11-12 10:43:32 +00:00
BridgeConnector . Socket . Off ( "autoUpdater-checkForUpdates-completed" + guid ) ;
2020-07-08 11:16:00 +02:00
string message = "An error occurred in CheckForUpdatesAsync" ;
2025-11-09 15:46:33 +01:00
if ( ! string . IsNullOrEmpty ( result ) ) message = result ;
2020-08-21 12:13:38 +02:00
taskCompletionSource . SetException ( new Exception ( message ) ) ;
2019-05-20 01:08:26 +02:00
} ) ;
2025-11-12 10:43:32 +00:00
BridgeConnector . Socket . Emit ( "autoUpdater-checkForUpdates" , guid ) ;
2019-05-20 01:08:26 +02:00
return taskCompletionSource . Task ;
}
/// <summary>
/// Asks the server whether there is an update.
///
/// This will immediately download an update, then install when the app quits.
/// </summary>
/// <returns></returns>
public Task < UpdateCheckResult > CheckForUpdatesAndNotifyAsync ( )
{
var taskCompletionSource = new TaskCompletionSource < UpdateCheckResult > ( ) ;
string guid = Guid . NewGuid ( ) . ToString ( ) ;
2025-11-12 10:43:32 +00:00
BridgeConnector . Socket . Once < UpdateCheckResult > ( "autoUpdater-checkForUpdatesAndNotify-completed" + guid , ( result ) = >
2020-07-08 11:16:00 +02:00
{
try
{
2025-11-12 10:43:32 +00:00
BridgeConnector . Socket . Off ( "autoUpdater-checkForUpdatesAndNotifyError" + guid ) ;
2025-11-09 15:46:33 +01:00
taskCompletionSource . SetResult ( result ) ;
2020-07-08 11:16:00 +02:00
}
catch ( Exception ex )
{
taskCompletionSource . SetException ( ex ) ;
}
} ) ;
2025-11-12 10:43:32 +00:00
BridgeConnector . Socket . Once < string > ( "autoUpdater-checkForUpdatesAndNotifyError" + guid , ( result ) = >
2019-05-20 01:08:26 +02:00
{
2025-11-12 10:43:32 +00:00
BridgeConnector . Socket . Off ( "autoUpdater-checkForUpdatesAndNotify-completed" + guid ) ;
string message = "An error occurred in CheckForUpdatesAndNotifyAsync" ;
2025-11-09 15:46:33 +01:00
if ( ! string . IsNullOrEmpty ( result ) ) message = result ;
2020-08-21 12:13:38 +02:00
taskCompletionSource . SetException ( new Exception ( message ) ) ;
2019-05-20 01:08:26 +02:00
} ) ;
2025-11-12 10:43:32 +00:00
BridgeConnector . Socket . Emit ( "autoUpdater-checkForUpdatesAndNotify" , guid ) ;
2019-05-20 01:08:26 +02:00
return taskCompletionSource . Task ;
}
/// <summary>
/// Restarts the app and installs the update after it has been downloaded.
/// It should only be called after `update-downloaded` has been emitted.
///
/// Note: QuitAndInstall() will close all application windows first and only emit `before-quit` event on `app` after that.
/// This is different from the normal quit event sequence.
/// </summary>
/// <param name="isSilent">*windows-only* Runs the installer in silent mode. Defaults to `false`.</param>
/// <param name="isForceRunAfter">Run the app after finish even on silent install. Not applicable for macOS. Ignored if `isSilent` is set to `false`.</param>
public void QuitAndInstall ( bool isSilent = false , bool isForceRunAfter = false )
{
2025-11-12 10:43:32 +00:00
BridgeConnector . Socket . Emit ( "autoUpdater-quitAndInstall" , isSilent , isForceRunAfter ) ;
2019-05-20 01:08:26 +02:00
}
2019-05-23 23:10:45 +02:00
/// <summary>
/// Start downloading update manually. You can use this method if "AutoDownload" option is set to "false".
/// </summary>
/// <returns>Path to downloaded file.</returns>
public Task < string > DownloadUpdateAsync ( )
{
2025-11-10 10:40:51 +01:00
var tcs = new TaskCompletionSource < string > ( ) ;
2019-05-23 23:10:45 +02:00
string guid = Guid . NewGuid ( ) . ToString ( ) ;
2025-11-12 10:43:32 +00:00
BridgeConnector . Socket . Once < string > ( "autoUpdater-downloadUpdate-completed" + guid , tcs . SetResult ) ;
BridgeConnector . Socket . Emit ( "autoUpdater-downloadUpdate" , guid ) ;
2019-05-23 23:10:45 +02:00
2025-11-10 10:40:51 +01:00
return tcs . Task ;
2019-05-23 23:10:45 +02:00
}
/// <summary>
/// Feed URL.
/// </summary>
/// <returns>Feed URL.</returns>
public Task < string > GetFeedURLAsync ( )
{
2025-11-10 10:40:51 +01:00
var tcs = new TaskCompletionSource < string > ( ) ;
2019-05-23 23:10:45 +02:00
string guid = Guid . NewGuid ( ) . ToString ( ) ;
2025-11-12 10:43:32 +00:00
BridgeConnector . Socket . Once < string > ( "autoUpdater-getFeedURL-completed" + guid , tcs . SetResult ) ;
BridgeConnector . Socket . Emit ( "autoUpdater-getFeedURL" , guid ) ;
2019-05-23 23:10:45 +02:00
2025-11-10 10:40:51 +01:00
return tcs . Task ;
2019-05-23 23:10:45 +02:00
}
2020-07-08 11:16:00 +02:00
2025-11-09 12:05:07 +01:00
2019-05-20 01:08:26 +02:00
}
2025-11-09 12:05:07 +01:00
}