UNPKG

7.64 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2017 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.DefaultWindowService = void 0;
19const tslib_1 = require("tslib");
20const inversify_1 = require("inversify");
21const common_1 = require("../../common");
22const core_preferences_1 = require("../core-preferences");
23const contribution_provider_1 = require("../../common/contribution-provider");
24const frontend_application_contribution_1 = require("../frontend-application-contribution");
25const window_1 = require("../../common/window");
26const dialogs_1 = require("../dialogs");
27const frontend_application_state_1 = require("../../common/frontend-application-state");
28let DefaultWindowService = class DefaultWindowService {
29 constructor() {
30 this.allowVetoes = true;
31 this.onUnloadEmitter = new common_1.Emitter();
32 }
33 get onUnload() {
34 return this.onUnloadEmitter.event;
35 }
36 onStart(app) {
37 this.frontendApplication = app;
38 this.registerUnloadListeners();
39 }
40 openNewWindow(url) {
41 window.open(url, undefined, 'noopener');
42 return undefined;
43 }
44 openNewDefaultWindow() {
45 this.openNewWindow(`#${window_1.DEFAULT_WINDOW_HASH}`);
46 }
47 focus() {
48 window.focus();
49 }
50 /**
51 * Returns a list of actions that {@link FrontendApplicationContribution}s would like to take before shutdown
52 * It is expected that this will succeed - i.e. return an empty array - at most once per session. If no vetoes are received
53 * during any cycle, no further checks will be made. In that case, shutdown should proceed unconditionally.
54 */
55 collectContributionUnloadVetoes() {
56 var _a;
57 const vetoes = [];
58 if (this.allowVetoes) {
59 const shouldConfirmExit = this.corePreferences['application.confirmExit'];
60 for (const contribution of this.contributions.getContributions()) {
61 const veto = (_a = contribution.onWillStop) === null || _a === void 0 ? void 0 : _a.call(contribution, this.frontendApplication);
62 if (veto && shouldConfirmExit !== 'never') { // Ignore vetoes if we should not prompt the user on exit.
63 if (frontend_application_contribution_1.OnWillStopAction.is(veto)) {
64 vetoes.push(veto);
65 }
66 else {
67 vetoes.push({ reason: 'No reason given', action: () => false });
68 }
69 }
70 }
71 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); });
72 if (vetoes.length === 0 && shouldConfirmExit === 'always') {
73 vetoes.push({ reason: 'application.confirmExit preference', action: () => (0, dialogs_1.confirmExit)() });
74 }
75 if (vetoes.length === 0) {
76 this.allowVetoes = false;
77 }
78 }
79 return vetoes;
80 }
81 /**
82 * Implement the mechanism to detect unloading of the page.
83 */
84 registerUnloadListeners() {
85 window.addEventListener('beforeunload', event => this.handleBeforeUnloadEvent(event));
86 // In a browser, `unload` is correctly fired when the page unloads, unlike Electron.
87 // If `beforeunload` is cancelled, the user will be prompted to leave or stay.
88 // If the user stays, the page won't be unloaded, so `unload` is not fired.
89 // If the user leaves, the page will be unloaded, so `unload` is fired.
90 window.addEventListener('unload', () => this.onUnloadEmitter.fire());
91 }
92 async isSafeToShutDown(stopReason) {
93 const vetoes = this.collectContributionUnloadVetoes();
94 if (vetoes.length === 0) {
95 return true;
96 }
97 const preparedValues = await Promise.all(vetoes.map(e => { var _a; return (_a = e.prepare) === null || _a === void 0 ? void 0 : _a.call(e, stopReason); }));
98 console.debug('Shutdown prevented by', vetoes.map(({ reason }) => reason).join(', '));
99 for (let i = 0; i < vetoes.length; i++) {
100 try {
101 const result = await vetoes[i].action(preparedValues[i], stopReason);
102 if (!result) {
103 return false;
104 }
105 }
106 catch (e) {
107 console.error(e);
108 }
109 }
110 console.debug('OnWillStop actions resolved; allowing shutdown');
111 this.allowVetoes = false;
112 return true;
113 }
114 setSafeToShutDown() {
115 this.allowVetoes = false;
116 }
117 /**
118 * Called when the `window` is about to `unload` its resources.
119 * At this point, the `document` is still visible and the [`BeforeUnloadEvent`](https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event)
120 * event will be canceled if the return value of this method is `false`.
121 *
122 * In Electron, handleCloseRequestEvent is is run instead.
123 */
124 handleBeforeUnloadEvent(event) {
125 const vetoes = this.collectContributionUnloadVetoes();
126 if (vetoes.length) {
127 // 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.
128 console.debug('Shutdown prevented by', vetoes.map(({ reason }) => reason).join(', '));
129 return this.preventUnload(event);
130 }
131 console.debug('Shutdown will proceed.');
132 }
133 /**
134 * Notify the browser that we do not want to unload.
135 *
136 * Notes:
137 * - Shows a confirmation popup in browsers.
138 * - Prevents the window from closing without confirmation in electron.
139 *
140 * @param event The beforeunload event
141 */
142 preventUnload(event) {
143 event.returnValue = '';
144 event.preventDefault();
145 return '';
146 }
147 reload() {
148 this.isSafeToShutDown(frontend_application_state_1.StopReason.Reload).then(isSafe => {
149 if (isSafe) {
150 window.location.reload();
151 }
152 });
153 }
154};
155(0, tslib_1.__decorate)([
156 (0, inversify_1.inject)(core_preferences_1.CorePreferences),
157 (0, tslib_1.__metadata)("design:type", Object)
158], DefaultWindowService.prototype, "corePreferences", void 0);
159(0, tslib_1.__decorate)([
160 (0, inversify_1.inject)(contribution_provider_1.ContributionProvider),
161 (0, inversify_1.named)(frontend_application_contribution_1.FrontendApplicationContribution),
162 (0, tslib_1.__metadata)("design:type", Object)
163], DefaultWindowService.prototype, "contributions", void 0);
164DefaultWindowService = (0, tslib_1.__decorate)([
165 (0, inversify_1.injectable)()
166], DefaultWindowService);
167exports.DefaultWindowService = DefaultWindowService;
168//# sourceMappingURL=default-window-service.js.map
\No newline at end of file