-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBrowserHistoryHelper.cs
More file actions
119 lines (96 loc) · 3.38 KB
/
BrowserHistoryHelper.cs
File metadata and controls
119 lines (96 loc) · 3.38 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
using System;
using System.IO;
namespace TidyWindow.App.Services;
internal static class BrowserHistoryHelper
{
private static readonly string EdgeUserDataRoot = BuildEdgeUserDataRoot();
private static readonly string ChromeUserDataRoot = BuildChromeUserDataRoot();
public static bool TryGetBrowserProfile(string? candidatePath, out BrowserProfile profile)
{
profile = default;
if (TryGetProfile(candidatePath, EdgeUserDataRoot, profileFilter: null, out var edgeProfile))
{
profile = new BrowserProfile(BrowserKind.Edge, edgeProfile);
return true;
}
if (TryGetProfile(candidatePath, ChromeUserDataRoot, IsChromeProfile, out var chromeProfile))
{
profile = new BrowserProfile(BrowserKind.Chrome, chromeProfile);
return true;
}
return false;
}
private static bool TryGetProfile(string? candidatePath, string root, Func<string, bool>? profileFilter, out string profileDirectory)
{
profileDirectory = string.Empty;
if (string.IsNullOrWhiteSpace(candidatePath) || string.IsNullOrWhiteSpace(root))
{
return false;
}
if (!candidatePath.StartsWith(root, StringComparison.OrdinalIgnoreCase))
{
return false;
}
var relative = candidatePath.Substring(root.Length)
.TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
if (string.IsNullOrWhiteSpace(relative))
{
return false;
}
var separatorIndex = relative.IndexOfAny(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar });
if (separatorIndex <= 0)
{
return false;
}
var profileSegment = relative[..separatorIndex];
if (string.IsNullOrWhiteSpace(profileSegment))
{
return false;
}
if (profileFilter is not null && !profileFilter(profileSegment))
{
return false;
}
var candidate = Path.Combine(root, profileSegment);
if (!Directory.Exists(candidate))
{
return false;
}
profileDirectory = candidate;
return true;
}
private static string BuildEdgeUserDataRoot()
{
var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
if (string.IsNullOrWhiteSpace(localAppData))
{
return string.Empty;
}
return Path.Combine(localAppData, "Microsoft", "Edge", "User Data");
}
private static string BuildChromeUserDataRoot()
{
var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
if (string.IsNullOrWhiteSpace(localAppData))
{
return string.Empty;
}
return Path.Combine(localAppData, "Google", "Chrome", "User Data");
}
private static bool IsChromeProfile(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
return false;
}
if (string.Equals(name, "Default", StringComparison.OrdinalIgnoreCase))
{
return true;
}
if (name.StartsWith("Profile ", StringComparison.OrdinalIgnoreCase))
{
return true;
}
return name.StartsWith("Guest Profile", StringComparison.OrdinalIgnoreCase);
}
}