2017-07-14 10:28:59 +02:00
|
|
|
|
/*
|
2019-01-09 11:25:21 +01:00
|
|
|
|
* Copyright (c) 2019 ETH Zürich, Educational Development and Technology (LET)
|
2017-07-14 10:28:59 +02:00
|
|
|
|
*
|
|
|
|
|
* 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;
|
2017-07-24 17:31:28 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using CefSharp;
|
2019-01-08 14:30:38 +01:00
|
|
|
|
using CefSharp.WinForms;
|
2019-01-17 11:12:17 +01:00
|
|
|
|
using SafeExamBrowser.Browser.Events;
|
2019-01-23 15:57:49 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Applications;
|
2018-06-21 07:56:25 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Browser;
|
2017-07-24 17:31:28 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Configuration;
|
2017-07-31 20:22:53 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.I18n;
|
2018-03-08 15:27:12 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Logging;
|
2017-07-14 10:28:59 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.UserInterface;
|
2019-01-17 11:12:17 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.UserInterface.MessageBox;
|
2019-03-06 16:10:00 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.UserInterface.Shell;
|
2018-02-15 15:42:54 +01:00
|
|
|
|
using BrowserSettings = SafeExamBrowser.Contracts.Configuration.Settings.BrowserSettings;
|
2017-07-14 10:28:59 +02:00
|
|
|
|
|
|
|
|
|
namespace SafeExamBrowser.Browser
|
|
|
|
|
{
|
2018-06-21 07:56:25 +02:00
|
|
|
|
public class BrowserApplicationController : IBrowserApplicationController
|
2017-07-14 10:28:59 +02:00
|
|
|
|
{
|
2018-08-31 07:49:41 +02:00
|
|
|
|
private int instanceIdCounter = default(int);
|
|
|
|
|
|
2018-06-29 09:50:20 +02:00
|
|
|
|
private AppConfig appConfig;
|
2019-03-08 11:43:52 +01:00
|
|
|
|
private IList<IApplicationControl> controls;
|
2018-06-21 07:56:25 +02:00
|
|
|
|
private IList<IApplicationInstance> instances;
|
2019-01-17 11:12:17 +01:00
|
|
|
|
private IMessageBox messageBox;
|
2018-08-31 15:29:36 +02:00
|
|
|
|
private IModuleLogger logger;
|
2018-06-21 07:56:25 +02:00
|
|
|
|
private BrowserSettings settings;
|
2017-07-31 20:22:53 +02:00
|
|
|
|
private IText text;
|
2018-06-21 07:56:25 +02:00
|
|
|
|
private IUserInterfaceFactory uiFactory;
|
|
|
|
|
|
|
|
|
|
public event DownloadRequestedEventHandler ConfigurationDownloadRequested;
|
2017-07-24 17:31:28 +02:00
|
|
|
|
|
2018-01-17 14:08:39 +01:00
|
|
|
|
public BrowserApplicationController(
|
2018-06-29 09:50:20 +02:00
|
|
|
|
AppConfig appConfig,
|
2018-02-15 15:42:54 +01:00
|
|
|
|
BrowserSettings settings,
|
2019-01-17 11:12:17 +01:00
|
|
|
|
IMessageBox messageBox,
|
2018-08-31 15:29:36 +02:00
|
|
|
|
IModuleLogger logger,
|
2018-01-17 14:08:39 +01:00
|
|
|
|
IText text,
|
|
|
|
|
IUserInterfaceFactory uiFactory)
|
2017-07-24 17:31:28 +02:00
|
|
|
|
{
|
2018-06-29 09:50:20 +02:00
|
|
|
|
this.appConfig = appConfig;
|
2019-03-08 11:43:52 +01:00
|
|
|
|
this.controls = new List<IApplicationControl>();
|
2018-06-21 07:56:25 +02:00
|
|
|
|
this.instances = new List<IApplicationInstance>();
|
2018-03-08 15:27:12 +01:00
|
|
|
|
this.logger = logger;
|
2019-01-17 11:12:17 +01:00
|
|
|
|
this.messageBox = messageBox;
|
2017-07-24 17:31:28 +02:00
|
|
|
|
this.settings = settings;
|
2017-07-31 20:22:53 +02:00
|
|
|
|
this.text = text;
|
2017-07-24 17:31:28 +02:00
|
|
|
|
this.uiFactory = uiFactory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Initialize()
|
|
|
|
|
{
|
2018-03-02 15:41:04 +01:00
|
|
|
|
var cefSettings = InitializeCefSettings();
|
2019-01-22 09:07:34 +01:00
|
|
|
|
var success = Cef.Initialize(cefSettings, true, default(IApp));
|
2017-07-25 09:02:32 +02:00
|
|
|
|
|
2019-01-18 09:58:14 +01:00
|
|
|
|
logger.Info("Initialized browser.");
|
2018-08-31 07:49:41 +02:00
|
|
|
|
|
2017-07-25 09:02:32 +02:00
|
|
|
|
if (!success)
|
|
|
|
|
{
|
2019-01-18 09:58:14 +01:00
|
|
|
|
throw new Exception("Failed to initialize browser!");
|
2017-07-25 09:02:32 +02:00
|
|
|
|
}
|
2017-07-24 17:31:28 +02:00
|
|
|
|
}
|
2017-07-14 10:28:59 +02:00
|
|
|
|
|
2019-03-08 11:43:52 +01:00
|
|
|
|
public void RegisterApplicationControl(IApplicationControl control)
|
2017-07-14 10:28:59 +02:00
|
|
|
|
{
|
2019-03-08 11:43:52 +01:00
|
|
|
|
control.Clicked += ApplicationControl_Clicked;
|
|
|
|
|
controls.Add(control);
|
2017-07-14 10:28:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-15 08:45:17 +01:00
|
|
|
|
public void Start()
|
|
|
|
|
{
|
|
|
|
|
CreateNewInstance();
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-24 17:31:28 +02:00
|
|
|
|
public void Terminate()
|
|
|
|
|
{
|
2017-07-27 11:46:31 +02:00
|
|
|
|
foreach (var instance in instances)
|
|
|
|
|
{
|
2017-07-31 20:22:53 +02:00
|
|
|
|
instance.Terminated -= Instance_Terminated;
|
2017-07-27 11:46:31 +02:00
|
|
|
|
instance.Window.Close();
|
2018-08-31 07:49:41 +02:00
|
|
|
|
|
|
|
|
|
logger.Info($"Terminated browser instance {instance.Id}.");
|
2017-07-27 11:46:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-24 17:31:28 +02:00
|
|
|
|
Cef.Shutdown();
|
2018-08-31 07:49:41 +02:00
|
|
|
|
|
2019-01-18 09:58:14 +01:00
|
|
|
|
logger.Info("Terminated browser.");
|
2017-07-24 17:31:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-23 09:37:47 +01:00
|
|
|
|
private void CreateNewInstance(string url = null)
|
2017-07-28 09:12:17 +02:00
|
|
|
|
{
|
2018-08-31 07:49:41 +02:00
|
|
|
|
var id = new BrowserInstanceIdentifier(++instanceIdCounter);
|
|
|
|
|
var isMainInstance = instances.Count == 0;
|
2018-08-31 15:29:36 +02:00
|
|
|
|
var instanceLogger = logger.CloneFor($"BrowserInstance {id}");
|
2019-01-23 09:37:47 +01:00
|
|
|
|
var startUrl = url ?? settings.StartUrl;
|
|
|
|
|
var instance = new BrowserApplicationInstance(appConfig, settings, id, isMainInstance, messageBox, instanceLogger, text, uiFactory, startUrl);
|
2017-07-28 09:12:17 +02:00
|
|
|
|
|
2018-03-14 11:13:30 +01:00
|
|
|
|
instance.Initialize();
|
2018-06-21 07:56:25 +02:00
|
|
|
|
instance.ConfigurationDownloadRequested += (fileName, args) => ConfigurationDownloadRequested?.Invoke(fileName, args);
|
2019-01-17 11:12:17 +01:00
|
|
|
|
instance.PopupRequested += Instance_PopupRequested;
|
2018-03-14 11:13:30 +01:00
|
|
|
|
instance.Terminated += Instance_Terminated;
|
|
|
|
|
|
2019-03-08 11:43:52 +01:00
|
|
|
|
foreach (var control in controls)
|
|
|
|
|
{
|
|
|
|
|
control.RegisterInstance(instance);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-28 09:12:17 +02:00
|
|
|
|
instances.Add(instance);
|
2017-07-31 20:22:53 +02:00
|
|
|
|
instance.Window.Show();
|
2018-08-31 07:49:41 +02:00
|
|
|
|
|
|
|
|
|
logger.Info($"Created browser instance {instance.Id}.");
|
2017-07-28 09:12:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-02 15:41:04 +01:00
|
|
|
|
private CefSettings InitializeCefSettings()
|
|
|
|
|
{
|
2019-01-23 08:12:15 +01:00
|
|
|
|
var warning = logger.LogLevel == LogLevel.Warning;
|
|
|
|
|
var error = logger.LogLevel == LogLevel.Error;
|
2018-03-02 15:41:04 +01:00
|
|
|
|
var cefSettings = new CefSettings
|
|
|
|
|
{
|
2018-06-29 09:50:20 +02:00
|
|
|
|
CachePath = appConfig.BrowserCachePath,
|
2019-06-06 15:44:03 +02:00
|
|
|
|
LogFile = appConfig.BrowserLogFilePath,
|
2019-01-11 08:25:40 +01:00
|
|
|
|
LogSeverity = error ? LogSeverity.Error : (warning ? LogSeverity.Warning : LogSeverity.Info),
|
2019-05-16 14:41:38 +02:00
|
|
|
|
UserAgent = InitializeUserAgent()
|
2018-03-02 15:41:04 +01:00
|
|
|
|
};
|
|
|
|
|
|
2019-05-08 15:36:48 +02:00
|
|
|
|
cefSettings.CefCommandLineArgs.Add("touch-events", "enabled");
|
|
|
|
|
|
2019-01-18 09:58:14 +01:00
|
|
|
|
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}");
|
2018-08-31 07:49:41 +02:00
|
|
|
|
|
2018-03-02 15:41:04 +01:00
|
|
|
|
return cefSettings;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-08 11:43:52 +01:00
|
|
|
|
private void ApplicationControl_Clicked(InstanceIdentifier id = null)
|
2017-07-14 10:28:59 +02:00
|
|
|
|
{
|
2018-11-22 14:36:20 +01:00
|
|
|
|
if (id == null)
|
2017-07-24 17:31:28 +02:00
|
|
|
|
{
|
2018-08-31 07:49:41 +02:00
|
|
|
|
CreateNewInstance();
|
2017-07-24 17:31:28 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-08-31 07:49:41 +02:00
|
|
|
|
instances.FirstOrDefault(i => i.Id == id)?.Window?.BringToForeground();
|
2017-07-24 17:31:28 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-17 11:12:17 +01:00
|
|
|
|
private void Instance_PopupRequested(PopupRequestedEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
logger.Info($"Received request to create new instance for '{args.Url}'...");
|
2019-01-23 09:37:47 +01:00
|
|
|
|
CreateNewInstance(args.Url);
|
2019-01-17 11:12:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-31 07:49:41 +02:00
|
|
|
|
private void Instance_Terminated(InstanceIdentifier id)
|
2017-07-24 17:31:28 +02:00
|
|
|
|
{
|
2017-07-28 09:12:17 +02:00
|
|
|
|
instances.Remove(instances.FirstOrDefault(i => i.Id == id));
|
2018-08-31 07:49:41 +02:00
|
|
|
|
logger.Info($"Browser instance {id} was terminated.");
|
2017-07-14 10:28:59 +02:00
|
|
|
|
}
|
2019-05-16 14:41:38 +02:00
|
|
|
|
|
|
|
|
|
/// <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}";
|
2019-08-13 10:02:05 +02:00
|
|
|
|
var sebVersion = $"SEB/{appConfig.ProgramInformationalVersion}";
|
2019-05-16 14:41:38 +02:00
|
|
|
|
|
|
|
|
|
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}";
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-14 10:28:59 +02:00
|
|
|
|
}
|
|
|
|
|
}
|