private void Initialize(bool force = false)
{
if (this.initializeCalled && !force)
{
return;
}
this.initializeCalled = true;
var browserConfig = Bootstrapper.GetInstance<IBrowserConfiguration>().Config;
if (!Xpcom.IsInitialized)
{
PromptFactory.PromptServiceCreator = () => new FilteredPromptService();
Xpcom.Initialize(Path.GetFullPath(browserConfig.Runtime));
}
if (browserConfig.ClearCookiesAtStartup)
{
// https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface
var cookieManager = Xpcom.GetService<nsICookieManager>("@mozilla.org/cookiemanager;1");
cookieManager?.RemoveAll();
}
if (browserConfig.Cookie != null && browserConfig.Cookie.Any())
{
var cookieManager2 = Xpcom.GetService<nsICookieManager2>("@mozilla.org/cookiemanager;1");
if (cookieManager2 != null)
{
foreach (var cookie in browserConfig.Cookie.Where(cookie => cookie != null && (cookie.IsSession || cookie.ExpirationDate > DateTime.Now)))
{
cookieManager2.Add(
new nsAUTF8String(cookie.Host),
new nsAUTF8String(cookie.Path),
new nsACString(cookie.Name),
new nsACString(cookie.Value),
cookie.IsSecure,
cookie.IsHttpOnly,
cookie.IsSession,
(long)cookie.ExpirationDate.Subtract(new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds);
}
}
}
if (browserConfig.Proxy != null)
{
GeckoPreferences.User["network.proxy.no_proxies_on"] = "localhost, 127.0.0.1";
var num = browserConfig.Proxy.Type;
GeckoPreferences.User["network.proxy.type"] = (int)num;
switch (num)
{
case ProxyType.Manual:
GeckoPreferences.User["network.proxy.http"] = browserConfig.Proxy.Server;
GeckoPreferences.User["network.proxy.http_port"] = browserConfig.Proxy.Port;
GeckoPreferences.User["network.proxy.ftp"] = browserConfig.Proxy.Server;
GeckoPreferences.User["network.proxy.ftp_port"] = browserConfig.Proxy.Port;
GeckoPreferences.User["network.proxy.ssl"] = browserConfig.Proxy.Server;
GeckoPreferences.User["network.proxy.ssl_port"] = browserConfig.Proxy.Port;
GeckoPreferences.User["network.proxy.socks"] = browserConfig.Proxy.Server;
GeckoPreferences.User["network.proxy.socks_port"] = browserConfig.Proxy.Port;
break;
case ProxyType.AutoConfigurationPac:
GeckoPreferences.User["network.proxy.autoconfig_url"] = browserConfig.Proxy.Url;
break;
}
}
if (browserConfig.BrowserRegistry != null)
{
foreach (var item in browserConfig.BrowserRegistry)
{
try
{
object value;
switch (item.Type)
{
case BrowserRegistryType.Boolean:
bool parsedBool;
if (! bool.TryParse(item.Value.ToString(), out parsedBool))
{
throw new InvalidDataException("Value could not be parsed as boolean");
}
value = parsedBool;
break;
case BrowserRegistryType.Integer:
int parsedInt;
if (!int.TryParse(item.Value.ToString(), out parsedInt))
{
throw new InvalidDataException("Value could not be parsed as integer");
}
value = parsedInt;
break;
case BrowserRegistryType.String:
value = item.Value.ToString();
break;
default:
throw new NotSupportedException($"Registry type {item.Type} is not supported");
}
GeckoPreferences.User[item.Name] = value;
}
catch (Exception exception)
{
Logger.Error("Could not set user preference", exception);
}
}
}
}