forked from adobe/brackets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstallExtensionDialog.js
More file actions
438 lines (386 loc) · 18.2 KB
/
InstallExtensionDialog.js
File metadata and controls
438 lines (386 loc) · 18.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
* Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, window, $, brackets, Mustache, document */
/*unittests: Install Extension Dialog*/
define(function (require, exports, module) {
"use strict";
var Dialogs = require("widgets/Dialogs"),
StringUtils = require("utils/StringUtils"),
Strings = require("strings"),
Commands = require("command/Commands"),
CommandManager = require("command/CommandManager"),
FileSystem = require("filesystem/FileSystem"),
KeyEvent = require("utils/KeyEvent"),
Package = require("extensibility/Package"),
NativeApp = require("utils/NativeApp"),
InstallDialogTemplate = require("text!htmlContent/install-extension-dialog.html");
var STATE_CLOSED = 0,
STATE_START = 1,
STATE_VALID_URL = 2,
STATE_INSTALLING = 3,
STATE_INSTALLED = 4,
STATE_INSTALL_FAILED = 5,
STATE_CANCELING_INSTALL = 6,
STATE_CANCELING_HUNG = 7,
STATE_INSTALL_CANCELED = 8,
STATE_ALREADY_INSTALLED = 9,
STATE_OVERWRITE_CONFIRMED = 10,
STATE_NEEDS_UPDATE = 11;
/**
* @constructor
* Creates a new extension installer dialog.
* @param {{install: function(url), cancel: function()}} installer The installer backend to use.
*/
function InstallExtensionDialog(installer, _isUpdate) {
this._installer = installer;
this._state = STATE_CLOSED;
this._installResult = null;
this._isUpdate = _isUpdate;
// Timeout before we allow user to leave STATE_INSTALL_CANCELING without waiting for a resolution
// (per-instance so we can poke it for unit testing)
this._cancelTimeout = 10 * 1000;
}
/** @type {jQuery} The dialog root. */
InstallExtensionDialog.prototype.$dlg = null;
/** @type {jQuery} The url input field. */
InstallExtensionDialog.prototype.$url = null;
/** @type {jQuery} The ok button. */
InstallExtensionDialog.prototype.$okButton = null;
/** @type {jQuery} The cancel button. */
InstallExtensionDialog.prototype.$cancelButton = null;
/** @type {jQuery} The area containing the url input label and field. */
InstallExtensionDialog.prototype.$inputArea = null;
/** @type {jQuery} The area containing the installation message and spinner. */
InstallExtensionDialog.prototype.$msgArea = null;
/** @type {jQuery} The span containing the installation message. */
InstallExtensionDialog.prototype.$msg = null;
/** @type {jQuery} The "Browse Extensions" button. */
InstallExtensionDialog.prototype.$browseExtensionsButton = null;
/** @type {$.Deferred} A deferred that's resolved/rejected when the dialog is closed and
something has/hasn't been installed successfully. */
InstallExtensionDialog.prototype._dialogDeferred = null;
/** @type {{install: function(url), cancel: function()}} installer The installer backend for this dialog. */
InstallExtensionDialog.prototype._installer = null;
/** @type {number} The current state of the dialog; one of the STATE_* constants above. */
InstallExtensionDialog.prototype._state = null;
/**
* @private
* Transitions the dialog into a new state as the installation proceeds.
* @param {number} newState The state to transition into; one of the STATE_* variables.
*/
InstallExtensionDialog.prototype._enterState = function (newState) {
var url,
msg,
self = this,
prevState = this._state;
// Store the new state up front in case some of the processing below ends up changing
// the state again immediately.
this._state = newState;
switch (newState) {
case STATE_START:
// This should match the default appearance of the dialog when it first opens.
this.$msg.find(".spinner").remove();
this.$msgArea.hide();
this.$inputArea.show();
this.$okButton
.prop("disabled", true)
.text(Strings.INSTALL);
break;
case STATE_VALID_URL:
this.$okButton.prop("disabled", false);
break;
case STATE_INSTALLING:
url = $.trim(this.$url.val());
this.$inputArea.hide();
this.$browseExtensionsButton.hide();
this.$msg.text(StringUtils.format(Strings.INSTALLING_FROM, url))
.append("<span class='spinner spin'/>");
this.$msgArea.show();
this.$okButton.prop("disabled", true);
this._installer.install(url)
.done(function (result) {
self._installResult = result;
if (result.installationStatus === Package.InstallationStatuses.ALREADY_INSTALLED ||
result.installationStatus === Package.InstallationStatuses.OLDER_VERSION ||
result.installationStatus === Package.InstallationStatuses.SAME_VERSION) {
self._enterState(STATE_ALREADY_INSTALLED);
} else if (result.installationStatus === Package.InstallationStatuses.NEEDS_UPDATE) {
self._enterState(STATE_NEEDS_UPDATE);
} else {
self._enterState(STATE_INSTALLED);
}
})
.fail(function (err) {
// If the "failure" is actually a user-requested cancel, don't show an error UI
if (err === "CANCELED") {
console.assert(self._state === STATE_CANCELING_INSTALL || self._state === STATE_CANCELING_HUNG);
self._enterState(STATE_INSTALL_CANCELED);
} else {
self._errorMessage = Package.formatError(err);
self._enterState(STATE_INSTALL_FAILED);
}
});
break;
case STATE_CANCELING_INSTALL:
// This should call back the STATE_INSTALLING fail() handler above, unless it's too late to cancel
// in which case we'll still jump to STATE_INSTALLED after this
this.$cancelButton.prop("disabled", true);
this.$msg.text(Strings.CANCELING_INSTALL);
this._installer.cancel();
window.setTimeout(function () {
if (self._state === STATE_CANCELING_INSTALL) {
self._enterState(STATE_CANCELING_HUNG);
}
}, this._cancelTimeout);
break;
case STATE_CANCELING_HUNG:
this.$msg.text(Strings.CANCELING_HUNG);
this.$okButton
.removeAttr("disabled")
.text(Strings.CLOSE);
break;
case STATE_INSTALLED:
case STATE_INSTALL_FAILED:
case STATE_INSTALL_CANCELED:
case STATE_NEEDS_UPDATE:
if (newState === STATE_INSTALLED) {
msg = Strings.INSTALL_SUCCEEDED;
} else if (newState === STATE_INSTALL_FAILED) {
msg = Strings.INSTALL_FAILED;
} else if (newState === STATE_NEEDS_UPDATE) {
msg = Strings.EXTENSION_UPDATE_INSTALLED;
} else {
msg = Strings.INSTALL_CANCELED;
}
this.$msg.html($("<strong/>").text(msg));
if (this._errorMessage) {
this.$msg.append($("<p/>").text(this._errorMessage));
}
this.$okButton
.removeAttr("disabled")
.text(Strings.CLOSE);
this.$cancelButton.hide();
break;
case STATE_ALREADY_INSTALLED:
var installResult = this._installResult;
var status = installResult.installationStatus;
var msgText = Strings["EXTENSION_" + status];
if (status === Package.InstallationStatuses.OLDER_VERSION) {
msgText = StringUtils.format(msgText, installResult.metadata.version, installResult.installedVersion);
}
this.$msg.text(msgText);
this.$okButton
.prop("disabled", false)
.text(Strings.OVERWRITE);
break;
case STATE_OVERWRITE_CONFIRMED:
this._enterState(STATE_CLOSED);
break;
case STATE_CLOSED:
$(document.body).off(".installDialog");
// Only resolve as successful if we actually installed something.
Dialogs.cancelModalDialogIfOpen("install-extension-dialog");
if (prevState === STATE_INSTALLED || prevState === STATE_NEEDS_UPDATE ||
prevState === STATE_OVERWRITE_CONFIRMED) {
this._dialogDeferred.resolve(this._installResult);
} else {
this._dialogDeferred.reject();
}
break;
}
};
/**
* @private
* Handle a click on the Cancel button, which either cancels an ongoing installation (leaving
* the dialog open), or closes the dialog if no installation is in progress.
*/
InstallExtensionDialog.prototype._handleCancel = function () {
if (this._state === STATE_INSTALLING) {
this._enterState(STATE_CANCELING_INSTALL);
} else if (this._state === STATE_ALREADY_INSTALLED) {
// If we were prompting the user about overwriting a previous installation,
// and the user cancels, we can delete the downloaded file.
if (this._installResult && this._installResult.localPath) {
var filename = this._installResult.localPath;
FileSystem.getFileForPath(filename).unlink();
}
this._enterState(STATE_CLOSED);
} else if (this._state !== STATE_CANCELING_INSTALL) {
this._enterState(STATE_CLOSED);
}
};
/**
* @private
* Handle a click on the default button, which is "Install" while we're waiting for the
* user to enter a URL, and "Close" once we've successfully finished installation.
*/
InstallExtensionDialog.prototype._handleOk = function () {
if (this._state === STATE_INSTALLED ||
this._state === STATE_INSTALL_FAILED ||
this._state === STATE_INSTALL_CANCELED ||
this._state === STATE_CANCELING_HUNG ||
this._state === STATE_NEEDS_UPDATE) {
// In these end states, this is a "Close" button: just close the dialog and indicate
// success.
this._enterState(STATE_CLOSED);
} else if (this._state === STATE_VALID_URL) {
this._enterState(STATE_INSTALLING);
} else if (this._state === STATE_ALREADY_INSTALLED) {
this._enterState(STATE_OVERWRITE_CONFIRMED);
}
};
/**
* @private
* Handle key up events on the document. We use this to detect the Esc key.
*/
InstallExtensionDialog.prototype._handleKeyUp = function (e) {
if (e.keyCode === KeyEvent.DOM_VK_ESCAPE) {
this._handleCancel();
}
};
/**
* @private
* Handle typing in the URL field.
*/
InstallExtensionDialog.prototype._handleUrlInput = function (e) {
var url = this.$url.val(),
trimmedUrl = $.trim(url),
valid = (trimmedUrl !== "");
if (!valid && this._state === STATE_VALID_URL) {
this._enterState(STATE_START);
} else if (valid && this._state === STATE_START) {
this._enterState(STATE_VALID_URL);
}
};
/**
* @private
* Closes the dialog if it's not already closed. For unit testing only.
*/
InstallExtensionDialog.prototype._close = function () {
if (this._state !== STATE_CLOSED) {
this._enterState(STATE_CLOSED);
}
};
/**
* Initialize and show the dialog.
* @param {string=} urlToInstall If specified, immediately starts installing the given file as if the user had
* specified it.
* @return {$.Promise} A promise object that will be resolved when the selected extension
* has finished installing, or rejected if the dialog is cancelled.
*/
InstallExtensionDialog.prototype.show = function (urlToInstall) {
if (this._state !== STATE_CLOSED) {
// Somehow the dialog got invoked twice. Just ignore this.
return this._dialogDeferred.promise();
}
var context = {
Strings: Strings,
isUpdate: this._isUpdate,
includeBrowseExtensions: !!brackets.config.extension_listing_url
};
// We ignore the promise returned by showModalDialogUsingTemplate, since we're managing the
// lifecycle of the dialog ourselves.
Dialogs.showModalDialogUsingTemplate(Mustache.render(InstallDialogTemplate, context), false);
this.$dlg = $(".install-extension-dialog.instance");
this.$url = this.$dlg.find(".url").focus();
this.$okButton = this.$dlg.find(".dialog-button[data-button-id='ok']");
this.$cancelButton = this.$dlg.find(".dialog-button[data-button-id='cancel']");
this.$inputArea = this.$dlg.find(".input-field");
this.$msgArea = this.$dlg.find(".message-field");
this.$msg = this.$msgArea.find(".message");
this.$browseExtensionsButton = this.$dlg.find(".browse-extensions");
this.$okButton.on("click", this._handleOk.bind(this));
this.$cancelButton.on("click", this._handleCancel.bind(this));
this.$url.on("input", this._handleUrlInput.bind(this));
this.$browseExtensionsButton.on("click", function () {
NativeApp.openURLInDefaultBrowser(brackets.config.extension_listing_url);
});
$(document.body).on("keyup.installDialog", this._handleKeyUp.bind(this));
this._enterState(STATE_START);
if (urlToInstall) {
// Act as if the user had manually entered the URL.
this.$url.val(urlToInstall);
this._enterState(STATE_VALID_URL);
this._enterState(STATE_INSTALLING);
}
this._dialogDeferred = new $.Deferred();
return this._dialogDeferred.promise();
};
/** Mediates between this module and the Package extension-installation utils. Mockable for unit-testing. */
function InstallerFacade() { }
InstallerFacade.prototype.install = function (url) {
if (this.pendingInstall) {
console.error("Extension installation already pending");
return new $.Deferred().reject("DOWNLOAD_ID_IN_USE").promise();
}
this.pendingInstall = Package.installFromURL(url);
// Store now since we'll null pendingInstall immediately if the promise was resolved synchronously
var promise = this.pendingInstall.promise;
var self = this;
this.pendingInstall.promise.always(function () {
self.pendingInstall = null;
});
return promise;
};
InstallerFacade.prototype.cancel = function () {
this.pendingInstall.cancel();
};
/**
* @private
* Show a dialog that allows the user to enter the URL of an extension ZIP file to install.
* @return {$.Promise} A promise object that will be resolved when the selected extension
* has finished installing, or rejected if the dialog is cancelled.
*/
function showDialog() {
var dlg = new InstallExtensionDialog(new InstallerFacade());
return dlg.show();
}
/**
* @private
* Show the installation dialog and automatically begin installing the given URL.
* @param {string=} urlToInstall If specified, immediately starts installing the given file as if the user had
* specified it.
* @return {$.Promise} A promise object that will be resolved when the selected extension
* has finished installing, or rejected if the dialog is cancelled.
*/
function installUsingDialog(urlToInstall, _isUpdate) {
var dlg = new InstallExtensionDialog(new InstallerFacade(), _isUpdate);
return dlg.show(urlToInstall);
}
/**
* @private
* Show the update dialog and automatically begin downloading the update from the given URL.
* @param {string} urlToUpdate URL to download
* @return {$.Promise} A promise object that will be resolved when the selected extension
* has finished downloading, or rejected if the dialog is cancelled.
*/
function updateUsingDialog(urlToUpdate) {
return installUsingDialog(urlToUpdate, true);
}
exports.showDialog = showDialog;
exports.installUsingDialog = installUsingDialog;
exports.updateUsingDialog = updateUsingDialog;
// Exposed for unit testing only
exports._Dialog = InstallExtensionDialog;
});