OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview | 6 * @fileoverview |
7 * A module that contains basic utility components and methods for the | 7 * A module that contains basic utility components and methods for the |
8 * chromoting project | 8 * chromoting project |
9 * | 9 * |
10 */ | 10 */ |
(...skipping 28 matching lines...) Expand all Loading... |
39 } | 39 } |
40 } | 40 } |
41 }; | 41 }; |
42 | 42 |
43 /** | 43 /** |
44 * @return {string} The callstack of the current method. | 44 * @return {string} The callstack of the current method. |
45 */ | 45 */ |
46 base.debug.callstack = function() { | 46 base.debug.callstack = function() { |
47 try { | 47 try { |
48 throw new Error(); | 48 throw new Error(); |
49 } catch (e) { | 49 } catch (/** @type {Error} */ error) { |
50 var error = /** @type {Error} */ e; | |
51 var callstack = error.stack | 50 var callstack = error.stack |
52 .replace(/^\s+(at eval )?at\s+/gm, '') // Remove 'at' and indentation. | 51 .replace(/^\s+(at eval )?at\s+/gm, '') // Remove 'at' and indentation. |
53 .split('\n'); | 52 .split('\n'); |
54 callstack.splice(0,2); // Remove the stack of the current function. | 53 callstack.splice(0,2); // Remove the stack of the current function. |
55 } | 54 } |
56 return callstack.join('\n'); | 55 return callstack.join('\n'); |
57 }; | 56 }; |
58 | 57 |
59 /** | 58 /** |
60 * @interface | 59 * @interface |
(...skipping 23 matching lines...) Expand all Loading... |
84 base.debug.assert(!dest.hasOwnProperty(prop),"Don't override properties"); | 83 base.debug.assert(!dest.hasOwnProperty(prop),"Don't override properties"); |
85 dest[prop] = src[prop]; | 84 dest[prop] = src[prop]; |
86 } | 85 } |
87 } | 86 } |
88 }; | 87 }; |
89 | 88 |
90 /** | 89 /** |
91 * Adds a mixin to a class. | 90 * Adds a mixin to a class. |
92 * @param {Object} dest | 91 * @param {Object} dest |
93 * @param {Object} src | 92 * @param {Object} src |
94 * @suppress {checkTypes} | 93 * @suppress {checkTypes|reportUnknownTypes} |
95 */ | 94 */ |
96 base.extend = function(dest, src) { | 95 base.extend = function(dest, src) { |
97 base.mix(dest.prototype, src.prototype || src); | 96 base.mix(dest.prototype, src.prototype || src); |
98 }; | 97 }; |
99 | 98 |
100 base.doNothing = function() {}; | 99 base.doNothing = function() {}; |
101 | 100 |
102 /** | 101 /** |
103 * Returns an array containing the values of |dict|. | 102 * Returns an array containing the values of |dict|. |
104 * @param {!Object} dict | 103 * @param {!Object} dict |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 * window.setTimeout(function() { | 187 * window.setTimeout(function() { |
189 * deferred.resolve(); | 188 * deferred.resolve(); |
190 * }, 100); | 189 * }, 100); |
191 * return deferred.promise(); | 190 * return deferred.promise(); |
192 * }; | 191 * }; |
193 * | 192 * |
194 * @constructor | 193 * @constructor |
195 */ | 194 */ |
196 base.Deferred = function() { | 195 base.Deferred = function() { |
197 /** | 196 /** |
198 * @type {?function(?=)} | 197 * @type {?function(?):void} |
199 * @private | 198 * @private |
200 */ | 199 */ |
201 this.resolve_ = null; | 200 this.resolve_ = null; |
202 | 201 |
203 /** | 202 /** |
204 * @type {?function(?)} | 203 * @type {?function(?):void} |
205 * @private | 204 * @private |
206 */ | 205 */ |
207 this.reject_ = null; | 206 this.reject_ = null; |
208 | 207 |
209 /** | 208 /** |
| 209 * @this {base.Deferred} |
| 210 * @param {function(?):void} resolve |
| 211 * @param {function(*):void} reject |
| 212 */ |
| 213 var initPromise = function(resolve, reject) { |
| 214 this.resolve_ = resolve; |
| 215 this.reject_ = reject; |
| 216 }; |
| 217 |
| 218 /** |
210 * @type {Promise} | 219 * @type {Promise} |
211 * @private | 220 * @private |
212 */ | 221 */ |
213 this.promise_ = new Promise( | 222 this.promise_ = new Promise(initPromise.bind(this)); |
214 /** | |
215 * @param {function(?=):void} resolve | |
216 * @param {function(?):void} reject | |
217 * @this {base.Deferred} | |
218 */ | |
219 function(resolve, reject) { | |
220 this.resolve_ = resolve; | |
221 this.reject_ = reject; | |
222 }.bind(this) | |
223 ); | |
224 }; | 223 }; |
225 | 224 |
226 /** @param {*} reason */ | 225 /** @param {*} reason */ |
227 base.Deferred.prototype.reject = function(reason) { | 226 base.Deferred.prototype.reject = function(reason) { |
228 this.reject_(reason); | 227 this.reject_(reason); |
229 }; | 228 }; |
230 | 229 |
231 /** @param {*=} opt_value */ | 230 /** @param {*=} opt_value */ |
232 base.Deferred.prototype.resolve = function(opt_value) { | 231 base.Deferred.prototype.resolve = function(opt_value) { |
233 this.resolve_(opt_value); | 232 this.resolve_(opt_value); |
234 }; | 233 }; |
235 | 234 |
236 /** @return {Promise} */ | 235 /** @return {Promise} */ |
237 base.Deferred.prototype.promise = function() { | 236 base.Deferred.prototype.promise = function() { |
238 return this.promise_; | 237 return this.promise_; |
239 }; | 238 }; |
240 | 239 |
241 base.Promise = function() {}; | 240 base.Promise = function() {}; |
242 | 241 |
243 /** | 242 /** |
244 * @param {number} delay | 243 * @param {number} delay |
245 * @return {Promise} a Promise that will be fulfilled after |delay| ms. | 244 * @return {Promise} a Promise that will be fulfilled after |delay| ms. |
246 */ | 245 */ |
247 base.Promise.sleep = function(delay) { | 246 base.Promise.sleep = function(delay) { |
248 return new Promise( | 247 return new Promise( |
249 /** @param {function():void} fulfill */ | 248 /** @param {function(*):void} fulfill */ |
250 function(fulfill) { | 249 function(fulfill) { |
251 window.setTimeout(fulfill, delay); | 250 window.setTimeout(fulfill, delay); |
252 }); | 251 }); |
253 }; | 252 }; |
254 | 253 |
255 | 254 |
256 /** | 255 /** |
257 * @param {Promise} promise | 256 * @param {Promise} promise |
258 * @return {Promise} a Promise that will be fulfilled iff the specified Promise | 257 * @return {Promise} a Promise that will be fulfilled iff the specified Promise |
259 * is rejected. | 258 * is rejected. |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 * @return {string} A URL-Safe Base64-encoded 128-bit random value. */ | 428 * @return {string} A URL-Safe Base64-encoded 128-bit random value. */ |
430 base.generateXsrfToken = function() { | 429 base.generateXsrfToken = function() { |
431 var random = new Uint8Array(16); | 430 var random = new Uint8Array(16); |
432 window.crypto.getRandomValues(random); | 431 window.crypto.getRandomValues(random); |
433 var base64Token = window.btoa(String.fromCharCode.apply(null, random)); | 432 var base64Token = window.btoa(String.fromCharCode.apply(null, random)); |
434 return base64Token.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, ''); | 433 return base64Token.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, ''); |
435 }; | 434 }; |
436 | 435 |
437 /** | 436 /** |
438 * @param {string} jsonString A JSON-encoded string. | 437 * @param {string} jsonString A JSON-encoded string. |
439 * @return {*} The decoded object, or undefined if the string cannot be parsed. | 438 * @return {Object|undefined} The decoded object, or undefined if the string |
| 439 * cannot be parsed. |
440 */ | 440 */ |
441 base.jsonParseSafe = function(jsonString) { | 441 base.jsonParseSafe = function(jsonString) { |
442 try { | 442 try { |
443 return JSON.parse(jsonString); | 443 return /** @type {Object} */ (JSON.parse(jsonString)); |
444 } catch (err) { | 444 } catch (err) { |
445 return undefined; | 445 return undefined; |
446 } | 446 } |
447 } | 447 } |
OLD | NEW |