-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWebBrowserInfoFactory.cs
352 lines (229 loc) · 12.7 KB
/
WebBrowserInfoFactory.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
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
using Gsemac.Core;
using Gsemac.IO;
using Gsemac.Net.WebBrowsers.Properties;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace Gsemac.Net.WebBrowsers {
public class WebBrowserInfoFactory :
IWebBrowserInfoFactory {
// Public members
public static WebBrowserInfoFactory Default { get; } = new WebBrowserInfoFactory();
public IWebBrowserInfo GetWebBrowserInfo(string browserExecutablePath) {
if (browserExecutablePath is null)
throw new ArgumentNullException(nameof(browserExecutablePath));
if (!File.Exists(browserExecutablePath))
throw new FileNotFoundException(ExceptionMessages.WebBrowserExecutablePathNotFound, browserExecutablePath);
FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(browserExecutablePath);
WebBrowserId browserId = GetBrowserId(versionInfo);
return new WebBrowserInfo(GetBrowserProfilesReader(browserId)) {
ExecutablePath = browserExecutablePath,
UserDataDirectoryPath = GetUserDataDirectoryPath(browserId),
Name = GetBrowserName(versionInfo),
Version = GetBrowserVersion(versionInfo),
Is64Bit = Is64BitExecutable(browserExecutablePath),
IsDefault = defaultBrowserIdCache.Value.Equals(browserId),
Id = browserId,
};
}
public IWebBrowserInfo GetWebBrowserInfo(WebBrowserId browserId, IWebBrowserInfoOptions options) {
if (options is null)
throw new ArgumentNullException(nameof(options));
// Prefer newer, 64-bit executables.
var installedWebBrowsers = GetInstalledWebBrowsers(options);
return installedWebBrowsers
.Where(info => info.Id == browserId)
.OrderByDescending(info => info.Version)
.ThenByDescending(info => info.Is64Bit)
.FirstOrDefault();
}
public IWebBrowserInfo GetDefaultWebBrowser(IWebBrowserInfoOptions options) {
return GetWebBrowserInfo(defaultBrowserIdCache.Value, options);
}
public IEnumerable<IWebBrowserInfo> GetInstalledWebBrowsers(IWebBrowserInfoOptions options) {
if (options is null)
throw new ArgumentNullException(nameof(options));
if (options.BypassCache) {
defaultBrowserIdCache.Reset();
installedBrowsersCache.Reset();
}
return installedBrowsersCache.Value;
}
// Private members
private readonly ResettableLazy<WebBrowserId> defaultBrowserIdCache = new ResettableLazy<WebBrowserId>(GetDefaultWebBrowserId);
private readonly ResettableLazy<IEnumerable<IWebBrowserInfo>> installedBrowsersCache = new ResettableLazy<IEnumerable<IWebBrowserInfo>>(GetGetInstalledBrowsersInternal);
private static IEnumerable<string> GetWebBrowserExecutablePaths() {
// A better way of detecting installed web browsers might be looking up their associated keys in the registry.
IEnumerable<string> driveDirectoryPaths = DriveInfo.GetDrives().Select(info => info.RootDirectory.FullName);
IEnumerable<string> programFilesDirectoryPaths = new string[]{
@"Program Files",
@"Program Files (x86)",
@"Windows\SystemApps", // Microsoft Edge
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
}.SelectMany(path => driveDirectoryPaths.Select(drivePath => Path.Combine(drivePath, path)))
.Distinct();
IEnumerable<string> webBrowserExecutablePaths = new string[]{
@"BraveSoftware\Brave-Browser\Application\brave.exe",
@"Google\Application\chrome.exe", // Google Chrome, Windows 7
@"Google\Chrome\Application\chrome.exe", // Google Chrome, Windows 8.1+
@"Internet Explorer\iexplore.exe",
@"Microsoft.MicrosoftEdge_8wekyb3d8bbwe\MicrosoftEdge.exe", // EdgeHTML
@"Microsoft\Edge\Application\msedge.exe", // Microsoft Edge
@"Mozilla Firefox\firefox.exe",
@"Opera\launcher.exe",
@"Vivaldi\Application\vivaldi.exe",
}.SelectMany(path => programFilesDirectoryPaths.Select(programFilesDirectoryPath => Path.Combine(programFilesDirectoryPath, path)))
.Distinct();
return webBrowserExecutablePaths.Where(path => File.Exists(path));
}
private static IEnumerable<IWebBrowserInfo> GetGetInstalledBrowsersInternal() {
return GetWebBrowserExecutablePaths()
.Select(path => Default.GetWebBrowserInfo(path))
.OrderBy(info => info.Name)
.ThenBy(info => info.Version)
.ThenBy(info => info.Is64Bit);
}
private static string GetUserDataDirectoryPath(WebBrowserId webBrowserId) {
switch (webBrowserId) {
case WebBrowserId.Brave:
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"BraveSoftware\Brave-Browser\User Data\");
case WebBrowserId.GoogleChrome:
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Google\Chrome\User Data\");
case WebBrowserId.MicrosoftEdge:
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Microsoft\Edge\User Data\");
case WebBrowserId.Firefox:
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Mozilla\Firefox\");
default:
return string.Empty;
}
}
private static string GetBrowserName(FileVersionInfo versionInfo) {
if (versionInfo is null)
throw new ArgumentNullException(nameof(versionInfo));
return versionInfo.ProductName ?? string.Empty;
}
private static WebBrowserId GetBrowserId(FileVersionInfo versionInfo) {
if (versionInfo is null)
throw new ArgumentNullException(nameof(versionInfo));
string productName = GetBrowserName(versionInfo);
if (string.IsNullOrWhiteSpace(productName))
return WebBrowserId.Unknown;
if (productName.Equals("brave browser", StringComparison.OrdinalIgnoreCase))
return WebBrowserId.Brave;
if (productName.Equals("firefox", StringComparison.OrdinalIgnoreCase))
return WebBrowserId.Firefox;
if (productName.Equals("google chrome", StringComparison.OrdinalIgnoreCase))
return WebBrowserId.GoogleChrome;
if (productName.Equals("internet explorer", StringComparison.OrdinalIgnoreCase))
return WebBrowserId.InternetExplorer;
if (productName.Equals("microsoft edge", StringComparison.OrdinalIgnoreCase))
return WebBrowserId.MicrosoftEdge;
if (productName.EndsWith("opera", StringComparison.OrdinalIgnoreCase))
return WebBrowserId.Opera;
if (productName.Equals("vivaldi", StringComparison.OrdinalIgnoreCase))
return WebBrowserId.Vivaldi;
return WebBrowserId.Unknown;
}
private static System.Version GetBrowserVersion(FileVersionInfo versionInfo) {
if (versionInfo is null)
throw new ArgumentNullException(nameof(versionInfo));
string productVersionStr = versionInfo.ProductVersion;
if (!string.IsNullOrWhiteSpace(productVersionStr) && System.Version.TryParse(productVersionStr, out System.Version productVersion))
return productVersion;
return new System.Version();
}
private static bool Is64BitExecutable(string browserExecutablePath) {
// #todo We can determine this in a more portable way by reading PE headers from the executable.
return Environment.Is64BitOperatingSystem &&
PathUtilities.PathContainsSegment(browserExecutablePath, "Program Files");
}
private static IWebBrowserProfilesReader GetBrowserProfilesReader(WebBrowserId webBrowserId) {
switch (webBrowserId) {
case WebBrowserId.Brave:
case WebBrowserId.GoogleChrome:
case WebBrowserId.MicrosoftEdge:
return new ChromiumProfilesReader(GetUserDataDirectoryPath(webBrowserId));
case WebBrowserId.Firefox:
return new FirefoxProfilesReader(GetUserDataDirectoryPath(webBrowserId));
default:
return new NullProfileReader();
}
}
private static WebBrowserId GetDefaultBrowserIdFromUserChoiceKey() {
WebBrowserId id = WebBrowserId.Unknown;
using (RegistryKey userChoiceKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice", writable: false)) {
if (userChoiceKey is object) {
string progId = (string)userChoiceKey.GetValue("Progid");
if (!string.IsNullOrEmpty(progId)) {
switch (progId) {
case "AppXq0fevzme2pys62n3e0fbqa7peapykr8v":
id = WebBrowserId.MicrosoftEdge;
break;
case "BraveHTML":
id = WebBrowserId.Brave;
break;
case "ChromeHTML":
id = WebBrowserId.GoogleChrome;
break;
case "FirefoxURL":
id = WebBrowserId.Firefox;
break;
case "IE.HTTP":
id = WebBrowserId.InternetExplorer;
break;
case "OperaStable":
id = WebBrowserId.Opera;
break;
case "SafariHTML":
id = WebBrowserId.Safari;
break;
default:
id = WebBrowserId.Unknown;
break;
}
}
}
}
return id;
}
private static WebBrowserId GetDefaultBrowserIdFromClassesRootCommandKey() {
string browserExecutablePath = string.Empty;
using (RegistryKey commandKey = Registry.ClassesRoot.OpenSubKey(@"https\shell\open\command", writable: false)) {
if (commandKey is object) {
string defaultValue = (string)commandKey.GetValue(string.Empty);
if (!string.IsNullOrEmpty(defaultValue)) {
Match webBrowserPathMatch = Regex.Match(defaultValue, "^\"([^\"]+)\"");
if (webBrowserPathMatch.Success)
browserExecutablePath = webBrowserPathMatch.Groups[1].Value;
}
}
}
if (File.Exists(browserExecutablePath)) {
FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(browserExecutablePath);
return GetBrowserId(versionInfo);
}
else {
return WebBrowserId.Unknown;
}
}
private static WebBrowserId GetDefaultWebBrowserId() {
// From Windows 8 forward, this seems to be the most reliable location for finding the default browser.
// https://stackoverflow.com/a/17599201
WebBrowserId id = GetDefaultBrowserIdFromUserChoiceKey();
if (id != WebBrowserId.Unknown)
return id;
// For Windows 7 and previous, the UserChoice key may be empty.
// https://stackoverflow.com/a/56707674
id = GetDefaultBrowserIdFromClassesRootCommandKey();
if (id != WebBrowserId.Unknown)
return id;
// Default to Internet Explorer if we can't find the default web browser.
// On modern operating systems, this will redirect to Microsoft Edge.
return WebBrowserId.InternetExplorer;
}
}
}