-
Notifications
You must be signed in to change notification settings - Fork 8
/
base.js
630 lines (566 loc) · 15.7 KB
/
base.js
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview
* A module that contains basic utility components and methods for the
* chromoting project
*
*/
'use strict';
var base = {};
base.debug = function() {};
/**
* Whether to break in debugger and alert when an assertion fails.
* Set it to true for debugging.
* @type {boolean}
*/
base.debug.breakOnAssert = false;
/**
* Assert that |expr| is true else print the |opt_msg|.
* @param {boolean} expr
* @param {string=} opt_msg
*/
base.debug.assert = function(expr, opt_msg) {
if (!expr) {
var msg = 'Assertion Failed.';
if (opt_msg) {
msg += ' ' + opt_msg;
}
console.error(msg);
if (base.debug.breakOnAssert) {
alert(msg);
debugger;
}
}
};
/**
* @return {string} The callstack of the current method.
*/
base.debug.callstack = function() {
try {
throw new Error();
} catch (/** @type {Error} */ error) {
var callstack = error.stack
.replace(/^\s+(at eval )?at\s+/gm, '') // Remove 'at' and indentation.
.split('\n');
callstack.splice(0,2); // Remove the stack of the current function.
}
return callstack.join('\n');
};
/**
* @interface
*/
base.Disposable = function() {};
base.Disposable.prototype.dispose = function() {};
/**
* @constructor
* @param {...base.Disposable} var_args
* @implements {base.Disposable}
*/
base.Disposables = function(var_args) {
/**
* @type {Array<base.Disposable>}
* @private
*/
this.disposables_ = Array.prototype.slice.call(arguments, 0);
};
base.Disposables.prototype.dispose = function() {
for (var i = 0; i < this.disposables_.length; i++) {
this.disposables_[i].dispose();
}
this.disposables_ = null;
};
/**
* A utility function to invoke |obj|.dispose without a null check on |obj|.
* @param {base.Disposable} obj
*/
base.dispose = function(obj) {
if (obj) {
base.debug.assert(typeof obj.dispose == 'function');
obj.dispose();
}
};
/**
* Copy all properties from src to dest.
* @param {Object} dest
* @param {Object} src
*/
base.mix = function(dest, src) {
for (var prop in src) {
if (src.hasOwnProperty(prop)) {
base.debug.assert(!dest.hasOwnProperty(prop),"Don't override properties");
dest[prop] = src[prop];
}
}
};
/**
* Adds a mixin to a class.
* @param {Object} dest
* @param {Object} src
* @suppress {checkTypes|reportUnknownTypes}
*/
base.extend = function(dest, src) {
base.mix(dest.prototype, src.prototype || src);
};
base.doNothing = function() {};
/**
* Returns an array containing the values of |dict|.
* @param {!Object} dict
* @return {Array}
*/
base.values = function(dict) {
return Object.keys(dict).map(
/** @param {string} key */
function(key) {
return dict[key];
});
};
/**
* @param {*} value
* @return {*} a recursive copy of |value| or null if |value| is not copyable
* (e.g. undefined, NaN).
*/
base.deepCopy = function(value) {
try {
return JSON.parse(JSON.stringify(value));
} catch (e) {}
return null;
};
/**
* @type {boolean|undefined}
* @private
*/
base.isAppsV2_ = undefined;
/**
* @return {boolean} True if this is a v2 app; false if it is a legacy app.
*/
base.isAppsV2 = function() {
if (base.isAppsV2_ === undefined) {
var manifest = chrome.runtime.getManifest();
base.isAppsV2_ =
Boolean(manifest && manifest.app && manifest.app.background);
}
return base.isAppsV2_;
};
/**
* Joins the |url| with optional query parameters defined in |opt_params|
* See unit test for usage.
* @param {string} url
* @param {Object<string>=} opt_params
* @return {string}
*/
base.urlJoin = function(url, opt_params) {
if (!opt_params) {
return url;
}
var queryParameters = [];
for (var key in opt_params) {
queryParameters.push(encodeURIComponent(key) + "=" +
encodeURIComponent(opt_params[key]));
}
return url + '?' + queryParameters.join('&');
};
/**
* @return {Object<string, string>} The URL parameters.
*/
base.getUrlParameters = function() {
var result = {};
var parts = window.location.search.substring(1).split('&');
for (var i = 0; i < parts.length; i++) {
var pair = parts[i].split('=');
result[pair[0]] = decodeURIComponent(pair[1]);
}
return result;
};
/**
* Convert special characters (e.g. &, < and >) to HTML entities.
*
* @param {string} str
* @return {string}
*/
base.escapeHTML = function(str) {
var div = document.createElement('div');
div.appendChild(document.createTextNode(str));
return div.innerHTML;
};
/**
* Promise is a great tool for writing asynchronous code. However, the construct
* var p = new promise(function init(resolve, reject) {
* ... // code that fulfills the Promise.
* });
* forces the Promise-resolving logic to reside in the |init| function
* of the constructor. This is problematic when you need to resolve the
* Promise in a member function(which is quite common for event callbacks).
*
* base.Deferred comes to the rescue. It encapsulates a Promise
* object and exposes member methods (resolve/reject) to fulfill it.
*
* Here are the recommended steps to follow when implementing an asynchronous
* function that returns a Promise:
* 1. Create a deferred object by calling
* var deferred = new base.Deferred();
* 2. Call deferred.resolve() when the asynchronous operation finishes.
* 3. Call deferred.reject() when the asynchronous operation fails.
* 4. Return deferred.promise() to the caller so that it can subscribe
* to status changes using the |then| handler.
*
* Sample Usage:
* function myAsyncAPI() {
* var deferred = new base.Deferred();
* window.setTimeout(function() {
* deferred.resolve();
* }, 100);
* return deferred.promise();
* };
*
* @constructor
*/
base.Deferred = function() {
/**
* @type {?function(?):void}
* @private
*/
this.resolve_ = null;
/**
* @type {?function(?):void}
* @private
*/
this.reject_ = null;
/**
* @this {base.Deferred}
* @param {function(?):void} resolve
* @param {function(*):void} reject
*/
var initPromise = function(resolve, reject) {
this.resolve_ = resolve;
this.reject_ = reject;
};
/**
* @type {Promise}
* @private
*/
this.promise_ = new Promise(initPromise.bind(this));
};
/** @param {*} reason */
base.Deferred.prototype.reject = function(reason) {
this.reject_(reason);
};
/** @param {*=} opt_value */
base.Deferred.prototype.resolve = function(opt_value) {
this.resolve_(opt_value);
};
/** @return {Promise} */
base.Deferred.prototype.promise = function() {
return this.promise_;
};
base.Promise = function() {};
/**
* @param {number} delay
* @return {Promise} a Promise that will be fulfilled after |delay| ms.
*/
base.Promise.sleep = function(delay) {
return new Promise(
/** @param {function(*):void} fulfill */
function(fulfill) {
window.setTimeout(fulfill, delay);
});
};
/**
* @param {Promise} promise
* @return {Promise} a Promise that will be fulfilled iff the specified Promise
* is rejected.
*/
base.Promise.negate = function(promise) {
return promise.then(
/** @return {Promise} */
function() {
return Promise.reject();
},
/** @return {Promise} */
function() {
return Promise.resolve();
});
};
/**
* Converts a |method| with callbacks into a Promise.
*
* @param {Function} method
* @param {Array} params
* @param {*=} opt_context
* @param {boolean=} opt_hasErrorHandler whether the method has an error handler
* @return {Promise}
*/
base.Promise.as = function(method, params, opt_context, opt_hasErrorHandler) {
return new Promise(function(resolve, reject) {
params.push(resolve);
if (opt_hasErrorHandler) {
params.push(reject);
}
try {
method.apply(opt_context, params);
} catch (/** @type {*} */ e) {
reject(e);
}
});
};
/**
* A mixin for classes with events.
*
* For example, to create an alarm event for SmokeDetector:
* functionSmokeDetector() {
* this.defineEvents(['alarm']);
* };
* base.extend(SmokeDetector, base.EventSourceImpl);
*
* To fire an event:
* SmokeDetector.prototype.onCarbonMonoxideDetected = function() {
* var param = {} // optional parameters
* this.raiseEvent('alarm', param);
* }
*
* To listen to an event:
* var smokeDetector = new SmokeDetector();
* smokeDetector.addEventListener('alarm', listenerObj.someCallback)
*
*/
/**
* Helper interface for the EventSource.
* @constructor
*/
base.EventEntry = function() {
/** @type {Array<function():void>} */
this.listeners = [];
};
/** @interface */
base.EventSource = function() {};
/**
* Add a listener |fn| to listen to |type| event.
* @param {string} type
* @param {Function} fn
*/
base.EventSource.prototype.addEventListener = function(type, fn) {};
/**
* Remove a listener |fn| to listen to |type| event.
* @param {string} type
* @param {Function} fn
*/
base.EventSource.prototype.removeEventListener = function(type, fn) {};
/**
* @constructor
* Since this class is implemented as a mixin, the constructor may not be
* called. All initializations should be done in defineEvents.
* @implements {base.EventSource}
*/
base.EventSourceImpl = function() {
/** @type {Object<string, base.EventEntry>} */
this.eventMap_;
};
/**
* @param {base.EventSourceImpl} obj
* @param {string} type
*/
base.EventSourceImpl.isDefined = function(obj, type) {
base.debug.assert(Boolean(obj.eventMap_),
"The object doesn't support events");
base.debug.assert(Boolean(obj.eventMap_[type]), 'Event <' + type +
'> is undefined for the current object');
};
base.EventSourceImpl.prototype = {
/**
* Define |events| for this event source.
* @param {Array<string>} events
*/
defineEvents: function(events) {
base.debug.assert(!Boolean(this.eventMap_),
'defineEvents can only be called once.');
this.eventMap_ = {};
events.forEach(
/**
* @this {base.EventSourceImpl}
* @param {string} type
*/
function(type) {
base.debug.assert(typeof type == 'string');
this.eventMap_[type] = new base.EventEntry();
}, this);
},
/**
* @param {string} type
* @param {Function} fn
*/
addEventListener: function(type, fn) {
base.debug.assert(typeof fn == 'function');
base.EventSourceImpl.isDefined(this, type);
var listeners = this.eventMap_[type].listeners;
listeners.push(fn);
},
/**
* @param {string} type
* @param {Function} fn
*/
removeEventListener: function(type, fn) {
base.debug.assert(typeof fn == 'function');
base.EventSourceImpl.isDefined(this, type);
var listeners = this.eventMap_[type].listeners;
// find the listener to remove.
for (var i = 0; i < listeners.length; i++) {
var listener = listeners[i];
if (listener == fn) {
listeners.splice(i, 1);
break;
}
}
},
/**
* Fire an event of a particular type on this object.
* @param {string} type
* @param {*=} opt_details The type of |opt_details| should be ?= to
* match what is defined in add(remove)EventListener. However, JSCompile
* cannot handle invoking an unknown type as an argument to |listener|
* As a hack, we set the type to *=.
*/
raiseEvent: function(type, opt_details) {
base.EventSourceImpl.isDefined(this, type);
var entry = this.eventMap_[type];
var listeners = entry.listeners.slice(0); // Make a copy of the listeners.
listeners.forEach(
/** @param {function(*=):void} listener */
function(listener){
if (listener) {
listener(opt_details);
}
});
}
};
/**
* A lightweight object that helps manage the lifetime of an event listener.
*
* For example, do the following if you want to automatically unhook events
* when your object is disposed:
*
* var MyConstructor = function(domElement) {
* this.eventHooks_ = new base.Disposables(
* new base.EventHook(domElement, 'click', this.onClick_.bind(this)),
* new base.EventHook(domElement, 'keydown', this.onClick_.bind(this)),
* new base.ChromeEventHook(chrome.runtime.onMessage,
* this.onMessage_.bind(this))
* );
* }
*
* MyConstructor.prototype.dispose = function() {
* this.eventHooks_.dispose();
* this.eventHooks_ = null;
* }
*
* @param {base.EventSource} src
* @param {string} eventName
* @param {function(...?)} listener
*
* @constructor
* @implements {base.Disposable}
*/
base.EventHook = function(src, eventName, listener) {
this.src_ = src;
this.eventName_ = eventName;
this.listener_ = listener;
src.addEventListener(eventName, listener);
};
base.EventHook.prototype.dispose = function() {
this.src_.removeEventListener(this.eventName_, this.listener_);
};
/**
* An event hook implementation for DOM Events.
*
* @param {HTMLElement} src
* @param {string} eventName
* @param {function(...?)} listener
* @param {boolean} capture
*
* @constructor
* @implements {base.Disposable}
*/
base.DomEventHook = function(src, eventName, listener, capture) {
this.src_ = src;
this.eventName_ = eventName;
this.listener_ = listener;
this.capture_ = capture;
src.addEventListener(eventName, listener, capture);
};
base.DomEventHook.prototype.dispose = function() {
this.src_.removeEventListener(this.eventName_, this.listener_, this.capture_);
};
/**
* An event hook implementation for Chrome Events.
*
* @param {chrome.Event} src
* @param {function(...?)} listener
*
* @constructor
* @implements {base.Disposable}
*/
base.ChromeEventHook = function(src, listener) {
this.src_ = src;
this.listener_ = listener;
src.addListener(listener);
};
base.ChromeEventHook.prototype.dispose = function() {
this.src_.removeListener(this.listener_);
};
/**
* Converts UTF-8 string to ArrayBuffer.
*
* @param {string} string
* @return {ArrayBuffer}
*/
base.encodeUtf8 = function(string) {
var utf8String = unescape(encodeURIComponent(string));
var result = new Uint8Array(utf8String.length);
for (var i = 0; i < utf8String.length; i++)
result[i] = utf8String.charCodeAt(i);
return result.buffer;
};
/**
* Decodes UTF-8 string from ArrayBuffer.
*
* @param {ArrayBuffer} buffer
* @return {string}
*/
base.decodeUtf8 = function(buffer) {
return decodeURIComponent(
escape(String.fromCharCode.apply(null, new Uint8Array(buffer))));
};
/**
* Generate a nonce, to be used as an xsrf protection token.
*
* @return {string} A URL-Safe Base64-encoded 128-bit random value. */
base.generateXsrfToken = function() {
var random = new Uint8Array(16);
window.crypto.getRandomValues(random);
var base64Token = window.btoa(String.fromCharCode.apply(null, random));
return base64Token.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
};
/**
* @param {string} jsonString A JSON-encoded string.
* @return {Object|undefined} The decoded object, or undefined if the string
* cannot be parsed.
*/
base.jsonParseSafe = function(jsonString) {
try {
return /** @type {Object} */ (JSON.parse(jsonString));
} catch (err) {
return undefined;
}
};
/**
* Size the current window to fit its content vertically.
*/
base.resizeWindowToContent = function() {
var appWindow = chrome.app.window.current();
var outerBounds = appWindow.outerBounds;
var borderY = outerBounds.height - appWindow.innerBounds.height;
appWindow.resizeTo(outerBounds.width, document.body.clientHeight + borderY);
// Sometimes, resizing the window causes its position to be reset to (0, 0),
// so restore it explicitly.
appWindow.moveTo(outerBounds.left, outerBounds.top);
};