-
Notifications
You must be signed in to change notification settings - Fork 7
/
vaadin-license-checker.html
566 lines (478 loc) · 19.5 KB
/
vaadin-license-checker.html
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
<!-- This file is autogenerated from src/vaadin-license-checker.tpl.html -->
<link rel="import" href="../vaadin-development-mode-detector/vaadin-development-mode-detector.html">
<script>
(function() {
function maybeCheckLicenses() {
/** vaadin-dev-mode:start
(function () {
'use strict';
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function");
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
writable: true,
configurable: true
}
});
if (superClass) _setPrototypeOf(subClass, superClass);
}
function _getPrototypeOf(o) {
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _getPrototypeOf(o);
}
function _setPrototypeOf(o, p) {
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _setPrototypeOf(o, p);
}
function _isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
if (Reflect.construct.sham) return false;
if (typeof Proxy === "function") return true;
try {
Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
return true;
} catch (e) {
return false;
}
}
function _construct(Parent, args, Class) {
if (_isNativeReflectConstruct()) {
_construct = Reflect.construct;
} else {
_construct = function _construct(Parent, args, Class) {
var a = [null];
a.push.apply(a, args);
var Constructor = Function.bind.apply(Parent, a);
var instance = new Constructor();
if (Class) _setPrototypeOf(instance, Class.prototype);
return instance;
};
}
return _construct.apply(null, arguments);
}
function _isNativeFunction(fn) {
return Function.toString.call(fn).indexOf("[native code]") !== -1;
}
function _wrapNativeSuper(Class) {
var _cache = typeof Map === "function" ? new Map() : undefined;
_wrapNativeSuper = function _wrapNativeSuper(Class) {
if (Class === null || !_isNativeFunction(Class)) return Class;
if (typeof Class !== "function") {
throw new TypeError("Super expression must either be null or a function");
}
if (typeof _cache !== "undefined") {
if (_cache.has(Class)) return _cache.get(Class);
_cache.set(Class, Wrapper);
}
function Wrapper() {
return _construct(Class, arguments, _getPrototypeOf(this).constructor);
}
Wrapper.prototype = Object.create(Class.prototype, {
constructor: {
value: Wrapper,
enumerable: false,
writable: true,
configurable: true
}
});
return _setPrototypeOf(Wrapper, Class);
};
return _wrapNativeSuper(Class);
}
function _assertThisInitialized(self) {
if (self === void 0) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return self;
}
function _possibleConstructorReturn(self, call) {
if (call && (typeof call === "object" || typeof call === "function")) {
return call;
} else if (call !== void 0) {
throw new TypeError("Derived constructors may only return object or undefined");
}
return _assertThisInitialized(self);
}
function _createSuper(Derived) {
var hasNativeReflectConstruct = _isNativeReflectConstruct();
return function _createSuperInternal() {
var Super = _getPrototypeOf(Derived),
result;
if (hasNativeReflectConstruct) {
var NewTarget = _getPrototypeOf(this).constructor;
result = Reflect.construct(Super, arguments, NewTarget);
} else {
result = Super.apply(this, arguments);
}
return _possibleConstructorReturn(this, result);
};
}
var licenseBoxTemplate = document.createElement('template');
licenseBoxTemplate.innerHTML = "\n <style>\n :host {\n font: 16px/1.625 -apple-system, BlinkMacSystemFont, \"Roboto\", \"Segoe UI\", Helvetica, Arial, sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\";\n position: fixed;\n top: .5em;\n right: .5em;\n z-index: 10000;\n cursor: pointer;\n backface-visibility: hidden;\n }\n\n #content {\n padding: 1em 1.5em;\n margin: 0;\n display: flex;\n flex-direction:row;\n align-items: center;\n text-align: left;\n font-size: inherit;\n line-height: inherit;\n font-weight: inherit;\n -webkit-font-smoothing: antialiased;\n -moz-osx-font-smoothin: grayscale;\n white-space: nowrap;\n letter-spacing: 0;\n border-radius: 5px;\n }\n\n #content[type=\"needsvalidation\"] {\n background-color: #FFD5D8;\n color: #591217;\n box-shadow: 0 0 20px 1px rgba(242,51,51,0.10);\n }\n\n #content[type=\"ok\"] {\n background-color: #B2F5C2;\n color: #1C562A;\n box-shadow: 0 0 20px 1px rgba(43,193,78,0.10);\n }\n\n #content > svg {\n display: none;\n fill: currentColor;\n stroke: currentColor 1px;\n padding-left: 1.25em;\n }\n\n #content[type=\"needsvalidation\"] > #link {\n display: inline;\n }\n\n #content[type=\"ok\"] > #close {\n display: inline;\n vertical-align: -.125em;\n }\n </style>\n\n <div id=\"content\">\n <div></div>\n <svg id=\"link\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"16\" height=\"16\" viewBox=\"0 0 16 16\">\n <g id=\"external-link\"><path d=\"M8.6 3.5l3.5 3.5h-12.1v2h12.1l-3.5 3.5 1.4 1.4 6-5.9-6-5.9z\"></path></g>\n </svg>\n <svg id=\"close\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"16\" height=\"16\" viewBox=\"0 0 16 16\">\n <g id=\"close-big\"><path d=\"M16 0l-1 0.010-7 6.99-7-6.99-1-0.010v1l7 7-7 7v1h1l7-7 7 7h1v-1l-7-7 7-7v-1z\"></path></g>\n </svg>\n </div>\n";
window.ShadyCSS && window.ShadyCSS.prepareTemplate(licenseBoxTemplate, 'vaadin-license-box');
var LicenseBoxElement = function (_HTMLElement) {
_inherits(LicenseBoxElement, _HTMLElement);
var _super = _createSuper(LicenseBoxElement);
function LicenseBoxElement() {
_classCallCheck(this, LicenseBoxElement);
return _super.apply(this, arguments);
}
_createClass(LicenseBoxElement, [{
key: "connectedCallback",
value: function connectedCallback() {
window.ShadyCSS && window.ShadyCSS.styleElement(this);
if (!this.shadowRoot) {
this.attachShadow({
mode: 'open'
});
this.shadowRoot.appendChild(document.importNode(licenseBoxTemplate.content, true));
this._contentElement = this.shadowRoot.querySelector('#content');
this._render();
}
}
}, {
key: "_render",
value: function _render() {
if (!this._contentElement) {
return;
}
if (this._type) {
this._contentElement.setAttribute('type', this._type);
} else {
this._contentElement.removeAttribute('type');
}
this._contentElement.firstElementChild.innerHTML = this._content;
}
}, {
key: "type",
get: function () {
return this._type;
},
set: function (type) {
this._type = type;
this._render();
}
}, {
key: "content",
get: function () {
return this._content;
},
set: function (content) {
this._content = content;
this._render();
}
}]);
return LicenseBoxElement;
}( _wrapNativeSuper(HTMLElement));
customElements.define('vaadin-license-box', LicenseBoxElement);
var LicenseCheckerLogger = function () {
function LicenseCheckerLogger() {
_classCallCheck(this, LicenseCheckerLogger);
this.id = "vaadin-license-checker";
}
_createClass(LicenseCheckerLogger, [{
key: "isDebug",
value: function isDebug() {
return localStorage.getItem("vaadin.licenses.debug");
}
}, {
key: "debug",
value: function debug(msg) {
if (this.isDebug()) {
console.info(this.id + ": " + msg);
}
}
}]);
return LicenseCheckerLogger;
}();
var LicenseCheckerStorage = function () {
function LicenseCheckerStorage() {
_classCallCheck(this, LicenseCheckerStorage);
this.logger = new LicenseCheckerLogger();
}
_createClass(LicenseCheckerStorage, [{
key: "getLastCheckKey",
value: function getLastCheckKey(productInfo) {
return "vaadin.licenses.{product}.lastCheck".replace("{product}", productInfo.name);
}
}, {
key: "getLastCheck",
value: function getLastCheck(productInfo) {
return Number(localStorage.getItem(this.getLastCheckKey(productInfo)));
}
}, {
key: "setLastCheck",
value: function setLastCheck(productInfo, timestamp) {
this.logger.debug("Setting last check time to " + new Date(timestamp));
localStorage.setItem(this.getLastCheckKey(productInfo), timestamp);
}
}]);
return LicenseCheckerStorage;
}();
var VaadinLicenseChecker = function () {
function VaadinLicenseChecker() {
_classCallCheck(this, VaadinLicenseChecker);
this.okNotifier = new LicenseOkNotifier();
this.validationNeededNotifier = new LicenseValidationNeededNotifier();
this.storage = new LicenseCheckerStorage();
this.checkInterval = 1000 * 60 * 60 * 24;
this.firstCheckDelay = 1000 * 60 * 1;
this.logger = new LicenseCheckerLogger();
this.url = "https://tools.vaadin.com/vaadin-license-server/licenses/pro";
}
_createClass(VaadinLicenseChecker, [{
key: "getForcedResponseKey",
value: function getForcedResponseKey(productInfo) {
return 'vaadin.licenses.{product}.forcedResponse'.replace('{product}', productInfo.name);
}
}, {
key: "getForcedResponse",
value: function getForcedResponse(productInfo) {
return localStorage.getItem(this.getForcedResponseKey(productInfo));
}
}, {
key: "clearForcedResponse",
value: function clearForcedResponse(productInfo) {
return localStorage.removeItem(this.getForcedResponseKey(productInfo));
}
}, {
key: "maybeCheck",
value: function maybeCheck(productInfo) {
this.logger.debug("maybeCheck(" + JSON.stringify(productInfo) + ")"); // Defer first check until interval has expired to avoid interfering with tests etc
var now = new Date().getTime();
var lastCheck = this.storage.getLastCheck(productInfo);
if (!lastCheck) {
this.logger.debug("Deferring first check until " + new Date(now + this.firstCheckDelay));
this.storage.setLastCheck(productInfo, now - this.checkInterval + this.firstCheckDelay);
return;
} else {
var sinceLastCheck = Math.round((now - lastCheck) / 1000);
var nextCheck = Math.round(this.checkInterval / 1000 - sinceLastCheck);
if (nextCheck > 0) {
// Checked recently
var nextCheckDate = new Date(lastCheck + this.checkInterval);
this.logger.debug("Checked " + sinceLastCheck + "s ago. Next check in " + nextCheck + "s at " + nextCheckDate + ".");
return;
} else {
this.logger.debug("Last check was " + sinceLastCheck + "s ago.");
}
}
this.check(productInfo);
}
}, {
key: "check",
value: function check(productInfo) {
this.logger.debug("check(" + JSON.stringify(productInfo) + ")"); // Only show an ok notification if the "validation needed" notification was shown.
// For background checks, show no visual notification if all goes well
var showOkOnSuccess = this.validationNeededNotifier.isVisible(productInfo);
var checker = this;
var onerror = function () {
// Offline or blocked, just log to console and let people get work done
console.error("Unable to validate the license for " + productInfo.name + ". Check your internet access.");
};
var onresponse = function (responseText) {
var response = JSON.parse(responseText);
if (response.result == "ok") {
// Everything is fine, stop
checker.logger.debug("License check ok for " + JSON.stringify(productInfo));
checker.storage.setLastCheck(productInfo, new Date().getTime());
if (showOkOnSuccess) {
checker.logger.debug("Showing validation-ok dialog");
checker.okNotifier.show(productInfo);
}
} else {
checker.logger.debug("License check failed for " + JSON.stringify(productInfo));
checker.logger.debug("Showing validation-needed dialog");
checker.validationNeededNotifier.show(productInfo);
}
if (response.message) {
console.log(response.message);
}
}; // This is typically hidden already but when receiving a window message it is not
this.logger.debug("Ensuring validation-needed dialog is hidden");
this.validationNeededNotifier.hide(productInfo);
if (this.logger.isDebug() && this.getForcedResponse(productInfo)) {
var respJson = this.getForcedResponse(productInfo);
this.clearForcedResponse(productInfo);
if (JSON.parse(respJson).type == "error") {
this.logger.debug("Forced error for check");
onerror();
} else {
this.logger.debug("Forced response for check: " + respJson);
onresponse(respJson);
}
} else {
this.send(this.url, productInfo, onresponse, onerror);
}
}
}, {
key: "send",
value: function send(url, productInfo, onsuccess, onerror) {
this.logger.debug("Sending request to " + url);
var req = new XMLHttpRequest();
req.withCredentials = true;
req.addEventListener("readystatechange", function () {
if (req.readyState === XMLHttpRequest.DONE && req.status === 200) {
onsuccess(req.responseText);
}
});
req.addEventListener("error", function () {
onerror();
});
req.open("GET", url);
req.setRequestHeader("check-source", "webcomponent");
req.setRequestHeader("product-name", productInfo.name);
req.setRequestHeader("product-version", productInfo.version);
req.send();
}
}], [{
key: "version",
get: function () {
return '2.1.2';
}
}]);
return VaadinLicenseChecker;
}();
var LicenseOkNotifier = function () {
function LicenseOkNotifier() {
_classCallCheck(this, LicenseOkNotifier);
}
_createClass(LicenseOkNotifier, [{
key: "getInstance",
value: function getInstance() {
return document.getElementById(LicenseOkNotifier.id);
}
}, {
key: "show",
value: function show(productInfo) {
// Only show one ok box even if multiple licenses were checked
if (this.getInstance()) {
// Already shown
return;
}
var instance = document.createElement("vaadin-license-box");
instance.id = LicenseOkNotifier.id;
instance.type = "ok";
instance.content = "Your license has been validated";
document.body.appendChild(instance);
instance.addEventListener("click", function () {
instance.parentElement.removeChild(instance);
});
}
}], [{
key: "id",
get: function () {
return 'vaadin-license-validation-ok';
}
}]);
return LicenseOkNotifier;
}();
var LicenseValidationNeededNotifier = function () {
function LicenseValidationNeededNotifier() {
_classCallCheck(this, LicenseValidationNeededNotifier);
}
_createClass(LicenseValidationNeededNotifier, [{
key: "id",
value: function id(productInfo) {
return "vaadin-license-validation-notification-{product}".replace("{product}", productInfo.name);
}
}, {
key: "getInstance",
value: function getInstance(productInfo) {
return document.getElementById(this.id(productInfo));
}
}, {
key: "show",
value: function show(productInfo) {
if (this.getInstance(productInfo)) {
// Already shown
return;
}
var instance = document.createElement("vaadin-license-box");
instance.id = this.id(productInfo);
instance.type = "needsvalidation";
instance.content = "This application is using components which are part of a Vaadin subscription.<br>Click here to get a trial or validate your subscription";
document.body.appendChild(instance);
instance.addEventListener("click", function () {
window.open("https://vaadin.com/pro/validate-license", "_blank");
});
}
}, {
key: "hide",
value: function hide(productInfo) {
var instance = this.getInstance(productInfo);
if (instance) {
instance.parentElement.removeChild(instance);
}
}
}, {
key: "isVisible",
value: function isVisible(productInfo) {
return !!this.getInstance(productInfo);
}
}]);
return LicenseValidationNeededNotifier;
}();
var proProducts = [];
window.Vaadin = window.Vaadin || {};
window.Vaadin.LicenseChecker = window.Vaadin.VaadinLicenseChecker || new VaadinLicenseChecker();
window.Vaadin.LicenseCheckerClass = window.Vaadin.LicenseCheckerClass || VaadinLicenseChecker;
window.Vaadin.developmentModeCallback = window.Vaadin.developmentModeCallback || {};
window.Vaadin.developmentModeCallback["vaadin-license-checker"] = function (cls) {
var productInfo = {
name: cls.is,
version: cls.version
};
proProducts.push(productInfo);
window.addEventListener("message", function (e) {
if (e.data == "validate-license") {
window.Vaadin.LicenseChecker.check(productInfo);
}
}, false);
window.Vaadin.checkLicenses = function () {
// Force checking of all licenses to avoid e.g. popups during presentations when the grace period just has ended
proProducts.forEach(function (productInfo) {
window.Vaadin.LicenseChecker.check(productInfo);
});
};
window.Vaadin.LicenseChecker.maybeCheck(productInfo);
};
})();
vaadin-dev-mode:end **/
};
/**
* @namespace Vaadin
*/
window.Vaadin = window.Vaadin || {};
if (typeof window.Vaadin.runIfDevelopmentMode === 'function') {
window.Vaadin.runIfDevelopmentMode(maybeCheckLicenses);
}
})();
</script>