@theia/core
Version:
Theia is a cloud & desktop IDE framework implemented in TypeScript.
173 lines • 8.19 kB
JavaScript
// *****************************************************************************
// Copyright (C) 2017 TypeFox and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
// *****************************************************************************
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultWindowService = void 0;
const inversify_1 = require("inversify");
const common_1 = require("../../common");
const core_preferences_1 = require("../core-preferences");
const contribution_provider_1 = require("../../common/contribution-provider");
const frontend_application_1 = require("../frontend-application");
const window_1 = require("../../common/window");
const dialogs_1 = require("../dialogs");
const frontend_application_state_1 = require("../../common/frontend-application-state");
let DefaultWindowService = class DefaultWindowService {
constructor() {
this.allowVetoes = true;
this.onUnloadEmitter = new common_1.Emitter();
}
get onUnload() {
return this.onUnloadEmitter.event;
}
onStart(app) {
this.frontendApplication = app;
this.registerUnloadListeners();
}
openNewWindow(url) {
window.open(url, undefined, 'noopener');
return undefined;
}
openNewDefaultWindow() {
this.openNewWindow(`#${window_1.DEFAULT_WINDOW_HASH}`);
}
/**
* Returns a list of actions that {@link FrontendApplicationContribution}s would like to take before shutdown
* It is expected that this will succeed - i.e. return an empty array - at most once per session. If no vetoes are received
* during any cycle, no further checks will be made. In that case, shutdown should proceed unconditionally.
*/
collectContributionUnloadVetoes() {
var _a;
const vetoes = [];
if (this.allowVetoes) {
const shouldConfirmExit = this.corePreferences['application.confirmExit'];
for (const contribution of this.contributions.getContributions()) {
const veto = (_a = contribution.onWillStop) === null || _a === void 0 ? void 0 : _a.call(contribution, this.frontendApplication);
if (veto && shouldConfirmExit !== 'never') { // Ignore vetoes if we should not prompt the user on exit.
if (frontend_application_1.OnWillStopAction.is(veto)) {
vetoes.push(veto);
}
else {
vetoes.push({ reason: 'No reason given', action: () => false });
}
}
}
vetoes.sort((a, b) => { var _a, _b; return ((_a = a.priority) !== null && _a !== void 0 ? _a : -Infinity) - ((_b = b.priority) !== null && _b !== void 0 ? _b : -Infinity); });
if (vetoes.length === 0 && shouldConfirmExit === 'always') {
vetoes.push({ reason: 'application.confirmExit preference', action: () => (0, dialogs_1.confirmExit)() });
}
if (vetoes.length === 0) {
this.allowVetoes = false;
}
}
return vetoes;
}
/**
* Implement the mechanism to detect unloading of the page.
*/
registerUnloadListeners() {
window.addEventListener('beforeunload', event => this.handleBeforeUnloadEvent(event));
// In a browser, `unload` is correctly fired when the page unloads, unlike Electron.
// If `beforeunload` is cancelled, the user will be prompted to leave or stay.
// If the user stays, the page won't be unloaded, so `unload` is not fired.
// If the user leaves, the page will be unloaded, so `unload` is fired.
window.addEventListener('unload', () => this.onUnloadEmitter.fire());
}
async isSafeToShutDown(stopReason) {
const vetoes = this.collectContributionUnloadVetoes();
if (vetoes.length === 0) {
return true;
}
const preparedValues = await Promise.all(vetoes.map(e => { var _a; return (_a = e.prepare) === null || _a === void 0 ? void 0 : _a.call(e, stopReason); }));
console.debug('Shutdown prevented by', vetoes.map(({ reason }) => reason).join(', '));
for (let i = 0; i < vetoes.length; i++) {
try {
const result = await vetoes[i].action(preparedValues[i], stopReason);
if (!result) {
return false;
}
}
catch (e) {
console.error(e);
}
}
console.debug('OnWillStop actions resolved; allowing shutdown');
this.allowVetoes = false;
return true;
}
setSafeToShutDown() {
this.allowVetoes = false;
}
/**
* Called when the `window` is about to `unload` its resources.
* At this point, the `document` is still visible and the [`BeforeUnloadEvent`](https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event)
* event will be canceled if the return value of this method is `false`.
*
* In Electron, handleCloseRequestEvent is is run instead.
*/
handleBeforeUnloadEvent(event) {
const vetoes = this.collectContributionUnloadVetoes();
if (vetoes.length) {
// In the browser, we don't call the functions because this has to finish in a single tick, so we treat any desired action as a veto.
console.debug('Shutdown prevented by', vetoes.map(({ reason }) => reason).join(', '));
return this.preventUnload(event);
}
console.debug('Shutdown will proceed.');
}
/**
* Notify the browser that we do not want to unload.
*
* Notes:
* - Shows a confirmation popup in browsers.
* - Prevents the window from closing without confirmation in electron.
*
* @param event The beforeunload event
*/
preventUnload(event) {
event.returnValue = '';
event.preventDefault();
return '';
}
reload() {
this.isSafeToShutDown(frontend_application_state_1.StopReason.Reload).then(isSafe => {
if (isSafe) {
window.location.reload();
}
});
}
};
__decorate([
(0, inversify_1.inject)(core_preferences_1.CorePreferences),
__metadata("design:type", Object)
], DefaultWindowService.prototype, "corePreferences", void 0);
__decorate([
(0, inversify_1.inject)(contribution_provider_1.ContributionProvider),
(0, inversify_1.named)(frontend_application_1.FrontendApplicationContribution),
__metadata("design:type", Object)
], DefaultWindowService.prototype, "contributions", void 0);
DefaultWindowService = __decorate([
(0, inversify_1.injectable)()
], DefaultWindowService);
exports.DefaultWindowService = DefaultWindowService;
//# sourceMappingURL=default-window-service.js.map
;