diff --git a/SafeExamBrowser.Browser/Handlers/ContextMenuHandler.cs b/SafeExamBrowser.Browser/Handlers/ContextMenuHandler.cs index 7b218590..2245b66d 100644 --- a/SafeExamBrowser.Browser/Handlers/ContextMenuHandler.cs +++ b/SafeExamBrowser.Browser/Handlers/ContextMenuHandler.cs @@ -10,9 +10,6 @@ using CefSharp; namespace SafeExamBrowser.Browser.Handlers { - /// - /// See https://cefsharp.github.io/api/73.1.x/html/T_CefSharp_IContextMenuHandler.htm. - /// internal class ContextMenuHandler : IContextMenuHandler { public void OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model) diff --git a/SafeExamBrowser.Browser/Handlers/DisplayHandler.cs b/SafeExamBrowser.Browser/Handlers/DisplayHandler.cs index 4d4265ca..c5f604e3 100644 --- a/SafeExamBrowser.Browser/Handlers/DisplayHandler.cs +++ b/SafeExamBrowser.Browser/Handlers/DisplayHandler.cs @@ -15,9 +15,6 @@ using SafeExamBrowser.Contracts.Browser; namespace SafeExamBrowser.Browser.Handlers { - /// - /// See https://cefsharp.github.io/api/73.1.x/html/T_CefSharp_IDisplayHandler.htm. - /// internal class DisplayHandler : IDisplayHandler { public event FaviconChangedEventHandler FaviconChanged; diff --git a/SafeExamBrowser.Browser/Handlers/DownloadHandler.cs b/SafeExamBrowser.Browser/Handlers/DownloadHandler.cs index 4ff6a719..261155a1 100644 --- a/SafeExamBrowser.Browser/Handlers/DownloadHandler.cs +++ b/SafeExamBrowser.Browser/Handlers/DownloadHandler.cs @@ -18,9 +18,6 @@ using BrowserSettings = SafeExamBrowser.Contracts.Configuration.Settings.Browser namespace SafeExamBrowser.Browser.Handlers { - /// - /// See https://cefsharp.github.io/api/73.1.x/html/T_CefSharp_IDownloadHandler.htm. - /// internal class DownloadHandler : IDownloadHandler { private AppConfig appConfig; diff --git a/SafeExamBrowser.Browser/Handlers/KeyboardHandler.cs b/SafeExamBrowser.Browser/Handlers/KeyboardHandler.cs index 33898547..248a9a98 100644 --- a/SafeExamBrowser.Browser/Handlers/KeyboardHandler.cs +++ b/SafeExamBrowser.Browser/Handlers/KeyboardHandler.cs @@ -12,9 +12,6 @@ using SafeExamBrowser.Contracts.UserInterface; namespace SafeExamBrowser.Browser.Handlers { - /// - /// See https://cefsharp.github.io/api/73.1.x/html/T_CefSharp_IKeyboardHandler.htm. - /// internal class KeyboardHandler : IKeyboardHandler { public event ActionRequestedEventHandler ReloadRequested; diff --git a/SafeExamBrowser.Browser/Handlers/LifeSpanHandler.cs b/SafeExamBrowser.Browser/Handlers/LifeSpanHandler.cs index d984448f..c40130a0 100644 --- a/SafeExamBrowser.Browser/Handlers/LifeSpanHandler.cs +++ b/SafeExamBrowser.Browser/Handlers/LifeSpanHandler.cs @@ -11,9 +11,6 @@ using SafeExamBrowser.Browser.Events; namespace SafeExamBrowser.Browser.Handlers { - /// - /// See https://cefsharp.github.io/api/73.1.x/html/T_CefSharp_ILifeSpanHandler.htm. - /// internal class LifeSpanHandler : ILifeSpanHandler { public event PopupRequestedEventHandler PopupRequested; diff --git a/SafeExamBrowser.Browser/Handlers/RequestHandler.cs b/SafeExamBrowser.Browser/Handlers/RequestHandler.cs index c876c4c7..75813455 100644 --- a/SafeExamBrowser.Browser/Handlers/RequestHandler.cs +++ b/SafeExamBrowser.Browser/Handlers/RequestHandler.cs @@ -6,72 +6,25 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -using System; -using System.Collections.Specialized; using CefSharp; -using CefSharp.Handler; using SafeExamBrowser.Contracts.Configuration; namespace SafeExamBrowser.Browser.Handlers { - /// - /// See https://cefsharp.github.io/api/71.0.0/html/T_CefSharp_Handler_DefaultRequestHandler.htm. - /// - internal class RequestHandler : DefaultRequestHandler + internal class RequestHandler : CefSharp.Handler.RequestHandler { private AppConfig appConfig; + private ResourceRequestHandler resourceRequestHandler; internal RequestHandler(AppConfig appConfig) { this.appConfig = appConfig; + this.resourceRequestHandler = new ResourceRequestHandler(appConfig); } - public override CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback) + protected override IResourceRequestHandler GetResourceRequestHandler(IWebBrowser webBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling) { - // TODO: CEF does not yet support intercepting requests from service workers, thus the user agent must be statically set at browser - // startup for now. Once CEF has full support of service workers, the static user agent should be removed and the method below - // reactivated. See https://bitbucket.org/chromiumembedded/cef/issues/2622 for the current status of development. - // AppendCustomUserAgent(request); - - if (IsMailtoUrl(request.Url)) - { - return CefReturnValue.Cancel; - } - - ReplaceCustomScheme(request); - - return base.OnBeforeResourceLoad(browserControl, browser, frame, request, callback); - } - - private void AppendCustomUserAgent(IRequest request) - { - var headers = new NameValueCollection(request.Headers); - var userAgent = request.Headers["User-Agent"]; - - headers["User-Agent"] = $"{userAgent} SEB/{appConfig.ProgramInformationalVersion}"; - request.Headers = headers; - } - - private bool IsMailtoUrl(string url) - { - return url.StartsWith(Uri.UriSchemeMailto); - } - - private void ReplaceCustomScheme(IRequest request) - { - if (Uri.IsWellFormedUriString(request.Url, UriKind.RelativeOrAbsolute)) - { - var uri = new Uri(request.Url); - - if (uri.Scheme == appConfig.SebUriScheme) - { - request.Url = new UriBuilder(uri) { Scheme = Uri.UriSchemeHttp }.Uri.AbsoluteUri; - } - else if (uri.Scheme == appConfig.SebUriSchemeSecure) - { - request.Url = new UriBuilder(uri) { Scheme = Uri.UriSchemeHttps }.Uri.AbsoluteUri; - } - } + return resourceRequestHandler; } } } diff --git a/SafeExamBrowser.Browser/Handlers/ResourceRequestHandler.cs b/SafeExamBrowser.Browser/Handlers/ResourceRequestHandler.cs new file mode 100644 index 00000000..5134c0a2 --- /dev/null +++ b/SafeExamBrowser.Browser/Handlers/ResourceRequestHandler.cs @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2019 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using System; +using System.Collections.Specialized; +using CefSharp; +using SafeExamBrowser.Contracts.Configuration; + +namespace SafeExamBrowser.Browser.Handlers +{ + internal class ResourceRequestHandler : CefSharp.Handler.ResourceRequestHandler + { + private AppConfig appConfig; + + internal ResourceRequestHandler(AppConfig appConfig) + { + this.appConfig = appConfig; + } + + protected override CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback) + { + // TODO: CEF does not yet support intercepting requests from service workers, thus the user agent must be statically set at browser + // startup for now. Once CEF has full support of service workers, the static user agent should be removed and the method below + // reactivated. See https://bitbucket.org/chromiumembedded/cef/issues/2622 for the current status of development. + // AppendCustomUserAgent(request); + + if (IsMailtoUrl(request.Url)) + { + return CefReturnValue.Cancel; + } + + ReplaceCustomScheme(request); + + return base.OnBeforeResourceLoad(browserControl, browser, frame, request, callback); + } + + private void AppendCustomUserAgent(IRequest request) + { + var headers = new NameValueCollection(request.Headers); + var userAgent = request.Headers["User-Agent"]; + + headers["User-Agent"] = $"{userAgent} SEB/{appConfig.ProgramInformationalVersion}"; + request.Headers = headers; + } + + private bool IsMailtoUrl(string url) + { + return url.StartsWith(Uri.UriSchemeMailto); + } + + private void ReplaceCustomScheme(IRequest request) + { + if (Uri.IsWellFormedUriString(request.Url, UriKind.RelativeOrAbsolute)) + { + var uri = new Uri(request.Url); + + if (uri.Scheme == appConfig.SebUriScheme) + { + request.Url = new UriBuilder(uri) { Scheme = Uri.UriSchemeHttp }.Uri.AbsoluteUri; + } + else if (uri.Scheme == appConfig.SebUriSchemeSecure) + { + request.Url = new UriBuilder(uri) { Scheme = Uri.UriSchemeHttps }.Uri.AbsoluteUri; + } + } + } + } +} diff --git a/SafeExamBrowser.Browser/SafeExamBrowser.Browser.csproj b/SafeExamBrowser.Browser/SafeExamBrowser.Browser.csproj index d153231e..a68b08d9 100644 --- a/SafeExamBrowser.Browser/SafeExamBrowser.Browser.csproj +++ b/SafeExamBrowser.Browser/SafeExamBrowser.Browser.csproj @@ -1,9 +1,9 @@  - - - - + + + + Debug @@ -81,6 +81,7 @@ + @@ -106,13 +107,13 @@ This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - - - - - + + + + + + - - + + \ No newline at end of file diff --git a/SafeExamBrowser.Browser/packages.config b/SafeExamBrowser.Browser/packages.config index a991a14d..87ee5807 100644 --- a/SafeExamBrowser.Browser/packages.config +++ b/SafeExamBrowser.Browser/packages.config @@ -1,7 +1,7 @@  - - - - + + + + \ No newline at end of file