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;
|
2018-06-21 07:56:25 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Browser;
|
2017-07-24 17:31:28 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Configuration;
|
2018-08-31 15:29:36 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Core;
|
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;
|
2017-08-15 15:30:31 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.UserInterface.Taskbar;
|
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;
|
2017-08-15 15:30:31 +02:00
|
|
|
|
private IApplicationButton button;
|
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;
|
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();
|
2017-07-25 09:02:32 +02:00
|
|
|
|
var success = Cef.Initialize(cefSettings, true, null);
|
|
|
|
|
|
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
|
|
|
|
|
2017-08-15 15:30:31 +02:00
|
|
|
|
public void RegisterApplicationButton(IApplicationButton button)
|
2017-07-14 10:28:59 +02:00
|
|
|
|
{
|
|
|
|
|
this.button = button;
|
2017-08-02 08:31:12 +02:00
|
|
|
|
this.button.Clicked += Button_OnClick;
|
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-17 11:12:17 +01:00
|
|
|
|
private void CreateNewInstance(BrowserSettings custom = 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-17 11:12:17 +01:00
|
|
|
|
var instanceSettings = custom ?? settings;
|
|
|
|
|
var instance = new BrowserApplicationInstance(appConfig, instanceSettings, id, isMainInstance, messageBox, instanceLogger, text, uiFactory);
|
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;
|
|
|
|
|
|
2017-07-28 09:12:17 +02:00
|
|
|
|
button.RegisterInstance(instance);
|
|
|
|
|
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()
|
|
|
|
|
{
|
2018-08-31 07:49:41 +02:00
|
|
|
|
var warning = appConfig.LogLevel == LogLevel.Warning;
|
|
|
|
|
var error = appConfig.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,
|
|
|
|
|
LogFile = appConfig.BrowserLogFile,
|
2019-01-11 08:25:40 +01:00
|
|
|
|
LogSeverity = error ? LogSeverity.Error : (warning ? LogSeverity.Warning : LogSeverity.Info),
|
|
|
|
|
UserAgent = settings.UseCustomUserAgent ? settings.CustomUserAgent : string.Empty
|
2018-03-02 15:41:04 +01:00
|
|
|
|
};
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-31 07:49:41 +02:00
|
|
|
|
private void Button_OnClick(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)
|
|
|
|
|
{
|
2019-01-18 16:11:33 +01:00
|
|
|
|
// TODO: Use settings for additional browser windows!
|
2019-01-17 11:12:17 +01:00
|
|
|
|
var popupSettings = new BrowserSettings
|
|
|
|
|
{
|
|
|
|
|
AllowAddressBar = false,
|
|
|
|
|
AllowBackwardNavigation = false,
|
|
|
|
|
AllowConfigurationDownloads = settings.AllowConfigurationDownloads,
|
|
|
|
|
AllowDeveloperConsole = settings.AllowDeveloperConsole,
|
|
|
|
|
AllowDownloads = settings.AllowDownloads,
|
|
|
|
|
AllowForwardNavigation = false,
|
|
|
|
|
AllowPageZoom = settings.AllowPageZoom,
|
|
|
|
|
AllowPopups = settings.AllowPopups,
|
|
|
|
|
AllowReloading = settings.AllowReloading,
|
|
|
|
|
ShowReloadWarning = settings.ShowReloadWarning,
|
|
|
|
|
StartUrl = args.Url
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
logger.Info($"Received request to create new instance for '{args.Url}'...");
|
|
|
|
|
CreateNewInstance(popupSettings);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|