-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFirefoxProfilesReader.cs
81 lines (50 loc) · 2.72 KB
/
FirefoxProfilesReader.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
using Gsemac.Text.Ini;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Gsemac.Net.WebBrowsers {
public class FirefoxProfilesReader :
IWebBrowserProfilesReader {
// Public members
public FirefoxProfilesReader(string userDataDirectoryPath) {
if (userDataDirectoryPath is null)
throw new ArgumentNullException(nameof(userDataDirectoryPath));
this.userDataDirectoryPath = userDataDirectoryPath;
}
public IEnumerable<IWebBrowserProfile> GetProfiles() {
List<IWebBrowserProfile> profiles = new List<IWebBrowserProfile>();
// Profile information is stored in the "profiles.ini" file.
string profilesIniFilePath = Path.Combine(userDataDirectoryPath, "profiles.ini");
if (File.Exists(profilesIniFilePath)) {
// Read all profiles from the file.
// Older profiles will be named "*.default", and newer profiles will be named "*.default-release" (https://superuser.com/a/1556315/1762496).
IIni profilesIni = IniFactory.Default.FromFile(profilesIniFilePath, new IniOptions {
KeyComparer = StringComparer.OrdinalIgnoreCase,
});
// Determine which profiles are marked as "default" by iterating through the installs first.
HashSet<string> defaultProfileDirectoryPaths = new HashSet<string>();
foreach (IIniSection section in profilesIni.Sections) {
if (section.Name.StartsWith("Install", StringComparison.OrdinalIgnoreCase)) {
defaultProfileDirectoryPaths.Add(section["Default"]);
}
else if (section.Name.StartsWith("Profile", StringComparison.OrdinalIgnoreCase)) {
string name = section["Name"];
string directoryPath = section["Path"];
profiles.Add(new WebBrowserProfile(new FirefoxCookiesReader()) {
Identifier = section.Name,
Name = name,
IsDefault = defaultProfileDirectoryPaths.Contains(directoryPath),
DirectoryPath = Path.Combine(userDataDirectoryPath, directoryPath),
});
}
}
}
return profiles.Where(profile => Directory.Exists(profile.DirectoryPath))
.OrderByDescending(profile => profile.IsDefault)
.ThenByDescending(profile => profile.Name.EndsWith("-release"));
}
// Private members
private readonly string userDataDirectoryPath;
}
}