forked from files-community/Files
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppUpdateStoreService.cs
242 lines (198 loc) · 6.19 KB
/
AppUpdateStoreService.cs
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
// Copyright (c) Files Community
// Licensed under the MIT License.
using CommunityToolkit.WinUI.Helpers;
using Microsoft.Extensions.Logging;
using Microsoft.UI.Xaml.Controls;
using System.IO;
using System.Net.Http;
using Windows.Foundation.Metadata;
using Windows.Services.Store;
using Windows.Storage;
using WinRT.Interop;
namespace Files.App.Services
{
internal sealed class StoreUpdateService : ObservableObject, IUpdateService
{
private StoreContext? _storeContext;
private List<StorePackageUpdate>? _updatePackages;
private bool IsMandatory => _updatePackages?.Where(e => e.Mandatory).ToList().Count >= 1;
private bool _isUpdateAvailable;
public bool IsUpdateAvailable
{
get => _isUpdateAvailable;
set => SetProperty(ref _isUpdateAvailable, value);
}
private bool _isUpdating;
public bool IsUpdating
{
get => _isUpdating;
private set => SetProperty(ref _isUpdating, value);
}
public bool IsAppUpdated
{
get => SystemInformation.Instance.IsAppUpdated;
}
private bool _areReleaseNotesAvailable = false;
public bool AreReleaseNotesAvailable
{
get => _areReleaseNotesAvailable;
private set => SetProperty(ref _areReleaseNotesAvailable, value);
}
public StoreUpdateService()
{
_updatePackages = [];
}
public async Task DownloadUpdatesAsync()
{
OnUpdateInProgress();
if (!HasUpdates())
{
return;
}
// double check for Mandatory
if (IsMandatory)
{
// Show dialog
var dialog = await ShowDialogAsync();
if (!dialog)
{
// User rejected mandatory update.
OnUpdateCancelled();
return;
}
}
await DownloadAndInstallAsync();
OnUpdateCompleted();
}
public async Task DownloadMandatoryUpdatesAsync()
{
// Prompt the user to download if the package list
// contains mandatory updates.
if (IsMandatory && HasUpdates())
{
if (await ShowDialogAsync())
{
App.Logger.LogInformation("STORE: Downloading updates...");
OnUpdateInProgress();
await DownloadAndInstallAsync();
OnUpdateCompleted();
}
}
}
public async Task CheckForUpdatesAsync()
{
IsUpdateAvailable = false;
App.Logger.LogInformation("STORE: Checking for updates...");
await GetUpdatePackagesAsync();
if (_updatePackages is not null && _updatePackages.Count > 0)
{
App.Logger.LogInformation("STORE: Update found.");
IsUpdateAvailable = true;
}
}
public async Task CheckForReleaseNotesAsync()
{
using var client = new HttpClient();
try
{
var response = await client.GetAsync(Constants.ExternalUrl.ReleaseNotesUrl);
AreReleaseNotesAvailable = response.IsSuccessStatusCode;
}
catch
{
AreReleaseNotesAvailable = false;
}
}
private async Task DownloadAndInstallAsync()
{
// Save the updated tab list before installing the update
AppLifecycleHelper.SaveSessionTabs();
App.AppModel.ForceProcessTermination = true;
var downloadOperation = _storeContext?.RequestDownloadAndInstallStorePackageUpdatesAsync(_updatePackages);
var result = await downloadOperation.AsTask();
if (result.OverallState == StorePackageUpdateState.Canceled)
App.AppModel.ForceProcessTermination = false;
}
private async Task GetUpdatePackagesAsync()
{
try
{
_storeContext ??= await Task.Run(StoreContext.GetDefault);
InitializeWithWindow.Initialize(_storeContext, MainWindow.Instance.WindowHandle);
var updateList = await _storeContext.GetAppAndOptionalStorePackageUpdatesAsync();
_updatePackages = updateList?.ToList();
}
catch (Exception ex)
{
// GetAppAndOptionalStorePackageUpdatesAsync throws for unknown reasons.
App.Logger.LogWarning(ex, ex.Message);
}
}
private static async Task<bool> ShowDialogAsync()
{
//TODO: Use IDialogService in future.
ContentDialog dialog = new()
{
Title = "ConsentDialogTitle".GetLocalizedResource(),
Content = "ConsentDialogContent".GetLocalizedResource(),
CloseButtonText = "Close".GetLocalizedResource(),
PrimaryButtonText = "ConsentDialogPrimaryButtonText".GetLocalizedResource()
};
if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8))
dialog.XamlRoot = MainWindow.Instance.Content.XamlRoot;
ContentDialogResult result = await dialog.TryShowAsync();
return result == ContentDialogResult.Primary;
}
public async Task CheckAndUpdateFilesLauncherAsync()
{
var destFolderPath = Path.Combine(UserDataPaths.GetDefault().LocalAppData, "Files");
var destExeFilePath = Path.Combine(destFolderPath, "Files.App.Launcher.exe");
if (Path.Exists(destExeFilePath))
{
var hashEqual = false;
var srcHashFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/FilesOpenDialog/Files.App.Launcher.exe.sha256"));
var destHashFilePath = Path.Combine(destFolderPath, "Files.App.Launcher.exe.sha256");
if (Path.Exists(destHashFilePath))
{
await using var srcStream = (await srcHashFile.OpenReadAsync()).AsStream();
await using var destStream = File.OpenRead(destHashFilePath);
hashEqual = HashEqual(srcStream, destStream);
}
if (!hashEqual)
{
var srcExeFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/FilesOpenDialog/Files.App.Launcher.exe"));
var destFolder = await StorageFolder.GetFolderFromPathAsync(destFolderPath);
await srcExeFile.CopyAsync(destFolder, "Files.App.Launcher.exe", NameCollisionOption.ReplaceExisting);
await srcHashFile.CopyAsync(destFolder, "Files.App.Launcher.exe.sha256", NameCollisionOption.ReplaceExisting);
App.Logger.LogInformation("Files.App.Launcher updated.");
}
}
bool HashEqual(Stream a, Stream b)
{
Span<byte> bufferA = stackalloc byte[64];
Span<byte> bufferB = stackalloc byte[64];
a.Read(bufferA);
b.Read(bufferB);
return bufferA.SequenceEqual(bufferB);
}
}
private bool HasUpdates()
{
return _updatePackages is not null && _updatePackages.Count >= 1;
}
private void OnUpdateInProgress()
{
IsUpdating = true;
}
private void OnUpdateCompleted()
{
IsUpdating = false;
IsUpdateAvailable = false;
_updatePackages?.Clear();
}
private void OnUpdateCancelled()
{
IsUpdating = false;
}
}
}