11using ElectronNET . API . Entities ;
2+ using Newtonsoft . Json ;
23using Newtonsoft . Json . Linq ;
4+ using Newtonsoft . Json . Serialization ;
35using System ;
6+ using System . Collections . Generic ;
7+ using System . Runtime . CompilerServices ;
8+ using System . Security . Cryptography . X509Certificates ;
9+ using System . Threading ;
410using System . Threading . Tasks ;
511
612namespace ElectronNET . API
@@ -182,11 +188,48 @@ public string UpdateConfigPath
182188 }
183189 }
184190
191+ /// <summary>
192+ /// The current application version
193+ /// </summary>
194+ public Task < SemVer > CurrentVersionAsync
195+ {
196+ get
197+ {
198+ return Task . Run < SemVer > ( ( ) =>
199+ {
200+ var taskCompletionSource = new TaskCompletionSource < SemVer > ( ) ;
201+
202+ BridgeConnector . Socket . On ( "autoUpdater-currentVersion-get-reply" , ( result ) =>
203+ {
204+ BridgeConnector . Socket . Off ( "autoUpdater-currentVersion-get-reply" ) ;
205+ SemVer version = ( ( JObject ) result ) . ToObject < SemVer > ( ) ;
206+ taskCompletionSource . SetResult ( version ) ;
207+ } ) ;
208+ BridgeConnector . Socket . Emit ( "autoUpdater-currentVersion-get" ) ;
209+
210+ return taskCompletionSource . Task ;
211+ } ) ;
212+ }
213+ }
214+
185215 /// <summary>
186216 /// Get the update channel. Not applicable for GitHub.
187217 /// Doesn’t return channel from the update configuration, only if was previously set.
188218 /// </summary>
219+ [ Obsolete ( "Use the asynchronous version ChannelAsync instead" ) ]
189220 public string Channel
221+ {
222+ get
223+ {
224+ return AsyncHelper . RunSync ( async ( ) => await ChannelAsync ) ;
225+ }
226+ }
227+
228+ /// <summary>
229+ /// Get the update channel. Not applicable for GitHub.
230+ /// Doesn’t return channel from the update configuration, only if was previously set.
231+ /// </summary>
232+ public Task < string > ChannelAsync
190233 {
191234 get
192235 {
@@ -199,18 +242,52 @@ public string Channel
199242 BridgeConnector . Socket . Off ( "autoUpdater-channel-get-reply" ) ;
200243 taskCompletionSource . SetResult ( result . ToString ( ) ) ;
201244 } ) ;
202-
203245 BridgeConnector . Socket . Emit ( "autoUpdater-channel-get" ) ;
204246
205247 return taskCompletionSource . Task ;
206- } ) . Result ;
248+ } ) ;
249+ }
250+ }
251+
252+
253+
254+ /// <summary>
255+ /// The request headers.
256+ /// </summary>
257+ public Task < Dictionary < string , string > > RequestHeadersAsync
258+ {
259+ get
260+ {
261+ return Task . Run ( ( ) =>
262+ {
263+ var taskCompletionSource = new TaskCompletionSource < Dictionary < string , string > > ( ) ;
264+ BridgeConnector . Socket . On ( "autoUpdater-requestHeaders-get-reply" , ( headers ) =>
265+ {
266+ BridgeConnector . Socket . Off ( "autoUpdater-requestHeaders-get-reply" ) ;
267+ Dictionary < string , string > result = ( ( JObject ) headers ) . ToObject < Dictionary < string , string > > ( ) ;
268+ taskCompletionSource . SetResult ( result ) ;
269+ } ) ;
270+ BridgeConnector . Socket . Emit ( "autoUpdater-requestHeaders-get" ) ;
271+ return taskCompletionSource . Task ;
272+ } ) ;
207273 }
208274 }
209275
210276 /// <summary>
211- /// Emitted when there is an error while updating .
277+ /// The request headers .
212278 /// </summary>
213- public event Action < string > OnError
279+ public Dictionary < string , string > RequestHeaders
280+ {
281+ set
282+ {
283+ BridgeConnector . Socket . Emit ( "autoUpdater-requestHeaders-set" , JObject . FromObject ( value , _jsonSerializer ) ) ;
284+ }
285+ }
286+
287+ /// <summary>
288+ /// Emitted when there is an error while updating.
289+ /// </summary>
290+ public event Action < string > OnError
214291 {
215292 add
216293 {
@@ -416,9 +493,26 @@ public Task<UpdateCheckResult> CheckForUpdatesAsync()
416493 string guid = Guid . NewGuid ( ) . ToString ( ) ;
417494
418495 BridgeConnector . Socket . On ( "autoUpdaterCheckForUpdatesComplete" + guid , ( updateCheckResult ) =>
496+ {
497+ try
498+ {
499+ BridgeConnector . Socket . Off ( "autoUpdaterCheckForUpdatesComplete" + guid ) ;
500+ BridgeConnector . Socket . Off ( "autoUpdaterCheckForUpdatesError" + guid ) ;
501+ taskCompletionSource . SetResult ( JObject . Parse ( updateCheckResult . ToString ( ) ) . ToObject < UpdateCheckResult > ( ) ) ;
502+ }
503+ catch ( Exception ex )
504+ {
505+ taskCompletionSource . SetException ( ex ) ;
506+ }
507+ } ) ;
508+ BridgeConnector . Socket . On ( "autoUpdaterCheckForUpdatesError" + guid , ( error ) =>
419509 {
420510 BridgeConnector . Socket . Off ( "autoUpdaterCheckForUpdatesComplete" + guid ) ;
421- taskCompletionSource . SetResult ( JObject . Parse ( updateCheckResult . ToString ( ) ) . ToObject < UpdateCheckResult > ( ) ) ;
511+ BridgeConnector . Socket . Off ( "autoUpdaterCheckForUpdatesError" + guid ) ;
512+ string message = "An error occurred in CheckForUpdatesAsync" ;
513+ if ( error != null && ! string . IsNullOrEmpty ( error . ToString ( ) ) )
514+ message = JsonConvert . SerializeObject ( error ) ;
515+ taskCompletionSource . SetException ( new ElectronException ( message ) ) ;
422516 } ) ;
423517
424518 BridgeConnector . Socket . Emit ( "autoUpdaterCheckForUpdates" , guid ) ;
@@ -438,9 +532,29 @@ public Task<UpdateCheckResult> CheckForUpdatesAndNotifyAsync()
438532 string guid = Guid . NewGuid ( ) . ToString ( ) ;
439533
440534 BridgeConnector . Socket . On ( "autoUpdaterCheckForUpdatesAndNotifyComplete" + guid , ( updateCheckResult ) =>
535+ {
536+ try
537+ {
538+ BridgeConnector . Socket . Off ( "autoUpdaterCheckForUpdatesAndNotifyComplete" + guid ) ;
539+ BridgeConnector . Socket . Off ( "autoUpdaterCheckForUpdatesAndNotifyError" + guid ) ;
540+ if ( updateCheckResult == null )
541+ taskCompletionSource . SetResult ( null ) ;
542+ else
543+ taskCompletionSource . SetResult ( JObject . Parse ( updateCheckResult . ToString ( ) ) . ToObject < UpdateCheckResult > ( ) ) ;
544+ }
545+ catch ( Exception ex )
546+ {
547+ taskCompletionSource . SetException ( ex ) ;
548+ }
549+ } ) ;
550+ BridgeConnector . Socket . On ( "autoUpdaterCheckForUpdatesAndNotifyError" + guid , ( error ) =>
441551 {
442552 BridgeConnector . Socket . Off ( "autoUpdaterCheckForUpdatesAndNotifyComplete" + guid ) ;
443- taskCompletionSource . SetResult ( JObject . Parse ( updateCheckResult . ToString ( ) ) . ToObject < UpdateCheckResult > ( ) ) ;
553+ BridgeConnector . Socket . Off ( "autoUpdaterCheckForUpdatesAndNotifyError" + guid ) ;
554+ string message = "An error occurred in autoUpdaterCheckForUpdatesAndNotify" ;
555+ if ( error != null )
556+ message = JsonConvert . SerializeObject ( error ) ;
557+ taskCompletionSource . SetException ( new ElectronException ( message ) ) ;
444558 } ) ;
445559
446560 BridgeConnector . Socket . Emit ( "autoUpdaterCheckForUpdatesAndNotify" , guid ) ;
@@ -501,5 +615,10 @@ public Task<string> GetFeedURLAsync()
501615
502616 return taskCompletionSource . Task ;
503617 }
618+
619+ private readonly JsonSerializer _jsonSerializer = new JsonSerializer ( )
620+ {
621+ ContractResolver = new CamelCasePropertyNamesContractResolver ( )
622+ } ;
504623 }
505624}
0 commit comments