11using ElectronNET . API . Entities ;
2+ using Newtonsoft . Json ;
23using Newtonsoft . Json . Linq ;
4+ using Newtonsoft . Json . Serialization ;
35using System ;
6+ using System . Collections . Generic ;
47using System . Threading . Tasks ;
58
69namespace ElectronNET . API
@@ -182,11 +185,48 @@ public string UpdateConfigPath
182185 }
183186 }
184187
188+ /// <summary>
189+ /// The current application version
190+ /// </summary>
191+ public Task < SemVer > CurrentVersionAsync
192+ {
193+ get
194+ {
195+ return Task . Run < SemVer > ( ( ) =>
196+ {
197+ var taskCompletionSource = new TaskCompletionSource < SemVer > ( ) ;
198+
199+ BridgeConnector . Socket . On ( "autoUpdater-currentVersion-get-reply" , ( result ) =>
200+ {
201+ BridgeConnector . Socket . Off ( "autoUpdater-currentVersion-get-reply" ) ;
202+ SemVer version = ( ( JObject ) result ) . ToObject < SemVer > ( ) ;
203+ taskCompletionSource . SetResult ( version ) ;
204+ } ) ;
205+ BridgeConnector . Socket . Emit ( "autoUpdater-currentVersion-get" ) ;
206+
207+ return taskCompletionSource . Task ;
208+ } ) ;
209+ }
210+ }
211+
185212 /// <summary>
186213 /// Get the update channel. Not applicable for GitHub.
187214 /// Doesn’t return channel from the update configuration, only if was previously set.
188215 /// </summary>
216+ [ Obsolete ( "Use the asynchronous version ChannelAsync instead" ) ]
189217 public string Channel
218+ {
219+ get
220+ {
221+ return ChannelAsync . Result ;
222+ }
223+ }
224+
225+ /// <summary>
226+ /// Get the update channel. Not applicable for GitHub.
227+ /// Doesn’t return channel from the update configuration, only if was previously set.
228+ /// </summary>
229+ public Task < string > ChannelAsync
190230 {
191231 get
192232 {
@@ -199,11 +239,45 @@ public string Channel
199239 BridgeConnector . Socket . Off ( "autoUpdater-channel-get-reply" ) ;
200240 taskCompletionSource . SetResult ( result . ToString ( ) ) ;
201241 } ) ;
202-
203242 BridgeConnector . Socket . Emit ( "autoUpdater-channel-get" ) ;
204243
205244 return taskCompletionSource . Task ;
206- } ) . Result ;
245+ } ) ;
246+ }
247+ }
248+
249+
250+
251+ /// <summary>
252+ /// The request headers.
253+ /// </summary>
254+ public Task < Dictionary < string , string > > RequestHeadersAsync
255+ {
256+ get
257+ {
258+ return Task . Run ( ( ) =>
259+ {
260+ var taskCompletionSource = new TaskCompletionSource < Dictionary < string , string > > ( ) ;
261+ BridgeConnector . Socket . On ( "autoUpdater-requestHeaders-get-reply" , ( headers ) =>
262+ {
263+ BridgeConnector . Socket . Off ( "autoUpdater-requestHeaders-get-reply" ) ;
264+ Dictionary < string , string > result = ( ( JObject ) headers ) . ToObject < Dictionary < string , string > > ( ) ;
265+ taskCompletionSource . SetResult ( result ) ;
266+ } ) ;
267+ BridgeConnector . Socket . Emit ( "autoUpdater-requestHeaders-get" ) ;
268+ return taskCompletionSource . Task ;
269+ } ) ;
270+ }
271+ }
272+
273+ /// <summary>
274+ /// The request headers.
275+ /// </summary>
276+ public Dictionary < string , string > RequestHeaders
277+ {
278+ set
279+ {
280+ BridgeConnector . Socket . Emit ( "autoUpdater-requestHeaders-set" , JObject . FromObject ( value , _jsonSerializer ) ) ;
207281 }
208282 }
209283
@@ -416,9 +490,26 @@ public Task<UpdateCheckResult> CheckForUpdatesAsync()
416490 string guid = Guid . NewGuid ( ) . ToString ( ) ;
417491
418492 BridgeConnector . Socket . On ( "autoUpdaterCheckForUpdatesComplete" + guid , ( updateCheckResult ) =>
493+ {
494+ try
495+ {
496+ BridgeConnector . Socket . Off ( "autoUpdaterCheckForUpdatesComplete" + guid ) ;
497+ BridgeConnector . Socket . Off ( "autoUpdaterCheckForUpdatesError" + guid ) ;
498+ taskCompletionSource . SetResult ( JObject . Parse ( updateCheckResult . ToString ( ) ) . ToObject < UpdateCheckResult > ( ) ) ;
499+ }
500+ catch ( Exception ex )
501+ {
502+ taskCompletionSource . SetException ( ex ) ;
503+ }
504+ } ) ;
505+ BridgeConnector . Socket . On ( "autoUpdaterCheckForUpdatesError" + guid , ( error ) =>
419506 {
420507 BridgeConnector . Socket . Off ( "autoUpdaterCheckForUpdatesComplete" + guid ) ;
421- taskCompletionSource . SetResult ( JObject . Parse ( updateCheckResult . ToString ( ) ) . ToObject < UpdateCheckResult > ( ) ) ;
508+ BridgeConnector . Socket . Off ( "autoUpdaterCheckForUpdatesError" + guid ) ;
509+ string message = "An error occurred in CheckForUpdatesAsync" ;
510+ if ( error != null && ! string . IsNullOrEmpty ( error . ToString ( ) ) )
511+ message = JsonConvert . SerializeObject ( error ) ;
512+ taskCompletionSource . SetException ( new Exception ( message ) ) ;
422513 } ) ;
423514
424515 BridgeConnector . Socket . Emit ( "autoUpdaterCheckForUpdates" , guid ) ;
@@ -438,9 +529,29 @@ public Task<UpdateCheckResult> CheckForUpdatesAndNotifyAsync()
438529 string guid = Guid . NewGuid ( ) . ToString ( ) ;
439530
440531 BridgeConnector . Socket . On ( "autoUpdaterCheckForUpdatesAndNotifyComplete" + guid , ( updateCheckResult ) =>
532+ {
533+ try
534+ {
535+ BridgeConnector . Socket . Off ( "autoUpdaterCheckForUpdatesAndNotifyComplete" + guid ) ;
536+ BridgeConnector . Socket . Off ( "autoUpdaterCheckForUpdatesAndNotifyError" + guid ) ;
537+ if ( updateCheckResult == null )
538+ taskCompletionSource . SetResult ( null ) ;
539+ else
540+ taskCompletionSource . SetResult ( JObject . Parse ( updateCheckResult . ToString ( ) ) . ToObject < UpdateCheckResult > ( ) ) ;
541+ }
542+ catch ( Exception ex )
543+ {
544+ taskCompletionSource . SetException ( ex ) ;
545+ }
546+ } ) ;
547+ BridgeConnector . Socket . On ( "autoUpdaterCheckForUpdatesAndNotifyError" + guid , ( error ) =>
441548 {
442549 BridgeConnector . Socket . Off ( "autoUpdaterCheckForUpdatesAndNotifyComplete" + guid ) ;
443- taskCompletionSource . SetResult ( JObject . Parse ( updateCheckResult . ToString ( ) ) . ToObject < UpdateCheckResult > ( ) ) ;
550+ BridgeConnector . Socket . Off ( "autoUpdaterCheckForUpdatesAndNotifyError" + guid ) ;
551+ string message = "An error occurred in autoUpdaterCheckForUpdatesAndNotify" ;
552+ if ( error != null )
553+ message = JsonConvert . SerializeObject ( error ) ;
554+ taskCompletionSource . SetException ( new Exception ( message ) ) ;
444555 } ) ;
445556
446557 BridgeConnector . Socket . Emit ( "autoUpdaterCheckForUpdatesAndNotify" , guid ) ;
@@ -501,5 +612,10 @@ public Task<string> GetFeedURLAsync()
501612
502613 return taskCompletionSource . Task ;
503614 }
615+
616+ private readonly JsonSerializer _jsonSerializer = new JsonSerializer ( )
617+ {
618+ ContractResolver = new CamelCasePropertyNamesContractResolver ( )
619+ } ;
504620 }
505621}
0 commit comments