18using System.Collections.Generic;
22using System.Text.RegularExpressions;
23using System.Threading.Tasks;
28 internal class FirefoxProfileManager
61 private static readonly
string[] userJsPreferenceKeys =
64 "browser.download.useDownloadDir",
65 "browser.helperApps.neverAsk.saveToDisk",
66 "browser.download.dir",
67 "browser.download.folderList",
68 "permissions.default.image",
70 "browser.safebrowsing.enabled",
72 "xpinstall.signatures.required",
73 "dom.webdriver.enabled",
74 "browser.startup.page",
75 "browser.sessionstore.resume_from_crash",
84 private static Dictionary<string, string> ReadUserJs(
string userJsPath)
86 Dictionary<string, string> retVal =
new Dictionary<string, string>();
88 if (
true == File.Exists(userJsPath))
89 foreach (
string line
in File.ReadAllLines(userJsPath))
91 Match match = Regex.Match(line,
@"user_pref\(""([^""]+)"",\s*(.+)\);");
93 if (
true == match.Success)
94 retVal[match.Groups[1].Value] = match.Groups[2].Value.Trim();
106 private static void WriteUserJs(
string userJsPath, Dictionary<string, string> preferences)
108 StringBuilder builder =
new StringBuilder();
110 foreach (KeyValuePair<string, string> preference
in preferences.OrderBy(p => p.Key, StringComparer.Ordinal))
111 builder.AppendLine($
"user_pref(\"{preference.Key}\", {preference.Value});");
113 File.WriteAllText(userJsPath, builder.ToString());
121 private static string AsJsString(
string value)
123 string retVal = $
"\"{value?.Replace("\\
", "\\\\
").Replace("\
"",
"\\\"")}\
"";
139 public static Dictionary<string, string> CreateUserJs(
string profilePath,
BrowserSettings browserSettings)
141 Dictionary<string, string> retVal =
null;
142 string userJsPath = Path.Combine(profilePath,
"user.js");
146 Dictionary<string, string> preferences = ReadUserJs(userJsPath);
148 retVal =
new Dictionary<string, string>();
149 foreach (
string key
in userJsPreferenceKeys)
150 retVal[key] = preferences.ContainsKey(key) ? preferences[key] :
null;
158 preferences[
"pdfjs.disabled"] = browserSettings.OpenPDFExternally || browserSettings.UseHeadless ?
"true" :
"false";
161 preferences[
"browser.download.useDownloadDir"] = browserSettings.PromptForDownload ?
"false" :
"true";
162 preferences[
"browser.helperApps.neverAsk.saveToDisk"] =
true == browserSettings.PromptForDownload
164 : AsJsString(GPAL.GPALSettings.FirefoxDirectDownloadMimeTypes);
166 preferences[
"permissions.default.image"] = browserSettings.LoadImages ?
"1" :
"0";
169 preferences[
"dom.popup_maximum"] = browserSettings.BlockPopUps ?
"0" :
"20";
170 preferences[
"browser.safebrowsing.enabled"] =
"false";
176 preferences[
"browser.startup.page"] =
"0";
177 preferences[
"browser.sessionstore.resume_from_crash"] =
"false";
180 preferences[
"extensions.enabled"] =
"true";
181 preferences[
"xpinstall.signatures.required"] =
"false";
182 preferences[
"dom.webdriver.enabled"] =
"false";
184 if (
false ==
string.IsNullOrEmpty(browserSettings.DownloadLocation))
186 preferences[
"browser.download.dir"] = AsJsString(browserSettings.DownloadLocation);
187 preferences[
"browser.download.folderList"] =
"2";
193 preferences.Remove(
"browser.download.dir");
194 preferences.Remove(
"browser.download.folderList");
197 WriteUserJs(userJsPath, preferences);
202 GPAL.PublishSimpleEvent(GPALEventType.EXCEPTION, $
"Could not write preferences into [{userJsPath}]",
null, GPALObjectType.None, ex);
215 public static void RestoreUserJs(
string profilePath, Dictionary<string, string> previous)
217 string userJsPath =
null == previous ? null : Path.Combine(profilePath,
"user.js");
219 if (
null == userJsPath ||
false == File.Exists(userJsPath))
224 Dictionary<string, string> preferences = ReadUserJs(userJsPath);
226 foreach (KeyValuePair<string, string> was
in previous)
227 if (
null == was.Value)
228 preferences.Remove(was.Key);
230 preferences[was.Key] = was.Value;
232 WriteUserJs(userJsPath, preferences);
236 GPAL.PublishSimpleEvent(GPALEventType.EXCEPTION, $
"Could not restore preferences in [{userJsPath}]",
null, GPALObjectType.None, ex);
244 public static string CreateTempProfileDirectory()
247 string tempPath = Path.GetTempPath();
250 string randomString = Guid.NewGuid().ToString(
"N").Substring(0, 8);
253 string profileDirName = $
"rust_mozprofile{randomString}";
256 string profilePath = Path.Combine(tempPath, profileDirName);
259 Directory.CreateDirectory(profilePath);