UNPKG

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