-
-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathKeyman.System.UpdateStateMachine.pas
More file actions
1254 lines (1092 loc) · 31.7 KB
/
Copy pathKeyman.System.UpdateStateMachine.pas
File metadata and controls
1254 lines (1092 loc) · 31.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*
* Keyman is copyright (C) SIL Global. MIT License.
*
* Notes: For the state diagram in mermaid ../BackgroundUpdateStateDiagram.md
*)
unit Keyman.System.UpdateStateMachine;
interface
uses
System.SysUtils,
System.Types,
System.TypInfo,
Sentry.Client,
KeymanPaths,
Keyman.Configuration.Util.NetworkConnection,
Keyman.System.ExecutionHistory,
Keyman.System.UpdateCheckResponse,
utilkmshell;
type
EUpdateStateMachine = class(Exception);
TUpdateState = (usIdle, usUpdateAvailable, usDownloading, usWaitingRestart,
usInstalling);
// Forward declaration
TUpdateStateMachine = class;
{ State Classes Update }
TStateClass = class of TState;
TState = class abstract
private
bucStateContext: TUpdateStateMachine;
procedure ChangeState(newState: TStateClass);
public
constructor Create(Context: TUpdateStateMachine);
procedure EnterState; virtual; abstract;
procedure ExitState; virtual; abstract;
procedure HandleCheck; virtual; abstract;
function HandleKmShell: Integer; virtual; abstract;
procedure HandleDownload; virtual; abstract;
procedure HandleAbort; virtual; abstract;
procedure HandleInstallNow; virtual; abstract;
procedure HandleInstallPackages; virtual;
procedure HandleFirstRun; virtual;
end;
{ This class also controls the state flow see
../BackgroundUpdateStateDiagram.md }
TUpdateStateMachine = class
private
FForce: Boolean;
FAutomaticUpdate: Boolean;
CurrentState: TState;
// State object for performance (could lazy create?)
FStateInstance: array [TUpdateState] of TState;
function GetState: TStateClass;
procedure SetState(const Value: TStateClass);
procedure SetStateOnly(const enumState: TUpdateState);
function ConvertStateToEnum(const StateClass: TStateClass): TUpdateState;
function IsCurrentStateAssigned: Boolean;
procedure RemoveCachedFiles;
function SetRegistryState(Update: TUpdateState): Boolean;
function SetApplyNow(Value: Boolean): Boolean;
function GetApplyNow: Boolean;
protected
property State: TStateClass read GetState write SetState;
public
constructor Create(AForce: Boolean);
destructor Destroy; override;
procedure HandleCheck;
function HandleKmShell: Integer;
procedure HandleDownload;
procedure HandleAbort;
procedure HandleInstallNow;
procedure HandleInstallPackages;
procedure HandleFirstRun;
function CurrentStateName: string;
(**
* Checks if Keyman is the WaitingRestartState, the cache is valid
* and that Keyman has not run in this Windows session. It also confirms
* that automatic updates are still enabled #14295.
* The purpose is for the calling code can choose to produce
* a UI to confirm the user wants to install an update,
* if everything is ready to start a update.
*
* NOTE: If it finds the cache is invalid it will abort causing
* the State Machine to change state, returning to idle.
*
* @returns True if the Keyman is ready to install.
*)
function ValidateReadyToInstall: Boolean;
function IsInstallingState: Boolean;
function GetAutomaticUpdates: Boolean;
function CheckRegistryState: TUpdateState;
end;
implementation
uses
System.Win.Registry,
Winapi.Windows,
Winapi.WinINet,
ErrorControlledRegistry,
GlobalProxySettings,
kmint,
keymanapi_TLB,
KeymanMutex,
Keyman.System.KeymanSentryClient,
Keyman.System.DownloadUpdate,
Keyman.System.RemoteUpdateCheck,
Keyman.System.UpdateCheckStorage,
KLog,
RegistryKeys,
utilexecute,
UtilCheckOnline,
utiluac;
const
SPackageUpgradeFilename = 'upgrade_packages.inf';
kmShellContinue = 0;
kmShellExit = 1;
KeymanDownloadMutexName = 'KeymanDownloading';
{ State Class Memebers }
constructor TState.Create(Context: TUpdateStateMachine);
begin
inherited Create;
bucStateContext := Context;
end;
procedure TState.ChangeState(newState: TStateClass);
begin
bucStateContext.State := newState;
end;
type
// Derived classes for each state
IdleState = class(TState)
public
procedure EnterState; override;
procedure ExitState; override;
procedure HandleCheck; override;
function HandleKmShell: Integer; override;
procedure HandleDownload; override;
procedure HandleAbort; override;
procedure HandleInstallNow; override;
procedure HandleFirstRun; override;
end;
UpdateAvailableState = class(TState)
private
procedure StartDownloadProcess;
public
procedure EnterState; override;
procedure ExitState; override;
procedure HandleCheck; override;
function HandleKmShell: Integer; override;
procedure HandleDownload; override;
procedure HandleAbort; override;
procedure HandleInstallNow; override;
end;
DownloadingState = class(TState)
private
function DownloadUpdatesBackground: Boolean;
procedure EnterState; override;
procedure ExitState; override;
procedure HandleCheck; override;
function HandleKmShell: Integer; override;
procedure HandleDownload; override;
procedure HandleAbort; override;
procedure HandleInstallNow; override;
end;
WaitingRestartState = class(TState)
public
procedure EnterState; override;
procedure ExitState; override;
procedure HandleCheck; override;
function HandleKmShell: Integer; override;
procedure HandleDownload; override;
procedure HandleAbort; override;
procedure HandleInstallNow; override;
end;
InstallingState = class(TState)
private
(**
* Installs the Keyman setup file using separate shell.
*
* @params SavePath The path to the downloaded files.
*
* @returns True if the installation is successful, False otherwise.
*)
function DoInstallKeyman: Boolean; overload;
(**
* Installs the Keyman Keyboard files using separate shell.
*
* @params SavePath The path to the downloaded files.
*
* @returns True if the installation is successful, False otherwise.
*)
function DoInstallPackages(Params: TUpdateCheckResponse): Boolean;
function DoInstallPackage(PackageFileName: String): Boolean;
procedure LaunchInstallPackageProcess;
public
procedure EnterState; override;
procedure ExitState; override;
procedure HandleCheck; override;
function HandleKmShell: Integer; override;
procedure HandleDownload; override;
procedure HandleAbort; override;
procedure HandleInstallNow; override;
procedure HandleInstallPackages; override;
procedure HandleFirstRun; override;
end;
{ TUpdateStateMachine }
constructor TUpdateStateMachine.Create(AForce: Boolean);
begin
inherited Create;
FForce := AForce;
FAutomaticUpdate := GetAutomaticUpdates;
FStateInstance[usIdle] := IdleState.Create(Self);
FStateInstance[usUpdateAvailable] := UpdateAvailableState.Create(Self);
FStateInstance[usDownloading] := DownloadingState.Create(Self);
FStateInstance[usWaitingRestart] := WaitingRestartState.Create(Self);
FStateInstance[usInstalling] := InstallingState.Create(Self);
// Check the Registry setting.
SetStateOnly(CheckRegistryState);
end;
destructor TUpdateStateMachine.Destroy;
var
lpState: TUpdateState;
begin
for lpState := Low(TUpdateState) to High(TUpdateState) do
begin
FreeAndNil(FStateInstance[lpState]);
end;
inherited Destroy;
end;
function TUpdateStateMachine.SetRegistryState(Update: TUpdateState): Boolean;
var
UpdateStr: string;
Registry: TRegistryErrorControlled;
begin
Result := False;
Registry := TRegistryErrorControlled.Create;
try
Registry.RootKey := HKEY_CURRENT_USER;
if not Registry.OpenKey(SRegKey_KeymanEngine_CU, True) then
begin
TKeymanSentryClient.Client.MessageEvent(Sentry.Client.SENTRY_LEVEL_ERROR,
'Failed to open registry key: "' + SRegKey_KeymanEngine_CU + '"');
Exit;
end;
try
UpdateStr := GetEnumName(TypeInfo(TUpdateState), Ord(Update));
Registry.WriteString(SRegValue_Update_State, UpdateStr);
Result := True;
except
on E: ERegistryException do
begin
TKeymanSentryClient.ReportHandledException(E,
'Failed to write install state machine state');
end;
end;
finally
Registry.Free;
end;
end;
function TUpdateStateMachine.CheckRegistryState: TUpdateState;
var
UpdateState: TUpdateState;
Registry: TRegistryErrorControlled;
StateValue: string;
EnumValue: Integer;
begin
// Default to Idle state if any issues occur
UpdateState := usIdle;
Registry := TRegistryErrorControlled.Create;
try
Registry.RootKey := HKEY_CURRENT_USER;
if Registry.OpenKeyReadOnly(SRegKey_KeymanEngine_CU) and
Registry.ValueExists(SRegValue_Update_State) then
begin
try
StateValue := Registry.ReadString(SRegValue_Update_State);
EnumValue := GetEnumValue(TypeInfo(TUpdateState), StateValue);
// Bounds Check EnumValue against TUpdateState
if (EnumValue >= Ord(Low(TUpdateState))) and
(EnumValue <= Ord(High(TUpdateState))) then
UpdateState := TUpdateState(EnumValue)
else
UpdateState := usIdle; // Default if out of bounds
except
on E: ERegistryException do
begin
TKeymanSentryClient.ReportHandledException(E,
'Failed to read install state machine state');
UpdateState := usIdle;
end;
end;
end;
finally
Registry.Free;
end;
Result := UpdateState;
end;
function TUpdateStateMachine.GetAutomaticUpdates: Boolean; // I2329
var
Registry: TRegistryErrorControlled;
begin
// check the registry value
Registry := TRegistryErrorControlled.Create; // I2890
try
Registry.RootKey := HKEY_CURRENT_USER;
try
Result := not Registry.OpenKeyReadOnly(SRegKey_KeymanEngine_CU) or
not Registry.ValueExists(SRegValue_CheckForUpdates) or
Registry.ReadBool(SRegValue_CheckForUpdates);
except
on E: ERegistryException do
begin
TKeymanSentryClient.ReportHandledException(E,
'Failed to read automatic updates');
Result := False;
end;
end;
finally
Registry.Free;
end;
end;
function TUpdateStateMachine.SetApplyNow(Value: Boolean): Boolean;
var
Registry: TRegistryErrorControlled;
begin
Result := False;
Registry := TRegistryErrorControlled.Create;
try
Registry.RootKey := HKEY_CURRENT_USER;
if not Registry.OpenKey(SRegKey_KeymanEngine_CU, True) then
begin
Exit;
end;
try
Registry.WriteBool(SRegValue_ApplyNow, Value);
Result := True;
except
on E: ERegistryException do
begin
TKeymanSentryClient.ReportHandledException(E,
'Failed to write "apply now"');
end;
end;
finally
Registry.Free;
end;
end;
function TUpdateStateMachine.GetApplyNow: Boolean;
var
Registry: TRegistryErrorControlled;
begin
// check the registry value
Registry := TRegistryErrorControlled.Create;
try
Registry.RootKey := HKEY_CURRENT_USER;
try
Result := Registry.OpenKeyReadOnly(SRegKey_KeymanEngine_CU) and
Registry.ValueExists(SRegValue_ApplyNow) and
Registry.ReadBool(SRegValue_ApplyNow);
except
on E: ERegistryException do
begin
TKeymanSentryClient.Client.MessageEvent(Sentry.Client.SENTRY_LEVEL_ERROR,
'Failed to read registry: ' + E.Message);
Result := False;
end;
end;
finally
Registry.Free;
end;
end;
function TUpdateStateMachine.GetState: TStateClass;
begin
if Assigned(CurrentState) then
Result := TStateClass(CurrentState.ClassType)
else
begin
TKeymanSentryClient.Client.MessageEvent(Sentry.Client.SENTRY_LEVEL_ERROR,
'Error CurrentState was uninitiallised');
Result := nil;
end;
end;
procedure TUpdateStateMachine.SetState(const Value: TStateClass);
begin
if Assigned(CurrentState) then
begin
CurrentState.ExitState;
end;
SetStateOnly(ConvertStateToEnum(Value));
if Assigned(CurrentState) then
begin
CurrentState.EnterState;
end
else
begin
TKeymanSentryClient.Client.MessageEvent(Sentry.Client.SENTRY_LEVEL_ERROR,
'Set CurrentState was failed');
end;
end;
procedure TUpdateStateMachine.SetStateOnly(const enumState: TUpdateState);
begin
CurrentState := FStateInstance[enumState];
end;
function TUpdateStateMachine.ConvertStateToEnum(const StateClass: TStateClass)
: TUpdateState;
begin
if StateClass = IdleState then
Result := usIdle
else if StateClass = UpdateAvailableState then
Result := usUpdateAvailable
else if StateClass = DownloadingState then
Result := usDownloading
else if StateClass = WaitingRestartState then
Result := usWaitingRestart
else if StateClass = InstallingState then
Result := usInstalling
else
begin
Result := usIdle;
TKeymanSentryClient.Client.MessageEvent(Sentry.Client.SENTRY_LEVEL_ERROR,
'Unknown State Machine class');
end;
end;
function TUpdateStateMachine.IsCurrentStateAssigned: Boolean;
begin
if Assigned(CurrentState) then
Result := True
else
begin
TKeymanSentryClient.Client.MessageEvent(Sentry.Client.SENTRY_LEVEL_ERROR,
'Error CurrentState was uninitiallised');
Result := False;
end;
end;
procedure TUpdateStateMachine.RemoveCachedFiles;
var
SavePath: string;
FileName: String;
FileNames: TStringDynArray;
begin
SavePath := IncludeTrailingPathDelimiter(TKeymanPaths.KeymanUpdateCachePath);
GetFileNamesInDirectory(SavePath, FileNames);
for FileName in FileNames do
begin
System.SysUtils.DeleteFile(FileName);
end;
end;
procedure TUpdateStateMachine.HandleCheck;
begin
if not IsCurrentStateAssigned then
Exit;
CurrentState.HandleCheck;
end;
function TUpdateStateMachine.HandleKmShell: Integer;
begin
if not IsCurrentStateAssigned then
Exit(kmShellContinue);
Result := CurrentState.HandleKmShell;
end;
procedure TUpdateStateMachine.HandleDownload;
begin
if not IsCurrentStateAssigned then
Exit;
CurrentState.HandleDownload;
end;
procedure TUpdateStateMachine.HandleAbort;
begin
if not IsCurrentStateAssigned then
Exit;
CurrentState.HandleAbort;
end;
procedure TUpdateStateMachine.HandleInstallNow;
begin
if not IsCurrentStateAssigned then
Exit;
CurrentState.HandleInstallNow;
end;
procedure TUpdateStateMachine.HandleInstallPackages;
begin
CurrentState.HandleInstallPackages;
end;
procedure TUpdateStateMachine.HandleFirstRun;
begin
CurrentState.HandleFirstRun;
end;
function TUpdateStateMachine.CurrentStateName: string;
begin
if not IsCurrentStateAssigned then
Exit('Undefined');
Result := CurrentState.ClassName;
end;
function TUpdateStateMachine.ValidateReadyToInstall: Boolean;
begin
if not IsCurrentStateAssigned then
Exit(False);
if not (CurrentState is WaitingRestartState) then
Exit(False);
// Verify conditions since entering WaitingRestartState:
// If the cache is not valid or automatic updates have
// been disabled clear the cache and return to the idle state.
if not TUpdateCheckStorage.CheckMetaDataForUpdate or not Self.GetAutomaticUpdates then
begin
HandleAbort;
Exit(False);
end;
if not HasKeymanRun then
Exit(True);
Result := False;
end;
function TUpdateStateMachine.IsInstallingState: Boolean;
begin
Result := (CurrentState is InstallingState);
end;
// base implmentation to be overiden
procedure TState.HandleInstallPackages;
begin
// Do Nothing
end;
procedure TState.HandleFirstRun;
begin
// A Keyman install file can be downloaded and installed directly therefore
// the state machine could be in a "unexpected" state such as UpdateAvailable.
// It will still be good to record the breadcrumb of the state incase there is a error
// during the first run.
TKeymanSentryClient.Breadcrumb('info',
'TState.HandleFirstRun first run called in state:"' + Self.ClassName + '"', 'update');
bucStateContext.RemoveCachedFiles;
ChangeState(IdleState);
end;
{ IdleState }
procedure IdleState.EnterState;
begin
// Enter UpdateAvailableState
bucStateContext.SetRegistryState(usIdle);
end;
procedure IdleState.ExitState;
begin
end;
procedure IdleState.HandleCheck;
var
CheckForUpdates: TRemoteUpdateCheck;
Result: TRemoteUpdateCheckResult;
begin
CheckForUpdates := TRemoteUpdateCheck.Create(True);
try
Result := CheckForUpdates.Run;
finally
CheckForUpdates.Free;
end;
if (Result = wucUpdateAvailable) then
begin
ChangeState(UpdateAvailableState);
end;
// else stay in idle state
end;
function IdleState.HandleKmShell;
var
CheckForUpdates: TRemoteUpdateCheck;
UpdateCheckResult: TRemoteUpdateCheckResult;
begin
// ensure the cache is empty
bucStateContext.RemoveCachedFiles;
// Remote manages the last check time therefore
// we will allow it to return early if it hasn't reached
// the configured time between checks.
CheckForUpdates := TRemoteUpdateCheck.Create(False);
try
UpdateCheckResult := CheckForUpdates.Run;
finally
CheckForUpdates.Free;
end;
{ Response OK and Update is available }
if UpdateCheckResult = wucUpdateAvailable then
begin
ChangeState(UpdateAvailableState);
end;
Result := kmShellContinue;
end;
procedure IdleState.HandleDownload;
begin
// Do Nothing
end;
procedure IdleState.HandleAbort;
begin
// Do Nothing
end;
procedure IdleState.HandleInstallNow;
begin
// Do Nothing
end;
procedure IdleState.HandleFirstRun;
begin
// This would be the case if it was a clean first time install of Keyman
// and not an update. However, incase it was some sort of failed update
// clean any cached files.
bucStateContext.RemoveCachedFiles;
end;
{ UpdateAvailableState }
procedure UpdateAvailableState.StartDownloadProcess;
var
FResult: Boolean;
RootPath: string;
begin
// check if online
if not IsOnline then
begin
TKeymanSentryClient.Breadcrumb('default', 'Not Online, cant execute download process.', 'update');
Exit;
end;
// call separate process
RootPath := ExtractFilePath(ParamStr(0));
FResult := TUtilExecute.ShellCurrentUser(0, ParamStr(0),
IncludeTrailingPathDelimiter(RootPath), '-bd');
if not FResult then
begin
TKeymanSentryClient.Client.MessageEvent(Sentry.Client.SENTRY_LEVEL_ERROR,
'Executing kmshell process to download updated Failed');
ChangeState(IdleState);
end;
end;
procedure UpdateAvailableState.EnterState;
begin
// Enter UpdateAvailableState
bucStateContext.SetRegistryState(usUpdateAvailable);
if bucStateContext.FAutomaticUpdate and
not TNetworkConnection.IsBackgroundUpdateBlocked then
begin
StartDownloadProcess;
end;
end;
procedure UpdateAvailableState.ExitState;
begin
// Exit UpdateAvailableState
end;
procedure UpdateAvailableState.HandleCheck;
var
CheckForUpdates: TRemoteUpdateCheck;
Result: TRemoteUpdateCheckResult;
begin
// Check if new updates while in this state
CheckForUpdates := TRemoteUpdateCheck.Create(True);
try
Result := CheckForUpdates.Run;
finally
CheckForUpdates.Free;
end;
if Result = wucFailure then
begin
KL.Log('UpdateAvailableState.HandleCheck CheckForUpdates not successful: '+
GetEnumName(TypeInfo(TUpdateState), Ord(Result)));
end;
end;
function UpdateAvailableState.HandleKmShell;
begin
if bucStateContext.FAutomaticUpdate and
not TNetworkConnection.IsBackgroundUpdateBlocked then
begin
// we will use a new kmshell process to enable
// the download as background process.
StartDownloadProcess;
end;
Result := kmShellContinue;
end;
procedure UpdateAvailableState.HandleDownload;
begin
ChangeState(DownloadingState);
end;
procedure UpdateAvailableState.HandleAbort;
begin
bucStateContext.RemoveCachedFiles;
ChangeState(IdleState);
end;
procedure UpdateAvailableState.HandleInstallNow;
begin
// This is deliberate action therefore no
// need to check if background update is allowed.
bucStateContext.SetApplyNow(True);
// A new kmshell process will be used to download
StartDownloadProcess;
end;
{ DownloadingState }
procedure DownloadingState.EnterState;
var
DownloadResult: Boolean;
FMutex: TKeymanMutex;
begin
// Enter DownloadingState
bucStateContext.SetRegistryState(usDownloading);
FMutex := TKeymanMutex.Create(KeymanDownloadMutexName);
try
// Should be impossible but just exit anyway and let the process current
// downloading process finish.
if not FMutex.TakeOwnership then
begin
TKeymanSentryClient.Breadcrumb('default', 'DownloadingState.EnterState: Unable to get Mutex download process exists', 'update');
Exit;
end;
// verify there is an update available
if not TUpdateCheckStorage.CheckMetaDataForUpdate then
begin
// Return to Idle state
bucStateContext.RemoveCachedFiles;
ChangeState(IdleState);
Exit;
end;
DownloadResult := DownloadUpdatesBackground;
FMutex.ReleaseOwnership;
finally
FreeAndNil(FMutex);
end;
if (not DownloadResult) then
begin
// Failed download; return to the
// IdleState to wait 'CheckPeriod' before trying again
TKeymanSentryClient.Client.MessageEvent(Sentry.Client.SENTRY_LEVEL_ERROR,
'Error Updates not downloaded');
bucStateContext.RemoveCachedFiles;
ChangeState(IdleState);
end
else
begin
// TODO: #8993 for stage 2 we can got to the InstallingState
// or even have a call back to notify the user to installation
// is about to begin.
if bucStateContext.GetApplyNow then
begin
bucStateContext.SetApplyNow(False);
ChangeState(InstallingState);
end
else
begin
ChangeState(WaitingRestartState);
end;
end
end;
procedure DownloadingState.ExitState;
begin
// Exit DownloadingState
end;
procedure DownloadingState.HandleCheck;
begin
end;
function DownloadingState.HandleKmShell;
var
FMutex: TKeymanMutex;
begin
// Whether already downloading in another process or download has failed and
// this function has clean up and force a restart, kmshell should continue processing
Result := kmShellContinue;
// Check to ensure a download process is running if not
// clean up return to the the idle state and check for updates
FMutex := TKeymanMutex.Create(KeymanDownloadMutexName);
try
if FMutex.TakeOwnership then
begin
bucStateContext.RemoveCachedFiles;
FMutex.ReleaseOwnership; // Mutex must be freed before changing state
ChangeState(IdleState);
bucStateContext.CurrentState.HandleCheck;
end;
finally
FreeAndNil(FMutex);
end;
end;
procedure DownloadingState.HandleDownload;
var
FMutex: TKeymanMutex;
begin
// If downloading process is not running clean files and return to idle
FMutex := TKeymanMutex.Create(KeymanDownloadMutexName);
try
if FMutex.TakeOwnership then
begin
bucStateContext.RemoveCachedFiles;
FMutex.ReleaseOwnership; // Mutex must be freed before changing state
ChangeState(IdleState);
bucStateContext.CurrentState.HandleCheck;
end;
finally
FreeAndNil(FMutex);
end;
end;
procedure DownloadingState.HandleAbort;
begin
// don't abort during the downloading which is executing in a separate process
end;
procedure DownloadingState.HandleInstallNow;
begin
// Already downloading set the registry apply now
bucStateContext.SetApplyNow(True);
end;
function DownloadingState.DownloadUpdatesBackground: Boolean;
var
DownloadResult: Boolean;
DownloadUpdate: TDownloadUpdate;
begin
DownloadUpdate := TDownloadUpdate.Create;
try
DownloadResult := DownloadUpdate.DownloadUpdates;
Result := DownloadResult;
finally
DownloadUpdate.Free;
end;
end;
{ WaitingRestartState }
procedure WaitingRestartState.EnterState;
begin
// Entering this state means that it was not an 'apply now' update
// It should be then a automatic update. However, the automatic update
// may have been set to disable after the idle state. Is so abort
if not bucStateContext.FAutomaticUpdate then
begin
bucStateContext.RemoveCachedFiles;
ChangeState(IdleState);
Exit;
end;
// Enter WaitingRestartState
bucStateContext.SetRegistryState(usWaitingRestart);
end;
procedure WaitingRestartState.ExitState;
begin
// Exit DownloadingState
end;
procedure WaitingRestartState.HandleCheck;
var
CheckForUpdates: TRemoteUpdateCheck;
Result: TRemoteUpdateCheckResult;
begin
// Check if new updates while in this state
CheckForUpdates := TRemoteUpdateCheck.Create(True);
try
Result := CheckForUpdates.Run;
finally
CheckForUpdates.Free;
end;
{ Response OK and go back to update available so files can be downloaded }
// TODO: This actually needs to check if the updates available are newer than the already downloaded updates
if Result = wucUpdateAvailable then
begin
ChangeState(UpdateAvailableState);
end;
end;
function WaitingRestartState.HandleKmShell;
begin
// Still can't go if keyman has run
if HasKeymanRun then
begin
Result := kmShellContinue;
end
else
begin
if not (TUpdateCheckStorage.CheckMetaDataForUpdate) or
not bucStateContext.FAutomaticUpdate then // set by user after idle state
begin
// Return to Idle state and check for Updates state
ChangeState(IdleState);
bucStateContext.CurrentState.HandleCheck;
Result := kmShellContinue;
end
else
begin
ChangeState(InstallingState);
Result := kmShellExit;
end;
end;
end;
procedure WaitingRestartState.HandleDownload;
begin
end;
procedure WaitingRestartState.HandleAbort;