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;
|
2017-07-24 17:31:28 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Configuration;
|
2017-07-31 20:22:53 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.I18n;
|
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
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
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-01-17 14:08:39 +01:00
|
|
|
|
IText text,
|
|
|
|
|
IUserInterfaceFactory uiFactory)
|
2017-07-24 17:31:28 +02:00
|
|
|
|
{
|
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
|
|
|
|
|
|
|
|
|
button.RegisterInstance(instance);
|
|
|
|
|
instances.Add(instance);
|
|
|
|
|
|
2018-03-08 07:35:58 +01:00
|
|
|
|
instance.ConfigurationDetected += Instance_ConfigurationDetected;
|
2017-07-31 20:22:53 +02:00
|
|
|
|
instance.Terminated += Instance_Terminated;
|
|
|
|
|
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,
|
|
|
|
|
// TODO: Set according to current application LogLevel!
|
|
|
|
|
LogSeverity = LogSeverity.Verbose
|
|
|
|
|
};
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
// TODO:
|
|
|
|
|
// 1. Ask whether reconfiguration should be attempted
|
|
|
|
|
// 2. Contact runtime and ask whether configuration valid and reconfiguration allowed
|
|
|
|
|
// - If yes, do nothing and wait for shutdown command
|
|
|
|
|
// - If no, show message box and NAVIGATE TO PREVIOUS PAGE -> but how?
|
|
|
|
|
|
|
|
|
|
var result = uiFactory.Show(TextKey.MessageBox_ReconfigureQuestion, TextKey.MessageBox_ReconfigureQuestionTitle, MessageBoxAction.YesNo, MessageBoxIcon.Question);
|
|
|
|
|
|
|
|
|
|
args.Cancel = result == MessageBoxResult.No;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|