2017-07-14 10:28:59 +02:00
|
|
|
|
/*
|
2018-01-16 08:24:00 +01:00
|
|
|
|
* Copyright (c) 2018 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;
|
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;
|
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;
|
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,
|
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;
|
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);
|
|
|
|
|
|
2018-08-31 07:49:41 +02:00
|
|
|
|
logger.Info("Initialized CEF.");
|
|
|
|
|
|
2017-07-25 09:02:32 +02:00
|
|
|
|
if (!success)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Failed to initialize the browser engine!");
|
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
|
logger.Info("Terminated CEF.");
|
2017-07-24 17:31:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-28 09:12:17 +02:00
|
|
|
|
private void CreateNewInstance()
|
|
|
|
|
{
|
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}");
|
|
|
|
|
var instance = new BrowserApplicationInstance(appConfig, settings, id, isMainInstance, 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);
|
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,
|
2018-08-31 07:49:41 +02:00
|
|
|
|
LogSeverity = error ? LogSeverity.Error : (warning ? LogSeverity.Warning : LogSeverity.Info)
|
2018-03-02 15:41:04 +01:00
|
|
|
|
};
|
|
|
|
|
|
2018-08-31 07:49:41 +02:00
|
|
|
|
logger.Debug($"CEF cache path is '{cefSettings.CachePath}'.");
|
|
|
|
|
logger.Debug($"CEF log file is '{cefSettings.LogFile}'.");
|
|
|
|
|
logger.Debug($"CEF log severity is '{cefSettings.LogSeverity}'.");
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|