SEBWIN-302: Implemented configuration of internal PDF reader.

This commit is contained in:
dbuechel 2020-01-30 11:15:28 +01:00
parent cf2a74f6ce
commit 603c268839
6 changed files with 81 additions and 5 deletions

View file

@ -156,10 +156,16 @@ namespace SafeExamBrowser.Browser
cefSettings.CefCommandLineArgs.Add("touch-events", "enabled");
if (!settings.AllowPdfReader)
{
cefSettings.CefCommandLineArgs.Add("disable-pdf-extension", "");
}
logger.Debug($"Cache path: {cefSettings.CachePath}");
logger.Debug($"Engine version: Chromium {Cef.ChromiumVersion}, CEF {Cef.CefVersion}, CefSharp {Cef.CefSharpVersion}");
logger.Debug($"Log file: {cefSettings.LogFile}");
logger.Debug($"Log severity: {cefSettings.LogSeverity}");
logger.Debug($"PDF reader: {(settings.AllowPdfReader ? "Enabled" : "Disabled")}");
return cefSettings;
}

View file

@ -9,6 +9,7 @@
using System;
using System.Collections.Specialized;
using System.IO;
using System.Net.Mime;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
@ -76,6 +77,22 @@ namespace SafeExamBrowser.Browser.Handlers
return base.OnBeforeResourceLoad(webBrowser, browser, frame, request, callback);
}
protected override bool OnResourceResponse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
var abort = true;
if (RedirectToDisablePdfToolbar(request, response, out var url))
{
chromiumWebBrowser.Load(url);
}
else
{
abort = base.OnResourceResponse(chromiumWebBrowser, browser, frame, request, response);
}
return abort;
}
private void AppendCustomUserAgent(IRequest request)
{
var headers = new NameValueCollection(request.Headers);
@ -100,20 +117,20 @@ namespace SafeExamBrowser.Browser.Handlers
private bool Block(IRequest request)
{
var block = false;
if (settings.Filter.ProcessContentRequests)
{
var result = filter.Process(new Request { Url = request.Url });
var block = result == FilterResult.Block;
if (block)
if (result == FilterResult.Block)
{
block = true;
logger.Info($"Blocked content request for '{request.Url}'.");
}
return block;
}
return false;
return block;
}
private bool IsMailtoUrl(string url)
@ -121,6 +138,23 @@ namespace SafeExamBrowser.Browser.Handlers
return url.StartsWith(Uri.UriSchemeMailto);
}
private bool RedirectToDisablePdfToolbar(IRequest request, IResponse response, out string url)
{
const string DISABLE_PDF_TOOLBAR = "#toolbar=0";
var isPdf = response.Headers["Content-Type"] == MediaTypeNames.Application.Pdf;
var hasFragment = request.Url.Contains(DISABLE_PDF_TOOLBAR);
var redirect = settings.AllowPdfReader && !settings.AllowPdfReaderToolbar && isPdf && !hasFragment;
url = request.Url + DISABLE_PDF_TOOLBAR;
if (redirect)
{
logger.Info($"Redirecting to '{url}' to disable PDF reader toolbar.");
}
return redirect;
}
private void ReplaceSebScheme(IRequest request)
{
if (Uri.IsWellFormedUriString(request.Url, UriKind.RelativeOrAbsolute))

View file

@ -36,6 +36,9 @@ namespace SafeExamBrowser.Configuration.ConfigurationData.DataMapping
case Keys.Browser.AllowPageZoom:
MapAllowPageZoom(settings, value);
break;
case Keys.Browser.AllowPdfReaderToolbar:
MapAllowPdfReaderToolbar(settings, value);
break;
case Keys.Browser.AdditionalWindow.AllowAddressBar:
MapAllowAddressBarAdditionalWindow(settings, value);
break;
@ -60,6 +63,9 @@ namespace SafeExamBrowser.Configuration.ConfigurationData.DataMapping
case Keys.Browser.DownloadDirectory:
MapDownloadDirectory(settings, value);
break;
case Keys.Browser.DownloadPdfFiles:
MapDownloadPdfFiles(settings, value);
break;
case Keys.Browser.EnableBrowser:
MapEnableBrowser(settings, value);
break;
@ -197,6 +203,14 @@ namespace SafeExamBrowser.Configuration.ConfigurationData.DataMapping
}
}
private void MapAllowPdfReaderToolbar(AppSettings settings, object value)
{
if (value is bool allow)
{
settings.Browser.AllowPdfReaderToolbar = allow;
}
}
private void MapAllowReload(AppSettings settings, object value)
{
if (value is bool allow)
@ -221,6 +235,14 @@ namespace SafeExamBrowser.Configuration.ConfigurationData.DataMapping
}
}
private void MapDownloadPdfFiles(AppSettings settings, object value)
{
if (value is bool download)
{
settings.Browser.AllowPdfReader = !download;
}
}
private void MapEnableBrowser(AppSettings settings, object value)
{
if (value is bool enable)

View file

@ -126,6 +126,8 @@ namespace SafeExamBrowser.Configuration.ConfigurationData
settings.Browser.AllowCustomDownloadLocation = false;
settings.Browser.AllowDownloads = true;
settings.Browser.AllowPageZoom = true;
settings.Browser.AllowPdfReader = true;
settings.Browser.AllowPdfReaderToolbar = false;
settings.Browser.AllowUploads = true;
settings.Browser.MainWindow.AllowAddressBar = false;
settings.Browser.MainWindow.AllowBackwardNavigation = false;

View file

@ -46,9 +46,11 @@ namespace SafeExamBrowser.Configuration.ConfigurationData
internal const string AllowDeveloperConsole = "allowDeveloperConsole";
internal const string AllowDownloadsAndUploads = "allowDownUploads";
internal const string AllowPageZoom = "enableZoomPage";
internal const string AllowPdfReaderToolbar = "allowPDFReaderToolbar";
internal const string CustomUserAgentDesktop = "browserUserAgentWinDesktopModeCustom";
internal const string CustomUserAgentMobile = "browserUserAgentWinTouchModeCustom";
internal const string DownloadDirectory = "downloadDirectoryWin";
internal const string DownloadPdfFiles = "downloadPDFFiles";
internal const string EnableBrowser = "enableSebBrowser";
internal const string PopupPolicy = "newBrowserWindowByLinkPolicy";
internal const string PopupBlockForeignHost = "newBrowserWindowByLinkBlockForeign";

View file

@ -41,6 +41,16 @@ namespace SafeExamBrowser.Settings.Browser
/// </summary>
public bool AllowPageZoom { get; set; }
/// <summary>
/// Determines whether the internal PDF reader of the browser application is enabled. If not, documents will be downloaded by default.
/// </summary>
public bool AllowPdfReader { get; set; }
/// <summary>
/// Determines whether the toolbar of the internal PDF reader (which allows to e.g. download or print a document) will be enabled.
/// </summary>
public bool AllowPdfReaderToolbar { get; set; }
/// <summary>
/// Determines whether the user will be allowed to upload files.
/// </summary>