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;
|
2018-03-08 07:35:58 +01:00
|
|
|
|
using System.ComponentModel;
|
2017-07-24 17:31:28 +02:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using CefSharp;
|
2018-03-02 15:41:04 +01:00
|
|
|
|
using SafeExamBrowser.Browser.Handlers;
|
2017-07-14 10:28:59 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Behaviour;
|
2018-03-15 14:32:07 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Communication.Proxies;
|
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;
|
2018-03-14 12:07:20 +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
|
|
|
|
|
{
|
|
|
|
|
public class BrowserApplicationController : IApplicationController
|
|
|
|
|
{
|
2017-08-15 15:30:31 +02:00
|
|
|
|
private IApplicationButton button;
|
2017-07-24 17:31:28 +02:00
|
|
|
|
private IList<IApplicationInstance> instances = new List<IApplicationInstance>();
|
2018-02-15 15:42:54 +01:00
|
|
|
|
private BrowserSettings settings;
|
2018-03-08 15:27:12 +01:00
|
|
|
|
private ILogger logger;
|
2018-03-14 12:07:20 +01:00
|
|
|
|
private IMessageBox messageBox;
|
2018-03-08 15:27:12 +01:00
|
|
|
|
private IRuntimeProxy runtime;
|
2018-02-15 15:42:54 +01:00
|
|
|
|
private RuntimeInfo runtimeInfo;
|
2017-07-27 14:45:54 +02:00
|
|
|
|
private IUserInterfaceFactory uiFactory;
|
2017-07-31 20:22:53 +02:00
|
|
|
|
private IText text;
|
2017-07-24 17:31:28 +02:00
|
|
|
|
|
2018-01-17 14:08:39 +01:00
|
|
|
|
public BrowserApplicationController(
|
2018-02-15 15:42:54 +01:00
|
|
|
|
BrowserSettings settings,
|
|
|
|
|
RuntimeInfo runtimeInfo,
|
2018-03-08 15:27:12 +01:00
|
|
|
|
ILogger logger,
|
2018-03-14 12:07:20 +01:00
|
|
|
|
IMessageBox messageBox,
|
2018-03-08 15:27:12 +01:00
|
|
|
|
IRuntimeProxy runtime,
|
2018-01-17 14:08:39 +01:00
|
|
|
|
IText text,
|
|
|
|
|
IUserInterfaceFactory uiFactory)
|
2017-07-24 17:31:28 +02:00
|
|
|
|
{
|
2018-03-08 15:27:12 +01:00
|
|
|
|
this.logger = logger;
|
2018-03-14 12:07:20 +01:00
|
|
|
|
this.messageBox = messageBox;
|
2018-03-08 15:27:12 +01:00
|
|
|
|
this.runtime = runtime;
|
2018-01-17 14:08:39 +01:00
|
|
|
|
this.runtimeInfo = runtimeInfo;
|
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);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-24 17:31:28 +02:00
|
|
|
|
Cef.Shutdown();
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-28 09:12:17 +02:00
|
|
|
|
private void CreateNewInstance()
|
|
|
|
|
{
|
2018-01-17 08:26:44 +01:00
|
|
|
|
var instance = new BrowserApplicationInstance(settings, text, uiFactory, instances.Count == 0);
|
2017-07-28 09:12:17 +02:00
|
|
|
|
|
2018-03-14 11:13:30 +01:00
|
|
|
|
instance.Initialize();
|
|
|
|
|
instance.ConfigurationDetected += Instance_ConfigurationDetected;
|
|
|
|
|
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();
|
2017-07-28 09:12:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-02 15:41:04 +01:00
|
|
|
|
private CefSettings InitializeCefSettings()
|
|
|
|
|
{
|
|
|
|
|
var cefSettings = new CefSettings
|
|
|
|
|
{
|
|
|
|
|
CachePath = runtimeInfo.BrowserCachePath,
|
|
|
|
|
LogFile = runtimeInfo.BrowserLogFile,
|
2018-04-25 07:47:48 +02:00
|
|
|
|
// TODO: Set according to current application LogLevel, but avoid verbose!
|
|
|
|
|
LogSeverity = LogSeverity.Info
|
2018-03-02 15:41:04 +01:00
|
|
|
|
};
|
|
|
|
|
|
2018-03-08 07:35:58 +01:00
|
|
|
|
cefSettings.RegisterScheme(new CefCustomScheme { SchemeName = "seb", SchemeHandlerFactory = new SchemeHandlerFactory() });
|
|
|
|
|
cefSettings.RegisterScheme(new CefCustomScheme { SchemeName = "sebs", SchemeHandlerFactory = new SchemeHandlerFactory() });
|
2018-03-02 15:41:04 +01:00
|
|
|
|
|
|
|
|
|
return cefSettings;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-28 09:12:17 +02:00
|
|
|
|
private void Button_OnClick(Guid? instanceId = null)
|
2017-07-14 10:28:59 +02:00
|
|
|
|
{
|
2017-07-24 17:31:28 +02:00
|
|
|
|
if (instanceId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
instances.FirstOrDefault(i => i.Id == instanceId)?.Window?.BringToForeground();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CreateNewInstance();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-08 07:35:58 +01:00
|
|
|
|
private void Instance_ConfigurationDetected(string url, CancelEventArgs args)
|
|
|
|
|
{
|
2018-03-14 12:07:20 +01:00
|
|
|
|
var result = messageBox.Show(TextKey.MessageBox_ReconfigurationQuestion, TextKey.MessageBox_ReconfigurationQuestionTitle, MessageBoxAction.YesNo, MessageBoxIcon.Question);
|
2018-03-08 15:27:12 +01:00
|
|
|
|
var reconfigure = result == MessageBoxResult.Yes;
|
|
|
|
|
var allowed = false;
|
2018-03-08 07:35:58 +01:00
|
|
|
|
|
2018-03-08 15:27:12 +01:00
|
|
|
|
logger.Info($"Detected configuration request for '{url}'. The user chose to {(reconfigure ? "start" : "abort")} the reconfiguration.");
|
2018-03-08 07:35:58 +01:00
|
|
|
|
|
2018-03-08 15:27:12 +01:00
|
|
|
|
if (reconfigure)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
allowed = runtime.RequestReconfiguration(url);
|
|
|
|
|
logger.Info($"The runtime {(allowed ? "accepted" : "denied")} the reconfiguration request.");
|
|
|
|
|
|
|
|
|
|
if (!allowed)
|
|
|
|
|
{
|
2018-03-14 12:07:20 +01:00
|
|
|
|
messageBox.Show(TextKey.MessageBox_ReconfigurationDenied, TextKey.MessageBox_ReconfigurationDeniedTitle);
|
2018-03-08 15:27:12 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
logger.Error("Failed to communicate the reconfiguration request to the runtime!", e);
|
2018-03-14 12:07:20 +01:00
|
|
|
|
messageBox.Show(TextKey.MessageBox_ReconfigurationError, TextKey.MessageBox_ReconfigurationErrorTitle, icon: MessageBoxIcon.Error);
|
2018-03-08 15:27:12 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
args.Cancel = !allowed;
|
2018-03-08 07:35:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-31 20:22:53 +02:00
|
|
|
|
private void Instance_Terminated(Guid 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));
|
2017-07-14 10:28:59 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|