This repository was archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Expand file tree
/
Copy pathUpdateNotification.js
More file actions
341 lines (297 loc) · 14.3 KB
/
UpdateNotification.js
File metadata and controls
341 lines (297 loc) · 14.3 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
/*
* 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, regexp: true, indent: 4, maxerr: 50 */
/*global define, $, brackets, PathUtils, window, Mustache */
/**
* Utilities functions for displaying update notifications
*
*/
define(function (require, exports, module) {
"use strict";
var Dialogs = require("widgets/Dialogs"),
DefaultDialogs = require("widgets/DefaultDialogs"),
PreferencesManager = require("preferences/PreferencesManager"),
AppInit = require("utils/AppInit"),
Global = require("utils/Global"),
NativeApp = require("utils/NativeApp"),
StringUtils = require("utils/StringUtils"),
Strings = require("strings"),
UpdateDialogTemplate = require("text!htmlContent/update-dialog.html"),
UpdateListTemplate = require("text!htmlContent/update-list.html");
var defaultPrefs = {lastNotifiedBuildNumber: 0};
// Extract current build number from package.json version field 0.0.0-0
var _buildNumber = Number(/-([0-9]+)/.exec(brackets.metadata.version)[1]);
// PreferenceStorage
var _prefs = PreferencesManager.getPreferenceStorage(module, defaultPrefs);
//TODO: Remove preferences migration code
PreferencesManager.handleClientIdChange(_prefs, module.id);
// This is the last version we notified the user about. If checkForUpdate()
// is called with "false", only show the update notification dialog if there
// is an update newer than this one. This value is saved in preferences.
var _lastNotifiedBuildNumber = _prefs.getValue("lastNotifiedBuildNumber");
// Last time the versionInfoURL was fetched
var _lastInfoURLFetchTime = _prefs.getValue("lastInfoURLFetchTime");
// URL to load version info from. By default this is loaded no more than once a day. If
// you force an update check it is always loaded.
// URL to fetch the version information.
var _versionInfoURL;
// Information on all posted builds of Brackets. This is an Array, where each element is
// an Object with the following fields:
//
// {Number} buildNumber Number of the build
// {String} versionString String representation of the build number (ie "Sprint 14")
// {String} dateString Date of the build
// {String} releaseNotesURL URL of the release notes for this build
// {String} downloadURL URL to download this build
// {Array} newFeatures Array of new features in this build. Each entry has two fields:
// {String} name Name of the feature
// {String} description Description of the feature
//
// This array must be reverse sorted by buildNumber (newest build info first)
/**
* @private
* Flag that indicates if we've added a click handler to the update notification icon.
*/
var _addedClickHandler = false;
/**
* Get a data structure that has information for all builds of Brackets.
*
* If force is true, the information is always fetched from _versionInfoURL.
* If force is false, we try to use cached information. If more than
* 24 hours have passed since the last fetch, or if cached data can't be found,
* the data is fetched again.
*
* If new data is fetched and dontCache is false, the data is saved in preferences
* for quick fetching later.
*/
function _getUpdateInformation(force, dontCache) {
var result = new $.Deferred();
var fetchData = false;
var data;
// If force is true, always fetch
if (force) {
fetchData = true;
}
// If we don't have data saved in prefs, fetch
data = _prefs.getValue("updateInfo");
if (!data) {
fetchData = true;
}
// If more than 24 hours have passed since our last fetch, fetch again
if ((new Date()).getTime() > _lastInfoURLFetchTime + (1000 * 60 * 60 * 24)) {
fetchData = true;
}
if (fetchData) {
$.ajax(_versionInfoURL, {
dataType: "text",
cache: false,
complete: function (jqXHR, status) {
if (status === "success") {
try {
data = JSON.parse(jqXHR.responseText);
if (!dontCache) {
_lastInfoURLFetchTime = (new Date()).getTime();
_prefs.setValue("lastInfoURLFetchTime", _lastInfoURLFetchTime);
_prefs.setValue("updateInfo", data);
}
result.resolve(data);
} catch (e) {
console.log("Error parsing version information");
console.log(e);
result.reject();
}
}
},
error: function (jqXHR, status, error) {
// When loading data for unit tests, the error handler is
// called but the responseText is valid. Try to use it here,
// but *don't* save the results in prefs.
if (!jqXHR.responseText) {
// Text is NULL or empty string, reject().
result.reject();
return;
}
try {
data = JSON.parse(jqXHR.responseText);
result.resolve(data);
} catch (e) {
result.reject();
}
}
});
} else {
result.resolve(data);
}
return result.promise();
}
/**
* Return a new array of version information that is newer than "buildNumber".
* Returns null if there is no new version information.
*/
function _stripOldVersionInfo(versionInfo, buildNumber) {
// Do a simple linear search. Since we are going in reverse-chronological order, we
// should get through the search quickly.
var lastIndex = 0;
var len = versionInfo.length;
while (lastIndex < len) {
if (versionInfo[lastIndex].buildNumber <= buildNumber) {
break;
}
lastIndex++;
}
if (lastIndex > 0) {
return versionInfo.slice(0, lastIndex);
}
// No new version info
return null;
}
/**
* Show a dialog that shows the update
*/
function _showUpdateNotificationDialog(updates) {
Dialogs.showModalDialogUsingTemplate(Mustache.render(UpdateDialogTemplate, Strings))
.done(function (id) {
if (id === Dialogs.DIALOG_BTN_DOWNLOAD) {
// The first entry in the updates array has the latest download link
NativeApp.openURLInDefaultBrowser(updates[0].downloadURL);
}
});
// Populate the update data
var $dlg = $(".update-dialog.instance"),
$updateList = $dlg.find(".update-info");
updates.Strings = Strings;
$updateList.html(Mustache.render(UpdateListTemplate, updates));
}
/**
* Check for updates. If "force" is true, update notification dialogs are always displayed
* (if an update is available). If "force" is false, the update notification is only
* displayed for newly available updates.
*
* If an update is available, show the "update available" notification icon in the title bar.
*
* @param {boolean} force If true, always show the notification dialog.
* @param {Object} _testValues This should only be used for testing purposes. See comments for details.
* @return {$.Promise} jQuery Promise object that is resolved or rejected after the update check is complete.
*/
function checkForUpdate(force, _testValues) {
// The second param, if non-null, is an Object containing value overrides. Values
// in the object temporarily override the local values. This should *only* be used for testing.
// If any overrides are set, permanent changes are not made (including showing
// the update notification icon and saving prefs).
var oldValues;
var usingOverrides = false; // true if any of the values are overridden.
var result = new $.Deferred();
if (_testValues) {
oldValues = {};
if (_testValues.hasOwnProperty("_buildNumber")) {
oldValues._buildNumber = _buildNumber;
_buildNumber = _testValues._buildNumber;
usingOverrides = true;
}
if (_testValues.hasOwnProperty("_lastNotifiedBuildNumber")) {
oldValues._lastNotifiedBuildNumber = _lastNotifiedBuildNumber;
_lastNotifiedBuildNumber = _testValues._lastNotifiedBuildNumber;
usingOverrides = true;
}
if (_testValues.hasOwnProperty("_versionInfoURL")) {
oldValues._versionInfoURL = _versionInfoURL;
_versionInfoURL = _testValues._versionInfoURL;
usingOverrides = true;
}
}
_getUpdateInformation(force || usingOverrides, usingOverrides)
.done(function (versionInfo) {
// Get all available updates
var allUpdates = _stripOldVersionInfo(versionInfo, _buildNumber);
// When running directly from GitHub source (as opposed to
// an installed build), _buildNumber is 0. In this case, if the
// test is not forced, don't show the update notification icon or
// dialog.
if (_buildNumber === 0 && !force) {
result.resolve();
return;
}
if (allUpdates) {
// Always show the "update available" icon if any updates are available
var $updateNotification = $("#update-notification");
$updateNotification.css("display", "block");
if (!_addedClickHandler) {
_addedClickHandler = true;
$updateNotification.on("click", function () {
checkForUpdate(true);
});
}
// Only show the update dialog if force = true, or if the user hasn't been
// alerted of this update
if (force || allUpdates[0].buildNumber > _lastNotifiedBuildNumber) {
_showUpdateNotificationDialog(allUpdates);
// Update prefs with the last notified build number
_lastNotifiedBuildNumber = allUpdates[0].buildNumber;
// Don't save prefs is we have overridden values
if (!usingOverrides) {
_prefs.setValue("lastNotifiedBuildNumber", _lastNotifiedBuildNumber);
}
}
} else if (force) {
// No updates are available. If force == true, let the user know.
Dialogs.showModalDialog(
DefaultDialogs.DIALOG_ID_ERROR,
Strings.NO_UPDATE_TITLE,
Strings.NO_UPDATE_MESSAGE
);
}
if (oldValues) {
if (oldValues.hasOwnProperty("_buildNumber")) {
_buildNumber = oldValues._buildNumber;
}
if (oldValues.hasOwnProperty("_lastNotifiedBuildNumber")) {
_lastNotifiedBuildNumber = oldValues._lastNotifiedBuildNumber;
}
if (oldValues.hasOwnProperty("_versionInfoURL")) {
_versionInfoURL = oldValues._versionInfoURL;
}
}
result.resolve();
})
.fail(function () {
// Error fetching the update data. If this is a forced check, alert the user
if (force) {
Dialogs.showModalDialog(
DefaultDialogs.DIALOG_ID_ERROR,
Strings.ERROR_FETCHING_UPDATE_INFO_TITLE,
Strings.ERROR_FETCHING_UPDATE_INFO_MSG
);
}
result.reject();
});
return result.promise();
}
// Append locale to version info URL
_versionInfoURL = brackets.config.update_info_url + brackets.getLocale() + ".json";
// Check for updates on App Ready
AppInit.appReady(function () {
checkForUpdate();
});
// Define public API
exports.checkForUpdate = checkForUpdate;
});