SEBWIN-357: Implemented browser session configuration.
This commit is contained in:
parent
42e107d7c7
commit
91765e2d55
5 changed files with 111 additions and 33 deletions
|
@ -78,6 +78,8 @@ namespace SafeExamBrowser.Browser
|
||||||
|
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
{
|
{
|
||||||
|
logger.Info("Starting initialization...");
|
||||||
|
|
||||||
var cefSettings = InitializeCefSettings();
|
var cefSettings = InitializeCefSettings();
|
||||||
var success = Cef.Initialize(cefSettings, true, default(IApp));
|
var success = Cef.Initialize(cefSettings, true, default(IApp));
|
||||||
|
|
||||||
|
@ -85,6 +87,11 @@ namespace SafeExamBrowser.Browser
|
||||||
|
|
||||||
if (success)
|
if (success)
|
||||||
{
|
{
|
||||||
|
if (settings.DeleteCookiesOnStartup)
|
||||||
|
{
|
||||||
|
DeleteCookies();
|
||||||
|
}
|
||||||
|
|
||||||
logger.Info("Initialized browser.");
|
logger.Info("Initialized browser.");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -100,6 +107,8 @@ namespace SafeExamBrowser.Browser
|
||||||
|
|
||||||
public void Terminate()
|
public void Terminate()
|
||||||
{
|
{
|
||||||
|
logger.Info("Initiating termination...");
|
||||||
|
|
||||||
foreach (var instance in instances)
|
foreach (var instance in instances)
|
||||||
{
|
{
|
||||||
instance.Terminated -= Instance_Terminated;
|
instance.Terminated -= Instance_Terminated;
|
||||||
|
@ -107,19 +116,15 @@ namespace SafeExamBrowser.Browser
|
||||||
logger.Info($"Terminated browser instance {instance.Id}.");
|
logger.Info($"Terminated browser instance {instance.Id}.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (settings.DeleteCookiesOnShutdown)
|
||||||
|
{
|
||||||
|
DeleteCookies();
|
||||||
|
}
|
||||||
|
|
||||||
Cef.Shutdown();
|
Cef.Shutdown();
|
||||||
logger.Info("Terminated browser.");
|
logger.Info("Terminated browser.");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void InitializeApplicationInfo()
|
|
||||||
{
|
|
||||||
AutoStart = true;
|
|
||||||
Icon = new BrowserIconResource();
|
|
||||||
Id = Guid.NewGuid();
|
|
||||||
Name = text.Get(TextKey.Browser_Name);
|
|
||||||
Tooltip = text.Get(TextKey.Browser_Tooltip);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void CreateNewInstance(string url = null)
|
private void CreateNewInstance(string url = null)
|
||||||
{
|
{
|
||||||
var id = ++instanceIdCounter;
|
var id = ++instanceIdCounter;
|
||||||
|
@ -140,6 +145,41 @@ namespace SafeExamBrowser.Browser
|
||||||
WindowsChanged?.Invoke();
|
WindowsChanged?.Invoke();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void DeleteCookies()
|
||||||
|
{
|
||||||
|
var callback = new TaskDeleteCookiesCallback();
|
||||||
|
|
||||||
|
callback.Task.ContinueWith(task =>
|
||||||
|
{
|
||||||
|
if (!task.IsCompleted || task.Result == TaskDeleteCookiesCallback.InvalidNoOfCookiesDeleted)
|
||||||
|
{
|
||||||
|
logger.Warn("Failed to delete cookies!");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
logger.Debug($"Deleted {task.Result} cookies.");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (Cef.GetGlobalCookieManager().DeleteCookies(callback: callback))
|
||||||
|
{
|
||||||
|
logger.Debug("Successfully initiated cookie deletion.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
logger.Warn("Failed to initiate cookie deletion!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void InitializeApplicationInfo()
|
||||||
|
{
|
||||||
|
AutoStart = true;
|
||||||
|
Icon = new BrowserIconResource();
|
||||||
|
Id = Guid.NewGuid();
|
||||||
|
Name = text.Get(TextKey.Browser_Name);
|
||||||
|
Tooltip = text.Get(TextKey.Browser_Tooltip);
|
||||||
|
}
|
||||||
|
|
||||||
private CefSettings InitializeCefSettings()
|
private CefSettings InitializeCefSettings()
|
||||||
{
|
{
|
||||||
var warning = logger.LogLevel == LogLevel.Warning;
|
var warning = logger.LogLevel == LogLevel.Warning;
|
||||||
|
@ -149,6 +189,7 @@ namespace SafeExamBrowser.Browser
|
||||||
CachePath = appConfig.BrowserCachePath,
|
CachePath = appConfig.BrowserCachePath,
|
||||||
LogFile = appConfig.BrowserLogFilePath,
|
LogFile = appConfig.BrowserLogFilePath,
|
||||||
LogSeverity = error ? LogSeverity.Error : (warning ? LogSeverity.Warning : LogSeverity.Info),
|
LogSeverity = error ? LogSeverity.Error : (warning ? LogSeverity.Warning : LogSeverity.Info),
|
||||||
|
PersistSessionCookies = !settings.DeleteCookiesOnShutdown,
|
||||||
UserAgent = InitializeUserAgent()
|
UserAgent = InitializeUserAgent()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -161,11 +202,12 @@ namespace SafeExamBrowser.Browser
|
||||||
cefSettings.CefCommandLineArgs.Add("disable-pdf-extension", "");
|
cefSettings.CefCommandLineArgs.Add("disable-pdf-extension", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Debug($"Cache path: {cefSettings.CachePath}");
|
logger.Debug($"Cache Path: {cefSettings.CachePath}");
|
||||||
logger.Debug($"Engine version: Chromium {Cef.ChromiumVersion}, CEF {Cef.CefVersion}, CefSharp {Cef.CefSharpVersion}");
|
logger.Debug($"Engine Version: Chromium {Cef.ChromiumVersion}, CEF {Cef.CefVersion}, CefSharp {Cef.CefSharpVersion}");
|
||||||
logger.Debug($"Log file: {cefSettings.LogFile}");
|
logger.Debug($"Log File: {cefSettings.LogFile}");
|
||||||
logger.Debug($"Log severity: {cefSettings.LogSeverity}");
|
logger.Debug($"Log Severity: {cefSettings.LogSeverity}.");
|
||||||
logger.Debug($"PDF reader: {(settings.AllowPdfReader ? "Enabled" : "Disabled")}");
|
logger.Debug($"PDF Reader: {(settings.AllowPdfReader ? "Enabled" : "Disabled")}.");
|
||||||
|
logger.Debug($"Session Persistence: {(cefSettings.PersistSessionCookies ? "Enabled" : "Disabled")}.");
|
||||||
|
|
||||||
return cefSettings;
|
return cefSettings;
|
||||||
}
|
}
|
||||||
|
@ -203,6 +245,25 @@ namespace SafeExamBrowser.Browser
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// TODO: Workaround to correctly set the user agent due to missing support for request interception for requests made by service workers.
|
||||||
|
/// Remove once CEF fully supports service workers and reactivate the functionality in <see cref="Handlers.RequestHandler"/>!
|
||||||
|
/// </summary>
|
||||||
|
private string InitializeUserAgent()
|
||||||
|
{
|
||||||
|
var osVersion = $"{Environment.OSVersion.Version.Major}.{Environment.OSVersion.Version.Minor}";
|
||||||
|
var sebVersion = $"SEB/{appConfig.ProgramInformationalVersion}";
|
||||||
|
|
||||||
|
if (settings.UseCustomUserAgent)
|
||||||
|
{
|
||||||
|
return $"{settings.CustomUserAgent} {sebVersion}";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return $"Mozilla/5.0 (Windows NT {osVersion}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/{Cef.ChromiumVersion} {sebVersion}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private string ToScheme(ProxyProtocol protocol)
|
private string ToScheme(ProxyProtocol protocol)
|
||||||
{
|
{
|
||||||
switch (protocol)
|
switch (protocol)
|
||||||
|
@ -231,24 +292,5 @@ namespace SafeExamBrowser.Browser
|
||||||
instances.Remove(instances.First(i => i.Id == id));
|
instances.Remove(instances.First(i => i.Id == id));
|
||||||
WindowsChanged?.Invoke();
|
WindowsChanged?.Invoke();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// TODO: Workaround to correctly set the user agent due to missing support for request interception for requests made by service workers.
|
|
||||||
/// Remove once CEF fully supports service workers and reactivate the functionality in <see cref="Handlers.RequestHandler"/>!
|
|
||||||
/// </summary>
|
|
||||||
private string InitializeUserAgent()
|
|
||||||
{
|
|
||||||
var osVersion = $"{Environment.OSVersion.Version.Major}.{Environment.OSVersion.Version.Minor}";
|
|
||||||
var sebVersion = $"SEB/{appConfig.ProgramInformationalVersion}";
|
|
||||||
|
|
||||||
if (settings.UseCustomUserAgent)
|
|
||||||
{
|
|
||||||
return $"{settings.CustomUserAgent} {sebVersion}";
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return $"Mozilla/5.0 (Windows NT {osVersion}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/{Cef.ChromiumVersion} {sebVersion}";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,6 +61,12 @@ namespace SafeExamBrowser.Configuration.ConfigurationData.DataMapping
|
||||||
case Keys.Browser.AdditionalWindow.WindowWidth:
|
case Keys.Browser.AdditionalWindow.WindowWidth:
|
||||||
MapWindowWidthAdditionalWindow(settings, value);
|
MapWindowWidthAdditionalWindow(settings, value);
|
||||||
break;
|
break;
|
||||||
|
case Keys.Browser.DeleteCookiesOnShutdown:
|
||||||
|
MapDeleteCookiesOnShutdown(settings, value);
|
||||||
|
break;
|
||||||
|
case Keys.Browser.DeleteCookiesOnStartup:
|
||||||
|
MapDeleteCookiesOnStartup(settings, value);
|
||||||
|
break;
|
||||||
case Keys.Browser.DownloadDirectory:
|
case Keys.Browser.DownloadDirectory:
|
||||||
MapDownloadDirectory(settings, value);
|
MapDownloadDirectory(settings, value);
|
||||||
break;
|
break;
|
||||||
|
@ -231,6 +237,22 @@ namespace SafeExamBrowser.Configuration.ConfigurationData.DataMapping
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void MapDeleteCookiesOnShutdown(AppSettings settings, object value)
|
||||||
|
{
|
||||||
|
if (value is bool delete)
|
||||||
|
{
|
||||||
|
settings.Browser.DeleteCookiesOnShutdown = delete;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void MapDeleteCookiesOnStartup(AppSettings settings, object value)
|
||||||
|
{
|
||||||
|
if (value is bool delete)
|
||||||
|
{
|
||||||
|
settings.Browser.DeleteCookiesOnStartup = delete;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void MapDownloadDirectory(AppSettings settings, object value)
|
private void MapDownloadDirectory(AppSettings settings, object value)
|
||||||
{
|
{
|
||||||
if (value is string directory)
|
if (value is string directory)
|
||||||
|
|
|
@ -120,6 +120,8 @@ namespace SafeExamBrowser.Configuration.ConfigurationData
|
||||||
settings.Browser.AllowPdfReader = true;
|
settings.Browser.AllowPdfReader = true;
|
||||||
settings.Browser.AllowPdfReaderToolbar = false;
|
settings.Browser.AllowPdfReaderToolbar = false;
|
||||||
settings.Browser.AllowUploads = true;
|
settings.Browser.AllowUploads = true;
|
||||||
|
settings.Browser.DeleteCookiesOnShutdown = true;
|
||||||
|
settings.Browser.DeleteCookiesOnStartup = true;
|
||||||
settings.Browser.EnableBrowser = true;
|
settings.Browser.EnableBrowser = true;
|
||||||
settings.Browser.MainWindow.AllowAddressBar = false;
|
settings.Browser.MainWindow.AllowAddressBar = false;
|
||||||
settings.Browser.MainWindow.AllowBackwardNavigation = false;
|
settings.Browser.MainWindow.AllowBackwardNavigation = false;
|
||||||
|
|
|
@ -49,6 +49,8 @@ namespace SafeExamBrowser.Configuration.ConfigurationData
|
||||||
internal const string AllowPdfReaderToolbar = "allowPDFReaderToolbar";
|
internal const string AllowPdfReaderToolbar = "allowPDFReaderToolbar";
|
||||||
internal const string CustomUserAgentDesktop = "browserUserAgentWinDesktopModeCustom";
|
internal const string CustomUserAgentDesktop = "browserUserAgentWinDesktopModeCustom";
|
||||||
internal const string CustomUserAgentMobile = "browserUserAgentWinTouchModeCustom";
|
internal const string CustomUserAgentMobile = "browserUserAgentWinTouchModeCustom";
|
||||||
|
internal const string DeleteCookiesOnShutdown = "examSessionClearCookiesOnEnd";
|
||||||
|
internal const string DeleteCookiesOnStartup = "examSessionClearCookiesOnStart";
|
||||||
internal const string DownloadDirectory = "downloadDirectoryWin";
|
internal const string DownloadDirectory = "downloadDirectoryWin";
|
||||||
internal const string DownloadPdfFiles = "downloadPDFFiles";
|
internal const string DownloadPdfFiles = "downloadPDFFiles";
|
||||||
internal const string EnableBrowser = "enableSebBrowser";
|
internal const string EnableBrowser = "enableSebBrowser";
|
||||||
|
|
|
@ -71,6 +71,16 @@ namespace SafeExamBrowser.Settings.Browser
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string CustomUserAgent { get; set; }
|
public string CustomUserAgent { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Determines whether all cookies are deleted when terminating the browser application.
|
||||||
|
/// </summary>
|
||||||
|
public bool DeleteCookiesOnShutdown { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Determines whether all cookies are deleted when starting the browser application.
|
||||||
|
/// </summary>
|
||||||
|
public bool DeleteCookiesOnStartup { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Defines a custom directory for file downloads. If not defined, all downloads will be saved in the current user's download directory.
|
/// Defines a custom directory for file downloads. If not defined, all downloads will be saved in the current user's download directory.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
Loading…
Add table
Reference in a new issue